2 * symbol-table management routines for complex-number expression parser.
10 #include "complex.tab.h"
12 extern void cprin(FILE *, char *prefix
, char *suffix
, Complex z
);
14 /* Prototypes for static functions defined in symbol.c */
15 static void insert(Symbol
**tree
, Symbol
*item
);
16 static void traverse(Symbol
*tree
, short type
, void (*visit
)(Symbol
*));
17 static void printval(Symbol
*item
);
18 static void printname(Symbol
*item
);
20 static Symbol
*symtree
= NULL
; /* symbol table: binary tree */
22 Symbol
*lookup(s
) /* find s in symbol table */
30 cmp
= strcmp(s
, sp
->name
);
42 static void insert(tree
, item
) /* no duplicate names permitted... */
48 item
->left
= item
->right
= NULL
;
50 else if (strcmp(item
->name
, (*tree
)->name
) < 0)
51 insert(&(*tree
)->left
, item
);
53 insert(&(*tree
)->right
, item
);
56 Symbol
*allocsym(s
, t
) /* allocate a symbol */
62 sp
= (Symbol
*) emalloc(sizeof(Symbol
));
63 sp
->name
= emalloc(strlen(s
)+1);
70 Symbol
*install(s
, t
, cval
) /* install s in symbol table */
75 Symbol
*sp
= allocsym(s
, t
);
82 static void traverse(Symbol
*tree
,short type
, void (*visit
)(Symbol
*)) /* inorder traversal of tree */
86 traverse(tree
->left
, type
, visit
);
87 if (tree
->type
== type
)
89 traverse(tree
->right
, type
, visit
);
93 static void printval(item
)
96 fprintf(stdout
, "\t%s\t= ", item
->name
);
97 cprin(stdout
, NULL
, "\n", item
->u
.val
);
100 static void printname(item
)
103 fprintf(stdout
,"\t%s\n", item
->name
);
106 void printlist(type
) /* print names of symbols with given type */
107 int type
; /* simple-minded at moment */
109 if (type
== CONST
|| type
== VAR
)
110 traverse(symtree
, type
, printval
);
112 traverse(symtree
, type
, printname
);