///////////////////////////////////////////////////////////////////// // grammar.cpp - provides detectors for grammatical constructs // // ver 1.0 // // // // Language: Visual C++, ver 7.0 // // Platfrom: Dell Dimension 8100, Windows 2000, SP 2.0 // // Application: Prototype for CSE687 Pr#2, Spring 2003 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// #include "grammar.h" //----< is this string a special key? >------------------------ bool grammar::specialKeys(const std::string &s) { static string keys[4] = { "for", "if", "switch", "while" }; for(int i=0; i<4; ++i) if(s==keys[i]) return true; return false; } //----< beginning of a function definition? >------------------ bool grammar::functionDef() { if(_se[_se.length()-1] != std::string("{")) return false; size_t len = _se.find("("); if(len == _se.length()) return false; if(specialKeys(_se[len-1])) return false; return true; } //----< a declaration> >--------------------------------------- bool grammar::declar() { return false; } // #ifdef TEST_GRAMMAR #include #include int main(int argc, char *argv[]) { if(argc==1) { cout << "\n please enter name of file to process on command line\n\n"; return 1; } ifstream in(argv[1]); if(!in.good()) { cout << "\n can't read file " << argv[1] << "\n\n"; return 1; } semiExp se(in); while(se.get()) { se.display(); if(grammar(se).include()) cout << " is an include statement" << "\n\n"; else if(grammar(se).functionDef()) cout << " is a function definition" << "\n\n"; } } #endif