Homework 5
Symbol Tables and Type Checking  

powered by FreeFind

Modified: 

Overview

Type checking is necessary for strictly typed languages, such as MiniJava, to verify that operations are not being misused, for example arithmetic is only allowed on int. As described in Chapter 5, type checking is performed by:

Homework 3 defined the concrete grammar necessary for SableCC to generate a parser, however an abstract grammar results in a simpler solution for the type checker.

Homework 4 introduced the concrete to abstract grammar transformations of SableCC3 which will be applied to the MiniJava language. The implementation of the interpreter used the the DepthFirstAdapter class to visit nodes of the AST.

Homework 5 will combine these ideas with that of a symbol table to implement a type checker. You will be given the complete abstract grammar definition of MiniJava in SableCC3 form and skeleton classes for the necessary Java.

The given components will type check some parts of a MiniJava program and ignore the checking of others; the result is that incorrect typing is ignored by the skeleton classes in many cases. Homework 5 is to complete type checking.

The organization of the type checker is illustrated and described below.

Assignment

  1. Complete type checking for MiniJava. Print an error message when type violations are detected. Use the Java typing conventions.
  2. Type check any 3 provided MiniJava test programs.  

Files

Download and unzip HW5 file, a HW5 and 4 sub-directories will be created. The TypeCheckVisitor directory contains the necessary TypeCheckVisitor.java file to complete.

Getting Started

The HW5 directory contains a BlueJ project. To execute the partially completed type checker:

  1. Generate the minijava parser as described above.
  2. Start BlueJ and open the project.
  3. Right click on Main and compile (or Select | Tools | Compile Selected).
  4. Right click on Main and void main(arguments).
  5. Enter the name of a test file as: {"Factorial.java"}

Turn In

  1. Cover sheet with your name, C431, date, and Homework 5.
  2. Print out of TypeCheckVisitor.
  3. Print out of test results.

Things you need to know

  1. Grammar minijava.js
  2. Any Java files
  3. Productions as Java - SableCC generates a node directory which contains the class files defining each production and alternative. It is useful to look at the file names (e.g. p1 = ... will be in file node\AP1.java) and the contents (e.g. the grammar p1 = id p2; defines class fields of for id and p2 along with getId and getP2 methods to access the fields).
  4. Recursive production classes (e.g. statement)

        statement {-> statement} = {statementlist} l_brace statement* r_brace

    generates class AStatementlistStatement (a list of any statement) and class PStatement (any statement). The PStatement (which is any statement) can be used to visit each statement in the statement list (which is a LinkedList of Object). Recall that node.apply(this) calls the method for class node. To call apply requires a cast of an Object in the LinkedList to the correct class; since the AStatementlistStatement is a LinkedList of PStatements (which can be any statement alternative) apply iteration over the list can use the cast as:

    Iterator sl = ((LinkedList)node.getStatement()).iterator();
    while ( sl.hasNext() ) ((PStatement)sl.next()).apply(this);

  5. DepthFirstAdapter
  6. Java class - SableCC generates classes for each production defined in the MiniJava grammar; these classes inherit from Node, are defined in the minijava\node subdirectory and serve as the types recognized by DepthFirstAdapter and our own classes. The name of a Java class, such as AIntType, can be returned for methods and variables using the Object method getClass(). An example is given in Print() above where the type of a Variable object is printed. Note that Type() returns a reference from the SymbolTable to a Node object in the parse tree, in this case the object referenced is Variable v.

            Enumeration ge = c.getGlobals();

            while(ge.hasMoreElements()) {

                Variable v = (Variable)ge.nextElement();

                System.out.println("\t"+"\t"+v.id()+"\t<"+v.Type().getClass()+">");