///////////////////////////////////////////////////////////////// // simpleSemiExp.cpp - SemiExp extractor // // ver 1.0 // // Language: Visual C++, ver 7.1 // // Platform: Dell Dimension 8300, Windows XP Prof, SP 2 // // Application: CSE687 prototype for project #1, Spring 2005 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////// #include #include "SimpleSemiExp.h" //----< constructor with initializer >--------------------------- SimpleSemiExp::SimpleSemiExp(SimpleToker &toker) : pToker(&toker) {} //----< extract a semi-expression from a tokenizer >------------- void SimpleSemiExp::get() { semi.clear(); std::string tok = "tok"; while(pToker->good()) { if(pToker->peek() == '#' && length() > 0) return; std::string tok = pToker->getTok(); if(!(pToker->good())) break; if(tok.compare("\n") != 0) semi.push_back(tok); else if(semi.size() == 0) continue; // terminate SimpleSemiExp if tok is ";", "{", or "}" if((tok.compare(";")==0) || (tok.compare("{")==0) || (tok.compare("}")==0)) return; // terminate SimpleSemiExp if it starts with "#" and tok is newline if((tok.compare("\n")==0) && (semi[0].compare("#")==0)) { return; } } } // //----< non-const index operator >------------------------------- std::string& SimpleSemiExp::operator[](int i) { if(static_cast(semi.size()) <= i || i < 0) throw std::invalid_argument("index out of bounds"); return semi[i]; } //----< const index operator >----------------------------------- std::string SimpleSemiExp::operator[](int i) const { if(static_cast(semi.size()) <= i || i < 0) throw std::invalid_argument("index out of bounds"); return semi[i]; } //----< search for token in semi-expression >-------------------- unsigned int SimpleSemiExp::find(const std::string &tok) const { for(unsigned int i = 0; i-------------------------------------- std::string SimpleSemiExp::last() const { if(length() == 0) throw std::invalid_argument("no tokens in SimpleSemiExp"); return semi[length()-1]; } //----< global display function >-------------------------------- void show(const SimpleSemiExp &semi) { for(unsigned int i=0; i---------------------------------------------- #ifdef TEST_SIMPLESEMIEXP #include int main(int argc, char *argv[]) { if(argc < 2) { std::cout << "\n please enter name of file to process\n\n"; return 1; } std::ifstream in; try { in.open(argv[1]); if(!in.good()) { std::cout << "\n could not open file " << argv[1] << "\n\n"; return 1; } else { std::cout << "\n processing file " << argv[1]; std::cout << "\n -----------------" << std::string(strlen(argv[1])+1,'-').c_str() << "\n"; } } catch(std::exception e) { std::cout << "\n " << e.what() << "\n\n"; return 1; } SimpleToker toker(in); SimpleSemiExp semi(toker); while(in.good()) { semi.get(); show(semi); } in.clear(); in.close(); } #endif