Solved

Writing a Compiler HELP~

Posted on 2004-04-02
5
199 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:mayankeagle
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:mayankeagle
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now