1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005 Free Software
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
;
69 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
70 fatal (_("too many symbols in input grammar (limit is %d)"),
71 SYMBOL_NUMBER_MAXIMUM
);
77 /*------------------------------------------------------------------.
78 | Complain that S's WHAT is redeclared at SECOND, and was first set |
80 `------------------------------------------------------------------*/
83 redeclaration (symbol
* s
, const char *what
, location first
, location second
)
85 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
86 complain_at (first
, _("first declaration"));
90 /*-----------------------------------------------------------------.
91 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
93 `-----------------------------------------------------------------*/
96 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
101 redeclaration (sym
, "%type", sym
->type_location
, loc
);
102 uniqstr_assert (type_name
);
103 sym
->type_name
= type_name
;
104 sym
->type_location
= loc
;
109 /*------------------------------------------------------------------.
110 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
111 `------------------------------------------------------------------*/
114 symbol_destructor_set (symbol
*sym
, char *destructor
, location loc
)
119 redeclaration (sym
, "%destructor", sym
->destructor_location
, loc
);
120 sym
->destructor
= destructor
;
121 sym
->destructor_location
= loc
;
126 /*---------------------------------------------------------------.
127 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
128 `---------------------------------------------------------------*/
131 symbol_printer_set (symbol
*sym
, char *printer
, location loc
)
136 redeclaration (sym
, "%printer", sym
->destructor_location
, loc
);
137 sym
->printer
= printer
;
138 sym
->printer_location
= loc
;
143 /*-----------------------------------------------------------------.
144 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
145 | with UNDEF_ASSOC as ASSOC. |
146 `-----------------------------------------------------------------*/
149 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
151 if (a
!= undef_assoc
)
154 redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
, loc
);
157 sym
->prec_location
= loc
;
160 /* Only terminals have a precedence. */
161 symbol_class_set (sym
, token_sym
, loc
);
165 /*------------------------------------.
166 | Set the CLASS associated with SYM. |
167 `------------------------------------*/
170 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
)
172 if (sym
->class != unknown_sym
&& sym
->class != class)
173 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
175 if (class == nterm_sym
&& sym
->class != nterm_sym
)
176 sym
->number
= nvars
++;
177 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
178 sym
->number
= ntokens
++;
184 /*------------------------------------------------.
185 | Set the USER_TOKEN_NUMBER associated with SYM. |
186 `------------------------------------------------*/
189 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
191 if (sym
->class != token_sym
)
194 if (sym
->user_token_number
!= USER_NUMBER_UNDEFINED
195 && sym
->user_token_number
!= user_token_number
)
196 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
198 sym
->user_token_number
= user_token_number
;
199 /* User defined $end token? */
200 if (user_token_number
== 0)
203 endtoken
->number
= 0;
204 /* It is always mapped to 0, so it was already counted in
211 /*----------------------------------------------------------.
212 | If SYM is not defined, report an error, and consider it a |
214 `----------------------------------------------------------*/
217 symbol_check_defined (symbol
*sym
)
219 if (sym
->class == unknown_sym
)
223 _("symbol %s is used, but is not defined as a token and has no rules"),
225 sym
->class = nterm_sym
;
226 sym
->number
= nvars
++;
233 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
235 return symbol_check_defined (sym
);
239 /*------------------------------------------------------------------.
240 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
241 | SYMVAL with SYM's type. |
242 `------------------------------------------------------------------*/
245 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
248 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
251 warn_at (loc
, _("symbol `%s' given more than one literal string"),
255 symval
->class = token_sym
;
256 symval
->user_token_number
= sym
->user_token_number
;
257 sym
->user_token_number
= USER_NUMBER_ALIAS
;
260 /* sym and symval combined are only one symbol. */
263 if (ntokens
!= sym
->number
&& ntokens
!= symval
->number
)
265 sym
->number
= symval
->number
=
266 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
267 symbol_type_set (symval
, sym
->type_name
, loc
);
272 /*---------------------------------------------------------.
273 | Check that THIS, and its alias, have same precedence and |
275 `---------------------------------------------------------*/
278 symbol_check_alias_consistency (symbol
*this)
280 symbol
*alias
= this;
281 symbol
*orig
= this->alias
;
283 /* Check only those that _are_ the aliases. */
284 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
287 if (orig
->type_name
!= alias
->type_name
)
290 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
292 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
296 if (orig
->destructor
|| alias
->destructor
)
298 if (orig
->destructor
)
299 symbol_destructor_set (alias
, orig
->destructor
,
300 orig
->destructor_location
);
302 symbol_destructor_set (orig
, alias
->destructor
,
303 alias
->destructor_location
);
306 if (orig
->printer
|| alias
->printer
)
309 symbol_printer_set (alias
, orig
->printer
, orig
->printer_location
);
311 symbol_printer_set (orig
, alias
->printer
, alias
->printer_location
);
314 if (alias
->prec
|| orig
->prec
)
317 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
318 orig
->prec_location
);
320 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
321 alias
->prec_location
);
326 symbol_check_alias_consistency_processor (void *this,
327 void *null ATTRIBUTE_UNUSED
)
329 symbol_check_alias_consistency (this);
334 /*-------------------------------------------------------------------.
335 | Assign a symbol number, and write the definition of the token name |
336 | into FDEFINES. Put in SYMBOLS. |
337 `-------------------------------------------------------------------*/
340 symbol_pack (symbol
*this)
342 if (this->class == nterm_sym
)
344 this->number
+= ntokens
;
346 else if (this->alias
)
348 /* This symbol and its alias are a single token defn.
349 Allocate a tokno, and assign to both check agreement of
350 prec and assoc fields and make both the same */
351 if (this->number
== NUMBER_UNDEFINED
)
353 if (this == endtoken
|| this->alias
== endtoken
)
354 this->number
= this->alias
->number
= 0;
357 if (this->alias
->number
== NUMBER_UNDEFINED
)
359 this->number
= this->alias
->number
;
362 /* Do not do processing below for USER_NUMBER_ALIASes. */
363 if (this->user_token_number
== USER_NUMBER_ALIAS
)
366 else /* this->class == token_sym */
368 if (this->number
== NUMBER_UNDEFINED
)
372 symbols
[this->number
] = this;
377 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
379 return symbol_pack (this);
385 /*--------------------------------------------------.
386 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
387 `--------------------------------------------------*/
390 symbol_translation (symbol
*this)
393 if (this->class == token_sym
394 && this->user_token_number
!= USER_NUMBER_ALIAS
)
396 /* A token which translation has already been set? */
397 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
398 complain_at (this->location
,
399 _("tokens %s and %s both assigned number %d"),
400 symbols
[token_translations
[this->user_token_number
]]->tag
,
401 this->tag
, this->user_token_number
);
403 token_translations
[this->user_token_number
] = this->number
;
410 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
412 return symbol_translation (this);
416 /*----------------------.
417 | A symbol hash table. |
418 `----------------------*/
420 /* Initial capacity of symbols hash table. */
421 #define HT_INITIAL_CAPACITY 257
423 static struct hash_table
*symbol_table
= NULL
;
426 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
428 /* Since tags are unique, we can compare the pointers themselves. */
429 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
433 hash_symbol_comparator (void const *m1
, void const *m2
)
435 return hash_compare_symbol (m1
, m2
);
439 hash_symbol (const symbol
*m
, size_t tablesize
)
441 /* Since tags are unique, we can hash the pointer itself. */
442 return ((uintptr_t) m
->tag
) % tablesize
;
446 hash_symbol_hasher (void const *m
, size_t tablesize
)
448 return hash_symbol (m
, tablesize
);
452 /*-------------------------------.
453 | Create the symbol hash table. |
454 `-------------------------------*/
459 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
462 hash_symbol_comparator
,
467 /*----------------------------------------------------------------.
468 | Find the symbol named KEY, and return it. If it does not exist |
470 `----------------------------------------------------------------*/
473 symbol_get (const char *key
, location loc
)
478 key
= uniqstr_new (key
);
480 entry
= hash_lookup (symbol_table
, &probe
);
484 /* First insertion in the hash. */
485 entry
= symbol_new (key
, loc
);
486 hash_insert (symbol_table
, entry
);
492 /*------------------------------------------------------------------.
493 | Generate a dummy nonterminal, whose name cannot conflict with the |
495 `------------------------------------------------------------------*/
498 dummy_symbol_get (location loc
)
500 /* Incremented for each generated symbol. */
501 static int dummy_count
= 0;
502 static char buf
[256];
506 sprintf (buf
, "@%d", ++dummy_count
);
507 sym
= symbol_get (buf
, loc
);
508 sym
->class = nterm_sym
;
509 sym
->number
= nvars
++;
514 /*-------------------.
515 | Free the symbols. |
516 `-------------------*/
521 hash_free (symbol_table
);
526 /*---------------------------------------------------------------.
527 | Look for undefined symbols, report an error, and consider them |
529 `---------------------------------------------------------------*/
532 symbols_do (Hash_processor processor
, void *processor_data
)
534 hash_do_for_each (symbol_table
, processor
, processor_data
);
538 /*--------------------------------------------------------------.
539 | Check that all the symbols are defined. Report any undefined |
540 | symbols and consider them nonterminals. |
541 `--------------------------------------------------------------*/
544 symbols_check_defined (void)
546 symbols_do (symbol_check_defined_processor
, NULL
);
549 /*------------------------------------------------------------------.
550 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
552 `------------------------------------------------------------------*/
555 symbols_token_translations_init (void)
557 bool num_256_available_p
= true;
560 /* Find the highest user token number, and whether 256, the POSIX
561 preferred user token number for the error token, is used. */
562 max_user_token_number
= 0;
563 for (i
= 0; i
< ntokens
; ++i
)
565 symbol
*this = symbols
[i
];
566 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
568 if (this->user_token_number
> max_user_token_number
)
569 max_user_token_number
= this->user_token_number
;
570 if (this->user_token_number
== 256)
571 num_256_available_p
= false;
575 /* If 256 is not used, assign it to error, to follow POSIX. */
576 if (num_256_available_p
577 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
578 errtoken
->user_token_number
= 256;
580 /* Set the missing user numbers. */
581 if (max_user_token_number
< 256)
582 max_user_token_number
= 256;
584 for (i
= 0; i
< ntokens
; ++i
)
586 symbol
*this = symbols
[i
];
587 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
588 this->user_token_number
= ++max_user_token_number
;
589 if (this->user_token_number
> max_user_token_number
)
590 max_user_token_number
= this->user_token_number
;
593 token_translations
= xnmalloc (max_user_token_number
+ 1,
594 sizeof *token_translations
);
596 /* Initialize all entries for literal tokens to 2, the internal
597 token number for $undefined, which represents all invalid inputs.
599 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
600 token_translations
[i
] = undeftoken
->number
;
601 symbols_do (symbol_translation_processor
, NULL
);
605 /*----------------------------------------------------------------.
606 | Assign symbol numbers, and write definition of token names into |
607 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
608 `----------------------------------------------------------------*/
613 symbols
= xcalloc (nsyms
, sizeof *symbols
);
615 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
616 symbols_do (symbol_pack_processor
, NULL
);
618 symbols_token_translations_init ();
620 if (startsymbol
->class == unknown_sym
)
621 fatal_at (startsymbol_location
,
622 _("the start symbol %s is undefined"),
624 else if (startsymbol
->class == token_sym
)
625 fatal_at (startsymbol_location
,
626 _("the start symbol %s is a token"),