1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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
= MALLOC (res
, 1);
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
;
74 /*-----------------------------------------------------------------.
75 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
77 `-----------------------------------------------------------------*/
80 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
85 complain_at (loc
, _("type redeclaration for %s"), sym
->tag
);
86 uniqstr_assert (type_name
);
87 sym
->type_name
= type_name
;
92 /*------------------------------------------------------------------.
93 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
94 `------------------------------------------------------------------*/
97 symbol_destructor_set (symbol
*sym
, char *destructor
, location loc
)
102 complain_at (loc
, _("%s redeclaration for %s"),
103 "%destructor", sym
->tag
);
104 sym
->destructor
= destructor
;
105 sym
->destructor_location
= loc
;
110 /*---------------------------------------------------------------.
111 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
112 `---------------------------------------------------------------*/
115 symbol_printer_set (symbol
*sym
, char *printer
, location loc
)
120 complain_at (loc
, _("%s redeclaration for %s"),
121 "%printer", sym
->tag
);
122 sym
->printer
= printer
;
123 sym
->printer_location
= loc
;
128 /*-----------------------------------------------------------------.
129 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
130 | with UNDEF_ASSOC as ASSOC. |
131 `-----------------------------------------------------------------*/
134 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
136 if (a
!= undef_assoc
)
139 complain_at (loc
, _("redefining precedence of %s"), sym
->tag
);
144 /* Only terminals have a precedence. */
145 symbol_class_set (sym
, token_sym
, loc
);
149 /*------------------------------------.
150 | Set the CLASS associated with SYM. |
151 `------------------------------------*/
154 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
)
156 if (sym
->class != unknown_sym
&& sym
->class != class)
157 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
159 if (class == nterm_sym
&& sym
->class != nterm_sym
)
160 sym
->number
= nvars
++;
161 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
162 sym
->number
= ntokens
++;
168 /*------------------------------------------------.
169 | Set the USER_TOKEN_NUMBER associated with SYM. |
170 `------------------------------------------------*/
173 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
175 if (sym
->class != token_sym
)
178 if (sym
->user_token_number
!= USER_NUMBER_UNDEFINED
179 && sym
->user_token_number
!= user_token_number
)
180 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
182 sym
->user_token_number
= user_token_number
;
183 /* User defined $end token? */
184 if (user_token_number
== 0)
187 endtoken
->number
= 0;
188 /* It is always mapped to 0, so it was already counted in
195 /*----------------------------------------------------------.
196 | If SYM is not defined, report an error, and consider it a |
198 `----------------------------------------------------------*/
201 symbol_check_defined (symbol
*sym
)
203 if (sym
->class == unknown_sym
)
207 _("symbol %s is used, but is not defined as a token and has no rules"),
209 sym
->class = nterm_sym
;
210 sym
->number
= nvars
++;
217 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
219 return symbol_check_defined (sym
);
223 /*------------------------------------------------------------------.
224 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
225 | SYMVAL with SYM's type. |
226 `------------------------------------------------------------------*/
229 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
232 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
235 warn_at (loc
, _("symbol `%s' given more than one literal string"),
239 symval
->class = token_sym
;
240 symval
->user_token_number
= sym
->user_token_number
;
241 sym
->user_token_number
= USER_NUMBER_ALIAS
;
244 /* sym and symval combined are only one symbol. */
247 if (ntokens
!= sym
->number
&& ntokens
!= symval
->number
)
249 sym
->number
= symval
->number
=
250 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
251 symbol_type_set (symval
, sym
->type_name
, loc
);
256 /*---------------------------------------------------------.
257 | Check that THIS, and its alias, have same precedence and |
259 `---------------------------------------------------------*/
262 symbol_check_alias_consistency (symbol
*this)
264 /* Check only those who _are_ the aliases. */
265 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
267 if (this->prec
!= this->alias
->prec
)
269 if (this->prec
!= 0 && this->alias
->prec
!= 0)
270 complain_at (this->alias
->location
,
271 _("conflicting precedences for %s and %s"),
272 this->tag
, this->alias
->tag
);
274 this->alias
->prec
= this->prec
;
276 this->prec
= this->alias
->prec
;
279 if (this->assoc
!= this->alias
->assoc
)
281 if (this->assoc
!= undef_assoc
&& this->alias
->assoc
!= undef_assoc
)
282 complain_at (this->alias
->location
,
283 _("conflicting associativities for %s (%s) and %s (%s)"),
284 this->tag
, assoc_to_string (this->assoc
),
285 this->alias
->tag
, assoc_to_string (this->alias
->assoc
));
286 if (this->assoc
!= undef_assoc
)
287 this->alias
->assoc
= this->assoc
;
289 this->assoc
= this->alias
->assoc
;
296 symbol_check_alias_consistency_processor (void *this,
297 void *null ATTRIBUTE_UNUSED
)
299 return symbol_check_alias_consistency (this);
303 /*-------------------------------------------------------------------.
304 | Assign a symbol number, and write the definition of the token name |
305 | into FDEFINES. Put in SYMBOLS. |
306 `-------------------------------------------------------------------*/
309 symbol_pack (symbol
*this)
311 if (this->class == nterm_sym
)
313 this->number
+= ntokens
;
315 else if (this->alias
)
317 /* This symbol and its alias are a single token defn.
318 Allocate a tokno, and assign to both check agreement of
319 prec and assoc fields and make both the same */
320 if (this->number
== NUMBER_UNDEFINED
)
322 if (this == endtoken
|| this->alias
== endtoken
)
323 this->number
= this->alias
->number
= 0;
326 if (this->alias
->number
== NUMBER_UNDEFINED
)
328 this->number
= this->alias
->number
;
331 /* Do not do processing below for USER_NUMBER_ALIASes. */
332 if (this->user_token_number
== USER_NUMBER_ALIAS
)
335 else /* this->class == token_sym */
337 if (this->number
== NUMBER_UNDEFINED
)
341 symbols
[this->number
] = this;
346 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
348 return symbol_pack (this);
354 /*--------------------------------------------------.
355 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
356 `--------------------------------------------------*/
359 symbol_translation (symbol
*this)
362 if (this->class == token_sym
363 && this->user_token_number
!= USER_NUMBER_ALIAS
)
365 /* A token which translation has already been set? */
366 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
367 complain_at (this->location
,
368 _("tokens %s and %s both assigned number %d"),
369 symbols
[token_translations
[this->user_token_number
]]->tag
,
370 this->tag
, this->user_token_number
);
372 token_translations
[this->user_token_number
] = this->number
;
379 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
381 return symbol_translation (this);
385 /*----------------------.
386 | A symbol hash table. |
387 `----------------------*/
389 /* Initial capacity of symbols hash table. */
390 #define HT_INITIAL_CAPACITY 257
392 static struct hash_table
*symbol_table
= NULL
;
395 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
397 /* Since tags are unique, we can compare the pointers themselves. */
398 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
402 hash_symbol_comparator (void const *m1
, void const *m2
)
404 return hash_compare_symbol (m1
, m2
);
408 hash_symbol (const symbol
*m
, size_t tablesize
)
410 /* Since tags are unique, we can hash the pointer itself. */
411 return ((uintptr_t) m
->tag
) % tablesize
;
415 hash_symbol_hasher (void const *m
, size_t tablesize
)
417 return hash_symbol (m
, tablesize
);
421 /*-------------------------------.
422 | Create the symbol hash table. |
423 `-------------------------------*/
428 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
431 hash_symbol_comparator
,
436 /*----------------------------------------------------------------.
437 | Find the symbol named KEY, and return it. If it does not exist |
439 `----------------------------------------------------------------*/
442 symbol_get (const char *key
, location loc
)
447 /* Keep the symbol in a printable form. */
448 key
= uniqstr_new (quotearg_style (escape_quoting_style
, key
));
450 entry
= hash_lookup (symbol_table
, &probe
);
454 /* First insertion in the hash. */
455 entry
= symbol_new (key
, loc
);
456 hash_insert (symbol_table
, entry
);
462 /*------------------------------------------------------------------.
463 | Generate a dummy nonterminal, whose name cannot conflict with the |
465 `------------------------------------------------------------------*/
468 dummy_symbol_get (location loc
)
470 /* Incremented for each generated symbol. */
471 static int dummy_count
= 0;
472 static char buf
[256];
476 sprintf (buf
, "@%d", ++dummy_count
);
477 sym
= symbol_get (buf
, loc
);
478 sym
->class = nterm_sym
;
479 sym
->number
= nvars
++;
484 /*-------------------.
485 | Free the symbols. |
486 `-------------------*/
491 hash_free (symbol_table
);
496 /*---------------------------------------------------------------.
497 | Look for undefined symbols, report an error, and consider them |
499 `---------------------------------------------------------------*/
502 symbols_do (Hash_processor processor
, void *processor_data
)
504 hash_do_for_each (symbol_table
, processor
, processor_data
);
508 /*--------------------------------------------------------------.
509 | Check that all the symbols are defined. Report any undefined |
510 | symbols and consider them nonterminals. |
511 `--------------------------------------------------------------*/
514 symbols_check_defined (void)
516 symbols_do (symbol_check_defined_processor
, NULL
);
519 /*------------------------------------------------------------------.
520 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
522 `------------------------------------------------------------------*/
525 symbols_token_translations_init (void)
527 bool num_256_available_p
= true;
530 /* Find the highest user token number, and whether 256, the POSIX
531 preferred user token number for the error token, is used. */
532 max_user_token_number
= 0;
533 for (i
= 0; i
< ntokens
; ++i
)
535 symbol
*this = symbols
[i
];
536 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
538 if (this->user_token_number
> max_user_token_number
)
539 max_user_token_number
= this->user_token_number
;
540 if (this->user_token_number
== 256)
541 num_256_available_p
= false;
545 /* If 256 is not used, assign it to error, to follow POSIX. */
546 if (num_256_available_p
547 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
548 errtoken
->user_token_number
= 256;
550 /* Set the missing user numbers. */
551 if (max_user_token_number
< 256)
552 max_user_token_number
= 256;
554 for (i
= 0; i
< ntokens
; ++i
)
556 symbol
*this = symbols
[i
];
557 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
558 this->user_token_number
= ++max_user_token_number
;
559 if (this->user_token_number
> max_user_token_number
)
560 max_user_token_number
= this->user_token_number
;
563 CALLOC (token_translations
, max_user_token_number
+ 1);
565 /* Initialize all entries for literal tokens to 2, the internal
566 token number for $undefined, which represents all invalid inputs.
568 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
569 token_translations
[i
] = undeftoken
->number
;
570 symbols_do (symbol_translation_processor
, NULL
);
574 /*----------------------------------------------------------------.
575 | Assign symbol numbers, and write definition of token names into |
576 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
577 `----------------------------------------------------------------*/
582 CALLOC (symbols
, nsyms
);
584 symbols_do (symbol_check_alias_consistency_processor
, NULL
);
585 symbols_do (symbol_pack_processor
, NULL
);
587 symbols_token_translations_init ();
589 if (startsymbol
->class == unknown_sym
)
590 fatal_at (startsymbol_location
,
591 _("the start symbol %s is undefined"),
593 else if (startsymbol
->class == token_sym
)
594 fatal_at (startsymbol_location
,
595 _("the start symbol %s is a token"),