Solved

Writing a Compiler HELP~

Posted on 2004-04-02
5
204 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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

832 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