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 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
33 /*------------------------.
34 | Distinguished symbols. |
35 `------------------------*/
37 symbol
*errtoken
= NULL
;
38 symbol
*undeftoken
= NULL
;
39 symbol
*endtoken
= NULL
;
40 symbol
*accept
= NULL
;
41 symbol
*startsymbol
= NULL
;
42 location startsymbol_location
;
44 /*---------------------------------------.
45 | Default %destructor's and %printer's. |
46 `---------------------------------------*/
48 static code_props default_tagged_destructor
= CODE_PROPS_NONE_INIT
;
49 static code_props default_tagless_destructor
= CODE_PROPS_NONE_INIT
;
50 static code_props default_tagged_printer
= CODE_PROPS_NONE_INIT
;
51 static code_props default_tagless_printer
= CODE_PROPS_NONE_INIT
;
53 /*---------------------------------.
54 | Create a new symbol, named TAG. |
55 `---------------------------------*/
58 symbol_new (uniqstr tag
, location loc
)
60 symbol
*res
= xmalloc (sizeof *res
);
66 res
->type_name
= NULL
;
67 code_props_none_init (&res
->destructor
);
68 code_props_none_init (&res
->printer
);
70 res
->number
= NUMBER_UNDEFINED
;
72 res
->assoc
= undef_assoc
;
73 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
76 res
->class = unknown_sym
;
77 res
->declared
= false;
79 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
80 fatal (_("too many symbols in input grammar (limit is %d)"),
81 SYMBOL_NUMBER_MAXIMUM
);
86 /*----------------------------------------.
87 | Create a new semantic type, named TAG. |
88 `----------------------------------------*/
90 static semantic_type
*
91 semantic_type_new (uniqstr tag
)
93 semantic_type
*res
= xmalloc (sizeof *res
);
97 code_props_none_init (&res
->destructor
);
98 code_props_none_init (&res
->printer
);
108 #define SYMBOL_ATTR_PRINT(Attr) \
110 fprintf (f, " %s { %s }", #Attr, s->Attr)
112 #define SYMBOL_CODE_PRINT(Attr) \
114 fprintf (f, " %s { %s }", #Attr, s->Attr.code)
117 symbol_print (symbol
*s
, FILE *f
)
121 fprintf (f
, "\"%s\"", s
->tag
);
122 SYMBOL_ATTR_PRINT (type_name
);
123 SYMBOL_CODE_PRINT (destructor
);
124 SYMBOL_CODE_PRINT (printer
);
127 fprintf (f
, "<NULL>");
130 #undef SYMBOL_ATTR_PRINT
131 #undef SYMBOL_CODE_PRINT
133 /*------------------------------------------------------------------.
134 | Complain that S's WHAT is redeclared at SECOND, and was first set |
136 `------------------------------------------------------------------*/
139 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
142 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
143 complain_at (first
, _("previous declaration"));
147 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
150 complain_at (second
, _("%s redeclaration for <%s>"), what
, s
->tag
);
151 complain_at (first
, _("previous declaration"));
155 /*-----------------------------------------------------------------.
156 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
158 `-----------------------------------------------------------------*/
161 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
166 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
167 uniqstr_assert (type_name
);
168 sym
->type_name
= type_name
;
169 sym
->type_location
= loc
;
174 /*-----------------------------------------.
175 | Set the DESTRUCTOR associated with SYM. |
176 `-----------------------------------------*/
179 symbol_destructor_set (symbol
*sym
, code_props
const *destructor
)
181 if (sym
->destructor
.code
)
182 symbol_redeclaration (sym
, "%destructor", sym
->destructor
.location
,
183 destructor
->location
);
184 sym
->destructor
= *destructor
;
187 /*------------------------------------------.
188 | Set the DESTRUCTOR associated with TYPE. |
189 `------------------------------------------*/
192 semantic_type_destructor_set (semantic_type
*type
,
193 code_props
const *destructor
)
195 if (type
->destructor
.code
)
196 semantic_type_redeclaration (type
, "%destructor",
197 type
->destructor
.location
,
198 destructor
->location
);
199 type
->destructor
= *destructor
;
202 /*---------------------------------------.
203 | Get the computed %destructor for SYM. |
204 `---------------------------------------*/
207 symbol_destructor_get (symbol
const *sym
)
209 /* Per-symbol %destructor. */
210 if (sym
->destructor
.code
)
211 return &sym
->destructor
;
213 /* Per-type %destructor. */
216 code_props
const *destructor
=
217 &semantic_type_get (sym
->type_name
)->destructor
;
218 if (destructor
->code
)
222 /* Apply default %destructor's only to user-defined symbols. */
223 if (sym
->tag
[0] == '$' || sym
== errtoken
)
224 return &code_props_none
;
227 return &default_tagged_destructor
;
228 return &default_tagless_destructor
;
231 /*--------------------------------------.
232 | Set the PRINTER associated with SYM. |
233 `--------------------------------------*/
236 symbol_printer_set (symbol
*sym
, code_props
const *printer
)
238 if (sym
->printer
.code
)
239 symbol_redeclaration (sym
, "%printer",
240 sym
->printer
.location
, printer
->location
);
241 sym
->printer
= *printer
;
244 /*---------------------------------------.
245 | Set the PRINTER associated with TYPE. |
246 `---------------------------------------*/
249 semantic_type_printer_set (semantic_type
*type
, code_props
const *printer
)
251 if (type
->printer
.code
)
252 semantic_type_redeclaration (type
, "%printer",
253 type
->printer
.location
, printer
->location
);
254 type
->printer
= *printer
;
257 /*------------------------------------.
258 | Get the computed %printer for SYM. |
259 `------------------------------------*/
262 symbol_printer_get (symbol
const *sym
)
264 /* Per-symbol %printer. */
265 if (sym
->printer
.code
)
266 return &sym
->printer
;
268 /* Per-type %printer. */
271 code_props
const *printer
= &semantic_type_get (sym
->type_name
)->printer
;
276 /* Apply the default %printer only to user-defined symbols. */
277 if (sym
->tag
[0] == '$' || sym
== errtoken
)
278 return &code_props_none
;
281 return &default_tagged_printer
;
282 return &default_tagless_printer
;
285 /*-----------------------------------------------------------------.
286 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
287 | with UNDEF_ASSOC as ASSOC. |
288 `-----------------------------------------------------------------*/
291 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
293 if (a
!= undef_assoc
)
296 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
300 sym
->prec_location
= loc
;
303 /* Only terminals have a precedence. */
304 symbol_class_set (sym
, token_sym
, loc
, false);
308 /*------------------------------------.
309 | Set the CLASS associated with SYM. |
310 `------------------------------------*/
313 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
315 if (sym
->class != unknown_sym
&& sym
->class != class)
317 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
318 sym
->declared
= false;
321 if (class == nterm_sym
&& sym
->class != nterm_sym
)
322 sym
->number
= nvars
++;
323 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
324 sym
->number
= ntokens
++;
331 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
332 sym
->declared
= true;
337 /*------------------------------------------------.
338 | Set the USER_TOKEN_NUMBER associated with SYM. |
339 `------------------------------------------------*/
342 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
344 int *user_token_numberp
;
346 aver (sym
->class == token_sym
);
348 if (sym
->user_token_number
!= USER_NUMBER_ALIAS
)
349 user_token_numberp
= &sym
->user_token_number
;
351 user_token_numberp
= &sym
->alias
->user_token_number
;
352 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
353 && *user_token_numberp
!= user_token_number
)
354 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
356 *user_token_numberp
= user_token_number
;
357 /* User defined $end token? */
358 if (user_token_number
== 0)
361 endtoken
->number
= 0;
362 /* It is always mapped to 0, so it was already counted in
369 /*----------------------------------------------------------.
370 | If SYM is not defined, report an error, and consider it a |
372 `----------------------------------------------------------*/
375 symbol_check_defined (symbol
*sym
)
377 if (sym
->class == unknown_sym
)
381 _("symbol %s is used, but is not defined as a token and has no rules"),
383 sym
->class = nterm_sym
;
384 sym
->number
= nvars
++;
391 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
393 return symbol_check_defined (sym
);
397 /*------------------------------------------------------------------.
398 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
399 | SYMVAL with SYM's type. |
400 `------------------------------------------------------------------*/
403 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
406 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
409 warn_at (loc
, _("symbol `%s' given more than one literal string"),
413 symval
->class = token_sym
;
414 symval
->user_token_number
= sym
->user_token_number
;
415 sym
->user_token_number
= USER_NUMBER_ALIAS
;
418 symval
->number
= sym
->number
;
419 symbol_type_set (symval
, sym
->type_name
, loc
);
424 /*---------------------------------------------------------.
425 | Check that THIS, and its alias, have same precedence and |
427 `---------------------------------------------------------*/
430 symbol_check_alias_consistency (symbol
*this)
432 symbol
*alias
= this;
433 symbol
*orig
= this->alias
;
435 /* Check only those that _are_ the aliases. */
436 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
439 if (orig
->type_name
!= alias
->type_name
)
442 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
444 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
448 if (orig
->destructor
.code
|| alias
->destructor
.code
)
450 if (orig
->destructor
.code
)
451 symbol_destructor_set (alias
, &orig
->destructor
);
453 symbol_destructor_set (orig
, &alias
->destructor
);
456 if (orig
->printer
.code
|| alias
->printer
.code
)
458 if (orig
->printer
.code
)
459 symbol_printer_set (alias
, &orig
->printer
);
461 symbol_printer_set (orig
, &alias
->printer
);
464 if (alias
->prec
|| orig
->prec
)
467 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
468 orig
->prec_location
);
470 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
471 alias
->prec_location
);
476 symbol_check_alias_consistency_processor (void *this,
477 void *null ATTRIBUTE_UNUSED
)
479 symbol_check_alias_consistency (this);
484 /*-------------------------------------------------------------------.
485 | Assign a symbol number, and write the definition of the token name |
486 | into FDEFINES. Put in SYMBOLS. |
487 `-------------------------------------------------------------------*/
490 symbol_pack (symbol
*this)
492 if (this->class == nterm_sym
)
494 this->number
+= ntokens
;
496 else if (this->alias
)
498 /* This symbol and its alias are a single token defn.
499 Allocate a tokno, and assign to both check agreement of
500 prec and assoc fields and make both the same */
501 if (this->number
== NUMBER_UNDEFINED
)
503 if (this == endtoken
|| this->alias
== endtoken
)
504 this->number
= this->alias
->number
= 0;
507 aver (this->alias
->number
!= NUMBER_UNDEFINED
);
508 this->number
= this->alias
->number
;
511 /* Do not do processing below for USER_NUMBER_ALIASes. */
512 if (this->user_token_number
== USER_NUMBER_ALIAS
)
515 else /* this->class == token_sym */
516 aver (this->number
!= NUMBER_UNDEFINED
);
518 symbols
[this->number
] = this;
523 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
525 return symbol_pack (this);
531 /*--------------------------------------------------.
532 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
533 `--------------------------------------------------*/
536 symbol_translation (symbol
*this)
539 if (this->class == token_sym
540 && this->user_token_number
!= USER_NUMBER_ALIAS
)
542 /* A token which translation has already been set? */
543 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
544 complain_at (this->location
,
545 _("tokens %s and %s both assigned number %d"),
546 symbols
[token_translations
[this->user_token_number
]]->tag
,
547 this->tag
, this->user_token_number
);
549 token_translations
[this->user_token_number
] = this->number
;
556 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
558 return symbol_translation (this);
562 /*---------------------------------------.
563 | Symbol and semantic type hash tables. |
564 `---------------------------------------*/
566 /* Initial capacity of symbol and semantic type hash table. */
567 #define HT_INITIAL_CAPACITY 257
569 static struct hash_table
*symbol_table
= NULL
;
570 static struct hash_table
*semantic_type_table
= NULL
;
573 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
575 /* Since tags are unique, we can compare the pointers themselves. */
576 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
580 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
582 /* Since names are unique, we can compare the pointers themselves. */
583 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
587 hash_symbol_comparator (void const *m1
, void const *m2
)
589 return hash_compare_symbol (m1
, m2
);
593 hash_semantic_type_comparator (void const *m1
, void const *m2
)
595 return hash_compare_semantic_type (m1
, m2
);
599 hash_symbol (const symbol
*m
, size_t tablesize
)
601 /* Since tags are unique, we can hash the pointer itself. */
602 return ((uintptr_t) m
->tag
) % tablesize
;
606 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
608 /* Since names are unique, we can hash the pointer itself. */
609 return ((uintptr_t) m
->tag
) % tablesize
;
613 hash_symbol_hasher (void const *m
, size_t tablesize
)
615 return hash_symbol (m
, tablesize
);
619 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
621 return hash_semantic_type (m
, tablesize
);
624 /*-------------------------------.
625 | Create the symbol hash table. |
626 `-------------------------------*/
631 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
634 hash_symbol_comparator
,
636 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
638 hash_semantic_type_hasher
,
639 hash_semantic_type_comparator
,
644 /*----------------------------------------------------------------.
645 | Find the symbol named KEY, and return it. If it does not exist |
647 `----------------------------------------------------------------*/
650 symbol_from_uniqstr (const uniqstr key
, location loc
)
656 entry
= hash_lookup (symbol_table
, &probe
);
660 /* First insertion in the hash. */
661 entry
= symbol_new (key
, loc
);
662 hash_insert (symbol_table
, entry
);
668 /*-----------------------------------------------------------------------.
669 | Find the semantic type named KEY, and return it. If it does not exist |
671 `-----------------------------------------------------------------------*/
674 semantic_type_from_uniqstr (const uniqstr key
)
677 semantic_type
*entry
;
680 entry
= hash_lookup (semantic_type_table
, &probe
);
684 /* First insertion in the hash. */
685 entry
= semantic_type_new (key
);
686 hash_insert (semantic_type_table
, entry
);
692 /*----------------------------------------------------------------.
693 | Find the symbol named KEY, and return it. If it does not exist |
695 `----------------------------------------------------------------*/
698 symbol_get (const char *key
, location loc
)
700 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
704 /*-----------------------------------------------------------------------.
705 | Find the semantic type named KEY, and return it. If it does not exist |
707 `-----------------------------------------------------------------------*/
710 semantic_type_get (const char *key
)
712 return semantic_type_from_uniqstr (uniqstr_new (key
));
716 /*------------------------------------------------------------------.
717 | Generate a dummy nonterminal, whose name cannot conflict with the |
719 `------------------------------------------------------------------*/
722 dummy_symbol_get (location loc
)
724 /* Incremented for each generated symbol. */
725 static int dummy_count
= 0;
726 static char buf
[256];
730 sprintf (buf
, "$@%d", ++dummy_count
);
731 sym
= symbol_get (buf
, loc
);
732 sym
->class = nterm_sym
;
733 sym
->number
= nvars
++;
738 symbol_is_dummy (const symbol
*sym
)
740 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
743 /*-------------------.
744 | Free the symbols. |
745 `-------------------*/
750 hash_free (symbol_table
);
751 hash_free (semantic_type_table
);
756 /*---------------------------------------------------------------.
757 | Look for undefined symbols, report an error, and consider them |
759 `---------------------------------------------------------------*/
762 symbols_do (Hash_processor processor
, void *processor_data
)
764 hash_do_for_each (symbol_table
, processor
, processor_data
);
768 /*--------------------------------------------------------------.
769 | Check that all the symbols are defined. Report any undefined |
770 | symbols and consider them nonterminals. |
771 `--------------------------------------------------------------*/
774 symbols_check_defined (void)
776 symbols_do (symbol_check_defined_processor
, NULL
);
779 /*------------------------------------------------------------------.
780 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
782 `------------------------------------------------------------------*/
785 symbols_token_translations_init (void)
787 bool num_256_available_p
= true;
790 /* Find the highest user token number, and whether 256, the POSIX
791 preferred user token number for the error token, is used. */
792 max_user_token_number
= 0;
793 for (i
= 0; i
< ntokens
; ++i
)
795 symbol
*this = symbols
[i
];
796 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
798 if (this->user_token_number
> max_user_token_number
)
799 max_user_token_number
= this->user_token_number
;
800 if (this->user_token_number
== 256)
801 num_256_available_p
= false;
805 /* If 256 is not used, assign it to error, to follow POSIX. */
806 if (num_256_available_p
807 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
808 errtoken
->user_token_number
= 256;
810 /* Set the missing user numbers. */
811 if (max_user_token_number
< 256)
812 max_user_token_number
= 256;
814 for (i
= 0; i
< ntokens
; ++i
)
816 symbol
*this = symbols
[i
];
817 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
818 this->user_token_number
= ++max_user_token_number
;
819 if (this->user_token_number
> max_user_token_number
)
820 max_user_token_number
= this->user_token_number
;
823 token_translations
= xnmalloc (max_user_token_number
+ 1,
824 sizeof *token_translations
);
826 /* Initialize all entries for literal tokens to 2, the internal
827 token number for $undefined, which represents all invalid inputs.
829 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
830 token_translations
[i
] = undeftoken
->number
;
831 symbols_do (symbol_translation_processor
, NULL
);
835 /*----------------------------------------------------------------.
836 | Assign symbol numbers, and write definition of token names into |
837 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
838 `----------------------------------------------------------------*/
843 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
845 symbols
= xcalloc (nsyms
, sizeof *symbols
);
846 symbols_do (symbol_pack_processor
, NULL
);
848 /* Aliases leave empty slots in symbols, so remove them. */
852 int nsyms_old
= nsyms
;
853 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
855 if (symbols
[readi
] == NULL
)
862 symbols
[writei
] = symbols
[readi
];
863 symbols
[writei
]->number
= writei
;
864 if (symbols
[writei
]->alias
)
865 symbols
[writei
]->alias
->number
= writei
;
870 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
872 symbols_token_translations_init ();
874 if (startsymbol
->class == unknown_sym
)
875 fatal_at (startsymbol_location
,
876 _("the start symbol %s is undefined"),
878 else if (startsymbol
->class == token_sym
)
879 fatal_at (startsymbol_location
,
880 _("the start symbol %s is a token"),
885 /*--------------------------------------------------.
886 | Set default tagged/tagless %destructor/%printer. |
887 `--------------------------------------------------*/
890 default_tagged_destructor_set (code_props
const *destructor
)
892 if (default_tagged_destructor
.code
)
894 complain_at (destructor
->location
,
895 _("redeclaration for default tagged %%destructor"));
896 complain_at (default_tagged_destructor
.location
,
897 _("previous declaration"));
899 default_tagged_destructor
= *destructor
;
903 default_tagless_destructor_set (code_props
const *destructor
)
905 if (default_tagless_destructor
.code
)
907 complain_at (destructor
->location
,
908 _("redeclaration for default tagless %%destructor"));
909 complain_at (default_tagless_destructor
.location
,
910 _("previous declaration"));
912 default_tagless_destructor
= *destructor
;
916 default_tagged_printer_set (code_props
const *printer
)
918 if (default_tagged_printer
.code
)
920 complain_at (printer
->location
,
921 _("redeclaration for default tagged %%printer"));
922 complain_at (default_tagged_printer
.location
,
923 _("previous declaration"));
925 default_tagged_printer
= *printer
;
929 default_tagless_printer_set (code_props
const *printer
)
931 if (default_tagless_printer
.code
)
933 complain_at (printer
->location
,
934 _("redeclaration for default tagless %%printer"));
935 complain_at (default_tagless_printer
.location
,
936 _("previous declaration"));
938 default_tagless_printer
= *printer
;