/* reconhece e traduz uma expressão */ int expression(){ int val; if (isAddOp(look)) val = 0; else val = term(); while (isAddOp(look)) { switch (look) { case '+': match('+'); val += term(); break; case '-': match('-'); val -= term(); break; } } }
/* reconhece e traduz uma expressão */ void expression(){ if(isAddOp(look)){ emit("XOR AX, AX"); }else{ term(); } while(look == '+' || look == '-'){ emit("PUSH AX"); switch(look) { case '+': add(); break; case '-': subtract(); break; default: break; } } }
int expression() { int result; result = term(); while (isAddOp(*p)) { switch(*p) { case '-': match('-'); result = result - term(); break; case '+': match('+'); result = result + term(); break; } } return result; }
int getNumber() { int result = 0, value = 1; if( isAddOp(*p)) { if(*p == '-') { value = -1; } p++; } if (isdigit (*p)) { while(isdigit(*p)) { result = result * 10 + (*p - '0'); p++; } } else { printf("Error: expected digit\n"); exit(EXIT_FAILURE); } result = result * value; return result; }
/* analisa e traduz um termo */ int term() { int val; val = factor(); while (isAddOp(look)) { switch (look) { case '*': match('*'); val *= factor(); break; case '/': match('/'); val /= factor(); break; } } return val; }
void SimplExpr() { /* SimplExpr -> [ + | - ] term { addop term } */ if ( debugMode) printf( "In SimplExpr\n"); if ( sym == plus || sym == hyphen) { writesym(); nextsym(); } term(); while ( isAddOp( sym)) { nextsym(); term(); } if ( debugMode) printf( "Out SimplExpr\n"); }
/* Parse and Translate an Expression */ TreeNode* Parser::Expression() { TreeNode* retExp = Term(); // preset the base-TreeNode as it eventually will be returned TreeNode* pos = retExp; TreeNode* left = NULL; TreeNode* right = NULL; while ( isAddOp(currentToken) ) { left = pos; pos = new TreeNode(currentToken, Unknown); pos->appendChild(left); switch (currentToken.type) { case tokPlus: matchToken(tokPlus); right = Term(); pos->setType(addNode); break; case tokMinus: matchToken(tokMinus); right = Term(); pos->setType(subNode); break; case tokGt: matchToken(tokGt); right = Term(); pos->setType(nodeGT); break; case tokLt: matchToken(tokLt); right = Term(); pos->setType(nodeLT); break; case tokGe: matchToken(tokGe); right = Term(); pos->setType(nodeGE); break; case tokLe: matchToken(tokLe); right = Term(); pos->setType(nodeLE); break; case tokEq: matchToken(tokEq); right = Term(); pos->setType(nodeEQ); break; case tokNe: matchToken(tokNe); right = Term(); pos->setType(nodeNE); break; case tokOr: matchToken(tokOr); right = Term(); pos->setType(orNode); break; default: Error(currentToken, i18n("Expected '*' or '/'"), 1040); getToken(); return pos; break; } if (right != NULL) pos->appendChild(right); retExp = pos; } return retExp; }