Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / byacc / dist / symtab.c
blob5c8e69d177c7ef39744e7d6c489e912dfef35368
1 /* $NetBSD$ */
2 /* $Id: symtab.c,v 1.2 2009/10/29 00:56:20 christos Exp $ */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: closure.c,v 1.8 2006/05/24 18:01:43 christos Exp $");
9 /* TABLE_SIZE is the number of entries in the symbol table. */
10 /* TABLE_SIZE must be a power of two. */
12 #define TABLE_SIZE 1024
14 static bucket **symbol_table = 0;
15 bucket *first_symbol;
16 bucket *last_symbol;
18 int
19 hash(const char *name)
21 const char *s;
22 int c, k;
24 assert(name && *name);
25 s = name;
26 k = *s;
27 while ((c = *++s) != 0)
28 k = (31 * k + c) & (TABLE_SIZE - 1);
30 return (k);
33 bucket *
34 make_bucket(const char *name)
36 bucket *bp;
38 assert(name);
39 bp = (bucket *)MALLOC(sizeof(bucket));
40 if (bp == 0)
41 no_space();
42 bp->link = 0;
43 bp->next = 0;
44 bp->name = MALLOC(strlen(name) + 1);
45 if (bp->name == 0)
46 no_space();
47 bp->tag = 0;
48 bp->value = UNDEFINED;
49 bp->index = 0;
50 bp->prec = 0;
51 bp->class = UNKNOWN;
52 bp->assoc = TOKEN;
54 if (bp->name == 0)
55 no_space();
56 strcpy(bp->name, name);
58 return (bp);
61 bucket *
62 lookup(const char *name)
64 bucket *bp, **bpp;
66 bpp = symbol_table + hash(name);
67 bp = *bpp;
69 while (bp)
71 if (strcmp(name, bp->name) == 0)
72 return (bp);
73 bpp = &bp->link;
74 bp = *bpp;
77 *bpp = bp = make_bucket(name);
78 last_symbol->next = bp;
79 last_symbol = bp;
81 return (bp);
84 void
85 create_symbol_table(void)
87 int i;
88 bucket *bp;
90 symbol_table = (bucket **)MALLOC(TABLE_SIZE * sizeof(bucket *));
91 if (symbol_table == 0)
92 no_space();
93 for (i = 0; i < TABLE_SIZE; i++)
94 symbol_table[i] = 0;
96 bp = make_bucket("error");
97 bp->index = 1;
98 bp->class = TERM;
100 first_symbol = bp;
101 last_symbol = bp;
102 symbol_table[hash("error")] = bp;
105 void
106 free_symbol_table(void)
108 FREE(symbol_table);
109 symbol_table = 0;
112 void
113 free_symbols(void)
115 bucket *p, *q;
117 for (p = first_symbol; p; p = q)
119 q = p->next;
120 FREE(p);