| CIS 461/561 Home Page | Last updated 2008/05/11 20:38:00 |
This is the third phase of the project of building a compiler for the C minus language as described in Appendix A of the text book. In this phase, you will use the parser that you built in the second phase of the project.
Note that for this phase of the project, you are required to turn in a description of your symbol table and a list of all the semantic errors your parser detects as well as the code itself.
The purpose of this phase is to add semantic analysis to the C minus parser. The parser from the previous project checked the syntax and built an abstract syntax tree. But it did not do any semantic analysis. For example, if a variable was used but not declared, you can still build an abstract syntax tree because the program parses correctly according to the grammar. Likewise, if a function has too many or too few arguments at the call, the parse is still successful.
Now we are ready to add that kind of analysis to the tool and report semantic errors. Most of these will fall into the category of type errors, so we can think of this as adding a type checking pass to our parser. An essential part of doing this kind of analysis in a language like C minus where variables are typed and must be declared is to build a symbol table. The use of the symbol table must take into account scoping rules for the C minus language.
As in the previous project, you should start by understanding the implementation of the TINY compiler.
You will build on the code for the previous project, and most of that code will not need to change.
Your code will likely consist of multiple files. Make sure that you submit all necessary source files (but not object files or files generated by JFLEX or CUP). You must provide a Makefile with a default target that builds the C-minus compiler and your code must compile without errors.
If you are working on a team, make sure that each team member's name appears in comments at the beginning of your code.
All of your code files should be submitted through the class site at Blackboard. You can upload and update your files as you develop your solution or upload them all at once. Multiple submissions up to the due date are permitted, so if you want to change something after you submit, you can retrieve your solution and upload new versions or add new files.
A reasonable way to approach the project is:
You do not need to worry about efficiency, and can use arrays or vectors and linear searches for the symbol table.
Following are the files that make up a type checking version of the object oriented TINY compiler.
| Makefile | Compile commands and dependencies |
| Lexer.java | The scanner implementation (same as projectB) |
| Symtab.java | The symbol table implementation (new with this project) |
| Token.java | The token implementation (same as projectB) |
| tinyc.java | The driver for testing parsing and printing of the syntax tree (modified to now call the type checker) |
| ASTnode.java | The set of classes representing the nodes in the syntax tree (class hierarchy is the same as projectB, but typeCheck methods and some data have been added) |
| tiny.jflex | JFLEX specification (same as projectB) |
| tiny.cup | Java CUP specification of grammar and actions (same as projectB except it now references symbol table) |
If you don't use a Makefile, you can compile the parser with semantic checking as follows:
The change here is centered on defining a symbol table data structure and adding typeCheck methods to the appropriate nodes in the tree. Two different typeCheck methods are used: one for the statement hierarchy, which just descends the tree doing type checking; and the other for the expression hierarchy. The method for expressions sets the type of the expression and may be given an expected type.java -jar java-cup-11a.jar -parser TinyParser - symbols TokenType tiny.cup jflex tiny.jflex javac -classpath .:java-cup-11a.jar Token.java TokenType.java Lexer.java ASTnode.java TinyLexer.java TinyParser.java Symtab.java tinyc.java