Link to home
Start Free TrialLog in
Avatar of jtcy
jtcy

asked on

Writing a Compiler HELP~

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~
 
 
 
 
 
 
 
 
 
 
 
Avatar of Mayank S
Mayank S
Flag of India image

Avatar of jtcy
jtcy

ASKER

um..have added the classpath. but i just heard from friend that theres no need to change the classpath. but using packaging. : (
Well, can you post a comment on it so that we can carry the discussion on that page. Please tell the specific problem now.
ASKER CERTIFIED SOLUTION
Avatar of wll9111
wll9111

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial