Token Scanner::nextToken(Attribute& attr, int& lineno) { int c; lineno_ = 1; // // Eat whitespace and comments // top: do { c = in_.get(); } while (std::isspace(c)); if (c == '#') { do { c = in_.get(); if (c == EOF) return Token::EOT; } while (c != '\n'); lineno_++; goto top; } if (c == EOF) return Token::EOT; if (c == '+') return Token::PLUS; if (c == '-') return Token::MINUS; if (c == '*') return Token::MULT; if (c == '/') return Token::DIV; if (c == '(') return Token::LPAREN; if (c == ')') return Token::RPAREN; if (c == '=') return Token::EQ; if (c == '\n') lineno_++; if (c == ':') { // assign := c = in_.get(); if (c != '=') throw std::runtime_error("Unknown lexeme"); return Token::ASSIGN; } if (c == '>') { //greater or equal c = in_.get(); if (c != '='){ in_.unget(); return Token::GT; } return Token::GE; } if (c == '<') { //less then, Not equal, less then equal c = in_.get(); if (c == '>'){ return Token::NE; } else if (c == '='){ return Token::LE; } else { in_.unget(); return Token::LT; } } // // REAL // if (std::isdigit(c)) { std::string buf = ""; do { buf.push_back(c); c = in_.get(); } while (std::isdigit(c)); if (c == '.') { buf.push_back(c); c = in_.get(); while (std::isdigit(c)) { buf.push_back(c); c = in_.get(); } } in_.unget(); attr.f = std::stod(buf); return Token::REAL; } // // IDENT or a reserved word // if (std::isalpha(c) || c == '_') { std::string buf = ""; do { buf.push_back(c); c = in_.get(); } while (std::isalnum(c) || c == '_'); in_.unget(); attr.s = buf; Token T = stringToToken(buf); return T; } throw std::runtime_error("Unknown lexeme"); }
/** * function to get one word from the parsed file * @param pointer to the word returned * @return token token, if found, otherwise tokNone */ enum eToken parserGetWord(char * const pWord) { int ch; char * ptr = pWord; unsigned short i = 0; assert(NULL != pWord); ch = ' '; while(true) { while((' ' == ch) || ('\t' == ch) || ('\n' == ch) || ('\r' == ch)) { ch = parserGetc(); if('\n' == ch) { inLineNr++; } } /* we stand on a non empty char */ /* step 1: check if it is #, if so, don't care * until end of line */ if('#' == ch) { do { ch = parserGetc(); } while('\n' != ch); inLineNr++; continue; } else { break; } } * ptr = ch; ptr++; i++; while(NAMELEN > i) { ch = parserGetc(); if((' ' == ch) || ('\t' == ch) || ('\n' == ch) || ('\r' == ch)) { parserUngetc(ch); * ptr = '\0'; return stringToToken(pWord); } else { * ptr = ch; ptr++; i++; } } parserExit("name too long"); /* do never come to here, but write it for compilers sake */ return tokNone; }