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
);
81 #define SYMBOL_ATTR_PRINT(Attr) \
83 fprintf (stderr, " %s { %s }", #Attr, s->Attr)
86 symbol_print (FILE *f
, symbol
*s
)
88 fprintf (stderr
, "\"%s\"", s
->tag
);
89 SYMBOL_ATTR_PRINT (type_name
);
90 SYMBOL_ATTR_PRINT (destructor
);
91 SYMBOL_ATTR_PRINT (printer
);
94 #undef SYMBOL_ATTR_PRINT
96 /*------------------------------------------------------------------.
97 | Complain that S's WHAT is redeclared at SECOND, and was first set |
99 `------------------------------------------------------------------*/
102 redeclaration (symbol
* s
, const char *what
, location first
, location second
)
104 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
105 complain_at (first
, _("first declaration"));
109 /*-----------------------------------------------------------------.
110 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
112 `-----------------------------------------------------------------*/
115 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
120 redeclaration (sym
, "%type", sym
->type_location
, loc
);
121 uniqstr_assert (type_name
);
122 sym
->type_name
= type_name
;
123 sym
->type_location
= loc
;
128 /*------------------------------------------------------------------.
129 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
130 `------------------------------------------------------------------*/
133 symbol_destructor_set (symbol
*sym
, char *destructor
, location loc
)
138 redeclaration (sym
, "%destructor", sym
->destructor_location
, loc
);
139 sym
->destructor
= destructor
;
140 sym
->destructor_location
= loc
;
145 /*---------------------------------------------------------------.
146 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
147 `---------------------------------------------------------------*/
150 symbol_printer_set (symbol
*sym
, char *printer
, location loc
)
155 redeclaration (sym
, "%printer", sym
->destructor_location
, loc
);
156 sym
->printer
= printer
;
157 sym
->printer_location
= loc
;
162 /*-----------------------------------------------------------------.
163 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
164 | with UNDEF_ASSOC as ASSOC. |
165 `-----------------------------------------------------------------*/
168 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
170 if (a
!= undef_assoc
)
173 redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
, loc
);
176 sym
->prec_location
= loc
;
179 /* Only terminals have a precedence. */
180 symbol_class_set (sym
, token_sym
, loc
);
184 /*------------------------------------.
185 | Set the CLASS associated with SYM. |
186 `------------------------------------*/
189 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
)
191 if (sym
->class != unknown_sym
&& sym
->class != class)
192 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
194 if (class == nterm_sym
&& sym
->class != nterm_sym
)
195 sym
->number
= nvars
++;
196 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
197 sym
->number
= ntokens
++;
203 /*------------------------------------------------.
204 | Set the USER_TOKEN_NUMBER associated with SYM. |
205 `------------------------------------------------*/
208 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
210 if (sym
->class != token_sym
)
213 if (sym
->user_token_number
!= USER_NUMBER_UNDEFINED
214 && sym
->user_token_number
!= user_token_number
)
215 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
217 sym
->user_token_number
= user_token_number
;
218 /* User defined $end token? */
219 if (user_token_number
== 0)
222 endtoken
->number
= 0;
223 /* It is always mapped to 0, so it was already counted in
230 /*----------------------------------------------------------.
231 | If SYM is not defined, report an error, and consider it a |
233 `----------------------------------------------------------*/
236 symbol_check_defined (symbol
*sym
)
238 if (sym
->class == unknown_sym
)
242 _("symbol %s is used, but is not defined as a token and has no rules"),
244 sym
->class = nterm_sym
;
245 sym
->number
= nvars
++;
252 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
254 return symbol_check_defined (sym
);
258 /*------------------------------------------------------------------.
259 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
260 | SYMVAL with SYM's type. |
261 `------------------------------------------------------------------*/
264 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
267 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
270 warn_at (loc
, _("symbol `%s' given more than one literal string"),
274 symval
->class = token_sym
;
275 symval
->user_token_number
= sym
->user_token_number
;
276 sym
->user_token_number
= USER_NUMBER_ALIAS
;
279 /* sym and symval combined are only one symbol. */
282 if (ntokens
!= sym
->number
&& ntokens
!= symval
->number
)
284 sym
->number
= symval
->number
=
285 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
286 symbol_type_set (symval
, sym
->type_name
, loc
);
291 /*---------------------------------------------------------.
292 | Check that THIS, and its alias, have same precedence and |
294 `---------------------------------------------------------*/
297 symbol_check_alias_consistency (symbol
*this)
299 symbol
*alias
= this;
300 symbol
*orig
= this->alias
;
302 /* Check only those that _are_ the aliases. */
303 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
306 if (orig
->type_name
!= alias
->type_name
)
309 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
311 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
315 if (orig
->destructor
|| alias
->destructor
)
317 if (orig
->destructor
)
318 symbol_destructor_set (alias
, orig
->destructor
,
319 orig
->destructor_location
);
321 symbol_destructor_set (orig
, alias
->destructor
,
322 alias
->destructor_location
);
325 if (orig
->printer
|| alias
->printer
)
328 symbol_printer_set (alias
, orig
->printer
, orig
->printer_location
);
330 symbol_printer_set (orig
, alias
->printer
, alias
->printer_location
);
333 if (alias
->prec
|| orig
->prec
)
336 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
337 orig
->prec_location
);
339 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
340 alias
->prec_location
);
345 symbol_check_alias_consistency_processor (void *this,
346 void *null ATTRIBUTE_UNUSED
)
348 symbol_check_alias_consistency (this);
353 /*-------------------------------------------------------------------.
354 | Assign a symbol number, and write the definition of the token name |
355 | into FDEFINES. Put in SYMBOLS. |
356 `-------------------------------------------------------------------*/
359 symbol_pack (symbol
*this)
361 if (this->class == nterm_sym
)
363 this->number
+= ntokens
;
365 else if (this->alias
)
367 /* This symbol and its alias are a single token defn.
368 Allocate a tokno, and assign to both check agreement of
369 prec and assoc fields and make both the same */
370 if (this->number
== NUMBER_UNDEFINED
)
372 if (this == endtoken
|| this->alias
== endtoken
)
373 this->number
= this->alias
->number
= 0;
376 if (this->alias
->number
== NUMBER_UNDEFINED
)
378 this->number
= this->alias
->number
;
381 /* Do not do processing below for USER_NUMBER_ALIASes. */
382 if (this->user_token_number
== USER_NUMBER_ALIAS
)
385 else /* this->class == token_sym */
387 if (this->number
== NUMBER_UNDEFINED
)
391 symbols
[this->number
] = this;
396 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
398 return symbol_pack (this);
404 /*--------------------------------------------------.
405 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
406 `--------------------------------------------------*/
409 symbol_translation (symbol
*this)
412 if (this->class == token_sym
413 && this->user_token_number
!= USER_NUMBER_ALIAS
)
415 /* A token which translation has already been set? */
416 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
417 complain_at (this->location
,
418 _("tokens %s and %s both assigned number %d"),
419 symbols
[token_translations
[this->user_token_number
]]->tag
,
420 this->tag
, this->user_token_number
);
422 token_translations
[this->user_token_number
] = this->number
;
429 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
431 return symbol_translation (this);
435 /*----------------------.
436 | A symbol hash table. |
437 `----------------------*/
439 /* Initial capacity of symbols hash table. */
440 #define HT_INITIAL_CAPACITY 257
442 static struct hash_table
*symbol_table
= NULL
;
445 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
447 /* Since tags are unique, we can compare the pointers themselves. */
448 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
452 hash_symbol_comparator (void const *m1
, void const *m2
)
454 return hash_compare_symbol (m1
, m2
);
458 hash_symbol (const symbol
*m
, size_t tablesize
)
460 /* Since tags are unique, we can hash the pointer itself. */
461 return ((uintptr_t) m
->tag
) % tablesize
;
465 hash_symbol_hasher (void const *m
, size_t tablesize
)
467 return hash_symbol (m
, tablesize
);
471 /*-------------------------------.
472 | Create the symbol hash table. |
473 `-------------------------------*/
478 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
481 hash_symbol_comparator
,
486 /*----------------------------------------------------------------.
487 | Find the symbol named KEY, and return it. If it does not exist |
489 `----------------------------------------------------------------*/
492 symbol_get (const char *key
, location loc
)
497 key
= uniqstr_new (key
);
499 entry
= hash_lookup (symbol_table
, &probe
);
503 /* First insertion in the hash. */
504 entry
= symbol_new (key
, loc
);
505 hash_insert (symbol_table
, entry
);
511 /*------------------------------------------------------------------.
512 | Generate a dummy nonterminal, whose name cannot conflict with the |
514 `------------------------------------------------------------------*/
517 dummy_symbol_get (location loc
)
519 /* Incremented for each generated symbol. */
520 static int dummy_count
= 0;
521 static char buf
[256];
525 sprintf (buf
, "@%d", ++dummy_count
);
526 sym
= symbol_get (buf
, loc
);
527 sym
->class = nterm_sym
;
528 sym
->number
= nvars
++;
533 /*-------------------.
534 | Free the symbols. |
535 `-------------------*/
540 hash_free (symbol_table
);
545 /*---------------------------------------------------------------.
546 | Look for undefined symbols, report an error, and consider them |
548 `---------------------------------------------------------------*/
551 symbols_do (Hash_processor processor
, void *processor_data
)
553 hash_do_for_each (symbol_table
, processor
, processor_data
);
557 /*--------------------------------------------------------------.
558 | Check that all the symbols are defined. Report any undefined |
559 | symbols and consider them nonterminals. |
560 `--------------------------------------------------------------*/
563 symbols_check_defined (void)
565 symbols_do (symbol_check_defined_processor
, NULL
);
568 /*------------------------------------------------------------------.
569 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
571 `------------------------------------------------------------------*/
574 symbols_token_translations_init (void)
576 bool num_256_available_p
= true;
579 /* Find the highest user token number, and whether 256, the POSIX
580 preferred user token number for the error token, is used. */
581 max_user_token_number
= 0;
582 for (i
= 0; i
< ntokens
; ++i
)
584 symbol
*this = symbols
[i
];
585 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
587 if (this->user_token_number
> max_user_token_number
)
588 max_user_token_number
= this->user_token_number
;
589 if (this->user_token_number
== 256)
590 num_256_available_p
= false;
594 /* If 256 is not used, assign it to error, to follow POSIX. */
595 if (num_256_available_p
596 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
597 errtoken
->user_token_number
= 256;
599 /* Set the missing user numbers. */
600 if (max_user_token_number
< 256)
601 max_user_token_number
= 256;
603 for (i
= 0; i
< ntokens
; ++i
)
605 symbol
*this = symbols
[i
];
606 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
607 this->user_token_number
= ++max_user_token_number
;
608 if (this->user_token_number
> max_user_token_number
)
609 max_user_token_number
= this->user_token_number
;
612 token_translations
= xnmalloc (max_user_token_number
+ 1,
613 sizeof *token_translations
);
615 /* Initialize all entries for literal tokens to 2, the internal
616 token number for $undefined, which represents all invalid inputs.
618 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
619 token_translations
[i
] = undeftoken
->number
;
620 symbols_do (symbol_translation_processor
, NULL
);
624 /*----------------------------------------------------------------.
625 | Assign symbol numbers, and write definition of token names into |
626 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
627 `----------------------------------------------------------------*/
632 symbols
= xcalloc (nsyms
, sizeof *symbols
);
634 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
635 symbols_do (symbol_pack_processor
, NULL
);
637 symbols_token_translations_init ();
639 if (startsymbol
->class == unknown_sym
)
640 fatal_at (startsymbol_location
,
641 _("the start symbol %s is undefined"),
643 else if (startsymbol
->class == token_sym
)
644 fatal_at (startsymbol_location
,
645 _("the start symbol %s is a token"),