1 /* Abstract syntax tree manipulation functions
5 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
6 * Set (PCCTS) -- PCCTS is in the public domain. An individual or
7 * company may do whatever they wish with source code distributed with
8 * PCCTS or the code generated by PCCTS, including the incorporation of
9 * PCCTS, or its output, into commerical software.
11 * We encourage users to develop software with PCCTS. However, we do ask
12 * that credit is given to us for developing PCCTS. By "credit",
13 * we mean that if you incorporate our source code into one of your
14 * programs (commercial product, research project, or otherwise) that you
15 * acknowledge this fact somewhere in the documentation, research report,
16 * etc... If you like PCCTS and have developed a nice tool with the
17 * output, please mention that you developed it using PCCTS. In
18 * addition, we ask that this header remain intact in our source code.
19 * As long as these guidelines are kept, we expect to continue enhancing
20 * this system and expect to make other tools available as they are
25 * Parr Research Corporation
26 * with Purdue University and AHPCRC, University of Minnesota
35 /* ensure that tree manipulation variables are current after a rule
40 zzlink(AST **_root, AST **_sibling, AST **_tail)
42 zzlink(_root, _sibling, _tail)
43 AST **_root, **_sibling, **_tail;
46 if ( *_sibling == NULL ) return;
47 if ( *_root == NULL ) *_root = *_sibling;
48 else if ( *_root != *_sibling ) (*_root)->down = *_sibling;
49 if ( *_tail==NULL ) *_tail = *_sibling;
50 while ( (*_tail)->right != NULL ) *_tail = (*_tail)->right;
60 AST *p = (AST *) calloc(1, sizeof(AST));
61 if ( p == NULL ) fprintf(stderr,"%s(%d): cannot allocate AST node\n",__FILE__,__LINE__);
65 /* add a child node to the current sibling list */
68 zzsubchild(AST **_root, AST **_sibling, AST **_tail)
70 zzsubchild(_root, _sibling, _tail)
71 AST **_root, **_sibling, **_tail;
78 zzcr_ast(n, &(zzaCur), LA(0), LATEXT(0));
80 zzcr_ast(n, &(zzaCur), LA(1), LATEXT(1));
83 if ( *_tail != NULL ) (*_tail)->right = n;
86 if ( *_root != NULL ) (*_root)->down = *_sibling;
89 if ( *_root == NULL ) *_root = *_sibling;
93 /* make a new AST node. Make the newly-created
94 * node the root for the current sibling list. If a root node already
95 * exists, make the newly-created node the root of the current root.
99 zzsubroot(AST **_root, AST **_sibling, AST **_tail)
101 zzsubroot(_root, _sibling, _tail)
102 AST **_root, **_sibling, **_tail;
109 zzcr_ast(n, &(zzaCur), LA(0), LATEXT(0));
111 zzcr_ast(n, &(zzaCur), LA(1), LATEXT(1));
114 if ( *_root != NULL )
115 if ( (*_root)->down == *_sibling ) *_sibling = *_tail = *_root;
117 (*_root)->down = *_sibling;
121 /* Apply function to root then each sibling
122 * example: print tree in child-sibling LISP-format (AST has token field)
127 * if ( tree == NULL ) return;
128 * printf(" %s", zztokens[tree->token]);
131 * void before() { printf(" ("); }
132 * void after() { printf(" )"); }
134 * LISPdump() { zzpre_ast(tree, show, before, after); }
141 void (*func)(AST *), /* apply this to each tree node */
142 void (*before)(AST *), /* apply this to root of subtree before preordering it */
143 void (*after)(AST *)) /* apply this to root of subtree after preordering it */
145 zzpre_ast(tree, func, before, after)
147 void (*func)(), /* apply this to each tree node */
148 (*before)(), /* apply this to root of subtree before preordering it */
149 (*after)(); /* apply this to root of subtree after preordering it */
152 while ( tree!= NULL )
154 if ( tree->down != NULL ) (*before)(tree);
156 zzpre_ast(tree->down, func, before, after);
157 if ( tree->down != NULL ) (*after)(tree);
162 /* free all AST nodes in tree; apply func to each before freeing */
165 zzfree_ast(AST *tree)
171 if ( tree == NULL ) return;
172 zzfree_ast( tree->down );
173 zzfree_ast( tree->right );
177 /* build a tree (root child1 child2 ... NULL)
178 * If root is NULL, simply make the children siblings and return ptr
179 * to 1st sibling (child1). If root is not single node, return NULL.
181 * Siblings that are actually siblins lists themselves are handled
182 * correctly. For example #( NULL, #( NULL, A, B, C), D) results
183 * in the tree ( NULL A B C D ).
185 * Requires at least two parameters with the last one being NULL. If
186 * both are NULL, return NULL.
189 AST *zztmake(AST *rt, ...)
191 AST *zztmake(va_alist)
196 register AST *child, *sibling=NULL, *tail, *w;
204 root = va_arg(ap, AST *);
208 if ( root->down != NULL ) return NULL;
209 child = va_arg(ap, AST *);
210 while ( child != NULL )
212 for (w=child; w->right!=NULL; w=w->right) {;} /* find end of child */
213 if ( sibling == NULL ) {sibling = child; tail = w;}
214 else {tail->right = child; tail = w;}
215 child = va_arg(ap, AST *);
217 if ( root==NULL ) root = sibling;
218 else root->down = sibling;
234 if ( t == NULL ) return NULL;
238 u->up = NULL; /* set by calling invocation */
241 u->right = zzdup_ast(t->right);
242 u->down = zzdup_ast(t->down);
244 if ( u->right!=NULL ) u->right->left = u;
245 if ( u->down!=NULL ) u->down->up = u;
266 * Set the 'up', and 'left' pointers of all nodes in 't'.
267 * Initial call is double_link(your_tree, NULL, NULL).
271 zzdouble_link(AST *t, AST *left, AST *up)
273 zzdouble_link(t, left, up)
277 if ( t==NULL ) return;
280 zzdouble_link(t->down, NULL, t);
281 zzdouble_link(t->right, t, up);