#ifndef GLRSYMBOLTABLE #define GLRSYMBOLTABLE #include #include #include #include #include using namespace std; /** * * Class glrSymbolTable is used to store string represetations of symbols * of grammar and to convert them to symbols and backward. */ class glrSymbolTable { public: typedef enum{ glrPreterminal, glrNonterminal } glrSymbolType; /** * * The glrSymbol type is used internally by parser to represent symbols of grammar. * In this documenation the term "symbol" is used for symbols represented * by this type and the term "string representation of symbol" is used * for symbols represented by strings. */ typedef int glrSymbol; private: map table; vector istable; public: /** * * The function returns the reference to the symbol associated with its string representation textSymbol. */ const glrSymbol &getSymbolFromString(const string &textSymbol) const { map::const_iterator ret = table.find(textSymbol); if(ret==table.end()) throw glrNoSuchTextSymbolException("const glrSymbol& glrSymbolTable::getSymbolFromString(const string &textSymbol) const: text symbol ,," + textSymbol + "'' not found in this table of symbols"); return table.find(textSymbol)->second; } /** * * The function returns a reference to the string representation of the symbol specified by argument symbol. */ const string &getStringFromSymbol(const glrSymbol &symbol) const { if((((int)symbol)>=istable.size())){ ostrstream out; out << "const string& glrSymbolTable::getStringFromSymbol(const glrSymbol &symbol) const: symbol " << symbol << " not found in this table of symbols" << ends; throw glrNoSuchSymbolException(out.str()); } return istable[symbol]; } /** * * The function adds the textSymbol to the table and returns associated value of type glrSymbol. * If the textSymbol is already in the table, the function just returns its associated * symbol, but nothing is added. * */ const glrSymbol &addTextSymbol(const string &textSymbol){ map::value_type p(textSymbol,istable.size()); pair::iterator,bool> ret=table.insert(p); if(ret.second)istable.push_back(textSymbol); return ret.first->second; } /** * * The function returns the size of the table -- number of symbols stored. * */ int size() const { return table.size(); } /** * * The function returns the type of the symbol specified by the argument symbol. It can by either the * glrPreterminal or the glrNonterminal. If the text representation of the symbol begins with * lower case ascii letter, it is nonterminal, otherwise it is a preterminal. * */ glrSymbolType getSymbolType(const glrSymbol &symbol) const { const string &textSymbol=getStringFromSymbol(symbol); return ((textSymbol[0]>='a')&&(textSymbol[0]<='z'))?glrNonterminal:glrPreterminal; } }; #endif