#ifndef _STRINGTAB #define _STRINGTAB /* * StringTab and Symbol interface file * created by CMB 8/04 * * * MOD 3/21/06 by CMB * changed << operator on pointer to take pointer to const object * * MOD 9/28/04 by CMB * added << operator on Symbol & for printing symbol table * to interface file. * * MOD 9/21/04 by CMB * added << operator on Symbol * useful for printing ASTs * also changed getName to return const-ref (so it doesn't copy it) * * MOD 9/15/04 by CMB * added #include to get it to compile with g++ version 3 * * * StringTab class: * The StringTab class is for storing the strings used while compiling. * (e.g., string constants or identifiers from the source code) * It stores the strings uniquely. E.g., if you insert the same string * a second time, it returns the first one in the table * (using a Symbol*). * * Member functions: *** StringTable st; * Creates an empty StringTable. * * Symbol *sym; *** sym = st.addString(someString); * Adds someString to the StringTable st. * If someString is already present in the table, returns a pointer * to the existing symbol instance. * Once we add a string to the table, we access the string * via a Symbol pointer (sym). * *** st.dumpStringTab(myOStream); * Prints the strings in the table to myOStream, one per line. * * * Symbol class: * * Since Symbols are unique if they are created via the same StringTab * object, we can compare two Symbol *pointers* find out if they * are the same symbol. (but not if they are from different * StringTabs) * * One may not create Symbol objects outside of a StringTable. * * Symbol *sym; * *** sym->getName(); * Get the string itself * * o << sym; * Print out the sym * */ #include #include #include using namespace std; class Symbol; class StringTab { private: typedef map StrTabMap; StrTabMap tab; public: Symbol *addString(string newId); void dumpStringTab(ostream &o); }; class Symbol { public: const string &getName() const { return id; } private: string id; friend class StringTab; // constructor is private: only allow StringTab to create symbols Symbol(string newId) : id(newId) { } }; ostream& operator<<(ostream& o, const Symbol *symPtr); ostream& operator<<(ostream& o, const Symbol& symbol) ; #endif