Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / yacc / symtab.c
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, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/usr.bin/yacc/symtab.c,v 1.6 1999/08/28 01:08:03 peter Exp $
37  */
38
39 #ifndef lint
40 static char const sccsid[] = "@(#)symtab.c      5.3 (Berkeley) 6/1/90";
41 #endif /* not lint */
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include "defs.h"
46
47 /* TABLE_SIZE is the number of entries in the symbol table. */
48 /* TABLE_SIZE must be a power of two.                       */
49
50 #define TABLE_SIZE 1024
51
52 static int hash __P((char *));
53
54 bucket **symbol_table;
55 bucket *first_symbol;
56 bucket *last_symbol;
57
58
59 static int
60 hash(name)
61 char *name;
62 {
63     register char *s;
64     register int c, k;
65
66     assert(name && *name);
67     s = name;
68     k = *s;
69     while ((c = *++s))
70         k = (31*k + c) & (TABLE_SIZE - 1);
71
72     return (k);
73 }
74
75
76 bucket *
77 make_bucket(name)
78 char *name;
79 {
80     register bucket *bp;
81
82     assert(name);
83     bp = (bucket *) MALLOC(sizeof(bucket));
84     if (bp == 0) no_space();
85     bp->link = 0;
86     bp->next = 0;
87     bp->name = MALLOC(strlen(name) + 1);
88     if (bp->name == 0) no_space();
89     bp->tag = 0;
90     bp->value = UNDEFINED;
91     bp->index = 0;
92     bp->prec = 0;
93     bp-> class = UNKNOWN;
94     bp->assoc = TOKEN;
95
96     if (bp->name == 0) no_space();
97     strcpy(bp->name, name);
98
99     return (bp);
100 }
101
102
103 bucket *
104 lookup(name)
105 char *name;
106 {
107     register bucket *bp, **bpp;
108
109     bpp = symbol_table + hash(name);
110     bp = *bpp;
111
112     while (bp)
113     {
114         if (strcmp(name, bp->name) == 0) return (bp);
115         bpp = &bp->link;
116         bp = *bpp;
117     }
118
119     *bpp = bp = make_bucket(name);
120     last_symbol->next = bp;
121     last_symbol = bp;
122
123     return (bp);
124 }
125
126
127 void
128 create_symbol_table()
129 {
130     register int i;
131     register bucket *bp;
132
133     symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
134     if (symbol_table == 0) no_space();
135     for (i = 0; i < TABLE_SIZE; i++)
136         symbol_table[i] = 0;
137
138     bp = make_bucket("error");
139     bp->index = 1;
140     bp->class = TERM;
141
142     first_symbol = bp;
143     last_symbol = bp;
144     symbol_table[hash("error")] = bp;
145 }
146
147
148 void
149 free_symbol_table()
150 {
151     FREE(symbol_table);
152     symbol_table = 0;
153 }
154
155
156 void
157 free_symbols()
158 {
159     register bucket *p, *q;
160
161     for (p = first_symbol; p; p = q)
162     {
163         q = p->next;
164         FREE(p);
165     }
166 }