/////////////////////////////////////////////////////////////// // semi.cpp - build semiexpression, e.g., list of tokens // // ver 1.8 terminated by special characters // // // // Language: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: CSE784 Example // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// #pragma warning(disable : 4786) #include #include #include "tok.h" #include "semi.h" using namespace std; //----< constructor >------------------------------------------ semiExp::semiExp(istream &input) : toker(input), _next(0), linesAtFirstTok(0) { } //----< append token to end of semiExpression >---------------- int semiExp::append(const string &token) { se.push_back(token); return se.size(); } //----< return number of tokens in semiExpression >------------ int semiExp::length() const { return se.size(); } //----< remove all elements from semiExpression >-------------- void semiExp::flush() { se.erase(se.begin(),se.end()); } //----< is first char of token one of the special chars? >----- bool semiExp::isTerminator(const string &token) { switch(token[0]) { case '{' : return true; case '}' : return true; case ';' : return true; case '\n': if(se.size() > 0) if(se[0][0] == '#') return true; return false; default : return false; } } // //----< collect a semiExp from input stream >------------------ bool semiExp::get(bool includeNewline) { string tok; se.erase(se.begin(),se.end()); _next = 0; bool more; while((more = toker.getTok(tok)) == true) { if(includeNewline || tok[0] != '\n') { if(se.size() == 0) linesAtFirstTok = lines(); se.push_back(tok); } if(isTerminator(tok)) break; } return more; } //----< how many lines when we collected first token? >-------- unsigned int semiExp::linesAtFirstToken() { return linesAtFirstTok; } //----< if this condition is false, abort with message >------- void semiExp::check(bool condition, const char *msg) { if(!condition) throw msg; } //----< index operator >--------------------------------------- string& semiExp::operator[](int n) { check(0<=n && n---------------------------------- semiExp& semiExp::operator +=(const string &tok) { se.push_back(tok); return *this; } //----< send each token in semiExpression to stdout >---------- void semiExp::display(int i, int j) { int k; for(k=i; k<=j && k---------------------------- int semiExp::find(const string &tok, int j) { int i; for(i=j; i------------------ int semiExp::textFind(const string &s, int j) { int i; for(i=j; i-------------------------------- int semiExp::next(const string &tok) { int i; for(i=_next; i------------------------------------- void semiExp::resetNext() { _next = 0; } //----< is this closing token match? >------------------------- string semiExp::closes(const string &tok) { switch(tok[0]) { case '(' : return ")"; case '[' : return "]"; case '{' : return "}"; default : return "no match"; } } // //----< find closure >----------------------------------------- // // Useful only if preceded by successful search for '(' or '[' int semiExp::findClosure() { string tok; if(_next) tok = se[_next-1]; else return se.size(); int i, level = 1; for(i=_next; i-------------------------------- // // Useful only if preceded by successful search for '(', '[', or '{' int semiExp::findExClosure() { string tok; if(_next) tok = se[_next-1]; else return -1; int i, level = 1; do { for(i=_next; i---------------------------- void semiExp::remove(int i) { if(i<0 || se.size() <= i) return; if(i == _next-1) se.pop_back(); vector::iterator it = std::find(se.begin(),se.end(),se[i]); se.erase(it); } //----< constructor >------------------------------------------ packedTokens::packedTokens() { toks["::"] = both; toks["(" ] = both; toks[")" ] = both; toks["[" ] = both; toks["]" ] = both; toks["." ] = both; toks["," ] = left; toks[";" ] = left; toks["*" ] = right; toks["&" ] = right; toks[":" ] = not; toks["{" ] = not; toks["}" ] = not; toks["const"] = not; } //----< is this token in the packed token set >---------------- bool packedTokens::in(const string &s,packType pt) const { map::const_iterator it = toks.find(s); if(it != toks.end()) if(it->second == pt) return true; return false; } // //----< format semi-expression using token packing >----------- string packedTokens::format(semiExp &se) { int i; string temp; for(i=0; i-------------------------------------------- #ifdef TEST_SEMI #include #include void main(int argc, char *argv[]) { ifstream in; semiExp se(in); if(argc == 1) { cout,"\n please enter name of file to process\n"; return; } cout << "\n Testing Collection of SemiExpressions " << "\n =======================================\n"; int i; for(i=1; i