//Parse and translate a Math Expression void expression(){ if (isAddop(look)) { emitln("xor\t%eax,%eax"); //unary - or + so first operand is zero }else{ term(); } while (isAddop(look)) { emitln("push\t%eax"); //push eax to top of stack switch (look) { case '+': add(); break; case '-': substract(); break; default: expected("Addop"); break; } } }
int expression() { int retVal; if (isAddop(look)) { retVal = 0; } else { retVal = term(); } while (isAddop(look)) { switch (look) { case '+': match('+'); retVal += term(); break; case '-': match('-'); retVal -= term(); break; } } return retVal; }
/* EBNF: additive-expression -> term {addop term} */ static TreeNode * additive_expression (void) { TreeNode * t = term(); TreeNode * newNode; while (token == COMMENT) unexpectedTokenHandling(); /* {addop term} */ while (isAddop(token)) { newNode = newExpNode (OpK); if (newNode != NULL) { newNode->child[0] = t; newNode->attr.op = token; t = newNode; } match (token); // match addop if (t != NULL) t->child[1] = term(); } return t; }
static int isOp (TokenType token) { if (isRelop(token)||isAddop(token)||isMulop(token)) return TRUE; else return FALSE; }