Game Programming Language
Phase 4: Symbol table

NOTE: the gpl.cpp file originally posted w/p3 was missing some includes that the new compiler requires.  Please download a new copy of gpl.cpp from the p4 source directory (it is part of p4_src.tar).


Overview:

Implement a symbol table for the gpl interpreter (class Symbol and class Symbol_table)

Add actions (code inside of the {} after a production) to your gpl.y that will insert integer, double, string, and array declarations into the symbol table.

In order to test your symbols, initialized all the integers to 42, all the doubles to 3.145, and all the strings to "Hello world"  In the next assignment you will implement expressions which will be used initialized variables.

You may work individually or with one other student, even if you did the previous projects on your own. 



Requirements:

You must implement two classes: class Symbol and class Symbol_table

Class Symbol_table must be implemented as a singleton (see singleton for an example of a singleton)

You must implement class Symbol to hold the name,  value, and type of each variable. You may use the enumerated type Gpl_type to store type, but it is not required.

The order you print your variables must match the order in my tests cases.  I use an STL map keyed on C++ strings.  If you use an STL map keyed on C++ strings, your print order will match my print order.  If you use some other mechanism, or a map keyed on C-style strings, you will have to sort your variables when you print them.

class Symbol_table must have the following public functions (because I will call these functions in gpl.cpp to test your Symbol/Symbol_table classes):

    	static Symbol_table *instance();

Symbol_table::print(ostream &os)

prints all the elements of the symbol table in the following format (order is unimportant)

<type> <name> <value>

for example:

int a 42
double x 3.145
int count 42
string name "Hello world"

Note:  add " " when printing string constants, but the actual string should NOT to contain quotes (your scanner should strip the quotes off of string constants)

For this assignment the following rule in the grammar:
simple_type T_ID T_LBRACKET expression T_RBRACKET
Must be changed to:
simple_type T_ID T_LBRACKET T_INT_CONSTANT T_RBRACKET
In the next assignment you will implement expressions and will have to change this rule back.

Arrarys are implemented by creating a series of variables.  For example:
int nums[5];

is as if you created variables with names that include "[" and "]"
int nums[0];  // gpl does not allow a variable to be named like this
int nums[1];
int nums[2];
int nums[3];
int nums[4];
Thus when your symbol table is printed it will show the elements not the array.  This little hack makes implementing arrays much easier.

Note:  Even though arrays elements are inserted into the symbol table with generated names, it is an error to have another variable with the same name.  For example:

int nums[3];
int nums;        <== this is an error


Hints:

The current grammar (the one in p3) initializes ints, doubles, and strings using an expression.  It also uses an expression for the size of arrays.  Building the code for the expressions is hard and is the bulk of the next assignment.  For this assignment give default values to variables (see above) so that you can test your class Symbol's set and print functions.

Write and test your Symbol and Symbol_table classes before you call them from actions embedded in your gpl.y.  In other words, write a stand alone program to test your Symbol and Symbol_table classes.  It is easier to test code when bison is not involved.

It is easiest to use the standard template library map to implement class Symbol_table.

I included a lot of error checking asserts in my program.  It made it much easier to debug.  I suggest that you use asserts liberally.  When programming, every time you think a variable has a specific value, write an assert statement.

You can convert an integer to a string using either ostringstream  or sprintf().


Where to start:

Start with your solution to p3.  Download the two new files posted for p4.  Remember to update your Makefile and then run makedepend.

Files Purpose Changes
gpl_type.h Contains three enumerated types for types, operators, and error status You may change this file but probably won't need to change it.
gpl_type.cpp Provides routines for converting the enumerated types in gpl_types.h into strings.  This is helpful for debugging and for printing. You may change this file but probably won't need to change it.



Implementation order:

class Symbol is pretty simple.  I recommend implementing it first.  Then get class Symbol_table working.

After both these classes are written and tested, then work on adding actions to your gpl.y to create new symbols and to insert the symbols into the symbol table.




Turning in and Testing:

See docs/turnin.html for a description of how to turn in assingments.

See docs/testing.html for a description of how to test your program.