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.
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; } } |
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.
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).
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.
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).
February 18th., before midnight..