Project 2: MiniJava Parser

Due 11:59pm Wednesday February 20

Useful Links

Description

You will use JavaCC to implement a parser for the MiniJava language, which will generate abstract syntax trees for MiniJava.

Resources

Before starting this project, familiarize yourself with Chapters 3 & 4 of the textbook. In addition to the lectures, this will give you a solid foundation for starting your parser.

Files to get started with are available in $MJ/chap4. The parser is generated from the input file MiniJava.jj.

Much of the parser has already been written for you but you must fill in the gaps by adding code for the productions that haven't been written.

If you have questions please ask either the instructor or a TA, or post a message to the class news group at purdue.class.cs352.

Getting Started

As in project 1, this lab requires you to run the setup script to initialize your environment. This script is either:

	source /homes/cs352/SETUP.csh

if using csh or tcsh, or

	. /homes/cs352/SETUP.sh

if using sh or bash.

You can then copy the code from $MJ/chap4. This can be done with the following command:

	cp -R $MJ/chap4 .

You can now use this copy as your working copy, so simply

	cd chap4

to begin working on it. All commands below will assume that you have first set your current directory to the chap4 subdirectory using the above command.

All changes for this project will be done to the MiniJava lexical/syntactic specification file MiniJava.jj in the chap4 subdirectory. You can build the parser from this specification file by using the commands:

	javacc MiniJava.jj
	javac MiniJava/*.java

You should run the given javacc command every time you change the MiniJava.jj specification file. This will generate Java source code files in the package subdirectory MiniJava. You can compile these Java source code files using the given javac command. You can run the parser (which also drives the scanner) with the command:

	java MiniJava.Parse file.java

where file.java is a MiniJava source code file. Take a look at the implementation of MiniJava/Parse.java that is generated from MiniJava.jj to see the main method there.

Your parser should also produce an abstract syntax tree, which can be pretty-printed using the command:

	java MiniJava.Print file.java

This command first runs the parser before printing the AST result. Take a look at MiniJava/Print.java to see the pretty-printer, which traverses the AST produced by the parser and prints it to the standard output.

Some guidelines for testing your parser

The directory $MJ/examples contains some example MiniJava programs. I have provided sample pretty-printed output for these examples. However, in testing your parser you should aim to approach the project one syntactic construct at a time, hypothesizing correct output for a given construct and attempting to produce that output from a simple test case.

You will be building an abstract syntax tree for the parsed program. You will need to become familiar with the MiniJava/Absyn.java implementation of MiniJava ASTs. Understanding this file will help you understand how to use the classes it defines in building ASTs with your parser.

At the top of the MiniJava.jj file, there is a line of code that begins with the string options. This specifies options to the javacc command. We suggest that when debugging your parser you use the options DEBUG_PARSER=true and DEBUG_LOOKAHEAD=true to aid in understanding the operation of your parser. These options are described in the javacc documentation. They allow you to "see" each step of the parser as it executes.

If you use these options, please try to remember to take them out before turning in your solution.

Turnin

You should only need to turn in your MiniJava.jj file, since that is the only file you need to change:

	cd chap4
	turnin -ccs352 MiniJava.jj

Grading

For this project (and other projects), we will create a set of test cases, and then compare the output of your solution to our reference implementation. Grading test cases are intended to test separable functionality of your parser.

There will be a reconciliation period after initial grading when you will be able to compare outputs of your parser with those of the reference implementation, and you may then offer simple fixes for the TAs to apply to your implementation to improve your grade. These fixes will be accepted only at the discretion of the TAs and the instructor -- we will judge what "simple" means!

The reconciliation period is only intended for you to be able to fix simple problems that you may have mistakenly overlooked. Thus, you must make sure to test your implementation thoroughly.