CpS 450 Language Translation Systems

Phase 3: Semantic Analysis

Project Setup

Create your submission repository, and copy your dream folder into it, replacing the starter code that is there.

1. Create a SymbolTable class

See the Symbol Table notes for more details.

Your SymbolTable must be accessible to multiple tree walker classes, because it will be used both during semantic analysis and code generation. One way to do this is to make it a singleton.

2. Define a Type class

Define a new class named Type, like this:

public class Type {
        public static final Type
                ERROR = new Type("<error>"),
                INT = new Type("int"),
                BOOLEAN = new Type("boolean"),
          ...

        protected String name;

        protected Type(String name) {
                this.name = name;
        }

}

This class defines several type constants that you will use in your semantic checker by writing Type.INT, Type.BOOLEAN, etc.

3. Create a SemanticChecker class

Create a class named SemanticChecker. This class will contain methods to traverse the parse tree and conduct the semantic checks. You might start by creating a traverse() method to start the traversal at the root of your parse tree. See the TinyInterpreter class in the examples/interpreter project for inspiration on how to organize your traversal code.

As an initial goal, I suggest writing sufficient traversal logic to process variable declarations and assignment statements. For starters, just do variable declaration checks (no type checks) to exercise your SymbolTable class. Then, add code to your main program to invoke your SemanticChecker on the parse tree (see step 5), and run your program on the test programs in /tests/phase3/semcheck/decls.

After you’ve got that working, you can move on to type checking.