]> git.saurik.com Git - bison.git/blob - src/symtab.c
217b8e0a3ba14f3ce3f5714368f0d5b90fd46983
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 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 "hash.h"
24 #include "complain.h"
25 #include "symtab.h"
26 #include "gram.h"
27
28 /*------------------------.
29 | Distinguished symbols. |
30 `------------------------*/
31
32 symbol_t *errtoken = NULL;
33 symbol_t *undeftoken = NULL;
34 symbol_t *eoftoken = NULL;
35 symbol_t *axiom = NULL;
36 symbol_t *startsymbol = NULL;
37
38 /*---------------------------------.
39 | Create a new symbol, named TAG. |
40 `---------------------------------*/
41
42 static symbol_t *
43 symbol_new (const char *tag)
44 {
45 symbol_t *res = XMALLOC (symbol_t, 1);
46
47 res->tag = xstrdup (tag);
48 res->type_name = NULL;
49 res->number = NUMBER_UNDEFINED;
50 res->prec = 0;
51 res->assoc = right_assoc;
52 res->user_token_number = USER_NUMBER_UNDEFINED;
53 res->alias = NULL;
54 res->class = unknown_sym;
55
56 nsyms++;
57
58 return res;
59 }
60
61
62 /*------------.
63 | Free THIS. |
64 `------------*/
65
66 static void
67 symbol_free (symbol_t *this)
68 {
69 #if 0
70 /* This causes crashes because one string can appear more
71 than once. */
72 XFREE (this->type_name);
73 #endif
74 XFREE (this->tag);
75 XFREE (this);
76 }
77
78
79 /*-----------------------------------------------------------.
80 | If THIS is not defined, report an error, and consider it a |
81 | nonterminal. |
82 `-----------------------------------------------------------*/
83
84 static bool
85 symbol_check_defined (symbol_t *this)
86 {
87 if (this->class == unknown_sym)
88 {
89 complain
90 (_("symbol %s is used, but is not defined as a token and has no rules"),
91 this->tag);
92 this->class = nterm_sym;
93 this->number = nvars++;
94 }
95
96 return TRUE;
97 }
98
99
100 /*-------------------------------------------------------------------.
101 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
102 | with TYPENAME. |
103 `-------------------------------------------------------------------*/
104
105 void
106 symbol_make_alias (symbol_t *symbol, symbol_t *symval, char *typename)
107 {
108 if (symval->alias)
109 warn (_("symbol `%s' used more than once as a literal string"),
110 symval->tag);
111 else if (symbol->alias)
112 warn (_("symbol `%s' given more than one literal string"),
113 symbol->tag);
114 else
115 {
116 symval->class = token_sym;
117 symval->type_name = typename;
118 symval->user_token_number = symbol->user_token_number;
119 symbol->user_token_number = USER_NUMBER_ALIAS;
120 symval->alias = symbol;
121 symbol->alias = symval;
122 /* symbol and symval combined are only one symbol */
123 nsyms--;
124 ntokens--;
125 assert (ntokens == symbol->number || ntokens == symval->number);
126 symbol->number = symval->number =
127 (symval->number < symbol->number) ? symval->number : symbol->number;
128 }
129 }
130
131
132 /*---------------------------------------------------------.
133 | Check that THIS, and its alias, have same precedence and |
134 | associativity. |
135 `---------------------------------------------------------*/
136
137 static bool
138 symbol_check_alias_consistence (symbol_t *this)
139 {
140 /* Check only those who _are_ the aliases. */
141 if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
142 {
143 if (this->prec != this->alias->prec)
144 {
145 if (this->prec != 0 && this->alias->prec != 0)
146 complain (_("conflicting precedences for %s and %s"),
147 this->tag, this->alias->tag);
148 if (this->prec != 0)
149 this->alias->prec = this->prec;
150 else
151 this->prec = this->alias->prec;
152 }
153
154 if (this->assoc != this->alias->assoc)
155 {
156 if (this->assoc != 0 && this->alias->assoc != 0)
157 complain (_("conflicting assoc values for %s and %s"),
158 this->tag, this->alias->tag);
159 if (this->assoc != 0)
160 this->alias->assoc = this->assoc;
161 else
162 this->assoc = this->alias->assoc;
163 }
164 }
165 return TRUE;
166 }
167
168
169 /*-------------------------------------------------------------------.
170 | Assign a symbol number, and write the definition of the token name |
171 | into FDEFINES. Put in SYMBOLS. |
172 `-------------------------------------------------------------------*/
173
174 static bool
175 symbol_pack (symbol_t *this)
176 {
177 if (this->class == nterm_sym)
178 {
179 this->number += ntokens;
180 }
181 else if (this->alias)
182 {
183 /* This symbol and its alias are a single token defn.
184 Allocate a tokno, and assign to both check agreement of
185 prec and assoc fields and make both the same */
186 if (this->number == NUMBER_UNDEFINED)
187 {
188 if (this == eoftoken || this->alias == eoftoken)
189 this->number = this->alias->number = 0;
190 else
191 {
192 assert (this->alias->number != NUMBER_UNDEFINED);
193 this->number = this->alias->number;
194 }
195 }
196 /* Do not do processing below for USER_NUMBER_ALIASs. */
197 if (this->user_token_number == USER_NUMBER_ALIAS)
198 return TRUE;
199 }
200 else /* this->class == token_sym */
201 {
202 assert (this->number != NUMBER_UNDEFINED);
203 }
204
205 symbols[this->number] = this;
206 return TRUE;
207 }
208
209
210
211
212 /*--------------------------------------------------.
213 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
214 `--------------------------------------------------*/
215
216 static bool
217 symbol_translation (symbol_t *this)
218 {
219 /* Non-terminal? */
220 if (this->class == token_sym
221 && this->user_token_number != USER_NUMBER_ALIAS)
222 {
223 /* A token which translation has already been set? */
224 if (token_translations[this->user_token_number] != undeftoken->number)
225 complain (_("tokens %s and %s both assigned number %d"),
226 symbols[token_translations[this->user_token_number]]->tag,
227 this->tag, this->user_token_number);
228
229 token_translations[this->user_token_number] = this->number;
230 }
231
232 return TRUE;
233 }
234
235
236 /*----------------------.
237 | A symbol hash table. |
238 `----------------------*/
239
240 /* Initial capacity of symbols hash table. */
241 #define HT_INITIAL_CAPACITY 257
242
243 static struct hash_table *symbol_table = NULL;
244
245 static bool
246 hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
247 {
248 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
249 }
250
251 static unsigned int
252 hash_symbol_t (const symbol_t *m, unsigned int tablesize)
253 {
254 return hash_string (m->tag, tablesize);
255 }
256
257
258 /*-------------------------------.
259 | Create the symbol hash table. |
260 `-------------------------------*/
261
262 void
263 symbols_new (void)
264 {
265 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
266 NULL,
267 (Hash_hasher) hash_symbol_t,
268 (Hash_comparator) hash_compare_symbol_t,
269 (Hash_data_freer) symbol_free);
270 }
271
272
273 /*----------------------------------------------------------------.
274 | Find the symbol named KEY, and return it. If it does not exist |
275 | yet, create it. |
276 `----------------------------------------------------------------*/
277
278 symbol_t *
279 getsym (const char *key)
280 {
281 symbol_t probe;
282 symbol_t *entry;
283
284 (const char *) probe.tag = key;
285 entry = hash_lookup (symbol_table, &probe);
286
287 if (!entry)
288 {
289 /* First insertion in the hash. */
290 entry = symbol_new (key);
291 hash_insert (symbol_table, entry);
292 }
293 return entry;
294 }
295
296
297 /*-------------------.
298 | Free the symbols. |
299 `-------------------*/
300
301 void
302 symbols_free (void)
303 {
304 hash_free (symbol_table);
305 }
306
307
308 /*---------------------------------------------------------------.
309 | Look for undefined symbols, report an error, and consider them |
310 | terminals. |
311 `---------------------------------------------------------------*/
312
313 void
314 symbols_do (symbol_processor processor, void *processor_data)
315 {
316 hash_do_for_each (symbol_table,
317 (Hash_processor) processor,
318 processor_data);
319 }
320
321
322 /*--------------------------------------------------------------.
323 | Check that all the symbols are defined. Report any undefined |
324 | symbols and consider them nonterminals. |
325 `--------------------------------------------------------------*/
326
327 void
328 symbols_check_defined (void)
329 {
330 symbols_do (symbol_check_defined, NULL);
331 }
332
333 /*------------------------------------------------------------------.
334 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
335 | number. |
336 `------------------------------------------------------------------*/
337
338 static void
339 symbols_token_translations_init (void)
340 {
341 int num_256_available_p = TRUE;
342 int i;
343
344 /* Find the highest user token number, and whether 256, the POSIX
345 preferred user token number for the error token, is used. */
346 max_user_token_number = 0;
347 for (i = 0; i < ntokens; ++i)
348 {
349 symbol_t *this = symbols[i];
350 if (this->user_token_number != USER_NUMBER_UNDEFINED)
351 {
352 if (this->user_token_number > max_user_token_number)
353 max_user_token_number = this->user_token_number;
354 if (this->user_token_number == 256)
355 num_256_available_p = FALSE;
356 }
357 }
358
359 /* If 256 is not used, assign it to error, to follow POSIX. */
360 if (num_256_available_p
361 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
362 errtoken->user_token_number = 256;
363
364 /* Set the missing user numbers. */
365 if (max_user_token_number < 256)
366 max_user_token_number = 256;
367
368 for (i = 0; i < ntokens; ++i)
369 {
370 symbol_t *this = symbols[i];
371 if (this->user_token_number == USER_NUMBER_UNDEFINED)
372 this->user_token_number = ++max_user_token_number;
373 if (this->user_token_number > max_user_token_number)
374 max_user_token_number = this->user_token_number;
375 }
376
377 token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1);
378
379 /* Initialize all entries for literal tokens to 2, the internal
380 token number for $undefined., which represents all invalid
381 inputs. */
382 for (i = 0; i < max_user_token_number + 1; i++)
383 token_translations[i] = undeftoken->number;
384 symbols_do (symbol_translation, NULL);
385 }
386
387
388 /*----------------------------------------------------------------.
389 | Assign symbol numbers, and write definition of token names into |
390 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
391 `----------------------------------------------------------------*/
392
393 void
394 symbols_pack (void)
395 {
396 symbols = XCALLOC (symbol_t *, nsyms);
397
398 symbols_do (symbol_check_alias_consistence, NULL);
399 symbols_do (symbol_pack, NULL);
400
401 symbols_token_translations_init ();
402
403 if (startsymbol->class == unknown_sym)
404 fatal (_("the start symbol %s is undefined"), startsymbol->tag);
405 else if (startsymbol->class == token_sym)
406 fatal (_("the start symbol %s is a token"), startsymbol->tag);
407 }