Annotation of src/usr.bin/yacc/symtab.c, Revision 1.15
1.15 ! chl 1: /* $OpenBSD: symtab.c,v 1.14 2012/03/03 19:15:00 nicm Exp $ */
1.2 deraadt 2: /* $NetBSD: symtab.c,v 1.4 1996/03/19 03:21:48 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989 The Regents of the University of California.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Robert Paul Corbett.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.8 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.2 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
1.1 deraadt 35:
36: #include "defs.h"
37:
38: /* TABLE_SIZE is the number of entries in the symbol table. */
39: /* TABLE_SIZE must be a power of two. */
40:
41: #define TABLE_SIZE 1024
42:
43:
44: bucket **symbol_table;
45: bucket *first_symbol;
46: bucket *last_symbol;
47:
1.6 millert 48: int hash(char *);
1.4 pvalchev 49:
1.1 deraadt 50:
51: int
1.9 pvalchev 52: hash(char *name)
1.1 deraadt 53: {
1.5 mpech 54: char *s;
55: int c, k;
1.1 deraadt 56:
57: assert(name && *name);
58: s = name;
59: k = *s;
1.12 pvalchev 60: while ((c = *++s))
1.1 deraadt 61: k = (31*k + c) & (TABLE_SIZE - 1);
62:
63: return (k);
64: }
65:
66:
67: bucket *
1.9 pvalchev 68: make_bucket(char *name)
1.1 deraadt 69: {
1.5 mpech 70: bucket *bp;
1.1 deraadt 71:
72: assert(name);
73: bp = (bucket *) MALLOC(sizeof(bucket));
74: if (bp == 0) no_space();
75: bp->link = 0;
76: bp->next = 0;
1.7 deraadt 77: bp->name = strdup(name);
1.1 deraadt 78: if (bp->name == 0) no_space();
79: bp->tag = 0;
80: bp->value = UNDEFINED;
81: bp->index = 0;
82: bp->prec = 0;
83: bp-> class = UNKNOWN;
84: bp->assoc = TOKEN;
85:
86: return (bp);
87: }
88:
89:
90: bucket *
1.9 pvalchev 91: lookup(char *name)
1.1 deraadt 92: {
1.5 mpech 93: bucket *bp, **bpp;
1.1 deraadt 94:
95: bpp = symbol_table + hash(name);
96: bp = *bpp;
97:
98: while (bp)
99: {
100: if (strcmp(name, bp->name) == 0) return (bp);
101: bpp = &bp->link;
102: bp = *bpp;
103: }
104:
105: *bpp = bp = make_bucket(name);
106: last_symbol->next = bp;
107: last_symbol = bp;
108:
109: return (bp);
110: }
111:
112:
1.4 pvalchev 113: void
1.9 pvalchev 114: create_symbol_table(void)
1.1 deraadt 115: {
1.5 mpech 116: bucket *bp;
1.1 deraadt 117:
1.14 nicm 118: symbol_table = CALLOC(TABLE_SIZE, sizeof(bucket *));
119: if (symbol_table == NULL) no_space();
1.1 deraadt 120:
121: bp = make_bucket("error");
122: bp->index = 1;
123: bp->class = TERM;
124:
125: first_symbol = bp;
126: last_symbol = bp;
127: symbol_table[hash("error")] = bp;
128: }
129:
130:
1.4 pvalchev 131: void
1.9 pvalchev 132: free_symbol_table(void)
1.1 deraadt 133: {
134: FREE(symbol_table);
135: symbol_table = 0;
136: }
137:
138:
1.4 pvalchev 139: void
1.9 pvalchev 140: free_symbols(void)
1.1 deraadt 141: {
1.5 mpech 142: bucket *p, *q;
1.1 deraadt 143:
144: for (p = first_symbol; p; p = q)
145: {
146: q = p->next;
147: FREE(p);
148: }
149: }