Solved

Writing a Compiler HELP~

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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