Solved

Writing a Compiler HELP~

Posted on 2004-04-02
5
209 Views
Last Modified: 2013-11-23
Hi all, I am writing a compiler for a language called RCL, but stuck at the parser stage. Apparently, I have done the lexer, but now needs to do the parser. We are told to write a parser class, containing sub-parsers methods for each productions. And each sub parsers should also instantiate an AST node unique to that production. I am having problem of linking the lexer and the parser. i am stuck. here;s my lexer (its first part):

************************************************************************************
//First part of the descrition file: Contains codes to be added to the top of symbols.lex.java
import java.io.*;
import java.lang.System;
import compiler.sym;  // In here, all the definitions of token values
import ast.*;


class Compiler
  {
    /*
     * Just a simple driver - loops till end-of-file **
     * prints out the yytoken received..via the to_string method in yytoken
     *
     * **NOTE: Eof weirdness - For some strange reason (as yet unresolved)
     * it takes 3 (three) ^D's to make EOF happen.  Don't know why.
     *
     */
    static Yytoken current_symbol;
    static Yylex scanner;
   
    public static void main(String[] args) throws java.io.IOException
     {
            FileReader inFile = new FileReader(args[0]);
            scanner = new Yylex(inFile);
          
           do
            {
            
              System.out.println(current_symbol);
            }
            while (current_symbol.m_index != sym.EOF);
     }
     
     public Yytoken next_symbol() throws java.io.IOException
      {
          current_symbol = scanner.yylex();
          return current_symbol;
      }
      
      
      public boolean have(int s) throws java.io.IOException
      {
            if (current_symbol.m_index == s)
             {
                   next_symbol();
                   return (true);
             }
            else
                  return(false);
                  
      }

    public void mustbe(int s) throws java.io.IOException
    {
          if (current_symbol.m_index == s)
          {
                next_symbol();
          }
          else
          {
                System.out.println("error");
          }
    }
   
   
   
   
 }
 

 
class Utility
  {
    public static void my_assert(boolean expr)
      {
             if (false == expr)
               {
                  throw (new Error("Error: Assertion failed."));
               }
      }
    private static final String errorMsg[] =
     {
        "Error: Unmatched end-of-comment punctuation.",
        "Error: Unmatched start-of-comment punctuation.",
        "Error: Unclosed string.",
        "Error: Illegal character."
     };
    public static final int E_ENDCOMMENT = 0;
    public static final int E_STARTCOMMENT = 1;
    public static final int E_UNCLOSEDSTR = 2;
    public static final int E_UNMATCHED = 3;
    public static void error(int code)
      {
             System.out.println(errorMsg[code]);
      }
  }

class Yytoken
 {
   Yytoken (int index,String text,int line,int charBegin,int charEnd,String t_type)
      {
           m_index = index;
          m_text = new String(text);
          m_line = line;
          m_charBegin = charBegin;
          m_charEnd = charEnd;
          m_type = t_type;
      }
   public int m_index;
   public String m_text;
   public int m_line;
   public int m_charBegin;
   public int m_charEnd;
   public String m_type;
   public String toString()
    {
      return m_line +","+m_charBegin+":"+m_charEnd+"#"+m_type+"#"+m_text+"#";
    }
 }
 
******************************************************************************************

And here is my parser: i have only build an ifstatement production. just for testing.


import ast.*;
import compiler.sym;


public class Parser
{
   
   public Parser()
    {
          
    }

   
    public static ASTNode nIfStatement()
      {
            IfStatementNode thisOne = new IfStatementNode();
            mustbe(sym.tIF);
            thisOne.addchild(nRvalue());
            mustbe(sym.tTHEN);
            thisOne.addchild(nStatementList());
            while (have(sym.tELSEIF))
             {
                  thisOne.addchild(nRvalue());
                  mustbe(sym.tTHEN);
                  thisOne.addchild(nStatementList());
             };
            if (have(sym.tELSE))
             {
                   thisOne.addchild(nStatementList());
             }
            mustbe(sym.tENDIF);
            return thisOne;
      };
 
  }
  **************************************************************************************
 
 Okay, what i am not sure is where to put the mustbe() and have() method. I am confused on whether to implement like above, putting them in the lexer file or put in the parser file. Like now, the compiler would complain it cant find the mustbe method and have method.

Help~
 
 
 
 
 
 
 
 
 
 
 
0
Comment
Question by:jtcy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739393
0
 

Author Comment

by:jtcy
ID: 10739401
um..have added the classpath. but i just heard from friend that theres no need to change the classpath. but using packaging. : (
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739418
Well, can you post a comment on it so that we can carry the discussion on that page. Please tell the specific problem now.
0
 
LVL 1

Accepted Solution

by:
wll9111 earned 500 total points
ID: 10949543
 public static void main(String[] args) throws java.io.IOException
     {
            FileReader inFile = new FileReader(args[0]);                        // creating two copies of the input file...
            FileReader inFile2 = new FileReader(args[0]);
            
            Yylex lexer = new Yylex(inFile);                                          // creating a lexer...
            Parser parser = new Parser(lexer);                                          // creating a parser...
                                                
            parser.debugParser = true;                                                // debugging tools set off by default...
            parser.printAST = true;
            
            // get the start symbol...
            parser.next_symbol();                                                            
                parser.nProgram();                  
                      
                 // here we create a listing file...
            parser.writeFile(inFile2);

            
    }
   
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question