///////////////////////////////////////////////////////////////////// // Parser.cpp - Analyzes C++ language constructs // // ver 1.2 // // Language: Visual C++ 2008, SP1 // // Platform: Dell Precision T7400, Vista Ultimate SP1 // // Application: Prototype for CSE687 Pr1, Sp09 // // Author: Jim Fawcett, CST 4-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// #include #include #include "Tokenizer.h" #include "SemiExpression.h" #include "Parser.h" #include "ActionsAndRules.h" #include "ConfigureParser.h" //----< register parsing rule >-------------------------------- void Parser::addRule(IRule* pRule) { rules.push_back(pRule); } //----< parse the SemiExp by applying all rules to it >-------- bool Parser::parse() { bool succeeded = false; for(size_t i=0; idoTest(pTokColl)) succeeded = true; } return succeeded; } //----< register action with a rule >-------------------------- void IRule::addAction(IAction *pAction) { actions.push_back(pAction); } //----< invoke all actions associated with a rule >------------ void IRule::doActions(ITokCollection* pTokColl) { if(actions.size() > 0) for(size_t i=0; idoAction(pTokColl); } // //----< test stub >-------------------------------------------- #ifdef TEST_PARSER #include #include int main(int argc, char* argv[]) { std::cout << "\n Testing Parser class\n " << std::string(22,'=') << std::endl; // collecting tokens from files, named on the command line if(argc < 2) { std::cout << "\n please enter name of file to process on command line\n\n"; return 1; } for(int i=1; inext()) pParser->parse(); std::cout << "\n\n"; } catch(std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; } // std::cout << "\n Processing file for Queued Outputs " << argv[i]; std::cout << "\n " << std::string(35 + strlen(argv[i]),'-'); ConfigParseToQueue Qconfigure; pParser = Qconfigure.Build(); try { if(pParser) { if(!Qconfigure.Attach(argv[i])) { std::cout << "\n could not open file " << argv[i] << std::endl; continue; } } else { std::cout << "\n\n Parser not built\n\n"; return 1; } // now that parser is built, use it while(pParser->next()) pParser->parse(); std::cout << "\n\n"; } catch(std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; } std::queue* pQueue = Qconfigure.GetQueue(); size_t len = pQueue->size(); for(size_t i=0; ifront(); pQueue->pop(); } std::cout << "\n\n"; } } #endif