#ifndef ACTIONSANDRULES_H #define ACTIONSANDRULES_H ///////////////////////////////////////////////////////////////////// // ActionsAndRules.h - declares new parsing rules and actions // // // // Language: Visual C++ 2005 // // Platform: Dell Dimension 9150, Windows XP SP2 // // Application: Prototype for CSE687 Pr1, Sp06 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// /* Module Operations: ================== This module defines several action classes. Its classes provide specialized services needed for specific applications. The modules Parser, SemiExpression, and Tokenizer, are intended to be reusable without change. This module provides a place to put extensions of these facilities and is not expected to be reusable. Public Interface: ================= Toker t(someFile); // create tokenizer instance SemiExp se(&t); // create a SemiExp attached to tokenizer Parser parser(se); // now we have a parser Rule1 r1; // create instance of a derived Rule class Action1 a1; // create a derived action r1.addAction(&a1); // register action with the rule parser.addRule(&r1); // register rule with parser while(se.getSemiExp()) // get semi-expression parser.parse(); // and parse it Build Process: ============== Required files - Parser.h, Parser.cpp, ActionsAndRules.h, ActionsAndRules.cpp, SemiExpression.h, SemiExpression.cpp, tokenizer.h, tokenizer.cpp Build commands (either one) - devenv Project1HelpS06.sln - cl /EHsc /DTEST_PARSER parser.cpp ActionsAndRules.cpp \ semiexpression.cpp tokenizer.cpp /link setargv.obj Maintenance History: ==================== ver 1.0 : 12 Jan 06 - first release */ // #include #include #include #include "Parser.h" #include "semiexpression.h" void showMap(std::map* pMap); /////////////////////////////////////////////////////////////// // rule to detect preprocessor statements class CodeStatement : public IRule { public: bool doTest(SemiExp& se) { if(se.find("#") < se.length()) return false; for(int i=0; i* pMap; public: PreprocToMap(std::map& resultMap) : pMap(&resultMap) {} void doAction(SemiExp& se) { se.toLower(); if(se.length() < 2) return; int pos = se.find("ifndef"); if(pos < se.length()) (*pMap)[se[pos]] = se[pos+1]; pos = se.find("define"); if(pos < se.length()) (*pMap)[se[pos]] = se[pos+1]; pos = se.find("ifdef"); if(pos < se.length()) (*pMap)[se[pos]] = se[pos+1]; if(se.find("endif") < se.length()) (*pMap)["endif"] = ""; } }; class CommentToMap : public IAction { std::map* pMap; public: CommentToMap(std::map& resultMap) : pMap(&resultMap) {} void doAction(SemiExp& se) { se.toLower(); size_t pos; for(int i=0; i* pMap; public: CodeToMap(std::map& resultMap) : pMap(&resultMap) {} void doAction(SemiExp& se) { se.toLower(); if(se.find("main") < se.length()) (*pMap)["main"] = ""; } }; #endif