Assignment #4: Parsing

Due: .

Do the programming exercise "Parsing", from Appel, 2nd, page 81. Turn in phase03.jar containing your Java source code (no class files) and parser written for JavaCC. You will need to consult the JavaCC documentation. Create an executable jar file named parse.jar. (See the project overview document.) The parser parses source files according to the MiniJava grammar, and determines simply whether the source is syntactically legal or not. The last line out of output of the parser must have the file name and the number of errors discovered. This line should report the number of errors reported (possibly 0) and have the following form:
filename=./path/Error03.java, errors=3

You must add error productions to handle error recovery, at least in so far as to prevent the parser from getting stuck and raising an exception that halts the compiler. You may add error productions to guide the parser to finding as many syntax errors as possible. If the input has a syntax error, the parser must write (at least) one error message to the standard error stream. Error messages contain the file name, the line number, and the character position as in the following examples:

./path/Program.java:149.23: Syntax Error:  expecting a '('
./path/Program.java:171.18: Syntax Error:  beats me!

We will use JavaCC, an LL(k) parser generator for Java.

JavaCC input

In addition to token definitions and the Java code defining auxiliary functions (like error below), your JavaCC input will contain the rules definining the MiniJava grammar. For instance, it will contain the rule defining a program as a main class followed by any number of class declarations.
void Program() :
{}
{
                MainClass() ( ClassDecl() )* <EOF>
}
These rules may contain java code. So we may wish to gain control of parsing errors by introducing a Java exception handler.
void Program() :
{}
{
        try {

                MainClass() ( ClassDecl() )* <EOF>

        } catch (ParseException e) {
                error (e);
        }
}

See Grammar 3.31 on page 69 of the textbook for a longer example.

Using the JavaCC parser

Running JavaCC creates the Java code for the parser. The name of the class file can be anything you like. In the example below it is MiniJavaParser. The example shows how to invoke the parser. Notice the initilization of the parser though the constructor. Then the whole stream is parsed with the invocation of the main grammar rule.
final MiniJavaParser parser  = new MiniJavaParser (new FileInputStream (filename));
if (verbose) parser.enable_tracing(); else parser.disable_tracing();
parser.Program();  // void Program()
Tracing/debugging output to the standard output stream can be enabled by calling the method enable_tracing(). For this to work you must run JavaCC with the option debug_parser. Note: debug output should be sent to a file, not the standard output stream. Some Java hacks can do the diversion; cf
System.setOut ();

JavaCC and Variable Lookahead

JavaCC does not rely on LL(1) parsing exclusively. See

Turning it in

Running make should create an executable compile script as usual. But for my convenience running make should produce an executable jar named parse.jar in the top-level directory (i.e., in the same directory as the make file. So testing can proceed simply in these two ways follows:

make
jar -jar parse.jar  path1/TestCase1.java
jar -jar parse.jar  path2/TestCase2.java
./compile path3/TestCase3.java
./compile path4/TestCase4.java

The submit project tag for this assignment is asgn4. For your convenience, here is a submission form for this assignment.