H2: Symbol Table

Assignment: Symbol Table

In this homework you will be given a header file symtable.h and a driver file driver.cc. (The tar-file is here.) Header file declares class Symbol and class Table. Your task is to define the implementation of these classes. Put all your code into symtable.h. Driver file provides a very simple command line interface to the symbol table in order to test it. Do not modify driver.cc.

Description:

Symbol Table is a stack of scopes. Every time you enter a procedure you push another scope onto the stack. When you leave the procedure, you pop the scope from the stack. Within a scope, names are defined and stored in a symbol table. When a name is encountered in the program, it is looked up in the symbol table in order to obtain important information about this name (e.g. kind: procedure, or variable, or constant; type: int, pointer, etc.). For this assignment, just an integer value needs to be associated with a name, but in your program you will need to store more information about names.

A name defined in a local scope hides the same name defined in an outer scope. It is an error to define the same name twice in the same scope. Symbol lookup goes through all scopes on the stack and returns the first found definition of the name. If no definition is found, an invalid symbol is returned. Returned symbol is tested with Symbol::valid().

The Table::print() function, for each scope, starting with the innermost, prints 'Level' <space> <level number> and each name in the scope, one per line. Table::level() returns the level of current scope. Here's a sample session:

> driver
i a
i b
i a
insert failed
l a
a: 1
+
i a 
l a
a: 3
+
i b
l b
b: 4
p
Level 3
b
Level 2
a
Level 1
a
b
-
s
level = 2
x
> 

Turnin:

Copy all your source files, including a Makefile, to your turnin directory by the specified time. Do not include binaries or object files. The sample Makefile has a target 'turnin', so that when you are done, all you need to do is 'make turnin' (unless your project is different from the assumptions in the Makefile).

Grading:

Grading will be performed automatically by running a script and comparing actual output to expected output, therefore, make sure you do not modify driver.cc.
vin@nd.edu
Last modified: Wed Jan 23 14:37:15 EST 2002