1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006 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 and %printer. |
46 `-----------------------------------*/
48 static const char *default_destructor
= NULL
;
49 static location default_destructor_location
;
50 static const char *default_printer
= NULL
;
51 static location default_printer_location
;
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 res
->destructor
= NULL
;
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
);
91 #define SYMBOL_ATTR_PRINT(Attr) \
93 fprintf (f, " %s { %s }", #Attr, s->Attr)
96 symbol_print (symbol
*s
, FILE *f
)
100 fprintf (f
, "\"%s\"", s
->tag
);
101 SYMBOL_ATTR_PRINT (type_name
);
102 SYMBOL_ATTR_PRINT (destructor
);
103 SYMBOL_ATTR_PRINT (printer
);
106 fprintf (f
, "<NULL>");
109 #undef SYMBOL_ATTR_PRINT
111 /*------------------------------------------------------------------.
112 | Complain that S's WHAT is redeclared at SECOND, and was first set |
114 `------------------------------------------------------------------*/
117 redeclaration (symbol
* s
, const char *what
, location first
, location second
)
119 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
120 complain_at (first
, _("previous declaration"));
124 /*-----------------------------------------------------------------.
125 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
127 `-----------------------------------------------------------------*/
130 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
135 redeclaration (sym
, "%type", sym
->type_location
, loc
);
136 uniqstr_assert (type_name
);
137 sym
->type_name
= type_name
;
138 sym
->type_location
= loc
;
143 /*------------------------------------------------------------------.
144 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
145 `------------------------------------------------------------------*/
148 symbol_destructor_set (symbol
*sym
, const char *destructor
, location loc
)
153 redeclaration (sym
, "%destructor", sym
->destructor_location
, loc
);
154 sym
->destructor
= destructor
;
155 sym
->destructor_location
= loc
;
159 /*---------------------------------------.
160 | Get the computed %destructor for SYM. |
161 `---------------------------------------*/
164 symbol_destructor_get (symbol
*sym
)
166 /* Token 0 cannot have a %destructor unless the user renames it. */
167 if (UNIQSTR_EQ (sym
->tag
, uniqstr_new ("$end")))
170 if (sym
->destructor
!= NULL
)
171 return sym
->destructor
;
172 return default_destructor
;
175 /*---------------------------------------------------------------.
176 | Get the grammar location of the %destructor computed for SYM. |
177 `---------------------------------------------------------------*/
180 symbol_destructor_location_get (symbol
*sym
)
182 if (sym
->destructor
!= NULL
)
183 return sym
->destructor_location
;
184 return default_destructor_location
;
187 /*---------------------------------------------------------------.
188 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
189 `---------------------------------------------------------------*/
192 symbol_printer_set (symbol
*sym
, const char *printer
, location loc
)
197 redeclaration (sym
, "%printer", sym
->printer_location
, loc
);
198 sym
->printer
= printer
;
199 sym
->printer_location
= loc
;
203 /*------------------------------------.
204 | Get the computed %printer for SYM. |
205 `------------------------------------*/
208 symbol_printer_get (symbol
*sym
)
210 /* Token 0 cannot have a %printer unless the user renames it. */
211 if (UNIQSTR_EQ (sym
->tag
, uniqstr_new ("$end")))
214 if (sym
->printer
!= NULL
)
216 return default_printer
;
219 /*------------------------------------------------------------.
220 | Get the grammar location of the %printer computed for SYM. |
221 `------------------------------------------------------------*/
224 symbol_printer_location_get (symbol
*sym
)
226 if (sym
->printer
!= NULL
)
227 return sym
->printer_location
;
228 return default_printer_location
;
232 /*-----------------------------------------------------------------.
233 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
234 | with UNDEF_ASSOC as ASSOC. |
235 `-----------------------------------------------------------------*/
238 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
240 if (a
!= undef_assoc
)
243 redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
, loc
);
246 sym
->prec_location
= loc
;
249 /* Only terminals have a precedence. */
250 symbol_class_set (sym
, token_sym
, loc
, false);
254 /*------------------------------------.
255 | Set the CLASS associated with SYM. |
256 `------------------------------------*/
259 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
261 if (sym
->class != unknown_sym
&& sym
->class != class)
263 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
264 sym
->declared
= false;
267 if (class == nterm_sym
&& sym
->class != nterm_sym
)
268 sym
->number
= nvars
++;
269 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
270 sym
->number
= ntokens
++;
277 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
278 sym
->declared
= true;
283 /*------------------------------------------------.
284 | Set the USER_TOKEN_NUMBER associated with SYM. |
285 `------------------------------------------------*/
288 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
290 int *user_token_numberp
;
292 assert (sym
->class == token_sym
);
294 if (sym
->user_token_number
!= USER_NUMBER_ALIAS
)
295 user_token_numberp
= &sym
->user_token_number
;
297 user_token_numberp
= &sym
->alias
->user_token_number
;
298 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
299 && *user_token_numberp
!= user_token_number
)
300 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
302 *user_token_numberp
= user_token_number
;
303 /* User defined $end token? */
304 if (user_token_number
== 0)
307 endtoken
->number
= 0;
308 /* It is always mapped to 0, so it was already counted in
315 /*----------------------------------------------------------.
316 | If SYM is not defined, report an error, and consider it a |
318 `----------------------------------------------------------*/
321 symbol_check_defined (symbol
*sym
)
323 if (sym
->class == unknown_sym
)
327 _("symbol %s is used, but is not defined as a token and has no rules"),
329 sym
->class = nterm_sym
;
330 sym
->number
= nvars
++;
337 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
339 return symbol_check_defined (sym
);
343 /*------------------------------------------------------------------.
344 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
345 | SYMVAL with SYM's type. |
346 `------------------------------------------------------------------*/
349 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
352 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
355 warn_at (loc
, _("symbol `%s' given more than one literal string"),
359 symval
->class = token_sym
;
360 symval
->user_token_number
= sym
->user_token_number
;
361 sym
->user_token_number
= USER_NUMBER_ALIAS
;
364 /* sym and symval combined are only one symbol. */
367 assert (ntokens
== sym
->number
|| ntokens
== symval
->number
);
368 sym
->number
= symval
->number
=
369 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
370 symbol_type_set (symval
, sym
->type_name
, loc
);
375 /*---------------------------------------------------------.
376 | Check that THIS, and its alias, have same precedence and |
378 `---------------------------------------------------------*/
381 symbol_check_alias_consistency (symbol
*this)
383 symbol
*alias
= this;
384 symbol
*orig
= this->alias
;
386 /* Check only those that _are_ the aliases. */
387 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
390 if (orig
->type_name
!= alias
->type_name
)
393 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
395 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
399 if (orig
->destructor
|| alias
->destructor
)
401 if (orig
->destructor
)
402 symbol_destructor_set (alias
, orig
->destructor
,
403 orig
->destructor_location
);
405 symbol_destructor_set (orig
, alias
->destructor
,
406 alias
->destructor_location
);
409 if (orig
->printer
|| alias
->printer
)
412 symbol_printer_set (alias
, orig
->printer
, orig
->printer_location
);
414 symbol_printer_set (orig
, alias
->printer
, alias
->printer_location
);
417 if (alias
->prec
|| orig
->prec
)
420 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
421 orig
->prec_location
);
423 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
424 alias
->prec_location
);
429 symbol_check_alias_consistency_processor (void *this,
430 void *null ATTRIBUTE_UNUSED
)
432 symbol_check_alias_consistency (this);
437 /*-------------------------------------------------------------------.
438 | Assign a symbol number, and write the definition of the token name |
439 | into FDEFINES. Put in SYMBOLS. |
440 `-------------------------------------------------------------------*/
443 symbol_pack (symbol
*this)
445 if (this->class == nterm_sym
)
447 this->number
+= ntokens
;
449 else if (this->alias
)
451 /* This symbol and its alias are a single token defn.
452 Allocate a tokno, and assign to both check agreement of
453 prec and assoc fields and make both the same */
454 if (this->number
== NUMBER_UNDEFINED
)
456 if (this == endtoken
|| this->alias
== endtoken
)
457 this->number
= this->alias
->number
= 0;
460 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
461 this->number
= this->alias
->number
;
464 /* Do not do processing below for USER_NUMBER_ALIASes. */
465 if (this->user_token_number
== USER_NUMBER_ALIAS
)
468 else /* this->class == token_sym */
469 assert (this->number
!= NUMBER_UNDEFINED
);
471 symbols
[this->number
] = this;
476 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
478 return symbol_pack (this);
484 /*--------------------------------------------------.
485 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
486 `--------------------------------------------------*/
489 symbol_translation (symbol
*this)
492 if (this->class == token_sym
493 && this->user_token_number
!= USER_NUMBER_ALIAS
)
495 /* A token which translation has already been set? */
496 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
497 complain_at (this->location
,
498 _("tokens %s and %s both assigned number %d"),
499 symbols
[token_translations
[this->user_token_number
]]->tag
,
500 this->tag
, this->user_token_number
);
502 token_translations
[this->user_token_number
] = this->number
;
509 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
511 return symbol_translation (this);
515 /*----------------------.
516 | A symbol hash table. |
517 `----------------------*/
519 /* Initial capacity of symbols hash table. */
520 #define HT_INITIAL_CAPACITY 257
522 static struct hash_table
*symbol_table
= NULL
;
525 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
527 /* Since tags are unique, we can compare the pointers themselves. */
528 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
532 hash_symbol_comparator (void const *m1
, void const *m2
)
534 return hash_compare_symbol (m1
, m2
);
538 hash_symbol (const symbol
*m
, size_t tablesize
)
540 /* Since tags are unique, we can hash the pointer itself. */
541 return ((uintptr_t) m
->tag
) % tablesize
;
545 hash_symbol_hasher (void const *m
, size_t tablesize
)
547 return hash_symbol (m
, tablesize
);
551 /*-------------------------------.
552 | Create the symbol hash table. |
553 `-------------------------------*/
558 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
561 hash_symbol_comparator
,
566 /*----------------------------------------------------------------.
567 | Find the symbol named KEY, and return it. If it does not exist |
569 `----------------------------------------------------------------*/
572 symbol_from_uniqstr (const uniqstr key
, location loc
)
578 entry
= hash_lookup (symbol_table
, &probe
);
582 /* First insertion in the hash. */
583 entry
= symbol_new (key
, loc
);
584 hash_insert (symbol_table
, entry
);
590 /*----------------------------------------------------------------.
591 | Find the symbol named KEY, and return it. If it does not exist |
593 `----------------------------------------------------------------*/
596 symbol_get (const char *key
, location loc
)
598 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
602 /*------------------------------------------------------------------.
603 | Generate a dummy nonterminal, whose name cannot conflict with the |
605 `------------------------------------------------------------------*/
608 dummy_symbol_get (location loc
)
610 /* Incremented for each generated symbol. */
611 static int dummy_count
= 0;
612 static char buf
[256];
616 sprintf (buf
, "@%d", ++dummy_count
);
617 sym
= symbol_get (buf
, loc
);
618 sym
->class = nterm_sym
;
619 sym
->number
= nvars
++;
624 /*-------------------.
625 | Free the symbols. |
626 `-------------------*/
631 hash_free (symbol_table
);
636 /*---------------------------------------------------------------.
637 | Look for undefined symbols, report an error, and consider them |
639 `---------------------------------------------------------------*/
642 symbols_do (Hash_processor processor
, void *processor_data
)
644 hash_do_for_each (symbol_table
, processor
, processor_data
);
648 /*--------------------------------------------------------------.
649 | Check that all the symbols are defined. Report any undefined |
650 | symbols and consider them nonterminals. |
651 `--------------------------------------------------------------*/
654 symbols_check_defined (void)
656 symbols_do (symbol_check_defined_processor
, NULL
);
659 /*------------------------------------------------------------------.
660 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
662 `------------------------------------------------------------------*/
665 symbols_token_translations_init (void)
667 bool num_256_available_p
= true;
670 /* Find the highest user token number, and whether 256, the POSIX
671 preferred user token number for the error token, is used. */
672 max_user_token_number
= 0;
673 for (i
= 0; i
< ntokens
; ++i
)
675 symbol
*this = symbols
[i
];
676 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
678 if (this->user_token_number
> max_user_token_number
)
679 max_user_token_number
= this->user_token_number
;
680 if (this->user_token_number
== 256)
681 num_256_available_p
= false;
685 /* If 256 is not used, assign it to error, to follow POSIX. */
686 if (num_256_available_p
687 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
688 errtoken
->user_token_number
= 256;
690 /* Set the missing user numbers. */
691 if (max_user_token_number
< 256)
692 max_user_token_number
= 256;
694 for (i
= 0; i
< ntokens
; ++i
)
696 symbol
*this = symbols
[i
];
697 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
698 this->user_token_number
= ++max_user_token_number
;
699 if (this->user_token_number
> max_user_token_number
)
700 max_user_token_number
= this->user_token_number
;
703 token_translations
= xnmalloc (max_user_token_number
+ 1,
704 sizeof *token_translations
);
706 /* Initialize all entries for literal tokens to 2, the internal
707 token number for $undefined, which represents all invalid inputs.
709 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
710 token_translations
[i
] = undeftoken
->number
;
711 symbols_do (symbol_translation_processor
, NULL
);
715 /*----------------------------------------------------------------.
716 | Assign symbol numbers, and write definition of token names into |
717 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
718 `----------------------------------------------------------------*/
723 symbols
= xcalloc (nsyms
, sizeof *symbols
);
725 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
726 symbols_do (symbol_pack_processor
, NULL
);
728 symbols_token_translations_init ();
730 if (startsymbol
->class == unknown_sym
)
731 fatal_at (startsymbol_location
,
732 _("the start symbol %s is undefined"),
734 else if (startsymbol
->class == token_sym
)
735 fatal_at (startsymbol_location
,
736 _("the start symbol %s is a token"),
741 /*-----------------------------------.
742 | Set default %destructor/%printer. |
743 `-----------------------------------*/
746 default_destructor_set (const char *destructor
, location loc
)
748 if (default_destructor
!= NULL
)
750 complain_at (loc
, _("redeclaration for default %%destructor"));
751 complain_at (default_destructor_location
, _("previous declaration"));
753 default_destructor
= destructor
;
754 default_destructor_location
= loc
;
758 default_printer_set (const char *printer
, location loc
)
760 if (default_printer
!= NULL
)
762 complain_at (loc
, _("redeclaration for default %%printer"));
763 complain_at (default_printer_location
, _("previous declaration"));
765 default_printer
= printer
;
766 default_printer_location
= loc
;