Programming assignment 3
Managing data structures and attributes
CSCI 5220
Spring 2002

Due: April 1. Hand in as assignment 4 using handin. Please be sure to turn in all source files, including header files, and put your name in each source file.

This assignment is to add the beginnings of semantics to your compiler.

Types

Your program needs to be able to keep track of types. Create a data type for types. The types are of the following forms.

  1. Integer
  2. Real
  3. Integer array. Store the upper and lower bounds of the array in the data structure.
  4. Real array. Store the upper and lower bounds of the array in the data structure.
  5. Function. Store the return type and a list of the parameter types.

Modify your parser so that each type has an attribute that is a type data structure.

Tables

Add a table manager to the compiler. There should be one table for global variables and one for local variables. Each time you start another function declaration, clear out the table for local variables.

The table needs to store a type with each declared variable, so that the type information is available wherever you need it. The global table should store the type of each function.

The table should also provide information on where each variable is located. Each variable is either a global variable, local variable or parameter. If it is a global variable, store the offset among the global variables where it is found. If it is a local variable or parameter, indicate which it is, and store the offset among the local variables or parameters where the variable is found.

The global table also needs to store functions. For a function, store the return type and the parameter types.

Write a function that finds the information about a variable. It should first look in the local variable table, and then in the global variable table. If the variable does not exist, it should provide an indication of that. Also write functions to create new entries in the tables.

Modify the grammar to put entries into the tables at declarations. Think about how you can tell whether a declaration is a global variable or a local variable. Be sure to put information into the table for function declarations.

Syntax trees

Define a data structure for syntax trees. Each tree should represent the body of a function. All of the relevant information about the body of a function should be present in the tree.

Modify your parser so that each expression or statement produces a tree that describes its structure.

Thinking ahead

Your next assignment will be to add a stage that traverses the syntax tree and writes out a translation of each function into a byte code. We will use the byte code described in an abstract machine. You will want to see how this byte code works, and think about how the translations can be done from the tree. Some tools for using the byte code will be made available shortly.