In this assignment you will implement a semantic analyzer for your Decaf compiler. Your semantic analyzer will construct an abstract syntax tree and then perform various semantic checks including type checking.
Your AST may be a simplified form of your parse tree. The AST can be built to more closely match the original grammar of the Decaf language specification. Remember, this original grammar was slightly modified to make it LALR(1) and therefore acceptable to Yacc. You may choose to simplify your tree so that Kleene closure is represented by multiple subtrees instead a long, deep branch.
This list is not meant to be exhaustive; you may find other semantic checks that are not on this list.
Semantic errors should be reported. Semantic error messages should include the number of the line responsible for the error.
Your symbol table code will need to be augmented. Your symbol table entries still consist of strings (identifiers), but you will include information about type and possible structure (indication of an array, parameter list for a method, etc.).
You may find it convenient to have each node in your AST point to the symbol table to be used to analyze the section of code that is represented by that node.
It is very important to get the symbol table code integrated correctly. If you must compromise between increasing the number of semantic checks to perform or getting the symbol table code to work flawlessly, opt for getting the symbol table code to work. For one thing, your checks cannot really work very well anyway with faulty symbol table code. Perhaps even more importantly, correct symbol table management is essential to the final part of the project, code generation. If you perform no semantic checks but set up the symbols tables correctly, your code generator (if implemented correctly) will be able to emit correct assembly code for correct Decaf programs. (Illegal Decaf programs do not progress to the code generation phase.)
Your protocompiler should produce a set of error messages:
Each error message should consist of:
If the program has no errors, nothing should be printed.
You should do all your developement within a single directory. When you are ready to submit the assignment, remove all executable and object files from this directory and use the submit415 command while in this directory to submit your assignment. Provide a make file, so I need only unpack your files, run make, and then execute your program. The executable should be named decafc. It should be possible to execute this program on a file using the command
decafc < filename
Hand in a brief write-up describing
Your semantic analyzer will be tested against a variety of test files.
You source code should use comment blocks to explain any non-obvious operations (for example, how you handle errors).
The possible number of points for this assignment is 10. Points are weighted as follows:
Correctness of your program including the completeness
of your semantic checks |
8 |
Error messages are informative | 1 |
Source code is readable and well commented; write-up is well structured and informative |
1 |
I will deduct 1 point if your program does not build flawlessly.
Your protocompiler should not produce segmentation faults. I consider segmentation faults to be a strong indication of poor programming quality; please do your best to have your program behave gracefully in all situations.