#include #include #include #include #include "tree.h" #include "c-calc.tab.h" #include "errmsg.h" /* * Given a terminal or non-terminal symbol code, * allocate a parse tree node for it. * THE REST OF THE INITIALIZATION DEPENDS ON THE CALLER. */ struct node * treenode(int symbol) { struct node *p = (struct node *)calloc(1, sizeof(struct node)); if (p == NULL) stop("out of memory in treenode constructor"); p->symbol = symbol; return p; } struct node * alcleaf(int symbol, char *lexeme) { struct node * ret = treenode(symbol); ret->u.t.lexeme = strdup(lexeme); return ret; } struct node * alcternary(int symbol, int prodrule, struct node *k1, struct node *k2, struct node *k3) { struct node *rv = treenode(symbol); rv->u.nt.production_rule = prodrule; rv->u.nt.child[0] = k1; rv->u.nt.child[1] = k2; rv->u.nt.child[2] = k3; return rv; } struct node * alcnary(int symbol, int prodrule, int nkids, ...) { int i; va_list mylist; struct node *rv = treenode(symbol); rv->u.nt.production_rule = prodrule; va_start(mylist, nkids); for(i=0; iu.nt.child[i] = va_arg(mylist, struct node *); printf("set child %d to %p\n", rv->u.nt.child[i]); } va_end(mylist); return rv; } void treeprint(struct node *np) { /* printf("treeprint %p\n", np); */ /* * avoid segfault if something horrible went wrong. */ if (np == NULL) { warn("NULL tree pointer"); return; } /* printf("treeprint symbol %d\n", np->symbol); */ if (np->symbol < 1000) { printf("%s", np->u.t.lexeme); fflush(stdout); } else { int i; /* printf("treeprint child 0 %d\n", np->u.nt.child[0]); */ for (i=0; np->u.nt.child[i] != NULL; i++ ) { treeprint(np->u.nt.child[i]); } } /* .... now what? */ }