]> git.saurik.com Git - bison.git/blob - src/symtab.c
37677ed72f8f823a760e14fe99a99f33f002f761
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison.
2
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006 Free
4 Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include <config.h>
24 #include "system.h"
25
26 #include <hash.h>
27 #include <quotearg.h>
28
29 #include "complain.h"
30 #include "gram.h"
31 #include "symtab.h"
32
33 /*------------------------.
34 | Distinguished symbols. |
35 `------------------------*/
36
37 symbol *errtoken = NULL;
38 symbol *undeftoken = NULL;
39 symbol *endtoken = NULL;
40 symbol *accept = NULL;
41 symbol *startsymbol = NULL;
42 location startsymbol_location;
43
44 /*-----------------------------------.
45 | Default %destructor and %printer. |
46 `-----------------------------------*/
47
48 static const char *default_destructor = NULL;
49 static location default_destructor_location;
50 static const char *default_printer = NULL;
51 static location default_printer_location;
52
53 /*---------------------------------.
54 | Create a new symbol, named TAG. |
55 `---------------------------------*/
56
57 static symbol *
58 symbol_new (uniqstr tag, location loc)
59 {
60 symbol *res = xmalloc (sizeof *res);
61
62 uniqstr_assert (tag);
63 res->tag = tag;
64 res->location = loc;
65
66 res->type_name = NULL;
67 res->destructor = NULL;
68 res->printer = NULL;
69
70 res->number = NUMBER_UNDEFINED;
71 res->prec = 0;
72 res->assoc = undef_assoc;
73 res->user_token_number = USER_NUMBER_UNDEFINED;
74
75 res->alias = NULL;
76 res->class = unknown_sym;
77 res->declared = false;
78
79 if (nsyms == SYMBOL_NUMBER_MAXIMUM)
80 fatal (_("too many symbols in input grammar (limit is %d)"),
81 SYMBOL_NUMBER_MAXIMUM);
82 nsyms++;
83 return res;
84 }
85
86
87 /*-----------------.
88 | Print a symbol. |
89 `-----------------*/
90
91 #define SYMBOL_ATTR_PRINT(Attr) \
92 if (s->Attr) \
93 fprintf (f, " %s { %s }", #Attr, s->Attr)
94
95 void
96 symbol_print (symbol *s, FILE *f)
97 {
98 if (s)
99 {
100 fprintf (f, "\"%s\"", s->tag);
101 SYMBOL_ATTR_PRINT (type_name);
102 SYMBOL_ATTR_PRINT (destructor);
103 SYMBOL_ATTR_PRINT (printer);
104 }
105 else
106 fprintf (f, "<NULL>");
107 }
108
109 #undef SYMBOL_ATTR_PRINT
110
111 /*------------------------------------------------------------------.
112 | Complain that S's WHAT is redeclared at SECOND, and was first set |
113 | at FIRST. |
114 `------------------------------------------------------------------*/
115
116 static void
117 redeclaration (symbol* s, const char *what, location first, location second)
118 {
119 complain_at (second, _("%s redeclaration for %s"), what, s->tag);
120 complain_at (first, _("previous declaration"));
121 }
122
123
124 /*-----------------------------------------------------------------.
125 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
126 | as TYPE_NAME. |
127 `-----------------------------------------------------------------*/
128
129 void
130 symbol_type_set (symbol *sym, uniqstr type_name, location loc)
131 {
132 if (type_name)
133 {
134 if (sym->type_name)
135 redeclaration (sym, "%type", sym->type_location, loc);
136 uniqstr_assert (type_name);
137 sym->type_name = type_name;
138 sym->type_location = loc;
139 }
140 }
141
142
143 /*------------------------------------------------------------------.
144 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
145 `------------------------------------------------------------------*/
146
147 void
148 symbol_destructor_set (symbol *sym, const char *destructor, location loc)
149 {
150 if (destructor)
151 {
152 if (sym->destructor)
153 redeclaration (sym, "%destructor", sym->destructor_location, loc);
154 sym->destructor = destructor;
155 sym->destructor_location = loc;
156 }
157 }
158
159 /*---------------------------------------.
160 | Get the computed %destructor for SYM. |
161 `---------------------------------------*/
162
163 const char *
164 symbol_destructor_get (symbol *sym)
165 {
166 /* Token 0 cannot have a %destructor unless the user renames it. */
167 if (UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
168 return NULL;
169
170 if (sym->destructor != NULL)
171 return sym->destructor;
172 return default_destructor;
173 }
174
175 /*---------------------------------------------------------------.
176 | Get the grammar location of the %destructor computed for SYM. |
177 `---------------------------------------------------------------*/
178
179 location
180 symbol_destructor_location_get (symbol *sym)
181 {
182 if (sym->destructor != NULL)
183 return sym->destructor_location;
184 return default_destructor_location;
185 }
186
187 /*---------------------------------------------------------------.
188 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
189 `---------------------------------------------------------------*/
190
191 void
192 symbol_printer_set (symbol *sym, const char *printer, location loc)
193 {
194 if (printer)
195 {
196 if (sym->printer)
197 redeclaration (sym, "%printer", sym->printer_location, loc);
198 sym->printer = printer;
199 sym->printer_location = loc;
200 }
201 }
202
203 /*------------------------------------.
204 | Get the computed %printer for SYM. |
205 `------------------------------------*/
206
207 const char *
208 symbol_printer_get (symbol *sym)
209 {
210 /* Token 0 cannot have a %printer unless the user renames it. */
211 if (UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
212 return NULL;
213
214 if (sym->printer != NULL)
215 return sym->printer;
216 return default_printer;
217 }
218
219 /*------------------------------------------------------------.
220 | Get the grammar location of the %printer computed for SYM. |
221 `------------------------------------------------------------*/
222
223 location
224 symbol_printer_location_get (symbol *sym)
225 {
226 if (sym->printer != NULL)
227 return sym->printer_location;
228 return default_printer_location;
229 }
230
231
232 /*-----------------------------------------------------------------.
233 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
234 | with UNDEF_ASSOC as ASSOC. |
235 `-----------------------------------------------------------------*/
236
237 void
238 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
239 {
240 if (a != undef_assoc)
241 {
242 if (sym->prec != 0)
243 redeclaration (sym, assoc_to_string (a), sym->prec_location, loc);
244 sym->prec = prec;
245 sym->assoc = a;
246 sym->prec_location = loc;
247 }
248
249 /* Only terminals have a precedence. */
250 symbol_class_set (sym, token_sym, loc, false);
251 }
252
253
254 /*------------------------------------.
255 | Set the CLASS associated with SYM. |
256 `------------------------------------*/
257
258 void
259 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
260 {
261 if (sym->class != unknown_sym && sym->class != class)
262 {
263 complain_at (loc, _("symbol %s redefined"), sym->tag);
264 sym->declared = false;
265 }
266
267 if (class == nterm_sym && sym->class != nterm_sym)
268 sym->number = nvars++;
269 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
270 sym->number = ntokens++;
271
272 sym->class = class;
273
274 if (declaring)
275 {
276 if (sym->declared)
277 warn_at (loc, _("symbol %s redeclared"), sym->tag);
278 sym->declared = true;
279 }
280 }
281
282
283 /*------------------------------------------------.
284 | Set the USER_TOKEN_NUMBER associated with SYM. |
285 `------------------------------------------------*/
286
287 void
288 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
289 {
290 int *user_token_numberp;
291
292 assert (sym->class == token_sym);
293
294 if (sym->user_token_number != USER_NUMBER_ALIAS)
295 user_token_numberp = &sym->user_token_number;
296 else
297 user_token_numberp = &sym->alias->user_token_number;
298 if (*user_token_numberp != USER_NUMBER_UNDEFINED
299 && *user_token_numberp != user_token_number)
300 complain_at (loc, _("redefining user token number of %s"), sym->tag);
301
302 *user_token_numberp = user_token_number;
303 /* User defined $end token? */
304 if (user_token_number == 0)
305 {
306 endtoken = sym;
307 endtoken->number = 0;
308 /* It is always mapped to 0, so it was already counted in
309 NTOKENS. */
310 --ntokens;
311 }
312 }
313
314
315 /*----------------------------------------------------------.
316 | If SYM is not defined, report an error, and consider it a |
317 | nonterminal. |
318 `----------------------------------------------------------*/
319
320 static inline bool
321 symbol_check_defined (symbol *sym)
322 {
323 if (sym->class == unknown_sym)
324 {
325 complain_at
326 (sym->location,
327 _("symbol %s is used, but is not defined as a token and has no rules"),
328 sym->tag);
329 sym->class = nterm_sym;
330 sym->number = nvars++;
331 }
332
333 return true;
334 }
335
336 static bool
337 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
338 {
339 return symbol_check_defined (sym);
340 }
341
342
343 /*------------------------------------------------------------------.
344 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
345 | SYMVAL with SYM's type. |
346 `------------------------------------------------------------------*/
347
348 void
349 symbol_make_alias (symbol *sym, symbol *symval, location loc)
350 {
351 if (symval->alias)
352 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
353 symval->tag);
354 else if (sym->alias)
355 warn_at (loc, _("symbol `%s' given more than one literal string"),
356 sym->tag);
357 else
358 {
359 symval->class = token_sym;
360 symval->user_token_number = sym->user_token_number;
361 sym->user_token_number = USER_NUMBER_ALIAS;
362 symval->alias = sym;
363 sym->alias = symval;
364 symval->number = sym->number;
365 symbol_type_set (symval, sym->type_name, loc);
366 }
367 }
368
369
370 /*---------------------------------------------------------.
371 | Check that THIS, and its alias, have same precedence and |
372 | associativity. |
373 `---------------------------------------------------------*/
374
375 static inline void
376 symbol_check_alias_consistency (symbol *this)
377 {
378 symbol *alias = this;
379 symbol *orig = this->alias;
380
381 /* Check only those that _are_ the aliases. */
382 if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS))
383 return;
384
385 if (orig->type_name != alias->type_name)
386 {
387 if (orig->type_name)
388 symbol_type_set (alias, orig->type_name, orig->type_location);
389 else
390 symbol_type_set (orig, alias->type_name, alias->type_location);
391 }
392
393
394 if (orig->destructor || alias->destructor)
395 {
396 if (orig->destructor)
397 symbol_destructor_set (alias, orig->destructor,
398 orig->destructor_location);
399 else
400 symbol_destructor_set (orig, alias->destructor,
401 alias->destructor_location);
402 }
403
404 if (orig->printer || alias->printer)
405 {
406 if (orig->printer)
407 symbol_printer_set (alias, orig->printer, orig->printer_location);
408 else
409 symbol_printer_set (orig, alias->printer, alias->printer_location);
410 }
411
412 if (alias->prec || orig->prec)
413 {
414 if (orig->prec)
415 symbol_precedence_set (alias, orig->prec, orig->assoc,
416 orig->prec_location);
417 else
418 symbol_precedence_set (orig, alias->prec, alias->assoc,
419 alias->prec_location);
420 }
421 }
422
423 static bool
424 symbol_check_alias_consistency_processor (void *this,
425 void *null ATTRIBUTE_UNUSED)
426 {
427 symbol_check_alias_consistency (this);
428 return true;
429 }
430
431
432 /*-------------------------------------------------------------------.
433 | Assign a symbol number, and write the definition of the token name |
434 | into FDEFINES. Put in SYMBOLS. |
435 `-------------------------------------------------------------------*/
436
437 static inline bool
438 symbol_pack (symbol *this)
439 {
440 if (this->class == nterm_sym)
441 {
442 this->number += ntokens;
443 }
444 else if (this->alias)
445 {
446 /* This symbol and its alias are a single token defn.
447 Allocate a tokno, and assign to both check agreement of
448 prec and assoc fields and make both the same */
449 if (this->number == NUMBER_UNDEFINED)
450 {
451 if (this == endtoken || this->alias == endtoken)
452 this->number = this->alias->number = 0;
453 else
454 {
455 assert (this->alias->number != NUMBER_UNDEFINED);
456 this->number = this->alias->number;
457 }
458 }
459 /* Do not do processing below for USER_NUMBER_ALIASes. */
460 if (this->user_token_number == USER_NUMBER_ALIAS)
461 return true;
462 }
463 else /* this->class == token_sym */
464 assert (this->number != NUMBER_UNDEFINED);
465
466 symbols[this->number] = this;
467 return true;
468 }
469
470 static bool
471 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
472 {
473 return symbol_pack (this);
474 }
475
476
477
478
479 /*--------------------------------------------------.
480 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
481 `--------------------------------------------------*/
482
483 static inline bool
484 symbol_translation (symbol *this)
485 {
486 /* Non-terminal? */
487 if (this->class == token_sym
488 && this->user_token_number != USER_NUMBER_ALIAS)
489 {
490 /* A token which translation has already been set? */
491 if (token_translations[this->user_token_number] != undeftoken->number)
492 complain_at (this->location,
493 _("tokens %s and %s both assigned number %d"),
494 symbols[token_translations[this->user_token_number]]->tag,
495 this->tag, this->user_token_number);
496
497 token_translations[this->user_token_number] = this->number;
498 }
499
500 return true;
501 }
502
503 static bool
504 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
505 {
506 return symbol_translation (this);
507 }
508
509
510 /*----------------------.
511 | A symbol hash table. |
512 `----------------------*/
513
514 /* Initial capacity of symbols hash table. */
515 #define HT_INITIAL_CAPACITY 257
516
517 static struct hash_table *symbol_table = NULL;
518
519 static inline bool
520 hash_compare_symbol (const symbol *m1, const symbol *m2)
521 {
522 /* Since tags are unique, we can compare the pointers themselves. */
523 return UNIQSTR_EQ (m1->tag, m2->tag);
524 }
525
526 static bool
527 hash_symbol_comparator (void const *m1, void const *m2)
528 {
529 return hash_compare_symbol (m1, m2);
530 }
531
532 static inline size_t
533 hash_symbol (const symbol *m, size_t tablesize)
534 {
535 /* Since tags are unique, we can hash the pointer itself. */
536 return ((uintptr_t) m->tag) % tablesize;
537 }
538
539 static size_t
540 hash_symbol_hasher (void const *m, size_t tablesize)
541 {
542 return hash_symbol (m, tablesize);
543 }
544
545
546 /*-------------------------------.
547 | Create the symbol hash table. |
548 `-------------------------------*/
549
550 void
551 symbols_new (void)
552 {
553 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
554 NULL,
555 hash_symbol_hasher,
556 hash_symbol_comparator,
557 free);
558 }
559
560
561 /*----------------------------------------------------------------.
562 | Find the symbol named KEY, and return it. If it does not exist |
563 | yet, create it. |
564 `----------------------------------------------------------------*/
565
566 symbol *
567 symbol_from_uniqstr (const uniqstr key, location loc)
568 {
569 symbol probe;
570 symbol *entry;
571
572 probe.tag = key;
573 entry = hash_lookup (symbol_table, &probe);
574
575 if (!entry)
576 {
577 /* First insertion in the hash. */
578 entry = symbol_new (key, loc);
579 hash_insert (symbol_table, entry);
580 }
581 return entry;
582 }
583
584
585 /*----------------------------------------------------------------.
586 | Find the symbol named KEY, and return it. If it does not exist |
587 | yet, create it. |
588 `----------------------------------------------------------------*/
589
590 symbol *
591 symbol_get (const char *key, location loc)
592 {
593 return symbol_from_uniqstr (uniqstr_new (key), loc);
594 }
595
596
597 /*------------------------------------------------------------------.
598 | Generate a dummy nonterminal, whose name cannot conflict with the |
599 | user's names. |
600 `------------------------------------------------------------------*/
601
602 symbol *
603 dummy_symbol_get (location loc)
604 {
605 /* Incremented for each generated symbol. */
606 static int dummy_count = 0;
607 static char buf[256];
608
609 symbol *sym;
610
611 sprintf (buf, "@%d", ++dummy_count);
612 sym = symbol_get (buf, loc);
613 sym->class = nterm_sym;
614 sym->number = nvars++;
615 return sym;
616 }
617
618
619 /*-------------------.
620 | Free the symbols. |
621 `-------------------*/
622
623 void
624 symbols_free (void)
625 {
626 hash_free (symbol_table);
627 free (symbols);
628 }
629
630
631 /*---------------------------------------------------------------.
632 | Look for undefined symbols, report an error, and consider them |
633 | terminals. |
634 `---------------------------------------------------------------*/
635
636 static void
637 symbols_do (Hash_processor processor, void *processor_data)
638 {
639 hash_do_for_each (symbol_table, processor, processor_data);
640 }
641
642
643 /*--------------------------------------------------------------.
644 | Check that all the symbols are defined. Report any undefined |
645 | symbols and consider them nonterminals. |
646 `--------------------------------------------------------------*/
647
648 void
649 symbols_check_defined (void)
650 {
651 symbols_do (symbol_check_defined_processor, NULL);
652 }
653
654 /*------------------------------------------------------------------.
655 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
656 | number. |
657 `------------------------------------------------------------------*/
658
659 static void
660 symbols_token_translations_init (void)
661 {
662 bool num_256_available_p = true;
663 int i;
664
665 /* Find the highest user token number, and whether 256, the POSIX
666 preferred user token number for the error token, is used. */
667 max_user_token_number = 0;
668 for (i = 0; i < ntokens; ++i)
669 {
670 symbol *this = symbols[i];
671 if (this->user_token_number != USER_NUMBER_UNDEFINED)
672 {
673 if (this->user_token_number > max_user_token_number)
674 max_user_token_number = this->user_token_number;
675 if (this->user_token_number == 256)
676 num_256_available_p = false;
677 }
678 }
679
680 /* If 256 is not used, assign it to error, to follow POSIX. */
681 if (num_256_available_p
682 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
683 errtoken->user_token_number = 256;
684
685 /* Set the missing user numbers. */
686 if (max_user_token_number < 256)
687 max_user_token_number = 256;
688
689 for (i = 0; i < ntokens; ++i)
690 {
691 symbol *this = symbols[i];
692 if (this->user_token_number == USER_NUMBER_UNDEFINED)
693 this->user_token_number = ++max_user_token_number;
694 if (this->user_token_number > max_user_token_number)
695 max_user_token_number = this->user_token_number;
696 }
697
698 token_translations = xnmalloc (max_user_token_number + 1,
699 sizeof *token_translations);
700
701 /* Initialize all entries for literal tokens to 2, the internal
702 token number for $undefined, which represents all invalid inputs.
703 */
704 for (i = 0; i < max_user_token_number + 1; i++)
705 token_translations[i] = undeftoken->number;
706 symbols_do (symbol_translation_processor, NULL);
707 }
708
709
710 /*----------------------------------------------------------------.
711 | Assign symbol numbers, and write definition of token names into |
712 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
713 `----------------------------------------------------------------*/
714
715 void
716 symbols_pack (void)
717 {
718 symbols_do (symbol_check_alias_consistency_processor, NULL);
719
720 symbols = xcalloc (nsyms, sizeof *symbols);
721 symbols_do (symbol_pack_processor, NULL);
722
723 /* Aliases leave empty slots in symbols, so remove them. */
724 {
725 int writei;
726 int readi;
727 int nsyms_old = nsyms;
728 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
729 {
730 if (symbols[readi] == NULL)
731 {
732 nsyms -= 1;
733 ntokens -= 1;
734 }
735 else
736 {
737 symbols[writei] = symbols[readi];
738 symbols[writei]->number = writei;
739 if (symbols[writei]->alias)
740 symbols[writei]->alias->number = writei;
741 writei += 1;
742 }
743 }
744 }
745 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
746
747 symbols_token_translations_init ();
748
749 if (startsymbol->class == unknown_sym)
750 fatal_at (startsymbol_location,
751 _("the start symbol %s is undefined"),
752 startsymbol->tag);
753 else if (startsymbol->class == token_sym)
754 fatal_at (startsymbol_location,
755 _("the start symbol %s is a token"),
756 startsymbol->tag);
757 }
758
759
760 /*-----------------------------------.
761 | Set default %destructor/%printer. |
762 `-----------------------------------*/
763
764 void
765 default_destructor_set (const char *destructor, location loc)
766 {
767 if (default_destructor != NULL)
768 {
769 complain_at (loc, _("redeclaration for default %%destructor"));
770 complain_at (default_destructor_location, _("previous declaration"));
771 }
772 default_destructor = destructor;
773 default_destructor_location = loc;
774 }
775
776 void
777 default_printer_set (const char *printer, location loc)
778 {
779 if (default_printer != NULL)
780 {
781 complain_at (loc, _("redeclaration for default %%printer"));
782 complain_at (default_printer_location, _("previous declaration"));
783 }
784 default_printer = printer;
785 default_printer_location = loc;
786 }