CS245 Lab 3: Symbol Tables

Add, to your translator project, a symbol table data structure. The symbol table should provide the following functions:

struct st_info {          // This structure lists the things we need to know about any symbol
    int value;                // For this lab, we only need to store the value of integer constants
};

void enter(Table &t, string name, st_info information);  // enter info. into table T
st_info look(const Table &T, string name);                     // look for name "name" in table T
void beginScope(Table &t);                                              // enter a new scope
void endScope(Table &t);                                                 // leave the most recent scope

If you wish to make these member functions of class Table, that is fine.  Alternatively, you may use a class (or struct) with public data, and use regular functions as above.  You may find it convenient to have a "print" function which prints the entire symbol table for debugging purposes.

The symbol table should be used to extend our language to allow a simple variation of the lisp "let" special form. We will continue our tactic of using single letter input to avoid the need for a lexical analyzer, allowing only single-letter variable names, and using "l" as an abbreviation for let. Our "l" special form will take three parameters: the variable name, the value (a single digit integer constant), and an expression (which could contain anything that was legal before, or a use of a defined variable, or another variable definition).

In your translated results, you should substitute the value of the constant for its name.

For example, the following inputs are legal, and should produce the given infix translated results:

(l x 5 x) should produce 5

(l x 5 (+ x 1)) should produce 5 + 1

(l x 5 (l y 2 (+ x y))) should produce 5 + 2

As before, your program should respond to syntactically incorrect input by printing an error message and returning an error code other than 0 from main (this error code also be produced by calling the function "exit" when there's a problem). Your program only needs to translate one expression each time it is run, but if you wish to process all the expressions until the end of input, that is also fine.

Those of you who are familiar with C++ classes may want to take a look at lab 5 before doing this lab.

Your symbol table files should be part of the translator project. Use cvs add (if necessary) and cvs commit to hand in your files.