/* Procedure traverse is a generic recursive * syntax tree traversal routine: * it applies preProc in preorder and postProc * in postorder to tree pointed to by t */ static void traverse(TreeNode * t, void (*preProc)(TreeNode *), void (*postProc)(TreeNode *)) { if (t != NULL) { int locationBackup = gLocation; preProc(t); // gScope = 1, gLocation = 1 { int i; // backup the scope before getting new scope char *scopeBackUp = gScope; // get new scope and save it to gScope gScope = getNewScope(t); for (i = 0; i < MAXCHILDREN; i++) traverse(t->child[i], preProc, postProc); free(gScope); gLocation = locationBackup; gScope = scopeBackUp; } postProc(t); traverse(t->sibling, preProc, postProc); } }
/* Procedure traverse is a generic recursive * syntax tree traversal routine: * it applies preProc in preorder and postProc * in postorder to tree pointed to by t */ static void traverse( TreeNode * t, void (* preProc) (TreeNode *), void (* postProc) (TreeNode *) ) { if (t != NULL) { if (t->scope > depth) depth = t->scope; if (t->nodekind == StmtK) { if (t->kind.stmt == IfK || t->kind.stmt == WhileK || t->kind.stmt == CompoundK) location = 0; } preProc(t); { int i; for (i=0; i < MAXCHILDREN; i++) { if (t->child[i] == NULL) continue; if ( t->nodekind == StmtK && t->kind.stmt == CompoundK) { t->child[i]->scope = t->scope + 1; } else if(t->nodekind == DeclK && t->kind.decl == funK){ t->child[1]->scope = t->scope+1; return_type = t->child[0]->type; } else { t->child[i]->scope = t->scope; } traverse(t->child[i],preProc,postProc); } } deleteProc(t); postProc(t); if (t->sibling != NULL) t->sibling->scope = t->scope; traverse(t->sibling, preProc, postProc); } }
/* Procedure traverse is a generic recursive * syntax tree traversal routine: * it applies preProc in preorder and postProc * in postorder to tree pointed to by t */ static void traverse( TreeNode * t, void (* preProc) (TreeNode *), void (* postProc) (TreeNode *) ) { if (t != NULL) { preProc(t); { int i; for (i=0; i < MAXCHILDREN; i++) { traverse(t->child[i],preProc,postProc); } } if(t->nodekind==StmtK&&t->kind.stmt==RoutineK) { /* if leaving a function declaration */ if(funcPathI!=0) { /* if not leaving the main function, then set current symbol table to the upper one */ a_curFuncNum=funcPath[--funcPathI]; st_setCurFuncNum(a_curFuncNum); } } postProc(t); traverse(t->sibling,preProc,postProc); } }
/* Procedure traverse is a generic recursive * syntax tree traversal routine: * it applies preProc in preorder and postProc * in postorder to tree pointed to by t */ static void traverse( TreeNode * t, void (* preProc) (TreeNode *), void (* postProc) (TreeNode *) ) { if (t != NULL) { preProc(t); { int i; for (i=0; i < MAXCHILDREN; i++) traverse(t->child[i],preProc,postProc); } postProc(t); traverse(t->sibling,preProc,postProc); } }
/* Procedure traverse is a generic recursive * syntax tree traversal routine: * it applies prePro in preorder and postProc * in postorder to tree pointed to by t */ void Analyze::traverse( TreeNode *t, void (* preProc)( TreeNode * ), void (* postProc)( TreeNode * ) ) { if ( t != NULL ) { preProc( t ); for ( int i = 0; i < MAXCHILDREN; i++ ) { traverse( t->m_child[i], preProc, postProc ); } postProc( t ); traverse( t->m_sibling, preProc, postProc ); } }
static void traverse( TreeNode * t, void (* preProc) (TreeNode *), void (* postProc) (TreeNode *) ) { if (t != NULL) { preProc(t); { int i; for (i=0; i < MAXCHILDREN; i++){ traverse(t->child[i],preProc,postProc); if(t->nodekind==StmtK && t->kind.stmt==Compound && i==0)//local_declarations IdK_exception=0; } } postProc(t); traverse(t->sibling,preProc,postProc); } }