]> git.saurik.com Git - bison.git/blob - src/symtab.c
92d892a5d5304feb080962f70ba7f7bf3997bca2
[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 /* Per-symbol %destructor. */
167 if (sym->destructor != NULL)
168 return sym->destructor;
169
170 /* Apply the default %destructor only to user-defined symbols. */
171 if (sym == errtoken || sym == undeftoken || sym == accept
172 || UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
173 return NULL;
174 return default_destructor;
175 }
176
177 /*---------------------------------------------------------------.
178 | Get the grammar location of the %destructor computed for SYM. |
179 `---------------------------------------------------------------*/
180
181 location
182 symbol_destructor_location_get (symbol *sym)
183 {
184 if (sym->destructor != NULL)
185 return sym->destructor_location;
186 return default_destructor_location;
187 }
188
189 /*---------------------------------------------------------------.
190 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
191 `---------------------------------------------------------------*/
192
193 void
194 symbol_printer_set (symbol *sym, const char *printer, location loc)
195 {
196 if (printer)
197 {
198 if (sym->printer)
199 redeclaration (sym, "%printer", sym->printer_location, loc);
200 sym->printer = printer;
201 sym->printer_location = loc;
202 }
203 }
204
205 /*------------------------------------.
206 | Get the computed %printer for SYM. |
207 `------------------------------------*/
208
209 const char *
210 symbol_printer_get (symbol *sym)
211 {
212 /* Per-symbol %printer. */
213 if (sym->printer != NULL)
214 return sym->printer;
215
216 /* Apply the default %printer only to user-defined symbols. */
217 if (sym == errtoken || sym == undeftoken || sym == accept
218 || UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
219 return NULL;
220 return default_printer;
221 }
222
223 /*------------------------------------------------------------.
224 | Get the grammar location of the %printer computed for SYM. |
225 `------------------------------------------------------------*/
226
227 location
228 symbol_printer_location_get (symbol *sym)
229 {
230 if (sym->printer != NULL)
231 return sym->printer_location;
232 return default_printer_location;
233 }
234
235
236 /*-----------------------------------------------------------------.
237 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
238 | with UNDEF_ASSOC as ASSOC. |
239 `-----------------------------------------------------------------*/
240
241 void
242 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
243 {
244 if (a != undef_assoc)
245 {
246 if (sym->prec != 0)
247 redeclaration (sym, assoc_to_string (a), sym->prec_location, loc);
248 sym->prec = prec;
249 sym->assoc = a;
250 sym->prec_location = loc;
251 }
252
253 /* Only terminals have a precedence. */
254 symbol_class_set (sym, token_sym, loc, false);
255 }
256
257
258 /*------------------------------------.
259 | Set the CLASS associated with SYM. |
260 `------------------------------------*/
261
262 void
263 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
264 {
265 if (sym->class != unknown_sym && sym->class != class)
266 {
267 complain_at (loc, _("symbol %s redefined"), sym->tag);
268 sym->declared = false;
269 }
270
271 if (class == nterm_sym && sym->class != nterm_sym)
272 sym->number = nvars++;
273 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
274 sym->number = ntokens++;
275
276 sym->class = class;
277
278 if (declaring)
279 {
280 if (sym->declared)
281 warn_at (loc, _("symbol %s redeclared"), sym->tag);
282 sym->declared = true;
283 }
284 }
285
286
287 /*------------------------------------------------.
288 | Set the USER_TOKEN_NUMBER associated with SYM. |
289 `------------------------------------------------*/
290
291 void
292 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
293 {
294 int *user_token_numberp;
295
296 assert (sym->class == token_sym);
297
298 if (sym->user_token_number != USER_NUMBER_ALIAS)
299 user_token_numberp = &sym->user_token_number;
300 else
301 user_token_numberp = &sym->alias->user_token_number;
302 if (*user_token_numberp != USER_NUMBER_UNDEFINED
303 && *user_token_numberp != user_token_number)
304 complain_at (loc, _("redefining user token number of %s"), sym->tag);
305
306 *user_token_numberp = user_token_number;
307 /* User defined $end token? */
308 if (user_token_number == 0)
309 {
310 endtoken = sym;
311 endtoken->number = 0;
312 /* It is always mapped to 0, so it was already counted in
313 NTOKENS. */
314 --ntokens;
315 }
316 }
317
318
319 /*----------------------------------------------------------.
320 | If SYM is not defined, report an error, and consider it a |
321 | nonterminal. |
322 `----------------------------------------------------------*/
323
324 static inline bool
325 symbol_check_defined (symbol *sym)
326 {
327 if (sym->class == unknown_sym)
328 {
329 complain_at
330 (sym->location,
331 _("symbol %s is used, but is not defined as a token and has no rules"),
332 sym->tag);
333 sym->class = nterm_sym;
334 sym->number = nvars++;
335 }
336
337 return true;
338 }
339
340 static bool
341 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
342 {
343 return symbol_check_defined (sym);
344 }
345
346
347 /*------------------------------------------------------------------.
348 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
349 | SYMVAL with SYM's type. |
350 `------------------------------------------------------------------*/
351
352 void
353 symbol_make_alias (symbol *sym, symbol *symval, location loc)
354 {
355 if (symval->alias)
356 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
357 symval->tag);
358 else if (sym->alias)
359 warn_at (loc, _("symbol `%s' given more than one literal string"),
360 sym->tag);
361 else
362 {
363 symval->class = token_sym;
364 symval->user_token_number = sym->user_token_number;
365 sym->user_token_number = USER_NUMBER_ALIAS;
366 symval->alias = sym;
367 sym->alias = symval;
368 symval->number = sym->number;
369 symbol_type_set (symval, sym->type_name, loc);
370 }
371 }
372
373
374 /*---------------------------------------------------------.
375 | Check that THIS, and its alias, have same precedence and |
376 | associativity. |
377 `---------------------------------------------------------*/
378
379 static inline void
380 symbol_check_alias_consistency (symbol *this)
381 {
382 symbol *alias = this;
383 symbol *orig = this->alias;
384
385 /* Check only those that _are_ the aliases. */
386 if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS))
387 return;
388
389 if (orig->type_name != alias->type_name)
390 {
391 if (orig->type_name)
392 symbol_type_set (alias, orig->type_name, orig->type_location);
393 else
394 symbol_type_set (orig, alias->type_name, alias->type_location);
395 }
396
397
398 if (orig->destructor || alias->destructor)
399 {
400 if (orig->destructor)
401 symbol_destructor_set (alias, orig->destructor,
402 orig->destructor_location);
403 else
404 symbol_destructor_set (orig, alias->destructor,
405 alias->destructor_location);
406 }
407
408 if (orig->printer || alias->printer)
409 {
410 if (orig->printer)
411 symbol_printer_set (alias, orig->printer, orig->printer_location);
412 else
413 symbol_printer_set (orig, alias->printer, alias->printer_location);
414 }
415
416 if (alias->prec || orig->prec)
417 {
418 if (orig->prec)
419 symbol_precedence_set (alias, orig->prec, orig->assoc,
420 orig->prec_location);
421 else
422 symbol_precedence_set (orig, alias->prec, alias->assoc,
423 alias->prec_location);
424 }
425 }
426
427 static bool
428 symbol_check_alias_consistency_processor (void *this,
429 void *null ATTRIBUTE_UNUSED)
430 {
431 symbol_check_alias_consistency (this);
432 return true;
433 }
434
435
436 /*-------------------------------------------------------------------.
437 | Assign a symbol number, and write the definition of the token name |
438 | into FDEFINES. Put in SYMBOLS. |
439 `-------------------------------------------------------------------*/
440
441 static inline bool
442 symbol_pack (symbol *this)
443 {
444 if (this->class == nterm_sym)
445 {
446 this->number += ntokens;
447 }
448 else if (this->alias)
449 {
450 /* This symbol and its alias are a single token defn.
451 Allocate a tokno, and assign to both check agreement of
452 prec and assoc fields and make both the same */
453 if (this->number == NUMBER_UNDEFINED)
454 {
455 if (this == endtoken || this->alias == endtoken)
456 this->number = this->alias->number = 0;
457 else
458 {
459 assert (this->alias->number != NUMBER_UNDEFINED);
460 this->number = this->alias->number;
461 }
462 }
463 /* Do not do processing below for USER_NUMBER_ALIASes. */
464 if (this->user_token_number == USER_NUMBER_ALIAS)
465 return true;
466 }
467 else /* this->class == token_sym */
468 assert (this->number != NUMBER_UNDEFINED);
469
470 symbols[this->number] = this;
471 return true;
472 }
473
474 static bool
475 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
476 {
477 return symbol_pack (this);
478 }
479
480
481
482
483 /*--------------------------------------------------.
484 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
485 `--------------------------------------------------*/
486
487 static inline bool
488 symbol_translation (symbol *this)
489 {
490 /* Non-terminal? */
491 if (this->class == token_sym
492 && this->user_token_number != USER_NUMBER_ALIAS)
493 {
494 /* A token which translation has already been set? */
495 if (token_translations[this->user_token_number] != undeftoken->number)
496 complain_at (this->location,
497 _("tokens %s and %s both assigned number %d"),
498 symbols[token_translations[this->user_token_number]]->tag,
499 this->tag, this->user_token_number);
500
501 token_translations[this->user_token_number] = this->number;
502 }
503
504 return true;
505 }
506
507 static bool
508 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
509 {
510 return symbol_translation (this);
511 }
512
513
514 /*----------------------.
515 | A symbol hash table. |
516 `----------------------*/
517
518 /* Initial capacity of symbols hash table. */
519 #define HT_INITIAL_CAPACITY 257
520
521 static struct hash_table *symbol_table = NULL;
522
523 static inline bool
524 hash_compare_symbol (const symbol *m1, const symbol *m2)
525 {
526 /* Since tags are unique, we can compare the pointers themselves. */
527 return UNIQSTR_EQ (m1->tag, m2->tag);
528 }
529
530 static bool
531 hash_symbol_comparator (void const *m1, void const *m2)
532 {
533 return hash_compare_symbol (m1, m2);
534 }
535
536 static inline size_t
537 hash_symbol (const symbol *m, size_t tablesize)
538 {
539 /* Since tags are unique, we can hash the pointer itself. */
540 return ((uintptr_t) m->tag) % tablesize;
541 }
542
543 static size_t
544 hash_symbol_hasher (void const *m, size_t tablesize)
545 {
546 return hash_symbol (m, tablesize);
547 }
548
549
550 /*-------------------------------.
551 | Create the symbol hash table. |
552 `-------------------------------*/
553
554 void
555 symbols_new (void)
556 {
557 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
558 NULL,
559 hash_symbol_hasher,
560 hash_symbol_comparator,
561 free);
562 }
563
564
565 /*----------------------------------------------------------------.
566 | Find the symbol named KEY, and return it. If it does not exist |
567 | yet, create it. |
568 `----------------------------------------------------------------*/
569
570 symbol *
571 symbol_from_uniqstr (const uniqstr key, location loc)
572 {
573 symbol probe;
574 symbol *entry;
575
576 probe.tag = key;
577 entry = hash_lookup (symbol_table, &probe);
578
579 if (!entry)
580 {
581 /* First insertion in the hash. */
582 entry = symbol_new (key, loc);
583 hash_insert (symbol_table, entry);
584 }
585 return entry;
586 }
587
588
589 /*----------------------------------------------------------------.
590 | Find the symbol named KEY, and return it. If it does not exist |
591 | yet, create it. |
592 `----------------------------------------------------------------*/
593
594 symbol *
595 symbol_get (const char *key, location loc)
596 {
597 return symbol_from_uniqstr (uniqstr_new (key), loc);
598 }
599
600
601 /*------------------------------------------------------------------.
602 | Generate a dummy nonterminal, whose name cannot conflict with the |
603 | user's names. |
604 `------------------------------------------------------------------*/
605
606 symbol *
607 dummy_symbol_get (location loc)
608 {
609 /* Incremented for each generated symbol. */
610 static int dummy_count = 0;
611 static char buf[256];
612
613 symbol *sym;
614
615 sprintf (buf, "@%d", ++dummy_count);
616 sym = symbol_get (buf, loc);
617 sym->class = nterm_sym;
618 sym->number = nvars++;
619 return sym;
620 }
621
622 bool
623 symbol_is_dummy (const symbol *sym)
624 {
625 return sym->tag[0] == '@';
626 }
627
628 /*-------------------.
629 | Free the symbols. |
630 `-------------------*/
631
632 void
633 symbols_free (void)
634 {
635 hash_free (symbol_table);
636 free (symbols);
637 }
638
639
640 /*---------------------------------------------------------------.
641 | Look for undefined symbols, report an error, and consider them |
642 | terminals. |
643 `---------------------------------------------------------------*/
644
645 static void
646 symbols_do (Hash_processor processor, void *processor_data)
647 {
648 hash_do_for_each (symbol_table, processor, processor_data);
649 }
650
651
652 /*--------------------------------------------------------------.
653 | Check that all the symbols are defined. Report any undefined |
654 | symbols and consider them nonterminals. |
655 `--------------------------------------------------------------*/
656
657 void
658 symbols_check_defined (void)
659 {
660 symbols_do (symbol_check_defined_processor, NULL);
661 }
662
663 /*------------------------------------------------------------------.
664 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
665 | number. |
666 `------------------------------------------------------------------*/
667
668 static void
669 symbols_token_translations_init (void)
670 {
671 bool num_256_available_p = true;
672 int i;
673
674 /* Find the highest user token number, and whether 256, the POSIX
675 preferred user token number for the error token, is used. */
676 max_user_token_number = 0;
677 for (i = 0; i < ntokens; ++i)
678 {
679 symbol *this = symbols[i];
680 if (this->user_token_number != USER_NUMBER_UNDEFINED)
681 {
682 if (this->user_token_number > max_user_token_number)
683 max_user_token_number = this->user_token_number;
684 if (this->user_token_number == 256)
685 num_256_available_p = false;
686 }
687 }
688
689 /* If 256 is not used, assign it to error, to follow POSIX. */
690 if (num_256_available_p
691 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
692 errtoken->user_token_number = 256;
693
694 /* Set the missing user numbers. */
695 if (max_user_token_number < 256)
696 max_user_token_number = 256;
697
698 for (i = 0; i < ntokens; ++i)
699 {
700 symbol *this = symbols[i];
701 if (this->user_token_number == USER_NUMBER_UNDEFINED)
702 this->user_token_number = ++max_user_token_number;
703 if (this->user_token_number > max_user_token_number)
704 max_user_token_number = this->user_token_number;
705 }
706
707 token_translations = xnmalloc (max_user_token_number + 1,
708 sizeof *token_translations);
709
710 /* Initialize all entries for literal tokens to 2, the internal
711 token number for $undefined, which represents all invalid inputs.
712 */
713 for (i = 0; i < max_user_token_number + 1; i++)
714 token_translations[i] = undeftoken->number;
715 symbols_do (symbol_translation_processor, NULL);
716 }
717
718
719 /*----------------------------------------------------------------.
720 | Assign symbol numbers, and write definition of token names into |
721 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
722 `----------------------------------------------------------------*/
723
724 void
725 symbols_pack (void)
726 {
727 symbols_do (symbol_check_alias_consistency_processor, NULL);
728
729 symbols = xcalloc (nsyms, sizeof *symbols);
730 symbols_do (symbol_pack_processor, NULL);
731
732 /* Aliases leave empty slots in symbols, so remove them. */
733 {
734 int writei;
735 int readi;
736 int nsyms_old = nsyms;
737 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
738 {
739 if (symbols[readi] == NULL)
740 {
741 nsyms -= 1;
742 ntokens -= 1;
743 }
744 else
745 {
746 symbols[writei] = symbols[readi];
747 symbols[writei]->number = writei;
748 if (symbols[writei]->alias)
749 symbols[writei]->alias->number = writei;
750 writei += 1;
751 }
752 }
753 }
754 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
755
756 symbols_token_translations_init ();
757
758 if (startsymbol->class == unknown_sym)
759 fatal_at (startsymbol_location,
760 _("the start symbol %s is undefined"),
761 startsymbol->tag);
762 else if (startsymbol->class == token_sym)
763 fatal_at (startsymbol_location,
764 _("the start symbol %s is a token"),
765 startsymbol->tag);
766 }
767
768
769 /*-----------------------------------.
770 | Set default %destructor/%printer. |
771 `-----------------------------------*/
772
773 void
774 default_destructor_set (const char *destructor, location loc)
775 {
776 if (default_destructor != NULL)
777 {
778 complain_at (loc, _("redeclaration for default %%destructor"));
779 complain_at (default_destructor_location, _("previous declaration"));
780 }
781 default_destructor = destructor;
782 default_destructor_location = loc;
783 }
784
785 void
786 default_printer_set (const char *printer, location loc)
787 {
788 if (default_printer != NULL)
789 {
790 complain_at (loc, _("redeclaration for default %%printer"));
791 complain_at (default_printer_location, _("previous declaration"));
792 }
793 default_printer = printer;
794 default_printer_location = loc;
795 }