标签:
class Token { // 略 private: TokenType type_; int intValue_; };
enum class TokenType { INT, FLOAT, ADD, // + SUB, // - MUL, // * DIV, // / LEFT_PAR, // ( RIGHT_PAR, // ) INVALID, // 无效类型 };
Token Scanner::GetNextToken(stringstream &expression) { // first char auto currectChar = GetNextChar(expression); // state judge while (!expression.eof()) { // 第一部分 switch (state_) { case State::START: break; case State::NUMBER: return HandleNumberState(expression, currectChar); break; case State::OPERATOR: return HandleOperatorState(expression, currectChar); break; case State::ERROR: ErrorToken("error input"); return Token(TokenType::INVALID); } string currectStr; currectStr.push_back(currectChar); //第二部分 if (iswdigit(currectChar)) { state_ = State::NUMBER; } else if (dict_.HasToken(currectStr)) { state_ = State::OPERATOR; } else { state_ = State::ERROR; } } return Token(TokenType::INVALID); }
Token Scanner::HandleNumberState(stringstream &expression, char currectChar) { // first char string buffer; buffer.push_back(currectChar); while (!expression.eof() && isdigit(expression.peek())) { buffer += GetNextChar(expression); } // reset state state_ = State::START; // string to int int value; std::stringstream stream(buffer); stream >> value; return Token(TokenType::INT, value); }
Token Scanner::HandleOperatorState(stringstream &expression, char currectChar) { // first char string buffer; buffer.push_back(currectChar); auto token = dict_.FindToken(buffer); //reset state state_ = State::START; return token; }
GetTokenList、GetNextTokenList就是以此为基础获得一行内的所有Token。
Scanner的内容至此就讲完了,接下来是Parser部分。另外,写东西果然好难,磕磕巴巴,很多思路根本无法表达( ╯□╰ )
标签:
原文地址:http://www.cnblogs.com/elninovt9/p/5469305.html