Project 3: From concrete syntax to abstract syntax

We have already talked about how a compiler or other language system can be thought about as a series of transformations of the program from one representation to another. One such transformation is from parse (concrete syntax) trees to abstract syntax trees. In this project, you will write a visitor to do such a transformation/translation.

1. Set up

The parser, syntax tree classes, and generic visitors have already been generated for you from the Jay grammar, as well as the abstract syntax tree classes and a visitor to pretty-print the program once it has been transformed into abstract syntax. They are archived in a tar file. Copy the file from the class directory and untar it.

cp /cslab/class/cs365/proj3.tar .
tar xvf proj3.tar

A driver program is also provided in Main1.java and Main2.java. The difference will be explained below.

You should notice a change in the package structure. The packages syntaxtree, visitor, abssyntree, and astvisitor are all contained under the package jay.

2. Writing the visitor

Your task is to write a visitor which, when applied to a jay program represented as a jay.syntaxtree.Program object, will produce the same program represented as a jay.abssyntree.Program. The name of your visitor class will have to be called ToASTVisitor in order to work with the driver. You have two options:

Option 1: Make your ToASTVisitor implement jay.visitor.Visitor (actually, you'll probably want it to extend jay.visitor.DepthFirstVisitor). Include a method getResult() that returns a jay.abssyntree.Program. If you choose this option, use Main1 to test your program.

Option 2: Make your ToASTVisitor extend jay.visitor.GJNoArguDepthFirst<Object>, so that all visitmethods formally return Objects, but the method visit(Program n) actually returns a jay.abssyntree.Program. If you choose this option, use Main2 to test your program.

Either driver program will pretty-print the abstract syntax version you produce, so you'll be able to tell if it's parsing correctly. Note that as you test things, even if the error appears to happen during pretty-printing (for example, it throws a null pointer exception), that doesn't mean that the pretty-printer is to blame. It might be that your ToASTVisitor put a null reference in a place where it shouldn't be. Since you have the source code of the pretty-printer, that should help you figure out what it expects from the abstract syntax.

(Incidentally, if you do find a bug in the pretty-printer, don't just change it in your own copy. Let me know so that everyone else can have a corrected version, and so that I use a corrected version for grading.)

3. Testing

You can find two Jay examples in the directory given (add.jay and euclid.java). You are also encouraged to write your own examples and to share them with your classmates.

The command

java Main1 add.jay

or

java Main2 add.jay

will pretty-print the given file (depending on which option you choose).

4. Turn in

To turn in this project, please copy your ToASTVisitor.java to the turn-in directory I will prepare for you:

/cslab/class/cs365/turnin/proj3/xxxxxx

where "xxxxxx" is [andy|david|tyler|yelemis]. I will grade your project by running it against a collection of test files. Don't worry about indicating which option you have taken; I'll be able to figure it out.

DUE: Friday, Feb 8, 5:00 pm.


Thomas VanDrunen
Last modified: Tue Jan 29 15:10:20 CST 2008