CoutStatementNode * ParserClass::CoutStatement() { Match(COUT_TOKEN); Match(INSERTION_TOKEN); CoutStatementNode *csn = new CoutStatementNode(); ExpressionNode *expression = Expression(); csn->AddExpression(expression); while (true) { TokenType tt = mScanner->PeekNextToken().GetTokenType(); if (tt == INSERTION_TOKEN) { Match(INSERTION_TOKEN); TokenClass next = mScanner->PeekNextToken(); if (next.GetTokenType() == ENDL_TOKEN) { Match(ENDL_TOKEN); csn->AddExpression(NULL); } else { ExpressionNode *nextExpression = Expression(); csn->AddExpression(nextExpression); } } else { if (tt == ENDL_TOKEN) { Match(ENDL_TOKEN); csn->AddExpression(NULL); } Match(SEMICOLON_TOKEN); break; } } return csn; }
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; }
TokenClass ParserClass::Match(TokenType expectedType) { TokenClass currToken = mScanner->GetNextToken(); if (currToken.GetTokenType() != expectedType) { cerr << "Error in ParserClass::Match. " << endl; cerr << "Recieved token type " << currToken.GetTokenTypeName() << endl; exit(1); } MSG("Sucessfully matched Token Type: " << currentToken.GetTokenTypeName() << ". Lexeme: \"" << currentToken.GetLexeme() << "\""); return currToken; }
// 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 }
int main(int argc, char* argv[]) { ScannerClass scanner; TokenClass token = scanner.getToken(); while(token.type != EOF_T && token.type != EMPTY_T)//Check for EOF and error { token.display(); token = scanner.getToken(); } if(token.type == EMPTY_T)//If there is an error cout<<"Compilation error at line "<<scanner.getCurrentLine()<<". "; cout<<token.lexeme<<endl; scanner.printStateTable(); scanner.close(); cin.get(); return (0); }
StatementNode * ParserClass::Statement() { TokenClass nextToken = mScanner->PeekNextToken(); TokenType tt = nextToken.GetTokenType(); if (tt == INT_TOKEN) { return DeclarationStatement(); } else if (tt == IDENTIFIER_TOKEN) { return AssignmentStatement(); } else if (tt == COUT_TOKEN) { return CoutStatement(); } else if (tt == LCURLY_TOKEN) { return Block(); } else if (tt == IF_TOKEN) { return IfStatement(); } else if (tt == DO_TOKEN) { return DoWhile(); } return NULL; }
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; }