Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $DragonFly: src/usr.bin/yacc/symtab.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
38  *
39  * @(#)symtab.c 5.3 (Berkeley) 6/1/90
40  */
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include "defs.h"
45
46 /* TABLE_SIZE is the number of entries in the symbol table. */
47 /* TABLE_SIZE must be a power of two.                       */
48
49 #define TABLE_SIZE 1024
50
51 static int hash __P((char *));
52
53 bucket **symbol_table;
54 bucket *first_symbol;
55 bucket *last_symbol;
56
57
58 static int
59 hash(name)
60 char *name;
61 {
62     register char *s;
63     register int c, k;
64
65     assert(name && *name);
66     s = name;
67     k = *s;
68     while ((c = *++s))
69         k = (31*k + c) & (TABLE_SIZE - 1);
70
71     return (k);
72 }
73
74
75 bucket *
76 make_bucket(name)
77 char *name;
78 {
79     register bucket *bp;
80
81     assert(name);
82     bp = (bucket *) MALLOC(sizeof(bucket));
83     if (bp == 0) no_space();
84     bp->link = 0;
85     bp->next = 0;
86     bp->name = MALLOC(strlen(name) + 1);
87     if (bp->name == 0) no_space();
88     bp->tag = 0;
89     bp->value = UNDEFINED;
90     bp->index = 0;
91     bp->prec = 0;
92     bp-> class = UNKNOWN;
93     bp->assoc = TOKEN;
94
95     if (bp->name == 0) no_space();
96     strcpy(bp->name, name);
97
98     return (bp);
99 }
100
101
102 bucket *
103 lookup(name)
104 char *name;
105 {
106     register bucket *bp, **bpp;
107
108     bpp = symbol_table + hash(name);
109     bp = *bpp;
110
111     while (bp)
112     {
113         if (strcmp(name, bp->name) == 0) return (bp);
114         bpp = &bp->link;
115         bp = *bpp;
116     }
117
118     *bpp = bp = make_bucket(name);
119     last_symbol->next = bp;
120     last_symbol = bp;
121
122     return (bp);
123 }
124
125
126 void
127 create_symbol_table()
128 {
129     register int i;
130     register bucket *bp;
131
132     symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
133     if (symbol_table == 0) no_space();
134     for (i = 0; i < TABLE_SIZE; i++)
135         symbol_table[i] = 0;
136
137     bp = make_bucket("error");
138     bp->index = 1;
139     bp->class = TERM;
140
141     first_symbol = bp;
142     last_symbol = bp;
143     symbol_table[hash("error")] = bp;
144 }
145
146
147 void
148 free_symbol_table()
149 {
150     FREE(symbol_table);
151     symbol_table = 0;
152 }
153
154
155 void
156 free_symbols()
157 {
158     register bucket *p, *q;
159
160     for (p = first_symbol; p; p = q)
161     {
162         q = p->next;
163         FREE(p);
164     }
165 }