///////////////////////////////////////////////////////////////////// // grammar.cpp - provides detectors for grammatical constructs // // ver 1.4 // // // // 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 #include "grammar.h" using namespace Scanner; //----< is this string a special key? >------------------------ bool grammar::specialKeys(const std::string &s) { static std::string keys[6] = { "for", "if", "switch", "while", "=", "catch" }; for(int i=0; i<6; ++i) if(s==keys[i]) return true; return false; } //----< beginning of a function definition? >------------------ bool grammar::functionDef() { if(_se.length() == 0) return false; 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; if(ispunct(_se[len-1][0])) return false; return true; } //----< detects #include statements >-------------------------- bool grammar::include() { if(_se.length() > 1) { size_t pos = _se.find("#"); if(pos < _se.length()-1 && _se[pos+1] == "include") return true; } return false; } //----< a declaration> >--------------------------------------- bool grammar::declar() { return false; } // #ifdef TEST_GRAMMAR #include #include int main(int argc, char *argv[]) { std::cout << "\n Testing Prototype Grammar Module " << "\n ==================================\n"; if(argc==1) { std::cout << "\n please enter name of file to process on command line\n\n"; return 1; } std::ifstream in(argv[1]); if(!in.good()) { std::cout << "\n can't read file " << argv[1] << "\n\n"; return 1; } CToker toker(&in); toker.registerDefaultFilters(); CSemiExp se(&toker); se.registerDefaultFilters(); while(se.get()) { se.show(); if(grammar(se).include()) std::cout << "\n is an include statement" << "\n\n"; else if(grammar(se).functionDef()) std::cout << "\n is a function definition" << "\n\n"; } } #endif