Programming Assignment #4



Getting Started

Locate and install the Labs/4 project into your eclipse workspace following the usual instructions

Browse this lab document to get an idea of the work you will do for this assignment.



Abstract: The symbol table is the best example of a structure deserving global treatment in a compiler based on bottom-up parsing. In this homework, you will implement symbol-table processing for our Java-like language. This requires a traversal of the AST using the Visitor pattern and an optional implementation of a symbol-table manager.

The essential task of this assginment is to associate the declaration of a name to the relevant uses of that declared name.



Notes

For this assignment, you need a working implementation of Lab 3, in the form of a parser that creates an abstract syntax tree (AST). The AST will need a handful of specialized nodes so that the symbol table visitor can intercept and handle symbol declarations and uses.

There are two ways for you to do this, and either one is acceptable:

1.) You can use the supplied parser component found in the coursesolutions.courseparser package.

2.) You can use your own implementation:

* Copy your jmm.cup from Lab 3 into the top directory of this project.

* In the rule for the goal symbol CompilationUnit, be sure to set RESULT to the root node of the parse tree in the semantic action.

Tips for using your own AST implementation

Chances are your AST will not work as you submitted it for lab 3. This is okay! Now is a great time to go back and update the design of your AST to include additional functionality. In particular, you may want to:

- Create and use interfaces to group your nodes together. For example, any nodes that can declare identifiers (and therfore need to be added to the symbol table) could be grouped under a single interface. This would allow you to process each of these nodes using the same method, passing the interface as an input argument. See the Javadoc for the course solution to give you ideas of what interfaces you could create.

- Add fields to nodes to track type information. After this lab is over, every node that contains an identifier will need to be decorated with a SymInfo object, so you will need to make sure these nodes have a SymInfo field that can be set. You could consider using an interface for this task as well.

- You may find as you go through that your design is a little clunky or inefficient. Now is a great time to restructure your AST to make it as efficient as possible.

Running the code

The parser is chosen by the static method getParser(String[]) found in SymtabVisitor. The comments there instruct you how to change from the course reference parser to your own.



The ant run target runs your compiler on each file found in the TestFiles directory.

Take note of the following classes and their usage in this lab:
SymtabVisitor Found in the submit package, this is the only file you will submit. It contains the visitor as a public class and an implementation of a symbol table as a non-public class. Instructions on its use are found in the code.
CourseSymtabVisitor This is the reference implementation of the visitor, found in classsolutions.jar. Your installed code will use it until you make the appropriate change in SymtabVisitor, as directed by the comments.
Be sure to make that change, so that your symbol table vistor is tested.
CourseBuildSymtab This is the reference implementation of the symbol table itself, found in classsolutions.jar For this lab, you can continue using this one unless you want to use your own, or the one you developed in studio, in which case you should make the appropriate change in SymtabVisitor.


As installed, you are running the reference solution. Take a look at the AST dump to see the structure of the AST (or use your own from Lab 7). The indentation of the symbol-table pass shows the nest level.

For hints on how to do the visitor, the documentation for the reference symbol-table visitor . shows the visit methods present in that solution:

ClassDeclaring: Check that the name isn't already in the symbol table. Be sure to pass the visitor along to this node's children, as with BlockIsh.

MethodDeclaring: Check that the name isn't already in the symbol table. You should then pass the visitor on to the parameters and to the method's body. What you do with nest levels in and around those calls will affect how scoping works in your compiler. Document your approach using comments in the code you submit.

FieldDeclaring: Check for duplicate declaration, but treat the name as though "this." were prepended to it.

LocalDeclaring: If the symbol is already declared at the current nest level, complain as directed below. Otherwise, get this symbols type information, and enter the symbol and its type information into the symbol table. Look up the symbol in the symbol table, and use the setSymInfo method to set the symbol information for this declaration in the AST (so it looks nice when it prints).

LocalReferencing: Make sure there is a valid entry for the symbol in the symbol table at this point. Call setSymInfo on this node to establish the map between the reference and its type declaration.

BlockIsh: manipulate scopes properly and pass the visitor on.



The information set and retrieved from the symbol table must be an implementation of the SymInfo interface.

You must develop a local class within SymtabVisitor (because that's the only file you turn in) that implements the SymInfo interface, and you will have to instantiate it appropriately for a symbol before you insert the symbol into the symbol table.

Just implement the getRegister/setRegister methods as stubs—no meaningful implementation of them is needed at this point.



If you detect an undeclared reference, your output must contain the phrase

Error: Undefined Reference

and indicate the offending node. The easiest way to do this is to use the out method.

Likewise, if is a symbol is already declared at the current nest level, then you must emit a message

Error: Duplicate declaration