1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program 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 3 of the License, or
11 (at your option) any later version.
13 This program 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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 /*------------------------.
32 | Distinguished symbols. |
33 `------------------------*/
35 symbol
*errtoken
= NULL
;
36 symbol
*undeftoken
= NULL
;
37 symbol
*endtoken
= NULL
;
38 symbol
*accept
= NULL
;
39 symbol
*startsymbol
= NULL
;
40 location startsymbol_location
;
42 /*---------------------------------------.
43 | Default %destructor's and %printer's. |
44 `---------------------------------------*/
46 static code_props default_tagged_destructor
= CODE_PROPS_NONE_INIT
;
47 static code_props default_tagless_destructor
= CODE_PROPS_NONE_INIT
;
48 static code_props default_tagged_printer
= CODE_PROPS_NONE_INIT
;
49 static code_props default_tagless_printer
= CODE_PROPS_NONE_INIT
;
51 /*---------------------------------.
52 | Create a new symbol, named TAG. |
53 `---------------------------------*/
56 symbol_new (uniqstr tag
, location loc
)
58 symbol
*res
= xmalloc (sizeof *res
);
64 res
->type_name
= NULL
;
65 code_props_none_init (&res
->destructor
);
66 code_props_none_init (&res
->printer
);
68 res
->number
= NUMBER_UNDEFINED
;
70 res
->assoc
= undef_assoc
;
71 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
74 res
->class = unknown_sym
;
75 res
->declared
= false;
77 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
78 fatal (_("too many symbols in input grammar (limit is %d)"),
79 SYMBOL_NUMBER_MAXIMUM
);
84 /*----------------------------------------.
85 | Create a new semantic type, named TAG. |
86 `----------------------------------------*/
88 static semantic_type
*
89 semantic_type_new (uniqstr tag
)
91 semantic_type
*res
= xmalloc (sizeof *res
);
95 code_props_none_init (&res
->destructor
);
96 code_props_none_init (&res
->printer
);
106 #define SYMBOL_ATTR_PRINT(Attr) \
108 fprintf (f, " %s { %s }", #Attr, s->Attr)
110 #define SYMBOL_CODE_PRINT(Attr) \
112 fprintf (f, " %s { %s }", #Attr, s->Attr.code)
115 symbol_print (symbol
*s
, FILE *f
)
119 fprintf (f
, "\"%s\"", s
->tag
);
120 SYMBOL_ATTR_PRINT (type_name
);
121 SYMBOL_CODE_PRINT (destructor
);
122 SYMBOL_CODE_PRINT (printer
);
125 fprintf (f
, "<NULL>");
128 #undef SYMBOL_ATTR_PRINT
129 #undef SYMBOL_CODE_PRINT
131 /*------------------------------------------------------------------.
132 | Complain that S's WHAT is redeclared at SECOND, and was first set |
134 `------------------------------------------------------------------*/
137 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
140 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
141 complain_at (first
, _("previous declaration"));
145 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
148 complain_at (second
, _("%s redeclaration for <%s>"), what
, s
->tag
);
149 complain_at (first
, _("previous declaration"));
153 /*-----------------------------------------------------------------.
154 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
156 `-----------------------------------------------------------------*/
159 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
164 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
165 uniqstr_assert (type_name
);
166 sym
->type_name
= type_name
;
167 sym
->type_location
= loc
;
172 /*-----------------------------------------.
173 | Set the DESTRUCTOR associated with SYM. |
174 `-----------------------------------------*/
177 symbol_destructor_set (symbol
*sym
, code_props
const *destructor
)
179 if (sym
->destructor
.code
)
180 symbol_redeclaration (sym
, "%destructor", sym
->destructor
.location
,
181 destructor
->location
);
182 sym
->destructor
= *destructor
;
185 /*------------------------------------------.
186 | Set the DESTRUCTOR associated with TYPE. |
187 `------------------------------------------*/
190 semantic_type_destructor_set (semantic_type
*type
,
191 code_props
const *destructor
)
193 if (type
->destructor
.code
)
194 semantic_type_redeclaration (type
, "%destructor",
195 type
->destructor
.location
,
196 destructor
->location
);
197 type
->destructor
= *destructor
;
200 /*---------------------------------------.
201 | Get the computed %destructor for SYM. |
202 `---------------------------------------*/
205 symbol_destructor_get (symbol
const *sym
)
207 /* Per-symbol %destructor. */
208 if (sym
->destructor
.code
)
209 return &sym
->destructor
;
211 /* Per-type %destructor. */
214 code_props
const *destructor
=
215 &semantic_type_get (sym
->type_name
)->destructor
;
216 if (destructor
->code
)
220 /* Apply default %destructor's only to user-defined symbols. */
221 if (sym
->tag
[0] == '$' || sym
== errtoken
)
222 return &code_props_none
;
225 return &default_tagged_destructor
;
226 return &default_tagless_destructor
;
229 /*--------------------------------------.
230 | Set the PRINTER associated with SYM. |
231 `--------------------------------------*/
234 symbol_printer_set (symbol
*sym
, code_props
const *printer
)
236 if (sym
->printer
.code
)
237 symbol_redeclaration (sym
, "%printer",
238 sym
->printer
.location
, printer
->location
);
239 sym
->printer
= *printer
;
242 /*---------------------------------------.
243 | Set the PRINTER associated with TYPE. |
244 `---------------------------------------*/
247 semantic_type_printer_set (semantic_type
*type
, code_props
const *printer
)
249 if (type
->printer
.code
)
250 semantic_type_redeclaration (type
, "%printer",
251 type
->printer
.location
, printer
->location
);
252 type
->printer
= *printer
;
255 /*------------------------------------.
256 | Get the computed %printer for SYM. |
257 `------------------------------------*/
260 symbol_printer_get (symbol
const *sym
)
262 /* Per-symbol %printer. */
263 if (sym
->printer
.code
)
264 return &sym
->printer
;
266 /* Per-type %printer. */
269 code_props
const *printer
= &semantic_type_get (sym
->type_name
)->printer
;
274 /* Apply the default %printer only to user-defined symbols. */
275 if (sym
->tag
[0] == '$' || sym
== errtoken
)
276 return &code_props_none
;
279 return &default_tagged_printer
;
280 return &default_tagless_printer
;
283 /*-----------------------------------------------------------------.
284 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
285 | with UNDEF_ASSOC as ASSOC. |
286 `-----------------------------------------------------------------*/
289 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
291 if (a
!= undef_assoc
)
294 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
298 sym
->prec_location
= loc
;
301 /* Only terminals have a precedence. */
302 symbol_class_set (sym
, token_sym
, loc
, false);
306 /*------------------------------------.
307 | Set the CLASS associated with SYM. |
308 `------------------------------------*/
311 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
313 if (sym
->class != unknown_sym
&& sym
->class != class)
315 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
316 sym
->declared
= false;
319 if (class == nterm_sym
&& sym
->class != nterm_sym
)
320 sym
->number
= nvars
++;
321 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
322 sym
->number
= ntokens
++;
329 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
330 sym
->declared
= true;
335 /*------------------------------------------------.
336 | Set the USER_TOKEN_NUMBER associated with SYM. |
337 `------------------------------------------------*/
340 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
342 int *user_token_numberp
;
344 aver (sym
->class == token_sym
);
346 if (sym
->user_token_number
!= USER_NUMBER_ALIAS
)
347 user_token_numberp
= &sym
->user_token_number
;
349 user_token_numberp
= &sym
->alias
->user_token_number
;
350 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
351 && *user_token_numberp
!= user_token_number
)
352 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
354 *user_token_numberp
= user_token_number
;
355 /* User defined $end token? */
356 if (user_token_number
== 0)
359 endtoken
->number
= 0;
360 /* It is always mapped to 0, so it was already counted in
367 /*----------------------------------------------------------.
368 | If SYM is not defined, report an error, and consider it a |
370 `----------------------------------------------------------*/
373 symbol_check_defined (symbol
*sym
)
375 if (sym
->class == unknown_sym
)
379 _("symbol %s is used, but is not defined as a token and has no rules"),
381 sym
->class = nterm_sym
;
382 sym
->number
= nvars
++;
389 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
391 return symbol_check_defined (sym
);
395 /*------------------------------------------------------------------.
396 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
397 | SYMVAL with SYM's type. |
398 `------------------------------------------------------------------*/
401 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
404 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
407 warn_at (loc
, _("symbol `%s' given more than one literal string"),
411 symval
->class = token_sym
;
412 symval
->user_token_number
= sym
->user_token_number
;
413 sym
->user_token_number
= USER_NUMBER_ALIAS
;
416 symval
->number
= sym
->number
;
417 symbol_type_set (symval
, sym
->type_name
, loc
);
422 /*---------------------------------------------------------.
423 | Check that THIS, and its alias, have same precedence and |
425 `---------------------------------------------------------*/
428 symbol_check_alias_consistency (symbol
*this)
430 symbol
*alias
= this;
431 symbol
*orig
= this->alias
;
433 /* Check only those that _are_ the aliases. */
434 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
437 if (orig
->type_name
!= alias
->type_name
)
440 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
442 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
446 if (orig
->destructor
.code
|| alias
->destructor
.code
)
448 if (orig
->destructor
.code
)
449 symbol_destructor_set (alias
, &orig
->destructor
);
451 symbol_destructor_set (orig
, &alias
->destructor
);
454 if (orig
->printer
.code
|| alias
->printer
.code
)
456 if (orig
->printer
.code
)
457 symbol_printer_set (alias
, &orig
->printer
);
459 symbol_printer_set (orig
, &alias
->printer
);
462 if (alias
->prec
|| orig
->prec
)
465 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
466 orig
->prec_location
);
468 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
469 alias
->prec_location
);
474 symbol_check_alias_consistency_processor (void *this,
475 void *null ATTRIBUTE_UNUSED
)
477 symbol_check_alias_consistency (this);
482 /*-------------------------------------------------------------------.
483 | Assign a symbol number, and write the definition of the token name |
484 | into FDEFINES. Put in SYMBOLS. |
485 `-------------------------------------------------------------------*/
488 symbol_pack (symbol
*this)
490 if (this->class == nterm_sym
)
492 this->number
+= ntokens
;
494 else if (this->alias
)
496 /* This symbol and its alias are a single token defn.
497 Allocate a tokno, and assign to both check agreement of
498 prec and assoc fields and make both the same */
499 if (this->number
== NUMBER_UNDEFINED
)
501 if (this == endtoken
|| this->alias
== endtoken
)
502 this->number
= this->alias
->number
= 0;
505 aver (this->alias
->number
!= NUMBER_UNDEFINED
);
506 this->number
= this->alias
->number
;
509 /* Do not do processing below for USER_NUMBER_ALIASes. */
510 if (this->user_token_number
== USER_NUMBER_ALIAS
)
513 else /* this->class == token_sym */
514 aver (this->number
!= NUMBER_UNDEFINED
);
516 symbols
[this->number
] = this;
521 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
523 return symbol_pack (this);
529 /*--------------------------------------------------.
530 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
531 `--------------------------------------------------*/
534 symbol_translation (symbol
*this)
537 if (this->class == token_sym
538 && this->user_token_number
!= USER_NUMBER_ALIAS
)
540 /* A token which translation has already been set? */
541 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
542 complain_at (this->location
,
543 _("tokens %s and %s both assigned number %d"),
544 symbols
[token_translations
[this->user_token_number
]]->tag
,
545 this->tag
, this->user_token_number
);
547 token_translations
[this->user_token_number
] = this->number
;
554 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
556 return symbol_translation (this);
560 /*---------------------------------------.
561 | Symbol and semantic type hash tables. |
562 `---------------------------------------*/
564 /* Initial capacity of symbol and semantic type hash table. */
565 #define HT_INITIAL_CAPACITY 257
567 static struct hash_table
*symbol_table
= NULL
;
568 static struct hash_table
*semantic_type_table
= NULL
;
571 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
573 /* Since tags are unique, we can compare the pointers themselves. */
574 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
578 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
580 /* Since names are unique, we can compare the pointers themselves. */
581 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
585 hash_symbol_comparator (void const *m1
, void const *m2
)
587 return hash_compare_symbol (m1
, m2
);
591 hash_semantic_type_comparator (void const *m1
, void const *m2
)
593 return hash_compare_semantic_type (m1
, m2
);
597 hash_symbol (const symbol
*m
, size_t tablesize
)
599 /* Since tags are unique, we can hash the pointer itself. */
600 return ((uintptr_t) m
->tag
) % tablesize
;
604 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
606 /* Since names are unique, we can hash the pointer itself. */
607 return ((uintptr_t) m
->tag
) % tablesize
;
611 hash_symbol_hasher (void const *m
, size_t tablesize
)
613 return hash_symbol (m
, tablesize
);
617 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
619 return hash_semantic_type (m
, tablesize
);
622 /*-------------------------------.
623 | Create the symbol hash table. |
624 `-------------------------------*/
629 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
632 hash_symbol_comparator
,
634 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
636 hash_semantic_type_hasher
,
637 hash_semantic_type_comparator
,
642 /*----------------------------------------------------------------.
643 | Find the symbol named KEY, and return it. If it does not exist |
645 `----------------------------------------------------------------*/
648 symbol_from_uniqstr (const uniqstr key
, location loc
)
654 entry
= hash_lookup (symbol_table
, &probe
);
658 /* First insertion in the hash. */
659 entry
= symbol_new (key
, loc
);
660 hash_insert (symbol_table
, entry
);
666 /*-----------------------------------------------------------------------.
667 | Find the semantic type named KEY, and return it. If it does not exist |
669 `-----------------------------------------------------------------------*/
672 semantic_type_from_uniqstr (const uniqstr key
)
675 semantic_type
*entry
;
678 entry
= hash_lookup (semantic_type_table
, &probe
);
682 /* First insertion in the hash. */
683 entry
= semantic_type_new (key
);
684 hash_insert (semantic_type_table
, entry
);
690 /*----------------------------------------------------------------.
691 | Find the symbol named KEY, and return it. If it does not exist |
693 `----------------------------------------------------------------*/
696 symbol_get (const char *key
, location loc
)
698 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
702 /*-----------------------------------------------------------------------.
703 | Find the semantic type named KEY, and return it. If it does not exist |
705 `-----------------------------------------------------------------------*/
708 semantic_type_get (const char *key
)
710 return semantic_type_from_uniqstr (uniqstr_new (key
));
714 /*------------------------------------------------------------------.
715 | Generate a dummy nonterminal, whose name cannot conflict with the |
717 `------------------------------------------------------------------*/
720 dummy_symbol_get (location loc
)
722 /* Incremented for each generated symbol. */
723 static int dummy_count
= 0;
724 static char buf
[256];
728 sprintf (buf
, "$@%d", ++dummy_count
);
729 sym
= symbol_get (buf
, loc
);
730 sym
->class = nterm_sym
;
731 sym
->number
= nvars
++;
736 symbol_is_dummy (const symbol
*sym
)
738 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
741 /*-------------------.
742 | Free the symbols. |
743 `-------------------*/
748 hash_free (symbol_table
);
749 hash_free (semantic_type_table
);
754 /*---------------------------------------------------------------.
755 | Look for undefined symbols, report an error, and consider them |
757 `---------------------------------------------------------------*/
760 symbols_do (Hash_processor processor
, void *processor_data
)
762 hash_do_for_each (symbol_table
, processor
, processor_data
);
766 /*--------------------------------------------------------------.
767 | Check that all the symbols are defined. Report any undefined |
768 | symbols and consider them nonterminals. |
769 `--------------------------------------------------------------*/
772 symbols_check_defined (void)
774 symbols_do (symbol_check_defined_processor
, NULL
);
777 /*------------------------------------------------------------------.
778 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
780 `------------------------------------------------------------------*/
783 symbols_token_translations_init (void)
785 bool num_256_available_p
= true;
788 /* Find the highest user token number, and whether 256, the POSIX
789 preferred user token number for the error token, is used. */
790 max_user_token_number
= 0;
791 for (i
= 0; i
< ntokens
; ++i
)
793 symbol
*this = symbols
[i
];
794 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
796 if (this->user_token_number
> max_user_token_number
)
797 max_user_token_number
= this->user_token_number
;
798 if (this->user_token_number
== 256)
799 num_256_available_p
= false;
803 /* If 256 is not used, assign it to error, to follow POSIX. */
804 if (num_256_available_p
805 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
806 errtoken
->user_token_number
= 256;
808 /* Set the missing user numbers. */
809 if (max_user_token_number
< 256)
810 max_user_token_number
= 256;
812 for (i
= 0; i
< ntokens
; ++i
)
814 symbol
*this = symbols
[i
];
815 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
816 this->user_token_number
= ++max_user_token_number
;
817 if (this->user_token_number
> max_user_token_number
)
818 max_user_token_number
= this->user_token_number
;
821 token_translations
= xnmalloc (max_user_token_number
+ 1,
822 sizeof *token_translations
);
824 /* Initialize all entries for literal tokens to 2, the internal
825 token number for $undefined, which represents all invalid inputs.
827 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
828 token_translations
[i
] = undeftoken
->number
;
829 symbols_do (symbol_translation_processor
, NULL
);
833 /*----------------------------------------------------------------.
834 | Assign symbol numbers, and write definition of token names into |
835 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
836 `----------------------------------------------------------------*/
841 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
843 symbols
= xcalloc (nsyms
, sizeof *symbols
);
844 symbols_do (symbol_pack_processor
, NULL
);
846 /* Aliases leave empty slots in symbols, so remove them. */
850 int nsyms_old
= nsyms
;
851 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
853 if (symbols
[readi
] == NULL
)
860 symbols
[writei
] = symbols
[readi
];
861 symbols
[writei
]->number
= writei
;
862 if (symbols
[writei
]->alias
)
863 symbols
[writei
]->alias
->number
= writei
;
868 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
870 symbols_token_translations_init ();
872 if (startsymbol
->class == unknown_sym
)
873 fatal_at (startsymbol_location
,
874 _("the start symbol %s is undefined"),
876 else if (startsymbol
->class == token_sym
)
877 fatal_at (startsymbol_location
,
878 _("the start symbol %s is a token"),
883 /*--------------------------------------------------.
884 | Set default tagged/tagless %destructor/%printer. |
885 `--------------------------------------------------*/
888 default_tagged_destructor_set (code_props
const *destructor
)
890 if (default_tagged_destructor
.code
)
892 complain_at (destructor
->location
,
893 _("redeclaration for default tagged %%destructor"));
894 complain_at (default_tagged_destructor
.location
,
895 _("previous declaration"));
897 default_tagged_destructor
= *destructor
;
901 default_tagless_destructor_set (code_props
const *destructor
)
903 if (default_tagless_destructor
.code
)
905 complain_at (destructor
->location
,
906 _("redeclaration for default tagless %%destructor"));
907 complain_at (default_tagless_destructor
.location
,
908 _("previous declaration"));
910 default_tagless_destructor
= *destructor
;
914 default_tagged_printer_set (code_props
const *printer
)
916 if (default_tagged_printer
.code
)
918 complain_at (printer
->location
,
919 _("redeclaration for default tagged %%printer"));
920 complain_at (default_tagged_printer
.location
,
921 _("previous declaration"));
923 default_tagged_printer
= *printer
;
927 default_tagless_printer_set (code_props
const *printer
)
929 if (default_tagless_printer
.code
)
931 complain_at (printer
->location
,
932 _("redeclaration for default tagless %%printer"));
933 complain_at (default_tagless_printer
.location
,
934 _("previous declaration"));
936 default_tagless_printer
= *printer
;