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 (f, " %s { %s }", #Attr, s->Attr)
86 symbol_print (symbol
*s
, FILE *f
)
90 fprintf (f
, "\"%s\"", s
->tag
);
91 SYMBOL_ATTR_PRINT (type_name
);
92 SYMBOL_ATTR_PRINT (destructor
);
93 SYMBOL_ATTR_PRINT (printer
);
96 fprintf (f
, "<NULL>");
99 #undef SYMBOL_ATTR_PRINT
101 /*------------------------------------------------------------------.
102 | Complain that S's WHAT is redeclared at SECOND, and was first set |
104 `------------------------------------------------------------------*/
107 redeclaration (symbol
* s
, const char *what
, location first
, location second
)
109 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
110 complain_at (first
, _("first declaration"));
114 /*-----------------------------------------------------------------.
115 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
117 `-----------------------------------------------------------------*/
120 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
125 redeclaration (sym
, "%type", sym
->type_location
, loc
);
126 uniqstr_assert (type_name
);
127 sym
->type_name
= type_name
;
128 sym
->type_location
= loc
;
133 /*------------------------------------------------------------------.
134 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
135 `------------------------------------------------------------------*/
138 symbol_destructor_set (symbol
*sym
, const char *destructor
, location loc
)
143 redeclaration (sym
, "%destructor", sym
->destructor_location
, loc
);
144 sym
->destructor
= destructor
;
145 sym
->destructor_location
= loc
;
150 /*---------------------------------------------------------------.
151 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
152 `---------------------------------------------------------------*/
155 symbol_printer_set (symbol
*sym
, const char *printer
, location loc
)
160 redeclaration (sym
, "%printer", sym
->destructor_location
, loc
);
161 sym
->printer
= printer
;
162 sym
->printer_location
= loc
;
167 /*-----------------------------------------------------------------.
168 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
169 | with UNDEF_ASSOC as ASSOC. |
170 `-----------------------------------------------------------------*/
173 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
175 if (a
!= undef_assoc
)
178 redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
, loc
);
181 sym
->prec_location
= loc
;
184 /* Only terminals have a precedence. */
185 symbol_class_set (sym
, token_sym
, loc
);
189 /*------------------------------------.
190 | Set the CLASS associated with SYM. |
191 `------------------------------------*/
194 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
)
196 if (sym
->class != unknown_sym
&& sym
->class != class)
197 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
199 if (class == nterm_sym
&& sym
->class != nterm_sym
)
200 sym
->number
= nvars
++;
201 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
202 sym
->number
= ntokens
++;
208 /*------------------------------------------------.
209 | Set the USER_TOKEN_NUMBER associated with SYM. |
210 `------------------------------------------------*/
213 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
215 if (sym
->class != token_sym
)
218 if (sym
->user_token_number
!= USER_NUMBER_UNDEFINED
219 && sym
->user_token_number
!= user_token_number
)
220 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
222 sym
->user_token_number
= user_token_number
;
223 /* User defined $end token? */
224 if (user_token_number
== 0)
227 endtoken
->number
= 0;
228 /* It is always mapped to 0, so it was already counted in
235 /*----------------------------------------------------------.
236 | If SYM is not defined, report an error, and consider it a |
238 `----------------------------------------------------------*/
241 symbol_check_defined (symbol
*sym
)
243 if (sym
->class == unknown_sym
)
247 _("symbol %s is used, but is not defined as a token and has no rules"),
249 sym
->class = nterm_sym
;
250 sym
->number
= nvars
++;
257 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
259 return symbol_check_defined (sym
);
263 /*------------------------------------------------------------------.
264 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
265 | SYMVAL with SYM's type. |
266 `------------------------------------------------------------------*/
269 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
272 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
275 warn_at (loc
, _("symbol `%s' given more than one literal string"),
279 symval
->class = token_sym
;
280 symval
->user_token_number
= sym
->user_token_number
;
281 sym
->user_token_number
= USER_NUMBER_ALIAS
;
284 /* sym and symval combined are only one symbol. */
287 if (ntokens
!= sym
->number
&& ntokens
!= symval
->number
)
289 sym
->number
= symval
->number
=
290 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
291 symbol_type_set (symval
, sym
->type_name
, loc
);
296 /*---------------------------------------------------------.
297 | Check that THIS, and its alias, have same precedence and |
299 `---------------------------------------------------------*/
302 symbol_check_alias_consistency (symbol
*this)
304 symbol
*alias
= this;
305 symbol
*orig
= this->alias
;
307 /* Check only those that _are_ the aliases. */
308 if (!(this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
))
311 if (orig
->type_name
!= alias
->type_name
)
314 symbol_type_set (alias
, orig
->type_name
, orig
->type_location
);
316 symbol_type_set (orig
, alias
->type_name
, alias
->type_location
);
320 if (orig
->destructor
|| alias
->destructor
)
322 if (orig
->destructor
)
323 symbol_destructor_set (alias
, orig
->destructor
,
324 orig
->destructor_location
);
326 symbol_destructor_set (orig
, alias
->destructor
,
327 alias
->destructor_location
);
330 if (orig
->printer
|| alias
->printer
)
333 symbol_printer_set (alias
, orig
->printer
, orig
->printer_location
);
335 symbol_printer_set (orig
, alias
->printer
, alias
->printer_location
);
338 if (alias
->prec
|| orig
->prec
)
341 symbol_precedence_set (alias
, orig
->prec
, orig
->assoc
,
342 orig
->prec_location
);
344 symbol_precedence_set (orig
, alias
->prec
, alias
->assoc
,
345 alias
->prec_location
);
350 symbol_check_alias_consistency_processor (void *this,
351 void *null ATTRIBUTE_UNUSED
)
353 symbol_check_alias_consistency (this);
358 /*-------------------------------------------------------------------.
359 | Assign a symbol number, and write the definition of the token name |
360 | into FDEFINES. Put in SYMBOLS. |
361 `-------------------------------------------------------------------*/
364 symbol_pack (symbol
*this)
366 if (this->class == nterm_sym
)
368 this->number
+= ntokens
;
370 else if (this->alias
)
372 /* This symbol and its alias are a single token defn.
373 Allocate a tokno, and assign to both check agreement of
374 prec and assoc fields and make both the same */
375 if (this->number
== NUMBER_UNDEFINED
)
377 if (this == endtoken
|| this->alias
== endtoken
)
378 this->number
= this->alias
->number
= 0;
381 if (this->alias
->number
== NUMBER_UNDEFINED
)
383 this->number
= this->alias
->number
;
386 /* Do not do processing below for USER_NUMBER_ALIASes. */
387 if (this->user_token_number
== USER_NUMBER_ALIAS
)
390 else /* this->class == token_sym */
392 if (this->number
== NUMBER_UNDEFINED
)
396 symbols
[this->number
] = this;
401 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
403 return symbol_pack (this);
409 /*--------------------------------------------------.
410 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
411 `--------------------------------------------------*/
414 symbol_translation (symbol
*this)
417 if (this->class == token_sym
418 && this->user_token_number
!= USER_NUMBER_ALIAS
)
420 /* A token which translation has already been set? */
421 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
422 complain_at (this->location
,
423 _("tokens %s and %s both assigned number %d"),
424 symbols
[token_translations
[this->user_token_number
]]->tag
,
425 this->tag
, this->user_token_number
);
427 token_translations
[this->user_token_number
] = this->number
;
434 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
436 return symbol_translation (this);
440 /*----------------------.
441 | A symbol hash table. |
442 `----------------------*/
444 /* Initial capacity of symbols hash table. */
445 #define HT_INITIAL_CAPACITY 257
447 static struct hash_table
*symbol_table
= NULL
;
450 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
452 /* Since tags are unique, we can compare the pointers themselves. */
453 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
457 hash_symbol_comparator (void const *m1
, void const *m2
)
459 return hash_compare_symbol (m1
, m2
);
463 hash_symbol (const symbol
*m
, size_t tablesize
)
465 /* Since tags are unique, we can hash the pointer itself. */
466 return ((uintptr_t) m
->tag
) % tablesize
;
470 hash_symbol_hasher (void const *m
, size_t tablesize
)
472 return hash_symbol (m
, tablesize
);
476 /*-------------------------------.
477 | Create the symbol hash table. |
478 `-------------------------------*/
483 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
486 hash_symbol_comparator
,
491 /*----------------------------------------------------------------.
492 | Find the symbol named KEY, and return it. If it does not exist |
494 `----------------------------------------------------------------*/
497 symbol_get (const char *key
, location loc
)
502 key
= uniqstr_new (key
);
504 entry
= hash_lookup (symbol_table
, &probe
);
508 /* First insertion in the hash. */
509 entry
= symbol_new (key
, loc
);
510 hash_insert (symbol_table
, entry
);
516 /*------------------------------------------------------------------.
517 | Generate a dummy nonterminal, whose name cannot conflict with the |
519 `------------------------------------------------------------------*/
522 dummy_symbol_get (location loc
)
524 /* Incremented for each generated symbol. */
525 static int dummy_count
= 0;
526 static char buf
[256];
530 sprintf (buf
, "@%d", ++dummy_count
);
531 sym
= symbol_get (buf
, loc
);
532 sym
->class = nterm_sym
;
533 sym
->number
= nvars
++;
538 /*-------------------.
539 | Free the symbols. |
540 `-------------------*/
545 hash_free (symbol_table
);
550 /*---------------------------------------------------------------.
551 | Look for undefined symbols, report an error, and consider them |
553 `---------------------------------------------------------------*/
556 symbols_do (Hash_processor processor
, void *processor_data
)
558 hash_do_for_each (symbol_table
, processor
, processor_data
);
562 /*--------------------------------------------------------------.
563 | Check that all the symbols are defined. Report any undefined |
564 | symbols and consider them nonterminals. |
565 `--------------------------------------------------------------*/
568 symbols_check_defined (void)
570 symbols_do (symbol_check_defined_processor
, NULL
);
573 /*------------------------------------------------------------------.
574 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
576 `------------------------------------------------------------------*/
579 symbols_token_translations_init (void)
581 bool num_256_available_p
= true;
584 /* Find the highest user token number, and whether 256, the POSIX
585 preferred user token number for the error token, is used. */
586 max_user_token_number
= 0;
587 for (i
= 0; i
< ntokens
; ++i
)
589 symbol
*this = symbols
[i
];
590 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
592 if (this->user_token_number
> max_user_token_number
)
593 max_user_token_number
= this->user_token_number
;
594 if (this->user_token_number
== 256)
595 num_256_available_p
= false;
599 /* If 256 is not used, assign it to error, to follow POSIX. */
600 if (num_256_available_p
601 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
602 errtoken
->user_token_number
= 256;
604 /* Set the missing user numbers. */
605 if (max_user_token_number
< 256)
606 max_user_token_number
= 256;
608 for (i
= 0; i
< ntokens
; ++i
)
610 symbol
*this = symbols
[i
];
611 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
612 this->user_token_number
= ++max_user_token_number
;
613 if (this->user_token_number
> max_user_token_number
)
614 max_user_token_number
= this->user_token_number
;
617 token_translations
= xnmalloc (max_user_token_number
+ 1,
618 sizeof *token_translations
);
620 /* Initialize all entries for literal tokens to 2, the internal
621 token number for $undefined, which represents all invalid inputs.
623 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
624 token_translations
[i
] = undeftoken
->number
;
625 symbols_do (symbol_translation_processor
, NULL
);
629 /*----------------------------------------------------------------.
630 | Assign symbol numbers, and write definition of token names into |
631 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
632 `----------------------------------------------------------------*/
637 symbols
= xcalloc (nsyms
, sizeof *symbols
);
639 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
640 symbols_do (symbol_pack_processor
, NULL
);
642 symbols_token_translations_init ();
644 if (startsymbol
->class == unknown_sym
)
645 fatal_at (startsymbol_location
,
646 _("the start symbol %s is undefined"),
648 else if (startsymbol
->class == token_sym
)
649 fatal_at (startsymbol_location
,
650 _("the start symbol %s is a token"),