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
;
171 /*-----------------------------------.
172 | Get the CLASS associated with SYM. |
173 `-----------------------------------*/
176 symbol_class_get_string (symbol
*sym
)
180 if (sym
->class == token_sym
)
182 else if (sym
->class == nterm_sym
)
183 return "nonterminal";
189 /*-----------------------------------------.
190 | Set the DESTRUCTOR associated with SYM. |
191 `-----------------------------------------*/
194 symbol_destructor_set (symbol
*sym
, code_props
const *destructor
)
196 if (sym
->destructor
.code
)
197 symbol_redeclaration (sym
, "%destructor", sym
->destructor
.location
,
198 destructor
->location
);
199 sym
->destructor
= *destructor
;
202 /*------------------------------------------.
203 | Set the DESTRUCTOR associated with TYPE. |
204 `------------------------------------------*/
207 semantic_type_destructor_set (semantic_type
*type
,
208 code_props
const *destructor
)
210 if (type
->destructor
.code
)
211 semantic_type_redeclaration (type
, "%destructor",
212 type
->destructor
.location
,
213 destructor
->location
);
214 type
->destructor
= *destructor
;
217 /*---------------------------------------.
218 | Get the computed %destructor for SYM. |
219 `---------------------------------------*/
222 symbol_destructor_get (symbol
const *sym
)
224 /* Per-symbol %destructor. */
225 if (sym
->destructor
.code
)
226 return &sym
->destructor
;
228 /* Per-type %destructor. */
231 code_props
const *destructor
=
232 &semantic_type_get (sym
->type_name
)->destructor
;
233 if (destructor
->code
)
237 /* Apply default %destructor's only to user-defined symbols. */
238 if (sym
->tag
[0] == '$' || sym
== errtoken
)
239 return &code_props_none
;
242 return &default_tagged_destructor
;
243 return &default_tagless_destructor
;
246 /*--------------------------------------.
247 | Set the PRINTER associated with SYM. |
248 `--------------------------------------*/
251 symbol_printer_set (symbol
*sym
, code_props
const *printer
)
253 if (sym
->printer
.code
)
254 symbol_redeclaration (sym
, "%printer",
255 sym
->printer
.location
, printer
->location
);
256 sym
->printer
= *printer
;
259 /*---------------------------------------.
260 | Set the PRINTER associated with TYPE. |
261 `---------------------------------------*/
264 semantic_type_printer_set (semantic_type
*type
, code_props
const *printer
)
266 if (type
->printer
.code
)
267 semantic_type_redeclaration (type
, "%printer",
268 type
->printer
.location
, printer
->location
);
269 type
->printer
= *printer
;
272 /*------------------------------------.
273 | Get the computed %printer for SYM. |
274 `------------------------------------*/
277 symbol_printer_get (symbol
const *sym
)
279 /* Per-symbol %printer. */
280 if (sym
->printer
.code
)
281 return &sym
->printer
;
283 /* Per-type %printer. */
286 code_props
const *printer
= &semantic_type_get (sym
->type_name
)->printer
;
291 /* Apply the default %printer only to user-defined symbols. */
292 if (sym
->tag
[0] == '$' || sym
== errtoken
)
293 return &code_props_none
;
296 return &default_tagged_printer
;
297 return &default_tagless_printer
;
300 /*-----------------------------------------------------------------.
301 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
302 | with UNDEF_ASSOC as ASSOC. |
303 `-----------------------------------------------------------------*/
306 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
308 if (a
!= undef_assoc
)
311 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
315 sym
->prec_location
= loc
;
318 /* Only terminals have a precedence. */
319 symbol_class_set (sym
, token_sym
, loc
, false);
323 /*------------------------------------.
324 | Set the CLASS associated with SYM. |
325 `------------------------------------*/
328 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
330 if (sym
->class != unknown_sym
&& sym
->class != class)
332 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
333 sym
->declared
= false;
336 if (class == nterm_sym
&& sym
->class != nterm_sym
)
337 sym
->number
= nvars
++;
338 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
339 sym
->number
= ntokens
++;
346 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
347 sym
->declared
= true;
352 /*------------------------------------------------.
353 | Set the USER_TOKEN_NUMBER associated with SYM. |
354 `------------------------------------------------*/
357 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
359 int *user_token_numberp
;
361 aver (sym
->class == token_sym
);
363 if (sym
->user_token_number
!= USER_NUMBER_ALIAS
)
364 user_token_numberp
= &sym
->user_token_number
;
366 user_token_numberp
= &sym
->alias
->user_token_number
;
367 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
368 && *user_token_numberp
!= user_token_number
)
369 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
371 *user_token_numberp
= user_token_number
;
372 /* User defined $end token? */
373 if (user_token_number
== 0)
376 endtoken
->number
= 0;
377 /* It is always mapped to 0, so it was already counted in
384 /*----------------------------------------------------------.
385 | If SYM is not defined, report an error, and consider it a |
387 `----------------------------------------------------------*/
390 symbol_check_defined (symbol
*sym
)
392 if (sym
->class == unknown_sym
)
396 _("symbol %s is used, but is not defined as a token and has no rules"),
398 sym
->class = nterm_sym
;
399 sym
->number
= nvars
++;
406 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
408 return symbol_check_defined (sym
);
412 /*------------------------------------------------------------------.
413 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
414 | SYMVAL with SYM's type. |
415 `------------------------------------------------------------------*/
418 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
421 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
424 warn_at (loc
, _("symbol `%s' given more than one literal string"),
428 symval
->class = token_sym
;
429 symval
->user_token_number
= sym
->user_token_number
;
430 sym
->user_token_number
= USER_NUMBER_ALIAS
;
433 symval
->number
= sym
->number
;
434 symbol_type_set (symval
, sym
->type_name
, loc
);
439 /*---------------------------------------------------------.
440 | Check that THIS, and its alias, have same precedence and |
442 `---------------------------------------------------------*/
445 symbol_check_alias_consistency (symbol
*this)
447 symbol
*alias
= this;
448 symbol
*orig
= this->alias
;
450 /* Check only those that _are_ the aliases. */
451 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
454 if (orig
->type_name
!= alias
->type_name
)
457 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
459 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
463 if (orig
->destructor
.code
|| alias
->destructor
.code
)
465 if (orig
->destructor
.code
)
466 symbol_destructor_set (alias
, &orig
->destructor
);
468 symbol_destructor_set (orig
, &alias
->destructor
);
471 if (orig
->printer
.code
|| alias
->printer
.code
)
473 if (orig
->printer
.code
)
474 symbol_printer_set (alias
, &orig
->printer
);
476 symbol_printer_set (orig
, &alias
->printer
);
479 if (alias
->prec
|| orig
->prec
)
482 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
483 orig
->prec_location
);
485 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
486 alias
->prec_location
);
491 symbol_check_alias_consistency_processor (void *this,
492 void *null ATTRIBUTE_UNUSED
)
494 symbol_check_alias_consistency (this);
499 /*-------------------------------------------------------------------.
500 | Assign a symbol number, and write the definition of the token name |
501 | into FDEFINES. Put in SYMBOLS. |
502 `-------------------------------------------------------------------*/
505 symbol_pack (symbol
*this)
507 if (this->class == nterm_sym
)
509 this->number
+= ntokens
;
511 else if (this->alias
)
513 /* This symbol and its alias are a single token defn.
514 Allocate a tokno, and assign to both check agreement of
515 prec and assoc fields and make both the same */
516 if (this->number
== NUMBER_UNDEFINED
)
518 if (this == endtoken
|| this->alias
== endtoken
)
519 this->number
= this->alias
->number
= 0;
522 aver (this->alias
->number
!= NUMBER_UNDEFINED
);
523 this->number
= this->alias
->number
;
526 /* Do not do processing below for USER_NUMBER_ALIASes. */
527 if (this->user_token_number
== USER_NUMBER_ALIAS
)
530 else /* this->class == token_sym */
531 aver (this->number
!= NUMBER_UNDEFINED
);
533 symbols
[this->number
] = this;
538 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
540 return symbol_pack (this);
546 /*--------------------------------------------------.
547 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
548 `--------------------------------------------------*/
551 symbol_translation (symbol
*this)
554 if (this->class == token_sym
555 && this->user_token_number
!= USER_NUMBER_ALIAS
)
557 /* A token which translation has already been set? */
558 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
559 complain_at (this->location
,
560 _("tokens %s and %s both assigned number %d"),
561 symbols
[token_translations
[this->user_token_number
]]->tag
,
562 this->tag
, this->user_token_number
);
564 token_translations
[this->user_token_number
] = this->number
;
571 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
573 return symbol_translation (this);
577 /*---------------------------------------.
578 | Symbol and semantic type hash tables. |
579 `---------------------------------------*/
581 /* Initial capacity of symbol and semantic type hash table. */
582 #define HT_INITIAL_CAPACITY 257
584 static struct hash_table
*symbol_table
= NULL
;
585 static struct hash_table
*semantic_type_table
= NULL
;
588 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
590 /* Since tags are unique, we can compare the pointers themselves. */
591 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
595 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
597 /* Since names are unique, we can compare the pointers themselves. */
598 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
602 hash_symbol_comparator (void const *m1
, void const *m2
)
604 return hash_compare_symbol (m1
, m2
);
608 hash_semantic_type_comparator (void const *m1
, void const *m2
)
610 return hash_compare_semantic_type (m1
, m2
);
614 hash_symbol (const symbol
*m
, size_t tablesize
)
616 /* Since tags are unique, we can hash the pointer itself. */
617 return ((uintptr_t) m
->tag
) % tablesize
;
621 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
623 /* Since names are unique, we can hash the pointer itself. */
624 return ((uintptr_t) m
->tag
) % tablesize
;
628 hash_symbol_hasher (void const *m
, size_t tablesize
)
630 return hash_symbol (m
, tablesize
);
634 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
636 return hash_semantic_type (m
, tablesize
);
639 /*-------------------------------.
640 | Create the symbol hash table. |
641 `-------------------------------*/
646 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
649 hash_symbol_comparator
,
651 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
653 hash_semantic_type_hasher
,
654 hash_semantic_type_comparator
,
659 /*----------------------------------------------------------------.
660 | Find the symbol named KEY, and return it. If it does not exist |
662 `----------------------------------------------------------------*/
665 symbol_from_uniqstr (const uniqstr key
, location loc
)
671 entry
= hash_lookup (symbol_table
, &probe
);
675 /* First insertion in the hash. */
676 entry
= symbol_new (key
, loc
);
677 hash_insert (symbol_table
, entry
);
683 /*-----------------------------------------------------------------------.
684 | Find the semantic type named KEY, and return it. If it does not exist |
686 `-----------------------------------------------------------------------*/
689 semantic_type_from_uniqstr (const uniqstr key
)
692 semantic_type
*entry
;
695 entry
= hash_lookup (semantic_type_table
, &probe
);
699 /* First insertion in the hash. */
700 entry
= semantic_type_new (key
);
701 hash_insert (semantic_type_table
, entry
);
707 /*----------------------------------------------------------------.
708 | Find the symbol named KEY, and return it. If it does not exist |
710 `----------------------------------------------------------------*/
713 symbol_get (const char *key
, location loc
)
715 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
719 /*-----------------------------------------------------------------------.
720 | Find the semantic type named KEY, and return it. If it does not exist |
722 `-----------------------------------------------------------------------*/
725 semantic_type_get (const char *key
)
727 return semantic_type_from_uniqstr (uniqstr_new (key
));
731 /*------------------------------------------------------------------.
732 | Generate a dummy nonterminal, whose name cannot conflict with the |
734 `------------------------------------------------------------------*/
737 dummy_symbol_get (location loc
)
739 /* Incremented for each generated symbol. */
740 static int dummy_count
= 0;
741 static char buf
[256];
745 sprintf (buf
, "$@%d", ++dummy_count
);
746 sym
= symbol_get (buf
, loc
);
747 sym
->class = nterm_sym
;
748 sym
->number
= nvars
++;
753 symbol_is_dummy (const symbol
*sym
)
755 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
758 /*-------------------.
759 | Free the symbols. |
760 `-------------------*/
765 hash_free (symbol_table
);
766 hash_free (semantic_type_table
);
771 /*---------------------------------------------------------------.
772 | Look for undefined symbols, report an error, and consider them |
774 `---------------------------------------------------------------*/
777 symbols_do (Hash_processor processor
, void *processor_data
)
779 hash_do_for_each (symbol_table
, processor
, processor_data
);
783 /*--------------------------------------------------------------.
784 | Check that all the symbols are defined. Report any undefined |
785 | symbols and consider them nonterminals. |
786 `--------------------------------------------------------------*/
789 symbols_check_defined (void)
791 symbols_do (symbol_check_defined_processor
, NULL
);
794 /*------------------------------------------------------------------.
795 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
797 `------------------------------------------------------------------*/
800 symbols_token_translations_init (void)
802 bool num_256_available_p
= true;
805 /* Find the highest user token number, and whether 256, the POSIX
806 preferred user token number for the error token, is used. */
807 max_user_token_number
= 0;
808 for (i
= 0; i
< ntokens
; ++i
)
810 symbol
*this = symbols
[i
];
811 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
813 if (this->user_token_number
> max_user_token_number
)
814 max_user_token_number
= this->user_token_number
;
815 if (this->user_token_number
== 256)
816 num_256_available_p
= false;
820 /* If 256 is not used, assign it to error, to follow POSIX. */
821 if (num_256_available_p
822 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
823 errtoken
->user_token_number
= 256;
825 /* Set the missing user numbers. */
826 if (max_user_token_number
< 256)
827 max_user_token_number
= 256;
829 for (i
= 0; i
< ntokens
; ++i
)
831 symbol
*this = symbols
[i
];
832 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
833 this->user_token_number
= ++max_user_token_number
;
834 if (this->user_token_number
> max_user_token_number
)
835 max_user_token_number
= this->user_token_number
;
838 token_translations
= xnmalloc (max_user_token_number
+ 1,
839 sizeof *token_translations
);
841 /* Initialize all entries for literal tokens to 2, the internal
842 token number for $undefined, which represents all invalid inputs.
844 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
845 token_translations
[i
] = undeftoken
->number
;
846 symbols_do (symbol_translation_processor
, NULL
);
850 /*----------------------------------------------------------------.
851 | Assign symbol numbers, and write definition of token names into |
852 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
853 `----------------------------------------------------------------*/
858 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
860 symbols
= xcalloc (nsyms
, sizeof *symbols
);
861 symbols_do (symbol_pack_processor
, NULL
);
863 /* Aliases leave empty slots in symbols, so remove them. */
867 int nsyms_old
= nsyms
;
868 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
870 if (symbols
[readi
] == NULL
)
877 symbols
[writei
] = symbols
[readi
];
878 symbols
[writei
]->number
= writei
;
879 if (symbols
[writei
]->alias
)
880 symbols
[writei
]->alias
->number
= writei
;
885 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
887 symbols_token_translations_init ();
889 if (startsymbol
->class == unknown_sym
)
890 fatal_at (startsymbol_location
,
891 _("the start symbol %s is undefined"),
893 else if (startsymbol
->class == token_sym
)
894 fatal_at (startsymbol_location
,
895 _("the start symbol %s is a token"),
900 /*--------------------------------------------------.
901 | Set default tagged/tagless %destructor/%printer. |
902 `--------------------------------------------------*/
905 default_tagged_destructor_set (code_props
const *destructor
)
907 if (default_tagged_destructor
.code
)
909 complain_at (destructor
->location
,
910 _("redeclaration for default tagged %%destructor"));
911 complain_at (default_tagged_destructor
.location
,
912 _("previous declaration"));
914 default_tagged_destructor
= *destructor
;
918 default_tagless_destructor_set (code_props
const *destructor
)
920 if (default_tagless_destructor
.code
)
922 complain_at (destructor
->location
,
923 _("redeclaration for default tagless %%destructor"));
924 complain_at (default_tagless_destructor
.location
,
925 _("previous declaration"));
927 default_tagless_destructor
= *destructor
;
931 default_tagged_printer_set (code_props
const *printer
)
933 if (default_tagged_printer
.code
)
935 complain_at (printer
->location
,
936 _("redeclaration for default tagged %%printer"));
937 complain_at (default_tagged_printer
.location
,
938 _("previous declaration"));
940 default_tagged_printer
= *printer
;
944 default_tagless_printer_set (code_props
const *printer
)
946 if (default_tagless_printer
.code
)
948 complain_at (printer
->location
,
949 _("redeclaration for default tagless %%printer"));
950 complain_at (default_tagless_printer
.location
,
951 _("previous declaration"));
953 default_tagless_printer
= *printer
;