5 /* rudimentary symbol table package
11 /* void sym_enter(name, type)
15 /* struct symbol *sym_find(name)
18 /* This is a rudimentary symbol-table package, just enough to
19 /* keep track of a couple of C keywords.
21 /* sym_init() primes the table with C keywords. At present, most of
22 /* the keywords that have to do with types are left out.
23 /* We need a different strategy to detect type definitions because
24 /* we do not keep track of typedef names.
26 /* sym_enter() adds an entry to the symbol table.
28 /* sym_find() locates a symbol table entry (it returns 0 if
32 /* Eindhoven University of Technology
33 /* Department of Mathematics and Computer Science
34 /* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
41 static char symbol_sccsid[] = "@(#) symbol.c 1.4 92/02/15 18:59:56";
45 extern char *strcpy();
46 extern char *malloc();
48 /* Application-specific stuff */
54 #define SYM_TABSIZE 20
56 static struct symbol *sym_tab[SYM_TABSIZE] = {0,};
58 /* More string stuff. Maybe it should go to an #include file. */
60 #define STREQ(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0)
62 /* sym_enter - enter symbol into table */
64 void sym_enter(name, type)
71 if ((s = (struct symbol *) malloc(sizeof(*s))) == 0
72 || (s->name = malloc(strlen(name) + 1)) == 0)
73 fatal("out of memory");
74 (void) strcpy(s->name, name);
77 where = hash(name, SYM_TABSIZE);
78 s->next = sym_tab[where];
82 /* sym_find - locate symbol definition */
84 struct symbol *sym_find(name)
87 register struct symbol *s;
90 * This function is called for almost every "word" token, so it better be
94 for (s = sym_tab[hash(name, SYM_TABSIZE)]; s; s = s->next)
95 if (STREQ(name, s->name))
101 * Initialization data for symbol table. We do not enter keywords for types.
102 * We use a different strategy to detect type declarations because we do not
103 * keep track of typedef names.
111 static struct sym syms[] = {
115 "while", TOK_CONTROL,
117 "switch", TOK_CONTROL,
119 "default", TOK_CONTROL,
120 "return", TOK_CONTROL,
121 "continue", TOK_CONTROL,
122 "break", TOK_CONTROL,
124 "struct", TOK_COMPOSITE,
125 "union", TOK_COMPOSITE,
126 "__DATE__", TOK_DATE,
127 "__TIME__", TOK_TIME,
128 #if defined(MAP_VOID_STAR) || defined(MAP_VOID)
135 /* sym_init - enter known keywords into symbol table */
139 register struct sym *p;
141 for (p = syms; p->name; p++)
142 sym_enter(p->name, p->tokno);