Project B: The MiniJava Parser

Due: Thursday, February 5, 2009 at 11:00 pm. Use the online dropbox to turn in your files

In this assignment you will extend the initial MiniJava parser and AST representation with the extensions described in the course project description handout.

You should extend MiniJava's syntax to allow the following (all of which are legal in full Java too):

You should follow the precedence and associativity rules of regular Java for these extensions. It's OK to use CUP's precedence declarations to achieve this. It's OK to have one shift/reduce conflict in your CUP grammar, for the "dangling else" problem. Add the -expect 1 option before the minijava.cup argument in the Makefile to build Parser/ if you decide to accept this shift/reduce conflict. (FYI, in one sample solution, the author couldn't find a way to revise the CUP grammar specification to avoid this conflict.)

You should add new AST classes and/or modify existing AST classes so that you can represent the new MiniJava constructs. You should define the appropriate toString operations on these classes so that they can be pretty-printed in a form that is syntactically legal and produces the same AST if it is parsed again. The other operations required of AST nodes, e.g. typechecking, evaluating, and lowering, you should implement by throwing UnimplementedError exceptions (for now).

You only need to get the parser to work (and keep the extended scanner working). You do not need to do anything to enforce type checking rules or other semantic-analysis constraints on the input program.

Do the following:

  1. Extend the specification of MiniJava's syntactic structure to describe the extended language, in the same style. (You can assume precedence and associativity is specified separately, and it is OK to define a grammar that is ambiguous with respect to the "dangling else" problem.)
  2. Add and/or modify classes in the AST subdirectory to represent the extended language.
  3. Extend Parser/minijava.cup to parse the extended language and construct the abstract syntax tree representing the parsed program.
  4. Develop test cases that demonstrate that your extended parser and AST classes work, both in cases that should now be syntactically legal and in cases that should still be syntactically illegal. (Since the parser quits at the first error, you'll likely need several illegal test case files to test the different illegal cases.) You do not need to check for lexical errors, just syntactic errors. The SamplePrograms directory contains some files that should parse after you make your changes; some of the files should also parse successfully with the initial version of the MiniJava compiler.

You can use the -parse -printAST options to the MiniJava compiler to just run the parsing phase and print out the AST that it builds. See the test_parser target in the Makefile for an example, and feel free to make your own target(s) to make running the tests you like easier and more mechanical.

Turn in the following:

  1. Your extended MiniJava syntax specification.
  2. Your modified minijava.cup file. Clearly identify your changes using comments.
  3. Your new and/or modified AST/*.java files. Clearly identify any modifications using comments.
  4. Your test cases, with names of the form for test cases that should parse successfully and for test cases that should trigger syntax errors.
  5. A transcript of running your parser and printing out the resulting AST on each of your test cases (at least).

Create a single archive containing these files, and submit it using the online dropbox by the due date.