1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007,
5 Free Software Foundation, Inc.
7 This file is part of Bison, the GNU Compiler Compiler.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /*------------------------.
33 | Distinguished symbols. |
34 `------------------------*/
36 symbol
*errtoken
= NULL
;
37 symbol
*undeftoken
= NULL
;
38 symbol
*endtoken
= NULL
;
39 symbol
*accept
= NULL
;
40 symbol
*startsymbol
= NULL
;
41 location startsymbol_location
;
43 /*---------------------------------------.
44 | Default %destructor's and %printer's. |
45 `---------------------------------------*/
47 static code_props default_tagged_destructor
= CODE_PROPS_NONE_INIT
;
48 static code_props default_tagless_destructor
= CODE_PROPS_NONE_INIT
;
49 static code_props default_tagged_printer
= CODE_PROPS_NONE_INIT
;
50 static code_props default_tagless_printer
= CODE_PROPS_NONE_INIT
;
52 /*---------------------------------.
53 | Create a new symbol, named TAG. |
54 `---------------------------------*/
57 symbol_new (uniqstr tag
, location loc
)
59 symbol
*res
= xmalloc (sizeof *res
);
63 /* If the tag is not a string (starts with a double quote), check
64 that it is valid for Yacc. */
65 if (tag
[0] != '\"' && tag
[0] != '\'' && strchr (tag
, '-'))
66 yacc_at (loc
, _("POSIX Yacc forbids dashes in symbol names: %s"),
72 res
->type_name
= NULL
;
73 code_props_none_init (&res
->destructor
);
74 code_props_none_init (&res
->printer
);
76 res
->number
= NUMBER_UNDEFINED
;
78 res
->assoc
= undef_assoc
;
79 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
82 res
->class = unknown_sym
;
83 res
->declared
= false;
85 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
86 fatal (_("too many symbols in input grammar (limit is %d)"),
87 SYMBOL_NUMBER_MAXIMUM
);
92 /*----------------------------------------.
93 | Create a new semantic type, named TAG. |
94 `----------------------------------------*/
96 static semantic_type
*
97 semantic_type_new (uniqstr tag
)
99 semantic_type
*res
= xmalloc (sizeof *res
);
101 uniqstr_assert (tag
);
103 code_props_none_init (&res
->destructor
);
104 code_props_none_init (&res
->printer
);
114 #define SYMBOL_ATTR_PRINT(Attr) \
116 fprintf (f, " %s { %s }", #Attr, s->Attr)
118 #define SYMBOL_CODE_PRINT(Attr) \
120 fprintf (f, " %s { %s }", #Attr, s->Attr.code)
123 symbol_print (symbol
*s
, FILE *f
)
127 fprintf (f
, "\"%s\"", s
->tag
);
128 SYMBOL_ATTR_PRINT (type_name
);
129 SYMBOL_CODE_PRINT (destructor
);
130 SYMBOL_CODE_PRINT (printer
);
133 fprintf (f
, "<NULL>");
136 #undef SYMBOL_ATTR_PRINT
137 #undef SYMBOL_CODE_PRINT
139 /*------------------------------------------------------------------.
140 | Complain that S's WHAT is redeclared at SECOND, and was first set |
142 `------------------------------------------------------------------*/
145 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
148 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
149 complain_at (first
, _("previous declaration"));
153 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
156 complain_at (second
, _("%s redeclaration for <%s>"), what
, s
->tag
);
157 complain_at (first
, _("previous declaration"));
161 /*-----------------------------------------------------------------.
162 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
164 `-----------------------------------------------------------------*/
167 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
172 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
173 uniqstr_assert (type_name
);
174 sym
->type_name
= type_name
;
175 sym
->type_location
= loc
;
179 /*-----------------------------------.
180 | Get the CLASS associated with SYM. |
181 `-----------------------------------*/
184 symbol_class_get_string (symbol
*sym
)
188 if (sym
->class == token_sym
)
190 else if (sym
->class == nterm_sym
)
191 return "nonterminal";
197 /*-----------------------------------------.
198 | Set the DESTRUCTOR associated with SYM. |
199 `-----------------------------------------*/
202 symbol_destructor_set (symbol
*sym
, code_props
const *destructor
)
204 if (sym
->destructor
.code
)
205 symbol_redeclaration (sym
, "%destructor", sym
->destructor
.location
,
206 destructor
->location
);
207 sym
->destructor
= *destructor
;
210 /*------------------------------------------.
211 | Set the DESTRUCTOR associated with TYPE. |
212 `------------------------------------------*/
215 semantic_type_destructor_set (semantic_type
*type
,
216 code_props
const *destructor
)
218 if (type
->destructor
.code
)
219 semantic_type_redeclaration (type
, "%destructor",
220 type
->destructor
.location
,
221 destructor
->location
);
222 type
->destructor
= *destructor
;
225 /*---------------------------------------.
226 | Get the computed %destructor for SYM. |
227 `---------------------------------------*/
230 symbol_destructor_get (symbol
const *sym
)
232 /* Per-symbol %destructor. */
233 if (sym
->destructor
.code
)
234 return &sym
->destructor
;
236 /* Per-type %destructor. */
239 code_props
const *destructor
=
240 &semantic_type_get (sym
->type_name
)->destructor
;
241 if (destructor
->code
)
245 /* Apply default %destructor's only to user-defined symbols. */
246 if (sym
->tag
[0] == '$' || sym
== errtoken
)
247 return &code_props_none
;
250 return &default_tagged_destructor
;
251 return &default_tagless_destructor
;
254 /*--------------------------------------.
255 | Set the PRINTER associated with SYM. |
256 `--------------------------------------*/
259 symbol_printer_set (symbol
*sym
, code_props
const *printer
)
261 if (sym
->printer
.code
)
262 symbol_redeclaration (sym
, "%printer",
263 sym
->printer
.location
, printer
->location
);
264 sym
->printer
= *printer
;
267 /*---------------------------------------.
268 | Set the PRINTER associated with TYPE. |
269 `---------------------------------------*/
272 semantic_type_printer_set (semantic_type
*type
, code_props
const *printer
)
274 if (type
->printer
.code
)
275 semantic_type_redeclaration (type
, "%printer",
276 type
->printer
.location
, printer
->location
);
277 type
->printer
= *printer
;
280 /*------------------------------------.
281 | Get the computed %printer for SYM. |
282 `------------------------------------*/
285 symbol_printer_get (symbol
const *sym
)
287 /* Per-symbol %printer. */
288 if (sym
->printer
.code
)
289 return &sym
->printer
;
291 /* Per-type %printer. */
294 code_props
const *printer
= &semantic_type_get (sym
->type_name
)->printer
;
299 /* Apply the default %printer only to user-defined symbols. */
300 if (sym
->tag
[0] == '$' || sym
== errtoken
)
301 return &code_props_none
;
304 return &default_tagged_printer
;
305 return &default_tagless_printer
;
308 /*-----------------------------------------------------------------.
309 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
310 | with UNDEF_ASSOC as ASSOC. |
311 `-----------------------------------------------------------------*/
314 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
316 if (a
!= undef_assoc
)
319 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
323 sym
->prec_location
= loc
;
326 /* Only terminals have a precedence. */
327 symbol_class_set (sym
, token_sym
, loc
, false);
331 /*------------------------------------.
332 | Set the CLASS associated with SYM. |
333 `------------------------------------*/
336 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
338 if (sym
->class != unknown_sym
&& sym
->class != class)
340 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
341 sym
->declared
= false;
344 if (class == nterm_sym
&& sym
->class != nterm_sym
)
345 sym
->number
= nvars
++;
346 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
347 sym
->number
= ntokens
++;
354 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
355 sym
->declared
= true;
360 /*------------------------------------------------.
361 | Set the USER_TOKEN_NUMBER associated with SYM. |
362 `------------------------------------------------*/
365 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
367 int *user_token_numberp
;
369 if (sym
->user_token_number
!= USER_NUMBER_ALIAS
)
370 user_token_numberp
= &sym
->user_token_number
;
372 user_token_numberp
= &sym
->alias
->user_token_number
;
373 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
374 && *user_token_numberp
!= user_token_number
)
375 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
377 *user_token_numberp
= user_token_number
;
378 /* User defined $end token? */
379 if (user_token_number
== 0)
382 endtoken
->number
= 0;
383 /* It is always mapped to 0, so it was already counted in
390 /*----------------------------------------------------------.
391 | If SYM is not defined, report an error, and consider it a |
393 `----------------------------------------------------------*/
396 symbol_check_defined (symbol
*sym
)
398 if (sym
->class == unknown_sym
)
402 _("symbol %s is used, but is not defined as a token and has no rules"),
404 sym
->class = nterm_sym
;
405 sym
->number
= nvars
++;
412 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
414 return symbol_check_defined (sym
);
418 /*------------------------------------------------------------------.
419 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
420 | SYMVAL with SYM's type. |
421 `------------------------------------------------------------------*/
424 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
427 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
430 warn_at (loc
, _("symbol `%s' given more than one literal string"),
434 symval
->class = token_sym
;
435 symval
->user_token_number
= sym
->user_token_number
;
436 sym
->user_token_number
= USER_NUMBER_ALIAS
;
439 symval
->number
= sym
->number
;
440 symbol_type_set (symval
, sym
->type_name
, loc
);
445 /*---------------------------------------------------------.
446 | Check that THIS, and its alias, have same precedence and |
448 `---------------------------------------------------------*/
451 symbol_check_alias_consistency (symbol
*this)
453 symbol
*alias
= this;
454 symbol
*orig
= this->alias
;
456 /* Check only those that _are_ the aliases. */
457 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
460 if (orig
->type_name
!= alias
->type_name
)
463 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
465 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
469 if (orig
->destructor
.code
|| alias
->destructor
.code
)
471 if (orig
->destructor
.code
)
472 symbol_destructor_set (alias
, &orig
->destructor
);
474 symbol_destructor_set (orig
, &alias
->destructor
);
477 if (orig
->printer
.code
|| alias
->printer
.code
)
479 if (orig
->printer
.code
)
480 symbol_printer_set (alias
, &orig
->printer
);
482 symbol_printer_set (orig
, &alias
->printer
);
485 if (alias
->prec
|| orig
->prec
)
488 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
489 orig
->prec_location
);
491 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
492 alias
->prec_location
);
497 symbol_check_alias_consistency_processor (void *this,
498 void *null ATTRIBUTE_UNUSED
)
500 symbol_check_alias_consistency (this);
505 /*-------------------------------------------------------------------.
506 | Assign a symbol number, and write the definition of the token name |
507 | into FDEFINES. Put in SYMBOLS. |
508 `-------------------------------------------------------------------*/
511 symbol_pack (symbol
*this)
513 if (this->class == nterm_sym
)
515 this->number
+= ntokens
;
517 else if (this->alias
)
519 /* This symbol and its alias are a single token defn.
520 Allocate a tokno, and assign to both check agreement of
521 prec and assoc fields and make both the same */
522 if (this->number
== NUMBER_UNDEFINED
)
524 if (this == endtoken
|| this->alias
== endtoken
)
525 this->number
= this->alias
->number
= 0;
528 aver (this->alias
->number
!= NUMBER_UNDEFINED
);
529 this->number
= this->alias
->number
;
532 /* Do not do processing below for USER_NUMBER_ALIASes. */
533 if (this->user_token_number
== USER_NUMBER_ALIAS
)
536 else /* this->class == token_sym */
537 aver (this->number
!= NUMBER_UNDEFINED
);
539 symbols
[this->number
] = this;
544 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
546 return symbol_pack (this);
552 /*--------------------------------------------------.
553 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
554 `--------------------------------------------------*/
557 symbol_translation (symbol
*this)
560 if (this->class == token_sym
561 && this->user_token_number
!= USER_NUMBER_ALIAS
)
563 /* A token which translation has already been set? */
564 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
565 complain_at (this->location
,
566 _("tokens %s and %s both assigned number %d"),
567 symbols
[token_translations
[this->user_token_number
]]->tag
,
568 this->tag
, this->user_token_number
);
570 token_translations
[this->user_token_number
] = this->number
;
577 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
579 return symbol_translation (this);
583 /*---------------------------------------.
584 | Symbol and semantic type hash tables. |
585 `---------------------------------------*/
587 /* Initial capacity of symbol and semantic type hash table. */
588 #define HT_INITIAL_CAPACITY 257
590 static struct hash_table
*symbol_table
= NULL
;
591 static struct hash_table
*semantic_type_table
= NULL
;
594 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
596 /* Since tags are unique, we can compare the pointers themselves. */
597 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
601 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
603 /* Since names are unique, we can compare the pointers themselves. */
604 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
608 hash_symbol_comparator (void const *m1
, void const *m2
)
610 return hash_compare_symbol (m1
, m2
);
614 hash_semantic_type_comparator (void const *m1
, void const *m2
)
616 return hash_compare_semantic_type (m1
, m2
);
620 hash_symbol (const symbol
*m
, size_t tablesize
)
622 /* Since tags are unique, we can hash the pointer itself. */
623 return ((uintptr_t) m
->tag
) % tablesize
;
627 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
629 /* Since names are unique, we can hash the pointer itself. */
630 return ((uintptr_t) m
->tag
) % tablesize
;
634 hash_symbol_hasher (void const *m
, size_t tablesize
)
636 return hash_symbol (m
, tablesize
);
640 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
642 return hash_semantic_type (m
, tablesize
);
645 /*-------------------------------.
646 | Create the symbol hash table. |
647 `-------------------------------*/
652 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
655 hash_symbol_comparator
,
657 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
659 hash_semantic_type_hasher
,
660 hash_semantic_type_comparator
,
665 /*----------------------------------------------------------------.
666 | Find the symbol named KEY, and return it. If it does not exist |
668 `----------------------------------------------------------------*/
671 symbol_from_uniqstr (const uniqstr key
, location loc
)
677 entry
= hash_lookup (symbol_table
, &probe
);
681 /* First insertion in the hash. */
682 entry
= symbol_new (key
, loc
);
683 if (!hash_insert (symbol_table
, entry
))
690 /*-----------------------------------------------------------------------.
691 | Find the semantic type named KEY, and return it. If it does not exist |
693 `-----------------------------------------------------------------------*/
696 semantic_type_from_uniqstr (const uniqstr key
)
699 semantic_type
*entry
;
702 entry
= hash_lookup (semantic_type_table
, &probe
);
706 /* First insertion in the hash. */
707 entry
= semantic_type_new (key
);
708 if (!hash_insert (semantic_type_table
, entry
))
715 /*----------------------------------------------------------------.
716 | Find the symbol named KEY, and return it. If it does not exist |
718 `----------------------------------------------------------------*/
721 symbol_get (const char *key
, location loc
)
723 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
727 /*-----------------------------------------------------------------------.
728 | Find the semantic type named KEY, and return it. If it does not exist |
730 `-----------------------------------------------------------------------*/
733 semantic_type_get (const char *key
)
735 return semantic_type_from_uniqstr (uniqstr_new (key
));
739 /*------------------------------------------------------------------.
740 | Generate a dummy nonterminal, whose name cannot conflict with the |
742 `------------------------------------------------------------------*/
745 dummy_symbol_get (location loc
)
747 /* Incremented for each generated symbol. */
748 static int dummy_count
= 0;
749 static char buf
[256];
753 sprintf (buf
, "$@%d", ++dummy_count
);
754 sym
= symbol_get (buf
, loc
);
755 sym
->class = nterm_sym
;
756 sym
->number
= nvars
++;
761 symbol_is_dummy (const symbol
*sym
)
763 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
766 /*-------------------.
767 | Free the symbols. |
768 `-------------------*/
773 hash_free (symbol_table
);
774 hash_free (semantic_type_table
);
779 /*---------------------------------------------------------------.
780 | Look for undefined symbols, report an error, and consider them |
782 `---------------------------------------------------------------*/
785 symbols_do (Hash_processor processor
, void *processor_data
)
787 hash_do_for_each (symbol_table
, processor
, processor_data
);
791 /*--------------------------------------------------------------.
792 | Check that all the symbols are defined. Report any undefined |
793 | symbols and consider them nonterminals. |
794 `--------------------------------------------------------------*/
797 symbols_check_defined (void)
799 symbols_do (symbol_check_defined_processor
, NULL
);
802 /*------------------------------------------------------------------.
803 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
805 `------------------------------------------------------------------*/
808 symbols_token_translations_init (void)
810 bool num_256_available_p
= true;
813 /* Find the highest user token number, and whether 256, the POSIX
814 preferred user token number for the error token, is used. */
815 max_user_token_number
= 0;
816 for (i
= 0; i
< ntokens
; ++i
)
818 symbol
*this = symbols
[i
];
819 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
821 if (this->user_token_number
> max_user_token_number
)
822 max_user_token_number
= this->user_token_number
;
823 if (this->user_token_number
== 256)
824 num_256_available_p
= false;
828 /* If 256 is not used, assign it to error, to follow POSIX. */
829 if (num_256_available_p
830 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
831 errtoken
->user_token_number
= 256;
833 /* Set the missing user numbers. */
834 if (max_user_token_number
< 256)
835 max_user_token_number
= 256;
837 for (i
= 0; i
< ntokens
; ++i
)
839 symbol
*this = symbols
[i
];
840 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
841 this->user_token_number
= ++max_user_token_number
;
842 if (this->user_token_number
> max_user_token_number
)
843 max_user_token_number
= this->user_token_number
;
846 token_translations
= xnmalloc (max_user_token_number
+ 1,
847 sizeof *token_translations
);
849 /* Initialize all entries for literal tokens to 2, the internal
850 token number for $undefined, which represents all invalid inputs.
852 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
853 token_translations
[i
] = undeftoken
->number
;
854 symbols_do (symbol_translation_processor
, NULL
);
858 /*----------------------------------------------------------------.
859 | Assign symbol numbers, and write definition of token names into |
860 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
861 `----------------------------------------------------------------*/
866 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
868 symbols
= xcalloc (nsyms
, sizeof *symbols
);
869 symbols_do (symbol_pack_processor
, NULL
);
871 /* Aliases leave empty slots in symbols, so remove them. */
875 int nsyms_old
= nsyms
;
876 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
878 if (symbols
[readi
] == NULL
)
885 symbols
[writei
] = symbols
[readi
];
886 symbols
[writei
]->number
= writei
;
887 if (symbols
[writei
]->alias
)
888 symbols
[writei
]->alias
->number
= writei
;
893 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
895 symbols_token_translations_init ();
897 if (startsymbol
->class == unknown_sym
)
898 fatal_at (startsymbol_location
,
899 _("the start symbol %s is undefined"),
901 else if (startsymbol
->class == token_sym
)
902 fatal_at (startsymbol_location
,
903 _("the start symbol %s is a token"),
908 /*--------------------------------------------------.
909 | Set default tagged/tagless %destructor/%printer. |
910 `--------------------------------------------------*/
913 default_tagged_destructor_set (code_props
const *destructor
)
915 if (default_tagged_destructor
.code
)
917 complain_at (destructor
->location
,
918 _("redeclaration for default tagged %%destructor"));
919 complain_at (default_tagged_destructor
.location
,
920 _("previous declaration"));
922 default_tagged_destructor
= *destructor
;
926 default_tagless_destructor_set (code_props
const *destructor
)
928 if (default_tagless_destructor
.code
)
930 complain_at (destructor
->location
,
931 _("redeclaration for default tagless %%destructor"));
932 complain_at (default_tagless_destructor
.location
,
933 _("previous declaration"));
935 default_tagless_destructor
= *destructor
;
939 default_tagged_printer_set (code_props
const *printer
)
941 if (default_tagged_printer
.code
)
943 complain_at (printer
->location
,
944 _("redeclaration for default tagged %%printer"));
945 complain_at (default_tagged_printer
.location
,
946 _("previous declaration"));
948 default_tagged_printer
= *printer
;
952 default_tagless_printer_set (code_props
const *printer
)
954 if (default_tagless_printer
.code
)
956 complain_at (printer
->location
,
957 _("redeclaration for default tagless %%printer"));
958 complain_at (default_tagless_printer
.location
,
959 _("previous declaration"));
961 default_tagless_printer
= *printer
;