// a small program to test the symbol table package // by CMB 9/04 // version using a template #include #include #include "StringTab.h" #include "SymTab.h" // a typedef such as the following can make typing less tedious // when you are using template classes. Highly recommended. typedef SymTab SymToSymTab; void reportLookup(SymToSymTab *symtab, Symbol *key) { Symbol *v = symtab->lookup(key); if (v == NULL) { cout << key << " not found in table" << endl; } else { cout << key << " found in table with value " << *v << endl; } } void reportLookupCurr(SymToSymTab *symtab, Symbol *key) { Symbol *v = symtab->lookupCurr(key); if (v == NULL) { cout << key << " not found in innermost scope" << endl; } else { cout << key << " found in innermost scope with value " << *v << endl; } } int main () { SymToSymTab *sym = new SymToSymTab(); SymToSymTab *inner; // to create entries, we need the Symbol objects to put in // them. The local variables below are just for convenience to make // the later "add" expressions simpler. StringTab myTab; // a StringTab to hold the names Symbol *intSym = myTab.addString("int"); Symbol *boolSym = myTab.addString("boolean"); Symbol *x = myTab.addString("x"); Symbol *y = myTab.addString("y"); Symbol *foo = myTab.addString("foo"); Symbol *z = myTab.addString("z"); Symbol *blob = myTab.addString("blob"); Symbol *bar = myTab.addString("bar"); Symbol *MyType = myTab.addString("MyType"); sym->add(x, intSym); sym->add(y, intSym); sym->add(z, intSym); // create an inner scope, using sym as the outer scope inner = new SymToSymTab(sym); // add some stuff to the inner scope inner->add(x, boolSym); inner->add(foo, boolSym); // create and add some stuff to an even more inner table SymToSymTab *innermost = new SymToSymTab(inner); innermost->add(blob, intSym); innermost->add(x, MyType); cout << "*****************************" << endl; cout << "*** Outer table: " << endl; sym->dump(cout); cout << "*****************************" << endl; reportLookup(sym, blob); reportLookup(sym, y); reportLookup(sym, x); reportLookupCurr(sym, blob); reportLookupCurr(sym, y); reportLookupCurr(sym, x); cout << "*****************************" << endl; cout << "*** Inner table: " << endl; inner->dump(cout); cout << "*****************************" << endl; reportLookup(inner, blob); reportLookup(inner, y); reportLookup(inner, x); reportLookup(inner, foo); reportLookupCurr(inner, blob); reportLookupCurr(inner, y); reportLookupCurr(inner, x); cout << "*****************************" << endl; cout << "*** 3-level table: " << endl; innermost->dump(cout); cout << "*****************************" << endl; reportLookup(innermost, blob); reportLookup(innermost, y); reportLookup(innermost, x); reportLookup(innermost, bar); reportLookup(innermost, foo); reportLookupCurr(innermost, blob); reportLookupCurr(innermost, y); reportLookupCurr(innermost, x); reportLookupCurr(innermost, bar); return 0; }