CSCI 310 - Project 4: Parser (with abstract syntax tree) for C2008 language

Assigned: March 3, 2008

Due Date: March 31, 2008 at 11:59 pm

Enhance your recursive-descent parser from project 3 so that it produces an abstract syntax tree for the C2008 language. The tree should reflect the associativity and precedence of the language. Use good style (including meaningful variable names and appropriate indentation) in your coding and be liberal with comments.

Your parser should be written so that doParse returns a reference to the tree structure for the program being parsed and should include an option (set using a boolean command line argument) to print the tree (given a reference to the tree structure) with appropriate indentation so that the tree structure can be recognized. You’ll need to change the parser so that the number of errors found in parsing is kept as a data member of Parser and has an accessor method (rather than being returned by doParse).  So, you should have code something like this in your main program:

TreeNode progtree= myparser.doParse(traceparse);
System.out.println(“Number of errors “+myparser.getNumErrors());
PrintVisitor p=new PrintVisitor();
if (progtree != null && tracetree){
   progtree.apply(p);
}
 

The PrintVisitor class should utilize the visitor pattern to print the tree as in Project 1. The PrintVisitor should inherit from Visitor.  In a later project, we will create another type of visitor that also inherits from Visitor.  If errors are encountered during parsing, the tree does not have to be correct. Just make sure that the program ends gracefully (no Java exceptions) if errors are encountered. This means that you need to be careful to check for null references while accessing the tree. You should create a class for each node type on the sheet given out in class as well as Statement, Declaration, and Expression classes.  TreeNode should be the abstract base class and the three classes above inherit from that and are also abstract.  The other classes inherit from one of these three.  

You should test your parser on test cases and make corrections as necessary. As always, you will also be graded on the quality of your test cases.  Include comments in your test cases indicating what is being tested in each.  The handin name is project4.