TreeNode * factor(void) { TreeNode * t = NULL; switch (token) { case NUM : t = newExpNode(ConstK); if ((t!=NULL) && (token==NUM)) t->attr.val = atoi(tokenString); match(NUM); break; case ID : t = newExpNode(IdK); if ((t!=NULL) && (token==ID)) t->attr.name = copyString(tokenString); match(ID); break; case LPAREN : match(LPAREN); t = expres(); match(RPAREN); break; default: syntaxError("unexpected token -> "); printToken(token,tokenString); token = getToken(); break; } return t; }
TreeNode * repeat_stmt(void) { TreeNode * t = newStmtNode(RepeatK); match(REPEAT); if (t!=NULL) t->child[0] = stmt_sequence(); match(UNTIL); if (t!=NULL) t->child[1] = expres(); return t; }
TreeNode * assign_stmt(void) { TreeNode * t = newStmtNode(AssignK); if ((t!=NULL) && (token==ID)) t->attr.name = copyString(tokenString); match(ID); match(ASSIGN); if (t!=NULL) t->child[0] = expres(); return t; }
TreeNode * if_stmt(void) { TreeNode * t = newStmtNode(IfK); match(IF); if (t!=NULL) t->child[0] = expres(); match(THEN); if (t!=NULL) t->child[1] = stmt_sequence(); if (token==ELSE) { match(ELSE); if (t!=NULL) t->child[2] = stmt_sequence(); } match(ENDIF); return t; }
TreeNode * for_stmt(void) { TreeNode * t = newStmtNode(ForK); TreeNode * temp ; TreeNode * ultimoIrmao ; match(FOR); switch(token){ case LPAREN: match(LPAREN); if (t!= NULL) t->child[0] = assign_stmt(); match(COMMA); if (t!= NULL) t->child[1] = expres(); match(COMMA); if (temp!= NULL) temp = assign_stmt(); match(RPAREN); if (t!= NULL) t->child[2] = stmt_sequence(); if (t->child[2] != NULL){ //Se o terceiro filho tiver algo, if(t->child[2]->sibling != NULL){ //Se o nó tem irmão, vamos fazer um loop para chegar até o ultimo irmão. ultimoIrmao = t->child[2]->sibling; while ( ultimoIrmao->sibling != NULL){ //Loop para chegar em ultimo irmão ultimoIrmao = ultimoIrmao->sibling; } ultimoIrmao->sibling = temp; } else { t->child[2]->sibling = temp; } } else{ //Se o que tem dentro do FOR for nada, então vamos só fazer as iterações t->child[2] = temp; } match(ENDFOR); break; default: syntaxError("unexpected token -> "); printToken(token,tokenString); token = getToken(); break; } return t; }
TreeNode * write_stmt(void) { TreeNode * t = newStmtNode(WriteK); match(WRITE); if (t!=NULL) t->child[0] = expres(); return t; }
PUBLIC void absexpres() { expres(); chkabs(); }