1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000-2002, 2004-2012 Free Software
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/>. */
32 /*-------------------------------------------------------------------.
33 | Symbols sorted by tag. Allocated by the first invocation of |
34 | symbols_do, after which no more symbols should be created. |
35 `-------------------------------------------------------------------*/
37 static symbol
**symbols_sorted
= NULL
;
39 /*------------------------.
40 | Distinguished symbols. |
41 `------------------------*/
43 symbol
*errtoken
= NULL
;
44 symbol
*undeftoken
= NULL
;
45 symbol
*endtoken
= NULL
;
46 symbol
*accept
= NULL
;
47 symbol
*startsymbol
= NULL
;
48 location startsymbol_location
;
50 /*---------------------------------------.
51 | Default %destructor's and %printer's. |
52 `---------------------------------------*/
54 static code_props default_tagged_destructor
= CODE_PROPS_NONE_INIT
;
55 static code_props default_tagless_destructor
= CODE_PROPS_NONE_INIT
;
56 static code_props default_tagged_printer
= CODE_PROPS_NONE_INIT
;
57 static code_props default_tagless_printer
= CODE_PROPS_NONE_INIT
;
59 /*---------------------------------.
60 | Create a new symbol, named TAG. |
61 `---------------------------------*/
64 symbol_new (uniqstr tag
, location loc
)
66 symbol
*res
= xmalloc (sizeof *res
);
70 /* If the tag is not a string (starts with a double quote), check
71 that it is valid for Yacc. */
72 if (tag
[0] != '\"' && tag
[0] != '\'' && mbschr (tag
, '-'))
73 yacc_at (loc
, _("POSIX Yacc forbids dashes in symbol names: %s"),
79 res
->type_name
= NULL
;
80 code_props_none_init (&res
->destructor
);
81 code_props_none_init (&res
->printer
);
83 res
->number
= NUMBER_UNDEFINED
;
85 res
->assoc
= undef_assoc
;
86 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
89 res
->class = unknown_sym
;
90 res
->declared
= false;
92 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
93 fatal (_("too many symbols in input grammar (limit is %d)"),
94 SYMBOL_NUMBER_MAXIMUM
);
99 /*----------------------------------------.
100 | Create a new semantic type, named TAG. |
101 `----------------------------------------*/
103 static semantic_type
*
104 semantic_type_new (uniqstr tag
)
106 semantic_type
*res
= xmalloc (sizeof *res
);
108 uniqstr_assert (tag
);
110 code_props_none_init (&res
->destructor
);
111 code_props_none_init (&res
->printer
);
121 #define SYMBOL_ATTR_PRINT(Attr) \
123 fprintf (f, " %s { %s }", #Attr, s->Attr)
125 #define SYMBOL_CODE_PRINT(Attr) \
127 fprintf (f, " %s { %s }", #Attr, s->Attr.code)
130 symbol_print (symbol
*s
, FILE *f
)
134 fprintf (f
, "\"%s\"", s
->tag
);
135 SYMBOL_ATTR_PRINT (type_name
);
136 SYMBOL_CODE_PRINT (destructor
);
137 SYMBOL_CODE_PRINT (printer
);
140 fprintf (f
, "<NULL>");
143 #undef SYMBOL_ATTR_PRINT
144 #undef SYMBOL_CODE_PRINT
146 /*------------------------------------------------------------------.
147 | Complain that S's WHAT is redeclared at SECOND, and was first set |
149 `------------------------------------------------------------------*/
152 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
155 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
156 complain_at (first
, _("previous declaration"));
160 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
163 complain_at (second
, _("%s redeclaration for <%s>"), what
, s
->tag
);
164 complain_at (first
, _("previous declaration"));
169 /*-----------------------------------------------------------------.
170 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
172 `-----------------------------------------------------------------*/
175 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
180 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
181 uniqstr_assert (type_name
);
182 sym
->type_name
= type_name
;
183 sym
->type_location
= loc
;
187 /*-----------------------------------------.
188 | Set the DESTRUCTOR associated with SYM. |
189 `-----------------------------------------*/
192 symbol_destructor_set (symbol
*sym
, code_props
const *destructor
)
194 if (sym
->destructor
.code
)
195 symbol_redeclaration (sym
, "%destructor", sym
->destructor
.location
,
196 destructor
->location
);
197 sym
->destructor
= *destructor
;
200 /*------------------------------------------.
201 | Set the DESTRUCTOR associated with TYPE. |
202 `------------------------------------------*/
205 semantic_type_destructor_set (semantic_type
*type
,
206 code_props
const *destructor
)
208 if (type
->destructor
.code
)
209 semantic_type_redeclaration (type
, "%destructor",
210 type
->destructor
.location
,
211 destructor
->location
);
212 type
->destructor
= *destructor
;
215 /*---------------------------------------.
216 | Get the computed %destructor for SYM. |
217 `---------------------------------------*/
220 symbol_destructor_get (symbol
const *sym
)
222 /* Per-symbol %destructor. */
223 if (sym
->destructor
.code
)
224 return &sym
->destructor
;
226 /* Per-type %destructor. */
229 code_props
const *destructor
=
230 &semantic_type_get (sym
->type_name
)->destructor
;
231 if (destructor
->code
)
235 /* Apply default %destructor's only to user-defined symbols. */
236 if (sym
->tag
[0] == '$' || sym
== errtoken
)
237 return &code_props_none
;
240 return &default_tagged_destructor
;
241 return &default_tagless_destructor
;
244 /*--------------------------------------.
245 | Set the PRINTER associated with SYM. |
246 `--------------------------------------*/
249 symbol_printer_set (symbol
*sym
, code_props
const *printer
)
251 if (sym
->printer
.code
)
252 symbol_redeclaration (sym
, "%printer",
253 sym
->printer
.location
, printer
->location
);
254 sym
->printer
= *printer
;
257 /*---------------------------------------.
258 | Set the PRINTER associated with TYPE. |
259 `---------------------------------------*/
262 semantic_type_printer_set (semantic_type
*type
, code_props
const *printer
)
264 if (type
->printer
.code
)
265 semantic_type_redeclaration (type
, "%printer",
266 type
->printer
.location
, printer
->location
);
267 type
->printer
= *printer
;
270 /*------------------------------------.
271 | Get the computed %printer for SYM. |
272 `------------------------------------*/
275 symbol_printer_get (symbol
const *sym
)
277 /* Per-symbol %printer. */
278 if (sym
->printer
.code
)
279 return &sym
->printer
;
281 /* Per-type %printer. */
284 code_props
const *printer
= &semantic_type_get (sym
->type_name
)->printer
;
289 /* Apply the default %printer only to user-defined symbols. */
290 if (sym
->tag
[0] == '$' || sym
== errtoken
)
291 return &code_props_none
;
294 return &default_tagged_printer
;
295 return &default_tagless_printer
;
298 /*-----------------------------------------------------------------.
299 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
300 | with UNDEF_ASSOC as ASSOC. |
301 `-----------------------------------------------------------------*/
304 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
306 if (a
!= undef_assoc
)
309 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
313 sym
->prec_location
= loc
;
316 /* Only terminals have a precedence. */
317 symbol_class_set (sym
, token_sym
, loc
, false);
321 /*------------------------------------.
322 | Set the CLASS associated with SYM. |
323 `------------------------------------*/
326 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
328 if (sym
->class != unknown_sym
&& sym
->class != class)
330 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
331 sym
->declared
= false;
334 if (class == nterm_sym
&& sym
->class != nterm_sym
)
335 sym
->number
= nvars
++;
336 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
337 sym
->number
= ntokens
++;
344 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
345 sym
->declared
= true;
350 /*------------------------------------------------.
351 | Set the USER_TOKEN_NUMBER associated with SYM. |
352 `------------------------------------------------*/
355 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
357 int *user_token_numberp
;
359 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
360 user_token_numberp
= &sym
->user_token_number
;
362 user_token_numberp
= &sym
->alias
->user_token_number
;
363 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
364 && *user_token_numberp
!= user_token_number
)
365 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
367 *user_token_numberp
= user_token_number
;
368 /* User defined $end token? */
369 if (user_token_number
== 0)
372 /* It is always mapped to 0, so it was already counted in
374 if (endtoken
->number
!= NUMBER_UNDEFINED
)
376 endtoken
->number
= 0;
381 /*----------------------------------------------------------.
382 | If SYM is not defined, report an error, and consider it a |
384 `----------------------------------------------------------*/
387 symbol_check_defined (symbol
*sym
)
389 if (sym
->class == unknown_sym
)
393 _("symbol %s is used, but is not defined as a token and has no rules"),
395 sym
->class = nterm_sym
;
396 sym
->number
= nvars
++;
403 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
405 return symbol_check_defined (sym
);
410 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
413 warn_at (loc
, _("symbol %s used more than once as a literal string"),
416 warn_at (loc
, _("symbol %s given more than one literal string"),
420 str
->class = token_sym
;
421 str
->user_token_number
= sym
->user_token_number
;
422 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
425 str
->number
= sym
->number
;
426 symbol_type_set (str
, sym
->type_name
, loc
);
431 /*---------------------------------------------------------.
432 | Check that THIS, and its alias, have same precedence and |
434 `---------------------------------------------------------*/
437 symbol_check_alias_consistency (symbol
*this)
440 symbol
*str
= this->alias
;
442 /* Check only the symbol in the symbol-string pair. */
444 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
447 if (str
->type_name
!= sym
->type_name
)
450 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
452 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
456 if (str
->destructor
.code
|| sym
->destructor
.code
)
458 if (str
->destructor
.code
)
459 symbol_destructor_set (sym
, &str
->destructor
);
461 symbol_destructor_set (str
, &sym
->destructor
);
464 if (str
->printer
.code
|| sym
->printer
.code
)
466 if (str
->printer
.code
)
467 symbol_printer_set (sym
, &str
->printer
);
469 symbol_printer_set (str
, &sym
->printer
);
472 if (sym
->prec
|| str
->prec
)
475 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
478 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
484 symbol_check_alias_consistency_processor (void *this,
485 void *null ATTRIBUTE_UNUSED
)
487 symbol_check_alias_consistency (this);
492 /*-------------------------------------------------------------------.
493 | Assign a symbol number, and write the definition of the token name |
494 | into FDEFINES. Put in SYMBOLS. |
495 `-------------------------------------------------------------------*/
498 symbol_pack (symbol
*this)
500 aver (this->number
!= NUMBER_UNDEFINED
);
501 if (this->class == nterm_sym
)
502 this->number
+= ntokens
;
503 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
506 symbols
[this->number
] = this;
511 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
513 return symbol_pack (this);
518 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
520 /* User token numbers are not assigned during the parsing, but in a
521 second step, via a traversal of the symbol table sorted on tag.
523 However, error messages make more sense if we keep the first
524 declaration first. */
525 if (location_cmp (first
->location
, second
->location
) > 0)
531 complain_at (second
->location
,
532 _("user token number %d redeclaration for %s"),
534 complain_at (first
->location
, _("previous declaration for %s"),
538 /*--------------------------------------------------.
539 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
540 `--------------------------------------------------*/
543 symbol_translation (symbol
*this)
546 if (this->class == token_sym
547 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
549 /* A token which translation has already been set? */
550 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
551 user_token_number_redeclaration
552 (this->user_token_number
,
553 symbols
[token_translations
[this->user_token_number
]],
556 token_translations
[this->user_token_number
] = this->number
;
563 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
565 return symbol_translation (this);
569 /*---------------------------------------.
570 | Symbol and semantic type hash tables. |
571 `---------------------------------------*/
573 /* Initial capacity of symbol and semantic type hash table. */
574 #define HT_INITIAL_CAPACITY 257
576 static struct hash_table
*symbol_table
= NULL
;
577 static struct hash_table
*semantic_type_table
= NULL
;
580 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
582 /* Since tags are unique, we can compare the pointers themselves. */
583 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
587 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
589 /* Since names are unique, we can compare the pointers themselves. */
590 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
594 hash_symbol_comparator (void const *m1
, void const *m2
)
596 return hash_compare_symbol (m1
, m2
);
600 hash_semantic_type_comparator (void const *m1
, void const *m2
)
602 return hash_compare_semantic_type (m1
, m2
);
606 hash_symbol (const symbol
*m
, size_t tablesize
)
608 /* Since tags are unique, we can hash the pointer itself. */
609 return ((uintptr_t) m
->tag
) % tablesize
;
613 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
615 /* Since names are unique, we can hash the pointer itself. */
616 return ((uintptr_t) m
->tag
) % tablesize
;
620 hash_symbol_hasher (void const *m
, size_t tablesize
)
622 return hash_symbol (m
, tablesize
);
626 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
628 return hash_semantic_type (m
, tablesize
);
631 /*-------------------------------.
632 | Create the symbol hash table. |
633 `-------------------------------*/
638 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
641 hash_symbol_comparator
,
643 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
645 hash_semantic_type_hasher
,
646 hash_semantic_type_comparator
,
651 /*----------------------------------------------------------------.
652 | Find the symbol named KEY, and return it. If it does not exist |
654 `----------------------------------------------------------------*/
657 symbol_from_uniqstr (const uniqstr key
, location loc
)
663 entry
= hash_lookup (symbol_table
, &probe
);
667 /* First insertion in the hash. */
668 aver (!symbols_sorted
);
669 entry
= symbol_new (key
, loc
);
670 if (!hash_insert (symbol_table
, entry
))
677 /*-----------------------------------------------------------------------.
678 | Find the semantic type named KEY, and return it. If it does not exist |
680 `-----------------------------------------------------------------------*/
683 semantic_type_from_uniqstr (const uniqstr key
)
686 semantic_type
*entry
;
689 entry
= hash_lookup (semantic_type_table
, &probe
);
693 /* First insertion in the hash. */
694 entry
= semantic_type_new (key
);
695 if (!hash_insert (semantic_type_table
, entry
))
702 /*----------------------------------------------------------------.
703 | Find the symbol named KEY, and return it. If it does not exist |
705 `----------------------------------------------------------------*/
708 symbol_get (const char *key
, location loc
)
710 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
714 /*-----------------------------------------------------------------------.
715 | Find the semantic type named KEY, and return it. If it does not exist |
717 `-----------------------------------------------------------------------*/
720 semantic_type_get (const char *key
)
722 return semantic_type_from_uniqstr (uniqstr_new (key
));
726 /*------------------------------------------------------------------.
727 | Generate a dummy nonterminal, whose name cannot conflict with the |
729 `------------------------------------------------------------------*/
732 dummy_symbol_get (location loc
)
734 /* Incremented for each generated symbol. */
735 static int dummy_count
= 0;
736 static char buf
[256];
740 sprintf (buf
, "$@%d", ++dummy_count
);
741 sym
= symbol_get (buf
, loc
);
742 sym
->class = nterm_sym
;
743 sym
->number
= nvars
++;
748 symbol_is_dummy (const symbol
*sym
)
750 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
753 /*-------------------.
754 | Free the symbols. |
755 `-------------------*/
760 hash_free (symbol_table
);
761 hash_free (semantic_type_table
);
763 free (symbols_sorted
);
767 /*---------------------------------------------------------------.
768 | Look for undefined symbols, report an error, and consider them |
770 `---------------------------------------------------------------*/
773 symbols_cmp (symbol
const *a
, symbol
const *b
)
775 return strcmp (a
->tag
, b
->tag
);
779 symbols_cmp_qsort (void const *a
, void const *b
)
781 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
785 symbols_do (Hash_processor processor
, void *processor_data
)
787 size_t count
= hash_get_n_entries (symbol_table
);
790 symbols_sorted
= xnmalloc (count
, sizeof *symbols_sorted
);
791 hash_get_entries (symbol_table
, (void**)symbols_sorted
, count
);
792 qsort (symbols_sorted
, count
, sizeof *symbols_sorted
,
797 for (i
= 0; i
< count
; ++i
)
798 processor (symbols_sorted
[i
], processor_data
);
802 /*--------------------------------------------------------------.
803 | Check that all the symbols are defined. Report any undefined |
804 | symbols and consider them nonterminals. |
805 `--------------------------------------------------------------*/
808 symbols_check_defined (void)
810 symbols_do (symbol_check_defined_processor
, NULL
);
813 /*------------------------------------------------------------------.
814 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
816 `------------------------------------------------------------------*/
819 symbols_token_translations_init (void)
821 bool num_256_available_p
= true;
824 /* Find the highest user token number, and whether 256, the POSIX
825 preferred user token number for the error token, is used. */
826 max_user_token_number
= 0;
827 for (i
= 0; i
< ntokens
; ++i
)
829 symbol
*this = symbols
[i
];
830 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
832 if (this->user_token_number
> max_user_token_number
)
833 max_user_token_number
= this->user_token_number
;
834 if (this->user_token_number
== 256)
835 num_256_available_p
= false;
839 /* If 256 is not used, assign it to error, to follow POSIX. */
840 if (num_256_available_p
841 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
842 errtoken
->user_token_number
= 256;
844 /* Set the missing user numbers. */
845 if (max_user_token_number
< 256)
846 max_user_token_number
= 256;
848 for (i
= 0; i
< ntokens
; ++i
)
850 symbol
*this = symbols
[i
];
851 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
852 this->user_token_number
= ++max_user_token_number
;
853 if (this->user_token_number
> max_user_token_number
)
854 max_user_token_number
= this->user_token_number
;
857 token_translations
= xnmalloc (max_user_token_number
+ 1,
858 sizeof *token_translations
);
860 /* Initialize all entries for literal tokens to 2, the internal
861 token number for $undefined, which represents all invalid inputs.
863 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
864 token_translations
[i
] = undeftoken
->number
;
865 symbols_do (symbol_translation_processor
, NULL
);
869 /*----------------------------------------------------------------.
870 | Assign symbol numbers, and write definition of token names into |
871 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
872 `----------------------------------------------------------------*/
877 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
879 symbols
= xcalloc (nsyms
, sizeof *symbols
);
880 symbols_do (symbol_pack_processor
, NULL
);
882 /* Aliases leave empty slots in symbols, so remove them. */
886 int nsyms_old
= nsyms
;
887 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
889 if (symbols
[readi
] == NULL
)
896 symbols
[writei
] = symbols
[readi
];
897 symbols
[writei
]->number
= writei
;
898 if (symbols
[writei
]->alias
)
899 symbols
[writei
]->alias
->number
= writei
;
904 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
906 symbols_token_translations_init ();
908 if (startsymbol
->class == unknown_sym
)
909 fatal_at (startsymbol_location
,
910 _("the start symbol %s is undefined"),
912 else if (startsymbol
->class == token_sym
)
913 fatal_at (startsymbol_location
,
914 _("the start symbol %s is a token"),
919 /*--------------------------------------------------.
920 | Set default tagged/tagless %destructor/%printer. |
921 `--------------------------------------------------*/
924 default_tagged_destructor_set (code_props
const *destructor
)
926 if (default_tagged_destructor
.code
)
928 complain_at (destructor
->location
,
929 _("redeclaration for default tagged %%destructor"));
930 complain_at (default_tagged_destructor
.location
,
931 _("previous declaration"));
933 default_tagged_destructor
= *destructor
;
937 default_tagless_destructor_set (code_props
const *destructor
)
939 if (default_tagless_destructor
.code
)
941 complain_at (destructor
->location
,
942 _("redeclaration for default tagless %%destructor"));
943 complain_at (default_tagless_destructor
.location
,
944 _("previous declaration"));
946 default_tagless_destructor
= *destructor
;
950 default_tagged_printer_set (code_props
const *printer
)
952 if (default_tagged_printer
.code
)
954 complain_at (printer
->location
,
955 _("redeclaration for default tagged %%printer"));
956 complain_at (default_tagged_printer
.location
,
957 _("previous declaration"));
959 default_tagged_printer
= *printer
;
963 default_tagless_printer_set (code_props
const *printer
)
965 if (default_tagless_printer
.code
)
967 complain_at (printer
->location
,
968 _("redeclaration for default tagless %%printer"));
969 complain_at (default_tagless_printer
.location
,
970 _("previous declaration"));
972 default_tagless_printer
= *printer
;