Assignment 1 JavaCC grammars

 

Goal

  1. To understand JavaCC grammars, take an existing grammar for language L1 and to modify it.
  2. To annotate the grammar with jjtree annotations and to extend the abstract syntax trees generated by jjtree to implement an interpreter for L1.

Language L1

Language L1 has a syntax based on Modula 2. Your task is to take the existing JavaCC file and to modify the syntax to conform to C, and then to modify the existing abstract syntax tree classes to make the interpreter work with the new grammar. Call your language L1a.

Modifying the Grammar

You should not try to include more of C syntax than is in the grammar for L1. The only big changes necessary are to the syntax for statements. Since arithmetic expressions have the same syntax in Modula 2 and in C, the grammar rules for expressions do not need to be altered. Other changes are:

 

As an example, the two programs below are semantically the same (i.e. they execute the same sequence of assignments and tests):

 

 

x := 3;

y := (x * 2) + 12;

z := 0;

while y > 0 do

  z := z + x;

  if y = 15 then

    y := y - 14

  else

    y := y - 1

  end

end.

{  x = 3;

    y = (x * 2) + 12;

    z = 0;

    while (y > 0) {

       z = z + x;

       if (y == 15)

          y = y - 14;

       else

         y = y - 1;

    }

}

Hints

  1. The only statement that needs ‘;’ as a terminator is the assignment statement.
  2. ‘end’ in Modula removes the ambiguity of the dangling else, but the problem does exist in C. Your grammar will be able to handle the optional else (e.g. if (x == 1) y = 1; versus if (x == 1) y = 1; else y = 2;) by assuming ‘else’ is always paired with the immediately preceeding ‘then’ statement. JavaCC will issue a warning in this case, but this can be removed by making the optional else a separate rule which can have an empty production. The rule might be something like:

    void OptionalElse :
    {}
    {
        <ELSE> Statement()
    |
       {}  // i.e. can be empty
    }
  3. There is no need to change the data structures for the store and the stack, or the way they are handled.

 

Downloading the files

A Java archive has been created of all the .java files and the grammar file l1.jjt. Create a new directory for the project, and download the jar file into it. Run the command ‘jar xvf l1.jar’ which will unpack the files from the archive.

Running jjtree, JavaCC and java 1.5

Add the following lines to your .cshrc file and remember to ‘source’ it after the changes.

 

setenv PATH ~rth/public/javacc-4.0/bin:$PATH

setenv PATH ~rth/public/jdk1.5.0_04/bin:$PATH

 

Run jjtree on the .jjt file to generate the .jj file. E.g.:

jjtree l1a.jjt

Run javacc on the .jj file to generate the parser classes and the AST classes. E.g.:

javacc l1a.jj

Run javac to compile the .java files. E.g.:

javac *.java

Run java with the name of your interpreter class, and the name of the test input file. E.g.:

            java L1aIntepreter test1.l1a

 

NOTE: If you want to use Eclipse for running JavaCC, follow these instructions to set up your environment (You will have to download the JavaCC plugin and install it in your own space).

Testing your interpreter

Your interpreter must produce the correct store when given the C program in the table above. Test your interpreter with simpler programs before you give it the one above.

NOTE: Do not use cut and paste to create your C program - invisible characters from the HTML will cause mysterious syntax errors. Instead type the program into a new file.

What To Turn In

When you are done, make a Java archive with the command ‘jar cvf L1a.jar *.java l1a.jjt’, and submit it through the submission page (accessed from the Homework page).

Due date

February 18th., before midnight..