#include #include #include #include #include using namespace std; #ifdef DEBUG int glrStateVertex::numStateVertices = 0; #endif #ifdef LOG_GLR_TABLE #define glrStateImplementation glrMapState #else #define glrStateImplementation glrVectorState #endif class basicParser : public glrParser { private: map > terminals; istream *input; bool syntInput; static const int maxInputLineLength; char *line; protected: virtual void readToken(); public: void readTerminals(istream &input); void setInput(istream *inputA); void setSyntInput(istream *inputA); basicParser(); virtual ~basicParser(); }; const int basicParser::maxInputLineLength = 1024; basicParser::basicParser(){ line = new char[maxInputLineLength]; } basicParser::~basicParser(){ delete[] line; } void basicParser::readTerminals(istream &input){ string terminal,preterminal; while(input >> terminal >> preterminal){ terminals[terminal].push_back(symbols.getSymbolFromString(preterminal)); } } void basicParser::setInput(istream *inputA){ input=inputA; syntInput = false; } void basicParser::setSyntInput(istream *inputA){ input = inputA; syntInput = true; input->getline(line,maxInputLineLength); } void basicParser::readToken(){ if(syntInput){ while(*line!=0){ string s; char *c; for(c = line; ((*c!=':')&&(*c!='.')&&(*c!=0)); c++) s += *c; if(!*c)return; istrstream istr(s.c_str()); int newTokenNum; istr >> newTokenNum; if(getNumOfTokens()+1!=newTokenNum) break; for( ; ((*c!=' ')&&(*c)); ++c); if(!*c)return; string preterminal; c++; if(*c=='\'')c++; for( ; ((*c!=' ')&&(*c)&&(*c!='\'')); preterminal += *(c++)); if(!*c) return; for( ; ((*c!='"')&&(*c)); ++c); c++; string terminal; for( ; ((*c!='"')&&(*c)); terminal += *(c++)); if(!*c)return; preterminals.push_back(new glrNode(symbols.getSymbolFromString(preterminal))); input->getline(line,maxInputLineLength); } }else{ string terminal; if(*input >> terminal){ map >::iterator preter=terminals.find(terminal); if(preter!=terminals.end()){ for(vector::iterator t=preter->second.begin();t!=preter->second.end();++t) preterminals.push_back(new glrNode(*t)); } } } } int main(int argc,char**argv){ /* * The grammar and the glr table initialization. */ basicParser parser; ifstream *input; input=new ifstream(argv[1]); parser.initGrammar(*input); delete input; input = new ifstream(argv[2]); parser.readTable(*input); delete input; /* * The special value ,,__SYNT__'' for the third argument means, that * the input is in the ,,synt -pt'' form. No terminal file is needed * in this case. */ bool syntInput = (string(argv[3])=="__SYNT__"); if(!syntInput){ input=new ifstream(argv[3]); parser.readTerminals(*input); delete input; } /* * Parser can print the status to verify if grammar was succesfully * read and the glr table computed. */ parser.printStatus(cout); /* * Running the whole parsing and the time measuring. */ int times; { istrstream istr(argv[5]); istr >> times; } int numOfSentences; struct timeval begin,end,alltime; alltime.tv_sec = 0; alltime.tv_usec = 0; for(int i=0;igetline(inputSentence,1024)){ istrstream parserInput(inputSentence); parser.setInput(&parserInput); parser.releaseForestRoot(); gettimeofday(&begin,NULL); parser.parse(); gettimeofday(&end,NULL); alltime.tv_sec += (end.tv_sec-begin.tv_sec)-(end.tv_usec getForestRoot() should not be null if the * reduction was made along the rule number 0 upon whole input. */ if(parser.getForestRoot()){ if(syntInput){ cout << "the sentence is syntacticaly correct according to this grammar" << endl; }else{ cout << "the last sentence is syntacticaly correct according to this grammar" << endl; } }else{ cout << "no derivation tree found for input" << endl; } /* * Dalocation of the forest root possibly created */ parser.releaseForestRoot(); /* * Dealocating of the grammar. */ parser.releaseGrammar(); }