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 | Create a new symbol, named TAG. |
46 `---------------------------------*/
49 symbol_new (uniqstr tag
, location loc
)
51 symbol
*res
= xmalloc (sizeof *res
);
57 res
->type_name
= NULL
;
58 res
->destructor
= NULL
;
61 res
->number
= NUMBER_UNDEFINED
;
63 res
->assoc
= undef_assoc
;
64 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
67 res
->class = unknown_sym
;
68 res
->declared
= false;
70 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
71 fatal (_("too many symbols in input grammar (limit is %d)"),
72 SYMBOL_NUMBER_MAXIMUM
);
82 #define SYMBOL_ATTR_PRINT(Attr) \
84 fprintf (f, " %s { %s }", #Attr, s->Attr)
87 symbol_print (symbol
*s
, FILE *f
)
91 fprintf (f
, "\"%s\"", s
->tag
);
92 SYMBOL_ATTR_PRINT (type_name
);
93 SYMBOL_ATTR_PRINT (destructor
);
94 SYMBOL_ATTR_PRINT (printer
);
97 fprintf (f
, "<NULL>");
100 #undef SYMBOL_ATTR_PRINT
102 /*------------------------------------------------------------------.
103 | Complain that S's WHAT is redeclared at SECOND, and was first set |
105 `------------------------------------------------------------------*/
108 redeclaration (symbol
* s
, const char *what
, location first
, location second
)
110 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
111 complain_at (first
, _("first declaration"));
115 /*-----------------------------------------------------------------.
116 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
118 `-----------------------------------------------------------------*/
121 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
126 redeclaration (sym
, "%type", sym
->type_location
, loc
);
127 uniqstr_assert (type_name
);
128 sym
->type_name
= type_name
;
129 sym
->type_location
= loc
;
134 /*------------------------------------------------------------------.
135 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
136 `------------------------------------------------------------------*/
139 symbol_destructor_set (symbol
*sym
, const char *destructor
, location loc
)
144 redeclaration (sym
, "%destructor", sym
->destructor_location
, loc
);
145 sym
->destructor
= destructor
;
146 sym
->destructor_location
= loc
;
151 /*---------------------------------------------------------------.
152 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
153 `---------------------------------------------------------------*/
156 symbol_printer_set (symbol
*sym
, const char *printer
, location loc
)
161 redeclaration (sym
, "%printer", sym
->destructor_location
, loc
);
162 sym
->printer
= printer
;
163 sym
->printer_location
= loc
;
168 /*-----------------------------------------------------------------.
169 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
170 | with UNDEF_ASSOC as ASSOC. |
171 `-----------------------------------------------------------------*/
174 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
176 if (a
!= undef_assoc
)
179 redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
, loc
);
182 sym
->prec_location
= loc
;
185 /* Only terminals have a precedence. */
186 symbol_class_set (sym
, token_sym
, loc
, false);
190 /*------------------------------------.
191 | Set the CLASS associated with SYM. |
192 `------------------------------------*/
195 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
197 if (sym
->class != unknown_sym
&& sym
->class != class)
199 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
200 sym
->declared
= false;
203 if (class == nterm_sym
&& sym
->class != nterm_sym
)
204 sym
->number
= nvars
++;
205 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
206 sym
->number
= ntokens
++;
213 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
214 sym
->declared
= true;
219 /*------------------------------------------------.
220 | Set the USER_TOKEN_NUMBER associated with SYM. |
221 `------------------------------------------------*/
224 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
226 if (sym
->class != token_sym
)
229 if (sym
->user_token_number
!= USER_NUMBER_UNDEFINED
230 && sym
->user_token_number
!= user_token_number
)
231 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
233 sym
->user_token_number
= user_token_number
;
234 /* User defined $end token? */
235 if (user_token_number
== 0)
238 endtoken
->number
= 0;
239 /* It is always mapped to 0, so it was already counted in
246 /*----------------------------------------------------------.
247 | If SYM is not defined, report an error, and consider it a |
249 `----------------------------------------------------------*/
252 symbol_check_defined (symbol
*sym
)
254 if (sym
->class == unknown_sym
)
258 _("symbol %s is used, but is not defined as a token and has no rules"),
260 sym
->class = nterm_sym
;
261 sym
->number
= nvars
++;
268 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
270 return symbol_check_defined (sym
);
274 /*------------------------------------------------------------------.
275 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
276 | SYMVAL with SYM's type. |
277 `------------------------------------------------------------------*/
280 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
283 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
286 warn_at (loc
, _("symbol `%s' given more than one literal string"),
290 symval
->class = token_sym
;
291 symval
->user_token_number
= sym
->user_token_number
;
292 sym
->user_token_number
= USER_NUMBER_ALIAS
;
295 /* sym and symval combined are only one symbol. */
298 if (ntokens
!= sym
->number
&& ntokens
!= symval
->number
)
300 sym
->number
= symval
->number
=
301 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
302 symbol_type_set (symval
, sym
->type_name
, loc
);
307 /*---------------------------------------------------------.
308 | Check that THIS, and its alias, have same precedence and |
310 `---------------------------------------------------------*/
313 symbol_check_alias_consistency (symbol
*this)
315 symbol
*alias
= this;
316 symbol
*orig
= this->alias
;
318 /* Check only those that _are_ the aliases. */
319 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
322 if (orig
->type_name
!= alias
->type_name
)
325 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
327 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
331 if (orig
->destructor
|| alias
->destructor
)
333 if (orig
->destructor
)
334 symbol_destructor_set (alias
, orig
->destructor
,
335 orig
->destructor_location
);
337 symbol_destructor_set (orig
, alias
->destructor
,
338 alias
->destructor_location
);
341 if (orig
->printer
|| alias
->printer
)
344 symbol_printer_set (alias
, orig
->printer
, orig
->printer_location
);
346 symbol_printer_set (orig
, alias
->printer
, alias
->printer_location
);
349 if (alias
->prec
|| orig
->prec
)
352 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
353 orig
->prec_location
);
355 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
356 alias
->prec_location
);
361 symbol_check_alias_consistency_processor (void *this,
362 void *null ATTRIBUTE_UNUSED
)
364 symbol_check_alias_consistency (this);
369 /*-------------------------------------------------------------------.
370 | Assign a symbol number, and write the definition of the token name |
371 | into FDEFINES. Put in SYMBOLS. |
372 `-------------------------------------------------------------------*/
375 symbol_pack (symbol
*this)
377 if (this->class == nterm_sym
)
379 this->number
+= ntokens
;
381 else if (this->alias
)
383 /* This symbol and its alias are a single token defn.
384 Allocate a tokno, and assign to both check agreement of
385 prec and assoc fields and make both the same */
386 if (this->number
== NUMBER_UNDEFINED
)
388 if (this == endtoken
|| this->alias
== endtoken
)
389 this->number
= this->alias
->number
= 0;
392 if (this->alias
->number
== NUMBER_UNDEFINED
)
394 this->number
= this->alias
->number
;
397 /* Do not do processing below for USER_NUMBER_ALIASes. */
398 if (this->user_token_number
== USER_NUMBER_ALIAS
)
401 else /* this->class == token_sym */
403 if (this->number
== NUMBER_UNDEFINED
)
407 symbols
[this->number
] = this;
412 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
414 return symbol_pack (this);
420 /*--------------------------------------------------.
421 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
422 `--------------------------------------------------*/
425 symbol_translation (symbol
*this)
428 if (this->class == token_sym
429 && this->user_token_number
!= USER_NUMBER_ALIAS
)
431 /* A token which translation has already been set? */
432 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
433 complain_at (this->location
,
434 _("tokens %s and %s both assigned number %d"),
435 symbols
[token_translations
[this->user_token_number
]]->tag
,
436 this->tag
, this->user_token_number
);
438 token_translations
[this->user_token_number
] = this->number
;
445 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
447 return symbol_translation (this);
451 /*----------------------.
452 | A symbol hash table. |
453 `----------------------*/
455 /* Initial capacity of symbols hash table. */
456 #define HT_INITIAL_CAPACITY 257
458 static struct hash_table
*symbol_table
= NULL
;
461 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
463 /* Since tags are unique, we can compare the pointers themselves. */
464 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
468 hash_symbol_comparator (void const *m1
, void const *m2
)
470 return hash_compare_symbol (m1
, m2
);
474 hash_symbol (const symbol
*m
, size_t tablesize
)
476 /* Since tags are unique, we can hash the pointer itself. */
477 return ((uintptr_t) m
->tag
) % tablesize
;
481 hash_symbol_hasher (void const *m
, size_t tablesize
)
483 return hash_symbol (m
, tablesize
);
487 /*-------------------------------.
488 | Create the symbol hash table. |
489 `-------------------------------*/
494 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
497 hash_symbol_comparator
,
502 /*----------------------------------------------------------------.
503 | Find the symbol named KEY, and return it. If it does not exist |
505 `----------------------------------------------------------------*/
508 symbol_get (const char *key
, location loc
)
513 key
= uniqstr_new (key
);
515 entry
= hash_lookup (symbol_table
, &probe
);
519 /* First insertion in the hash. */
520 entry
= symbol_new (key
, loc
);
521 hash_insert (symbol_table
, entry
);
527 /*------------------------------------------------------------------.
528 | Generate a dummy nonterminal, whose name cannot conflict with the |
530 `------------------------------------------------------------------*/
533 dummy_symbol_get (location loc
)
535 /* Incremented for each generated symbol. */
536 static int dummy_count
= 0;
537 static char buf
[256];
541 sprintf (buf
, "@%d", ++dummy_count
);
542 sym
= symbol_get (buf
, loc
);
543 sym
->class = nterm_sym
;
544 sym
->number
= nvars
++;
549 /*-------------------.
550 | Free the symbols. |
551 `-------------------*/
556 hash_free (symbol_table
);
561 /*---------------------------------------------------------------.
562 | Look for undefined symbols, report an error, and consider them |
564 `---------------------------------------------------------------*/
567 symbols_do (Hash_processor processor
, void *processor_data
)
569 hash_do_for_each (symbol_table
, processor
, processor_data
);
573 /*--------------------------------------------------------------.
574 | Check that all the symbols are defined. Report any undefined |
575 | symbols and consider them nonterminals. |
576 `--------------------------------------------------------------*/
579 symbols_check_defined (void)
581 symbols_do (symbol_check_defined_processor
, NULL
);
584 /*------------------------------------------------------------------.
585 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
587 `------------------------------------------------------------------*/
590 symbols_token_translations_init (void)
592 bool num_256_available_p
= true;
595 /* Find the highest user token number, and whether 256, the POSIX
596 preferred user token number for the error token, is used. */
597 max_user_token_number
= 0;
598 for (i
= 0; i
< ntokens
; ++i
)
600 symbol
*this = symbols
[i
];
601 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
603 if (this->user_token_number
> max_user_token_number
)
604 max_user_token_number
= this->user_token_number
;
605 if (this->user_token_number
== 256)
606 num_256_available_p
= false;
610 /* If 256 is not used, assign it to error, to follow POSIX. */
611 if (num_256_available_p
612 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
613 errtoken
->user_token_number
= 256;
615 /* Set the missing user numbers. */
616 if (max_user_token_number
< 256)
617 max_user_token_number
= 256;
619 for (i
= 0; i
< ntokens
; ++i
)
621 symbol
*this = symbols
[i
];
622 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
623 this->user_token_number
= ++max_user_token_number
;
624 if (this->user_token_number
> max_user_token_number
)
625 max_user_token_number
= this->user_token_number
;
628 token_translations
= xnmalloc (max_user_token_number
+ 1,
629 sizeof *token_translations
);
631 /* Initialize all entries for literal tokens to 2, the internal
632 token number for $undefined, which represents all invalid inputs.
634 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
635 token_translations
[i
] = undeftoken
->number
;
636 symbols_do (symbol_translation_processor
, NULL
);
640 /*----------------------------------------------------------------.
641 | Assign symbol numbers, and write definition of token names into |
642 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
643 `----------------------------------------------------------------*/
648 symbols
= xcalloc (nsyms
, sizeof *symbols
);
650 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
651 symbols_do (symbol_pack_processor
, NULL
);
653 symbols_token_translations_init ();
655 if (startsymbol
->class == unknown_sym
)
656 fatal_at (startsymbol_location
,
657 _("the start symbol %s is undefined"),
659 else if (startsymbol
->class == token_sym
)
660 fatal_at (startsymbol_location
,
661 _("the start symbol %s is a token"),