]> git.saurik.com Git - bison.git/blob - src/symtab.c
* src/symtab.c (symbol_free): Remove dead deactivated code:
[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 "quotearg.h"
24 #include "hash.h"
25 #include "complain.h"
26 #include "symtab.h"
27 #include "gram.h"
28
29 /*------------------------.
30 | Distinguished symbols. |
31 `------------------------*/
32
33 symbol_t *errtoken = NULL;
34 symbol_t *undeftoken = NULL;
35 symbol_t *endtoken = NULL;
36 symbol_t *accept = NULL;
37 symbol_t *startsymbol = NULL;
38 location_t startsymbol_location;
39
40 /*---------------------------------.
41 | Create a new symbol, named TAG. |
42 `---------------------------------*/
43
44 static symbol_t *
45 symbol_new (const char *tag, location_t location)
46 {
47 symbol_t *res = XMALLOC (symbol_t, 1);
48
49 res->tag = xstrdup (tag);
50 res->location = location;
51
52 res->type_name = NULL;
53 res->destructor = NULL;
54 res->printer = NULL;
55
56 res->number = NUMBER_UNDEFINED;
57 res->prec = 0;
58 res->assoc = undef_assoc;
59 res->user_token_number = USER_NUMBER_UNDEFINED;
60
61 res->alias = NULL;
62 res->class = unknown_sym;
63
64 nsyms++;
65 return res;
66 }
67
68
69 /*------------------------------------------------------------------.
70 | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
71 | as TYPE_NAME. |
72 `------------------------------------------------------------------*/
73
74 void
75 symbol_type_set (symbol_t *symbol, char *type_name, location_t location)
76 {
77 if (type_name)
78 {
79 if (symbol->type_name)
80 complain_at (location,
81 _("type redeclaration for %s"), symbol->tag);
82 symbol->type_name = type_name;
83 }
84 }
85
86
87 /*-------------------------------------------------------------------.
88 | Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
89 `-------------------------------------------------------------------*/
90
91 void
92 symbol_destructor_set (symbol_t *symbol, char *destructor, location_t location)
93 {
94 if (destructor)
95 {
96 if (symbol->destructor)
97 complain_at (location,
98 _("%s redeclaration for %s"),
99 "%destructor", symbol->tag);
100 symbol->destructor = destructor;
101 symbol->destructor_location = location;
102 }
103 }
104
105
106 /*----------------------------------------------------------------.
107 | Set the PRINTER associated to SYMBOL. Do nothing if passed 0. |
108 `----------------------------------------------------------------*/
109
110 void
111 symbol_printer_set (symbol_t *symbol, char *printer, location_t location)
112 {
113 if (printer)
114 {
115 if (symbol->printer)
116 complain_at (location,
117 _("%s redeclaration for %s"),
118 "%printer", symbol->tag);
119 symbol->printer = printer;
120 symbol->printer_location = location;
121 }
122 }
123
124
125 /*------------------------------------------------------------------.
126 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
127 | with UNDEF_ASSOC as ASSOC. |
128 `------------------------------------------------------------------*/
129
130 void
131 symbol_precedence_set (symbol_t *symbol,
132 int prec, assoc_t assoc, location_t location)
133 {
134 if (assoc != undef_assoc)
135 {
136 if (symbol->prec != 0)
137 complain_at (location,
138 _("redefining precedence of %s"),
139 symbol->tag);
140 symbol->prec = prec;
141 symbol->assoc = assoc;
142 }
143
144 /* Only terminals have a precedence. */
145 symbol_class_set (symbol, token_sym, location);
146 }
147
148
149 /*-------------------------------------.
150 | Set the CLASS associated to SYMBOL. |
151 `-------------------------------------*/
152
153 void
154 symbol_class_set (symbol_t *symbol, symbol_class class, location_t location)
155 {
156 if (symbol->class != unknown_sym && symbol->class != class)
157 complain_at (location, _("symbol %s redefined"), symbol->tag);
158
159 if (class == nterm_sym && symbol->class != nterm_sym)
160 symbol->number = nvars++;
161 else if (class == token_sym && symbol->number == NUMBER_UNDEFINED)
162 symbol->number = ntokens++;
163
164 symbol->class = class;
165 }
166
167
168 /*-------------------------------------------------.
169 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
170 `-------------------------------------------------*/
171
172 void
173 symbol_user_token_number_set (symbol_t *symbol,
174 int user_token_number, location_t location)
175 {
176 assert (symbol->class == token_sym);
177
178 if (symbol->user_token_number != USER_NUMBER_UNDEFINED
179 && symbol->user_token_number != user_token_number)
180 complain_at (location, _("redefining user token number of %s"),
181 symbol->tag);
182
183 symbol->user_token_number = user_token_number;
184 /* User defined $end token? */
185 if (user_token_number == 0)
186 {
187 endtoken = symbol;
188 endtoken->number = 0;
189 /* It is always mapped to 0, so it was already counted in
190 NTOKENS. */
191 --ntokens;
192 }
193 }
194
195
196 /*------------.
197 | Free THIS. |
198 `------------*/
199
200 static void
201 symbol_free (symbol_t *this)
202 {
203 free (this->tag);
204 free (this);
205 }
206
207
208 /*-----------------------------------------------------------.
209 | If THIS is not defined, report an error, and consider it a |
210 | nonterminal. |
211 `-----------------------------------------------------------*/
212
213 static bool
214 symbol_check_defined (symbol_t *this)
215 {
216 if (this->class == unknown_sym)
217 {
218 complain_at
219 (this->location,
220 _("symbol %s is used, but is not defined as a token and has no rules"),
221 this->tag);
222 this->class = nterm_sym;
223 this->number = nvars++;
224 }
225
226 return true;
227 }
228
229
230 /*-------------------------------------------------------------------.
231 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
232 | with TYPENAME. |
233 `-------------------------------------------------------------------*/
234
235 void
236 symbol_make_alias (symbol_t *symbol, symbol_t *symval, location_t loc)
237 {
238 if (symval->alias)
239 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
240 symval->tag);
241 else if (symbol->alias)
242 warn_at (loc, _("symbol `%s' given more than one literal string"),
243 symbol->tag);
244 else
245 {
246 symval->class = token_sym;
247 symval->user_token_number = symbol->user_token_number;
248 symbol->user_token_number = USER_NUMBER_ALIAS;
249 symval->alias = symbol;
250 symbol->alias = symval;
251 /* symbol and symval combined are only one symbol */
252 nsyms--;
253 ntokens--;
254 assert (ntokens == symbol->number || ntokens == symval->number);
255 symbol->number = symval->number =
256 (symval->number < symbol->number) ? symval->number : symbol->number;
257 }
258 }
259
260
261 /*---------------------------------------------------------.
262 | Check that THIS, and its alias, have same precedence and |
263 | associativity. |
264 `---------------------------------------------------------*/
265
266 static bool
267 symbol_check_alias_consistence (symbol_t *this)
268 {
269 /* Check only those who _are_ the aliases. */
270 if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
271 {
272 if (this->prec != this->alias->prec)
273 {
274 if (this->prec != 0 && this->alias->prec != 0)
275 complain_at (this->alias->location,
276 _("conflicting precedences for %s and %s"),
277 this->tag, this->alias->tag);
278 if (this->prec != 0)
279 this->alias->prec = this->prec;
280 else
281 this->prec = this->alias->prec;
282 }
283
284 if (this->assoc != this->alias->assoc)
285 {
286 if (this->assoc != undef_assoc && this->alias->assoc != undef_assoc)
287 complain_at (this->alias->location,
288 _("conflicting associativities for %s (%s) and %s (%s)"),
289 this->tag, assoc_to_string (this->assoc),
290 this->alias->tag, assoc_to_string (this->alias->assoc));
291 if (this->assoc != undef_assoc)
292 this->alias->assoc = this->assoc;
293 else
294 this->assoc = this->alias->assoc;
295 }
296 }
297 return true;
298 }
299
300
301 /*-------------------------------------------------------------------.
302 | Assign a symbol number, and write the definition of the token name |
303 | into FDEFINES. Put in SYMBOLS. |
304 `-------------------------------------------------------------------*/
305
306 static bool
307 symbol_pack (symbol_t *this)
308 {
309 if (this->class == nterm_sym)
310 {
311 this->number += ntokens;
312 }
313 else if (this->alias)
314 {
315 /* This symbol and its alias are a single token defn.
316 Allocate a tokno, and assign to both check agreement of
317 prec and assoc fields and make both the same */
318 if (this->number == NUMBER_UNDEFINED)
319 {
320 if (this == endtoken || this->alias == endtoken)
321 this->number = this->alias->number = 0;
322 else
323 {
324 assert (this->alias->number != NUMBER_UNDEFINED);
325 this->number = this->alias->number;
326 }
327 }
328 /* Do not do processing below for USER_NUMBER_ALIASes. */
329 if (this->user_token_number == USER_NUMBER_ALIAS)
330 return true;
331 }
332 else /* this->class == token_sym */
333 {
334 assert (this->number != NUMBER_UNDEFINED);
335 }
336
337 symbols[this->number] = this;
338 return true;
339 }
340
341
342
343
344 /*--------------------------------------------------.
345 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
346 `--------------------------------------------------*/
347
348 static bool
349 symbol_translation (symbol_t *this)
350 {
351 /* Non-terminal? */
352 if (this->class == token_sym
353 && this->user_token_number != USER_NUMBER_ALIAS)
354 {
355 /* A token which translation has already been set? */
356 if (token_translations[this->user_token_number] != undeftoken->number)
357 complain_at (this->location,
358 _("tokens %s and %s both assigned number %d"),
359 symbols[token_translations[this->user_token_number]]->tag,
360 this->tag, this->user_token_number);
361
362 token_translations[this->user_token_number] = this->number;
363 }
364
365 return true;
366 }
367
368
369 /*----------------------.
370 | A symbol hash table. |
371 `----------------------*/
372
373 /* Initial capacity of symbols hash table. */
374 #define HT_INITIAL_CAPACITY 257
375
376 static struct hash_table *symbol_table = NULL;
377
378 static bool
379 hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
380 {
381 return strcmp (m1->tag, m2->tag) == 0;
382 }
383
384 static unsigned int
385 hash_symbol_t (const symbol_t *m, unsigned int tablesize)
386 {
387 return hash_string (m->tag, tablesize);
388 }
389
390
391 /*-------------------------------.
392 | Create the symbol hash table. |
393 `-------------------------------*/
394
395 void
396 symbols_new (void)
397 {
398 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
399 NULL,
400 (Hash_hasher) hash_symbol_t,
401 (Hash_comparator) hash_compare_symbol_t,
402 (Hash_data_freer) symbol_free);
403 }
404
405
406 /*----------------------------------------------------------------.
407 | Find the symbol named KEY, and return it. If it does not exist |
408 | yet, create it. |
409 `----------------------------------------------------------------*/
410
411 symbol_t *
412 symbol_get (const char *key, location_t location)
413 {
414 symbol_t probe;
415 symbol_t *entry;
416
417 /* Keep the symbol in a printable form. */
418 key = quotearg_style (escape_quoting_style, key);
419 *(char const **) &probe.tag = key;
420 entry = hash_lookup (symbol_table, &probe);
421
422 if (!entry)
423 {
424 /* First insertion in the hash. */
425 entry = symbol_new (key, location);
426 hash_insert (symbol_table, entry);
427 }
428 return entry;
429 }
430
431
432 /*------------------------------------------------------------------.
433 | Generate a dummy nonterminal, whose name cannot conflict with the |
434 | user's names. |
435 `------------------------------------------------------------------*/
436
437 symbol_t *
438 dummy_symbol_get (location_t location)
439 {
440 /* Incremented for each generated symbol. */
441 static int dummy_count = 0;
442 static char buf[256];
443
444 symbol_t *sym;
445
446 sprintf (buf, "@%d", ++dummy_count);
447 sym = symbol_get (buf, location);
448 sym->class = nterm_sym;
449 sym->number = nvars++;
450 return sym;
451 }
452
453
454 /*-------------------.
455 | Free the symbols. |
456 `-------------------*/
457
458 void
459 symbols_free (void)
460 {
461 hash_free (symbol_table);
462 free (symbols);
463 }
464
465
466 /*---------------------------------------------------------------.
467 | Look for undefined symbols, report an error, and consider them |
468 | terminals. |
469 `---------------------------------------------------------------*/
470
471 void
472 symbols_do (symbol_processor processor, void *processor_data)
473 {
474 hash_do_for_each (symbol_table,
475 (Hash_processor) processor,
476 processor_data);
477 }
478
479
480 /*--------------------------------------------------------------.
481 | Check that all the symbols are defined. Report any undefined |
482 | symbols and consider them nonterminals. |
483 `--------------------------------------------------------------*/
484
485 void
486 symbols_check_defined (void)
487 {
488 symbols_do (symbol_check_defined, NULL);
489 }
490
491 /*------------------------------------------------------------------.
492 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
493 | number. |
494 `------------------------------------------------------------------*/
495
496 static void
497 symbols_token_translations_init (void)
498 {
499 bool num_256_available_p = true;
500 int i;
501
502 /* Find the highest user token number, and whether 256, the POSIX
503 preferred user token number for the error token, is used. */
504 max_user_token_number = 0;
505 for (i = 0; i < ntokens; ++i)
506 {
507 symbol_t *this = symbols[i];
508 if (this->user_token_number != USER_NUMBER_UNDEFINED)
509 {
510 if (this->user_token_number > max_user_token_number)
511 max_user_token_number = this->user_token_number;
512 if (this->user_token_number == 256)
513 num_256_available_p = false;
514 }
515 }
516
517 /* If 256 is not used, assign it to error, to follow POSIX. */
518 if (num_256_available_p
519 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
520 errtoken->user_token_number = 256;
521
522 /* Set the missing user numbers. */
523 if (max_user_token_number < 256)
524 max_user_token_number = 256;
525
526 for (i = 0; i < ntokens; ++i)
527 {
528 symbol_t *this = symbols[i];
529 if (this->user_token_number == USER_NUMBER_UNDEFINED)
530 this->user_token_number = ++max_user_token_number;
531 if (this->user_token_number > max_user_token_number)
532 max_user_token_number = this->user_token_number;
533 }
534
535 token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1);
536
537 /* Initialize all entries for literal tokens to 2, the internal
538 token number for $undefined, which represents all invalid inputs.
539 */
540 for (i = 0; i < max_user_token_number + 1; i++)
541 token_translations[i] = undeftoken->number;
542 symbols_do (symbol_translation, NULL);
543 }
544
545
546 /*----------------------------------------------------------------.
547 | Assign symbol numbers, and write definition of token names into |
548 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
549 `----------------------------------------------------------------*/
550
551 void
552 symbols_pack (void)
553 {
554 symbols = XCALLOC (symbol_t *, nsyms);
555
556 symbols_do (symbol_check_alias_consistence, NULL);
557 symbols_do (symbol_pack, NULL);
558
559 symbols_token_translations_init ();
560
561 if (startsymbol->class == unknown_sym)
562 fatal_at (startsymbol_location,
563 _("the start symbol %s is undefined"),
564 startsymbol->tag);
565 else if (startsymbol->class == token_sym)
566 fatal_at (startsymbol_location,
567 _("the start symbol %s is a token"),
568 startsymbol->tag);
569 }