void compileExpression3(void) { while(lookAhead->tokenType == SB_PLUS || lookAhead->tokenType == SB_MINUS){ switch(lookAhead->tokenType){ case SB_PLUS: eat(SB_PLUS); compileTerm(); compileExpression3(); break; case SB_MINUS: eat(SB_MINUS); compileTerm(); compileExpression3(); break; /*case KW_TO: case KW_DO: case SB_RPAR: case SB_COMMA: case SB_EQ: case SB_NEQ: case SB_LE: case SB_LT: case SB_GE: case SB_GT: case SB_RSEL: case SB_SEMICOLON: case KW_END: case KW_ELSE: case KW_THEN: break;*/ default: error(ERR_INVALIDEXPRESSION, lookAhead->lineNo, lookAhead->colNo); break; } } }
void CompilationEngine::compileExpression() { /* term (op term)* */ tagNonTerminal("expression"); compileTerm(); while (jt.tokenType() == TokenType::kSYMBOL && isOp(jt.symbol())) { readOp(); nextToken(); compileTerm(); } untagNonTerminal("expression"); }
Type* compileExpression3(Type* argType1) { Type* argType2; Type* resultType; switch (lookAhead->tokenType) { case SB_PLUS: eat(SB_PLUS); checkIntType(argType1); argType2 = compileTerm(); checkIntType(argType2); genAD(); resultType = compileExpression3(argType1); break; case SB_MINUS: eat(SB_MINUS); checkIntType(argType1); argType2 = compileTerm(); checkIntType(argType2); genSB(); resultType = compileExpression3(argType1); break; // check the FOLLOW set case KW_TO: case KW_DO: case SB_RPAR: case SB_COMMA: case SB_EQ: case SB_NEQ: case SB_LE: case SB_LT: case SB_GE: case SB_GT: case SB_RSEL: case SB_SEMICOLON: case KW_END: case KW_ELSE: case KW_THEN: resultType = argType1; break; default: error(ERR_INVALID_EXPRESSION, lookAhead->lineNo, lookAhead->colNo); } return resultType; }
Type* compileExpression2(void) { Type* type; type = compileTerm(); compileExpression3(); return type; }
Type* compileExpression3(void) { Type* type1; Type* type2; switch (lookAhead->tokenType) { case SB_PLUS: eat(SB_PLUS); type1 = compileTerm(); checkIntType(type1); type2 = compileExpression3(); if (type2 != NULL) checkIntType(type2); return type1; break; case SB_MINUS: eat(SB_MINUS); type1 = compileTerm(); checkIntType(type1); type2 = compileExpression3(); if (type2 != NULL) checkIntType(type2); return type1; break; // check the FOLLOW set case KW_TO: case KW_DO: case SB_RPAR: case SB_COMMA: case SB_EQ: case SB_NEQ: case SB_LE: case SB_LT: case SB_GE: case SB_GT: case SB_RSEL: case SB_SEMICOLON: case KW_END: case KW_ELSE: case KW_THEN: return NULL; break; default: error(ERR_INVALID_EXPRESSION, lookAhead->lineNo, lookAhead->colNo); } return NULL; }
void compileExpression3(void) { // TODO switch(lookAhead->tokenType) { case SB_PLUS: eat(SB_PLUS); compileTerm(); compileExpression3(); break; case SB_MINUS: eat(SB_MINUS); compileTerm(); compileExpression3(); break; // Follow (statement) case SB_SEMICOLON: case KW_END: case KW_ELSE: // Follow (For statement) case KW_TO: case KW_DO: // Follow (arguments2) case SB_COMMA: // Follow (condition2) case SB_EQ: case SB_NEQ: case SB_LE: case SB_LT: case SB_GE: case SB_GT: // Follow (factor) case SB_RPAR: // Follow (indexes) case SB_RSEL: // Follow (if statement) case KW_THEN: break; // Error default: error(ERR_INVALIDEXPRESSION, lookAhead->lineNo, lookAhead->colNo); break; } }
Type* compileExpression2(void) { Type* type1; Type* type2; type1 = compileTerm(); type2 = compileExpression3(); if (type2 == NULL) return type1; else { checkTypeEquality(type1,type2); return type1; } }
void CompilationEng::compileExpression() { while(true) { getToken(); char c = m_token[0]; if(m_token == ")" ) { if(stringStack == "(") //empty expression { } else { numOfTab--; printNonterminal("expression",false); } m_bPutback = true; break; } if( m_token == "," || m_token == ";") //expressionList { m_bPutback =true; numOfTab--; printNonterminal("expression",false); break; } else if(m_sOp.count(c) >0) { printCurrentToken(false); //<symbol> op </symbol> compileTerm(); } else { printNonterminal("expression"); //<expression> numOfTab++; m_bPutback = true; compileTerm(); } } }
void compileExpression2(void) { // TODO compileTerm(); compileExpression3(); }
void compileExpression2(void) { compileTerm(); compileExpression3(); }
void CompilationEngine::compileTerm() // 362 { // integerConst | stringConst | keywordConst | // varName | varName '[' expression ']' | subroutineCall | // '(' expression ')' | unaryOp term tagNonTerminal("term"); string id; string oldTok; switch(jt.tokenType()) { case TokenType::kINT_CONST: readIntConst(); break; case TokenType::kSTRING_CONST: readStringConst(); break; case TokenType::kKEYWORD: readKeywordConstant(); break; case TokenType::kIDENTIFIER: id = jt.getToken(); // save current token nextToken(); if (jt.tokenType() == TokenType::kSYMBOL && jt.symbol() == '[') { oldTok = jt.getToken(); jt.setToken(id); readIdentifier(); jt.setToken(oldTok); readSymbol('['); nextToken(); compileExpression(); readSymbol(']'); nextToken(); } else if (jt.tokenType() == TokenType::kSYMBOL && (jt.symbol() == '.' || jt.symbol() == '(')) { compileSubroutineCall(id); } else { oldTok = jt.getToken(); jt.setToken(id); readIdentifier(); jt.setToken(oldTok); } untagNonTerminal("term"); return; // has already advanced break; case TokenType::kSYMBOL: if (jt.symbol() == '(') { // '(' expression ')' readSymbol('('); nextToken(); compileExpression(); readSymbol(')'); } else { // unaryOp term readUnaryOp(); nextToken(); compileTerm(); untagNonTerminal("term"); return; // has already advanced } break; default: break; } nextToken(); untagNonTerminal("term"); }
void CompilationEng::compileTerm() { getToken(); printNonterminal("term"); numOfTab++; if(tokenizer.tokenType() == "keyword") { printCurrentToken(false); //<keyword> keyword </keyword> } else if(tokenizer.tokenType() == "stringconst") { printCurrentToken(false); } else if(tokenizer.tokenType() == "integerConstant") { printCurrentToken(false); } else if(tokenizer.tokenType() == "symbol") { if(m_token == "(") // (expression) { printCurrentToken(false); compileExpression(); printCurrentToken(); //<symbol> )</symbol> } else if(m_token == "-" || m_token == "~") { printCurrentToken(false); //<symbol> -|~ </symbol> compileTerm(); } else //empty term { m_bPutback = true; } } else if(tokenizer.tokenType() == "identifier") { getToken(); if(m_token == "[") //varName [ expression ] { printToken(stringStack); printCurrentToken(false); compileExpression(); printCurrentToken(); //<symbol> ] </symbol> } else if(m_token == "(" ) // subroutineCall { printToken(stringStack); printCurrentToken(false); //<symbol> ( </symbol> compileExpressionList(); printCurrentToken(); } else if(m_token == ".") { printToken(stringStack); //className printCurrentToken(false); //<symbol> . </symbol> printCurrentToken(); //<id> subroutineName </id> printCurrentToken(); //<symbol> ( </symbol> compileExpressionList(); printCurrentToken(); //<symbol> ) </symbol> } else if(m_sOp.count(m_token[0]) > 0) //term op term { printToken(stringStack); //<id> </id> m_bPutback = true; } else//varName { printToken(stringStack); //<id> varName </id> m_bPutback = true; } } numOfTab--; printNonterminal("term",false); //end of term }