IntegerNode * ParserClass::Integer() { TokenClass tc = Match(INTEGER_TOKEN); string s = tc.GetLexeme(); const char *c = s.c_str(); IntegerNode *i = new IntegerNode(atoi(c)); return i; }
// Verify that the next token in the input file is of the same type // that the parser expects. TokenClass ParserClass::Match(TokenType expectedType) { TokenClass currentToken = this->mScanner->GetNextToken(); if (currentToken.GetTokenType() != expectedType) { std::cerr << "Error in ParserClass::Match. " << std::endl; std::cerr << "Expected token type " << TokenClass::GetTokenTypeName(expectedType) << ", but got type " << currentToken.GetTokenTypeName() << std::endl; exit(1); } MSG("\tSuccessfully matched Token Type: " << currentToken.GetTokenTypeName() << ". Lexeme: \"" << currentToken.GetLexeme() << "\""); return currentToken; // the one we just processed }
IdentifierNode * ParserClass::Identifier() { TokenClass token = Match(IDENTIFIER_TOKEN); IdentifierNode *in = new IdentifierNode(token.GetLexeme(), mSymbolTable); return in; }
IntegerNode * ParserClass::Integer() { TokenClass token = this->Match(INTEGER_TOKEN); IntegerNode * in = new IntegerNode(std::atoi(token.GetLexeme().c_str())); return in; }