could anyone help me construct a simple compiler...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moon123
    New Member
    • Feb 2008
    • 2

    could anyone help me construct a simple compiler...

    We have to program it in two parts: the lexical analyzer and the syntax analyzer plus semantic.The lexical analyzer should recognize the token using DFA and return the token and the value. The syntax and semantic part can be done by using any algorithm that can check for the correct syntax.You can select one programming language as the source language to build compiler to it or construct your own ones or take the one I write it in the appendix of this assignment (source program).The language that will be used as source program (calculator) will contain only the operations add, subtract, multiply, divide, power and minus unary operations and two types of variables, integer and real and also space has to be used to separate between tokens.

    The following is Syntax structure for a language that works as calculator. Any words in bold mean it is terminal

    Prog -> Header Declar Statm
    Header -> program ( id) ; id is name for the program
    Declar -> D Declar |D
    D -> id: Type ;
    Type -> real | integer
    Statm -> [ S ]
    S -> Exp1 ; S |Exp1 ;
    Exp1 -> id = Exp
    Exp -> Exp + Exp | Exp – Exp | Exp * Exp | Exp / Exp | Exp ^ Exp |_ Exp| (Exp) | id | num _ is unary minus operation
    ^ Power operation


    Code : (java)

    import java.io.*;


    public class readfile {



    /**
    * Creates a new instance of <code>readfil e</code>.
    */
    public readfile() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {


    FileInputStream myFile = null;
    try {
    myFile = new FileInputStream ("info.txt") ; // open the stream
    BufferedWriter out = new BufferedWriter( new FileWriter("new text.txt")); // open writing stream

    boolean eof = false;

    while (!eof) {
    int byteValue = myFile.read(); // read the stream


    char c = (char)byteValue ;
    c = Character.toLow erCase(c);
    System.out.prin tln(c + " ");
    out.write(c);


    if (byteValue == -1) //if it is at the end of file
    eof = true;
    }

    out.close();


    } catch (IOException e) {
    System.out.prin tln("Could not read file: " + e.toString());
    } finally{
    try{

    myFile.close(); // close the stream
    } catch (Exception e1){
    e1.printStackTr ace();
    }
    }



    }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

    Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

    Then when you are ready post a new question in this thread.

    MODERATOR

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Threre's a small series of articles in the Java section of the 'Howtos' that explains
      lexical analyzers, parsers, code generators and interpreters.

      kind regards,

      Jos

      Comment

      Working...