date and time created 90/02/15 09:39:17 by bostic
[unix-history] / usr / src / usr.bin / yacc / symtab.c
CommitLineData
92ec8d3b
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Paul Corbett.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21#ifndef lint
22static char sccsid[] = "@(#)symtab.c 5.1 (Berkeley) %G%";
23#endif /* not lint */
24
25#include "defs.h"
26
27/* TABLE_SIZE is the number of entries in the symbol table. */
28/* TABLE_SIZE must be a power of two. */
29
30#define TABLE_SIZE 1024
31
32
33bucket **symbol_table;
34bucket *first_symbol;
35bucket *last_symbol;
36
37
38int
39hash(name)
40char *name;
41{
42 register char *s;
43 register int c, k;
44
45 assert(name && *name);
46 s = name;
47 k = *s;
48 while (c = *++s)
49 k = (31*k + c) & (TABLE_SIZE - 1);
50
51 return (k);
52}
53
54
55bucket *
56make_bucket(name)
57char *name;
58{
59 register bucket *bp;
60
61 assert(name);
62 bp = (bucket *) MALLOC(sizeof(bucket));
63 if (bp == 0) no_space();
64 bp->link = 0;
65 bp->next = 0;
66 bp->name = MALLOC(strlen(name) + 1);
67 bp->tag = 0;
68 bp->value = UNDEFINED;
69 bp->index = 0;
70 bp->prec = 0;
71 bp-> class = UNKNOWN;
72 bp->assoc = TOKEN;
73
74 if (bp->name == 0) no_space();
75 strcpy(bp->name, name);
76
77 return (bp);
78}
79
80
81bucket *
82lookup(name)
83char *name;
84{
85 register bucket *bp, **bpp;
86
87 bpp = symbol_table + hash(name);
88 bp = *bpp;
89
90 while (bp)
91 {
92 if (strcmp(name, bp->name) == 0) return (bp);
93 bpp = &bp->link;
94 bp = *bpp;
95 }
96
97 *bpp = bp = make_bucket(name);
98 last_symbol->next = bp;
99 last_symbol = bp;
100
101 return (bp);
102}
103
104
105create_symbol_table()
106{
107 register int i;
108 register bucket *bp;
109
110 symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket));
111 for (i = 0; i < TABLE_SIZE; i++)
112 symbol_table[i] = 0;
113
114 bp = make_bucket("error");
115 bp->index = 1;
116 bp->class = TERM;
117
118 first_symbol = bp;
119 last_symbol = bp;
120 symbol_table[hash("error")] = bp;
121}
122
123
124free_symbol_table()
125{
126 FREE(symbol_table);
127 symbol_table = 0;
128}
129
130
131free_symbols()
132{
133 register bucket *p, *q;
134
135 for (p = first_symbol; p; p = q)
136 {
137 q = p->next;
138 FREE(p);
139 }
140}