]> git.saurik.com Git - bison.git/blob - src/symtab.c
* Makefile.maint, GNUmakefile: New, from Autoconf 2.52.
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison,
2 Copyright 1984, 1989, 2000 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include "system.h"
23 #include "xalloc.h"
24 #include "symtab.h"
25 #include "gram.h"
26
27
28 bucket *firstsymbol;
29 static bucket *lastsymbol;
30 static bucket **symtab;
31
32 static int
33 hash (const char *key)
34 {
35 const char *cp;
36 int k;
37
38 cp = key;
39 k = 0;
40 while (*cp)
41 k = ((k << 1) ^ (*cp++)) & 0x3fff;
42
43 return k % TABSIZE;
44 }
45
46 /*--------------------------------------------------------------.
47 | Create a new symbol, named TAG, which hash value is HASHVAL. |
48 `--------------------------------------------------------------*/
49
50 static bucket *
51 bucket_new (const char *tag, int hashval)
52 {
53 bucket *res = XMALLOC (bucket, 1);
54
55 res->link = symtab[hashval];
56 res->next = NULL;
57 res->tag = xstrdup (tag);
58 res->type_name = NULL;
59 res->value = 0;
60 res->prec = 0;
61 res->assoc = right_assoc;
62 res->user_token_number = 0;
63 res->alias = NULL;
64 res->class = unknown_sym;
65
66 nsyms++;
67
68 return res;
69 }
70
71
72 void
73 tabinit (void)
74 {
75 symtab = XCALLOC (bucket *, TABSIZE);
76
77 firstsymbol = NULL;
78 lastsymbol = NULL;
79 }
80
81
82 /*----------------------------------------------------------------.
83 | Find the symbol named KEY, and return it. If it does not exist |
84 | yet, create it. |
85 `----------------------------------------------------------------*/
86
87 bucket *
88 getsym (const char *key)
89 {
90 int hashval;
91 bucket *bp;
92 int found;
93
94 hashval = hash (key);
95 bp = symtab[hashval];
96
97 found = 0;
98 while (bp != NULL && found == 0)
99 {
100 if (strcmp (key, bp->tag) == 0)
101 found = 1;
102 else
103 bp = bp->link;
104 }
105
106 if (found == 0)
107 {
108 bp = bucket_new (key, hashval);
109
110 if (firstsymbol == NULL)
111 {
112 firstsymbol = bp;
113 lastsymbol = bp;
114 }
115 else
116 {
117 lastsymbol->next = bp;
118 lastsymbol = bp;
119 }
120
121 symtab[hashval] = bp;
122 }
123
124 return bp;
125 }
126
127
128 void
129 free_symtab (void)
130 {
131 int i;
132 bucket *bp, *bptmp; /* JF don't use ptr after free */
133
134 for (i = 0; i < TABSIZE; i++)
135 {
136 bp = symtab[i];
137 while (bp)
138 {
139 bptmp = bp->link;
140 #if 0
141 /* This causes crashes because one string can appear more
142 than once. */
143 if (bp->type_name)
144 XFREE (bp->type_name);
145 #endif
146 XFREE (bp);
147 bp = bptmp;
148 }
149 }
150 XFREE (symtab);
151 }