Unformatted text preview:

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 8 — Semantic Analysis Project Wednesday, September 21 DUE: Thursday, October 06 Warning: The static semantics phase of the project requires considerably more effort than the previous phase. Extend your compiler to find, report, and recover from semantic errors in Decaf programs. Most semantic errors can be checked by testing the rules enumerated in the section “Semantic Rules” of Handout 6. These rules are repeated at the end of this handout as well. However, you should read Handout 6 in its entirety to make sure that your compiler catches all semantic errors implied by the language definition. We have attempted to provide a precise statement of the semantic rules. If you feel some semantic rule in the language definition may have multiple interpretations, you should work with the interpretation that sounds most reasonable to you and clearly list your assumptions in your project documentation . This part of the project includes the following tasks: 1. Add semantic actions to your CUP parser specification to construct a high-level intermediate representation (IR) tree, and to perform any semantic checks you choose to do as part of the parsing process. The problem of designing an IR will be discussed in the lectures; some hints are given in the final section of this handout. When running in debug mode, your driver should pretty-print the constructed IR tree in some easily-readable form suitable for debugging. 2. Build symbol tables for the classes. (A symbol table is an environment, i.e. a mapping from identifiers to semantic objects such as variable declarations. Environments are structured hierarchically, in parallel with s ource-le vel constructs, such as class-bodies, method-bodies, loop-bo dies, etc .) 3. Perform all remaining semantic checks by traversing the IR and accessing the symbol tables. Note: the run-time checks are not required for this assignment. What to Hand In Follow the directions given in Handout 3 when writing up your project. Your design documentation should include a description of how your IR and symbol table structures are organized, as well as a discussion of your design for performing the semantic checks. The electronic portion of the hand-in procedure is similar to that of the previous segment. Provide a gzipped tar file named leNN-semantics.tar.gz in your group locker, where NN is your group number. This file should contain all relevant source code and a Makefile. Additionally, you should provide a Java archive, produced with the jar tool, named leNN-semantics.jar in the same directory.Unpacking the tar file and running make should pro duce the same Java archive. With the CLASSPATH set to leNN-parser.jar:<directory>/java_cup.jar:.., you should be able to run your compiler from the command line with: java Compiler <filename> The resulting output to the terminal should be a report of all errors encountered while compiling the file. Your compiler should give reasonable and s pecific error messages (with line numbers and identifier names) for all errors detected. It should avoid reporting multiple error message s for the same error. For example, if y has not been declared in the assignment statement “x=y+1;”, the compiler should report only one error message for y, rather than one error message for y, another error message for the +, and yet another error message for the assignment. After you implement the static s emantic checker, your compiler should be able to detect and report all static (i.e., compile-time) errors in any input Decaf program, including lexical and syntax errors detected by previous phases of the compiler. In addition, your com piler should not report any messages for valid Dec af programs. However, we do not expect you to avoid reporting spurious error messages that get triggered by error recovery. It is possible that your compiler might mistak-enly report spurious semantic errors in some cases depending on the effectiveness of your parser’s syntactic error recovery. Your semantic checker should verify that all decimal and hex integer literals in the input (which your scanner will have stored simply as strings) have numeric values within the range permitted by the specification in Handout 5. As mentioned, your compiler should have a debug mode in which the IR and symbol table data structures constructed by your compiler are printed in some form. This can be run from the command line by java Compiler -debug <filename> Test Cases The test cases provided for this project are on the course server. Read the comments in the test cases to see what we expect your compiler to do. Points will be awarded based on how well your compiler performs on these and hidden tests cases. Complete documentation is also required for full credit. Semantic Rules These rules place additional constraints on the set of valid Decaf programs besides the constraints implied by the grammar. A program that is grammatically well-formed and does not violate any of the following rules is called a legal program. A robust compiler will explicitly check each of these rules, and will generate an error message describing each violation it is able to find. A robust compiler will generate at least one error message for each illegal program, but will generate no errors for a legal program. 21. No identifier is declared twice in the same scope. 2. No identifier is used before it is declared. 3. The program contains a definition for a method called main that has no parameters (note that since execution starts at method main, any methods defined after main will never be executed). 4. The �int literal� in an array declaration must be greater than 0. 5. The number and types of arguments in a method call must be the same as the number and types of the formals i.e., the signatures must be identical. 6. If a method call is used as an expression, the method must return a result. 7. A return statement must not have a return value unless it appears in the body of a method that is declared to return a value. 8. The expression in a return statement must have the same type as the declared result type of the enclosing method definition. 9. An �id�


View Full Document

MIT 6 035 - Semantic Analysis Project

Download Semantic Analysis Project
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Semantic Analysis Project and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Semantic Analysis Project 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?