1 /* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
31 /*------------------------.
32 | Distinguished symbols. |
33 `------------------------*/
35 symbol
*errtoken
= NULL
;
36 symbol
*undeftoken
= NULL
;
37 symbol
*endtoken
= NULL
;
38 symbol
*accept
= NULL
;
39 symbol
*startsymbol
= NULL
;
40 location startsymbol_location
;
42 /*---------------------------------.
43 | Create a new symbol, named TAG. |
44 `---------------------------------*/
47 symbol_new (uniqstr tag
, location loc
)
49 symbol
*res
= XMALLOC (symbol
, 1);
55 res
->type_name
= NULL
;
56 res
->destructor
= NULL
;
59 res
->number
= NUMBER_UNDEFINED
;
61 res
->assoc
= undef_assoc
;
62 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
65 res
->class = unknown_sym
;
72 /*-----------------------------------------------------------------.
73 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
75 `-----------------------------------------------------------------*/
78 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
83 complain_at (loc
, _("type redeclaration for %s"), sym
->tag
);
84 uniqstr_assert (type_name
);
85 sym
->type_name
= type_name
;
90 /*------------------------------------------------------------------.
91 | Set the DESTRUCTOR associated with SYM. Do nothing if passed 0. |
92 `------------------------------------------------------------------*/
95 symbol_destructor_set (symbol
*sym
, char *destructor
, location loc
)
100 complain_at (loc
, _("%s redeclaration for %s"),
101 "%destructor", sym
->tag
);
102 sym
->destructor
= destructor
;
103 sym
->destructor_location
= loc
;
108 /*---------------------------------------------------------------.
109 | Set the PRINTER associated with SYM. Do nothing if passed 0. |
110 `---------------------------------------------------------------*/
113 symbol_printer_set (symbol
*sym
, char *printer
, location loc
)
118 complain_at (loc
, _("%s redeclaration for %s"),
119 "%printer", sym
->tag
);
120 sym
->printer
= printer
;
121 sym
->printer_location
= loc
;
126 /*-----------------------------------------------------------------.
127 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
128 | with UNDEF_ASSOC as ASSOC. |
129 `-----------------------------------------------------------------*/
132 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
134 if (a
!= undef_assoc
)
137 complain_at (loc
, _("redefining precedence of %s"), sym
->tag
);
142 /* Only terminals have a precedence. */
143 symbol_class_set (sym
, token_sym
, loc
);
147 /*------------------------------------.
148 | Set the CLASS associated with SYM. |
149 `------------------------------------*/
152 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
)
154 if (sym
->class != unknown_sym
&& sym
->class != class)
155 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
157 if (class == nterm_sym
&& sym
->class != nterm_sym
)
158 sym
->number
= nvars
++;
159 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
160 sym
->number
= ntokens
++;
166 /*------------------------------------------------.
167 | Set the USER_TOKEN_NUMBER associated with SYM. |
168 `------------------------------------------------*/
171 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
173 if (sym
->class != token_sym
)
176 if (sym
->user_token_number
!= USER_NUMBER_UNDEFINED
177 && sym
->user_token_number
!= user_token_number
)
178 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
180 sym
->user_token_number
= user_token_number
;
181 /* User defined $end token? */
182 if (user_token_number
== 0)
185 endtoken
->number
= 0;
186 /* It is always mapped to 0, so it was already counted in
198 symbol_free (symbol
*sym
)
204 /*----------------------------------------------------------.
205 | If SYM is not defined, report an error, and consider it a |
207 `----------------------------------------------------------*/
210 symbol_check_defined (symbol
*sym
)
212 if (sym
->class == unknown_sym
)
216 _("symbol %s is used, but is not defined as a token and has no rules"),
218 sym
->class = nterm_sym
;
219 sym
->number
= nvars
++;
226 /*------------------------------------------------------------------.
227 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
228 | them with TYPENAME. |
229 `------------------------------------------------------------------*/
232 symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
)
235 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
238 warn_at (loc
, _("symbol `%s' given more than one literal string"),
242 symval
->class = token_sym
;
243 symval
->user_token_number
= sym
->user_token_number
;
244 sym
->user_token_number
= USER_NUMBER_ALIAS
;
247 /* sym and symval combined are only one symbol. */
250 if (ntokens
!= sym
->number
&& ntokens
!= symval
->number
)
252 sym
->number
= symval
->number
=
253 (symval
->number
< sym
->number
) ? symval
->number
: sym
->number
;
258 /*---------------------------------------------------------.
259 | Check that THIS, and its alias, have same precedence and |
261 `---------------------------------------------------------*/
264 symbol_check_alias_consistence (symbol
*this)
266 /* Check only those who _are_ the aliases. */
267 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
269 if (this->prec
!= this->alias
->prec
)
271 if (this->prec
!= 0 && this->alias
->prec
!= 0)
272 complain_at (this->alias
->location
,
273 _("conflicting precedences for %s and %s"),
274 this->tag
, this->alias
->tag
);
276 this->alias
->prec
= this->prec
;
278 this->prec
= this->alias
->prec
;
281 if (this->assoc
!= this->alias
->assoc
)
283 if (this->assoc
!= undef_assoc
&& this->alias
->assoc
!= undef_assoc
)
284 complain_at (this->alias
->location
,
285 _("conflicting associativities for %s (%s) and %s (%s)"),
286 this->tag
, assoc_to_string (this->assoc
),
287 this->alias
->tag
, assoc_to_string (this->alias
->assoc
));
288 if (this->assoc
!= undef_assoc
)
289 this->alias
->assoc
= this->assoc
;
291 this->assoc
= this->alias
->assoc
;
298 /*-------------------------------------------------------------------.
299 | Assign a symbol number, and write the definition of the token name |
300 | into FDEFINES. Put in SYMBOLS. |
301 `-------------------------------------------------------------------*/
304 symbol_pack (symbol
*this)
306 if (this->class == nterm_sym
)
308 this->number
+= ntokens
;
310 else if (this->alias
)
312 /* This symbol and its alias are a single token defn.
313 Allocate a tokno, and assign to both check agreement of
314 prec and assoc fields and make both the same */
315 if (this->number
== NUMBER_UNDEFINED
)
317 if (this == endtoken
|| this->alias
== endtoken
)
318 this->number
= this->alias
->number
= 0;
321 if (this->alias
->number
== NUMBER_UNDEFINED
)
323 this->number
= this->alias
->number
;
326 /* Do not do processing below for USER_NUMBER_ALIASes. */
327 if (this->user_token_number
== USER_NUMBER_ALIAS
)
330 else /* this->class == token_sym */
332 if (this->number
== NUMBER_UNDEFINED
)
336 symbols
[this->number
] = this;
343 /*--------------------------------------------------.
344 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
345 `--------------------------------------------------*/
348 symbol_translation (symbol
*this)
351 if (this->class == token_sym
352 && this->user_token_number
!= USER_NUMBER_ALIAS
)
354 /* A token which translation has already been set? */
355 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
356 complain_at (this->location
,
357 _("tokens %s and %s both assigned number %d"),
358 symbols
[token_translations
[this->user_token_number
]]->tag
,
359 this->tag
, this->user_token_number
);
361 token_translations
[this->user_token_number
] = this->number
;
368 /*----------------------.
369 | A symbol hash table. |
370 `----------------------*/
372 /* Initial capacity of symbols hash table. */
373 #define HT_INITIAL_CAPACITY 257
375 static struct hash_table
*symbol_table
= NULL
;
378 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
380 /* Since tags are unique, we can compare the pointers themselves. */
381 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
385 hash_symbol (const symbol
*m
, unsigned int tablesize
)
387 /* Since tags are unique, we can hash the pointer itself. */
388 return ((size_t) m
->tag
) % tablesize
;
392 /*-------------------------------.
393 | Create the symbol hash table. |
394 `-------------------------------*/
399 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
401 (Hash_hasher
) hash_symbol
,
402 (Hash_comparator
) hash_compare_symbol
,
403 (Hash_data_freer
) symbol_free
);
407 /*----------------------------------------------------------------.
408 | Find the symbol named KEY, and return it. If it does not exist |
410 `----------------------------------------------------------------*/
413 symbol_get (const char *key
, location loc
)
418 /* Keep the symbol in a printable form. */
419 key
= uniqstr_new (quotearg_style (escape_quoting_style
, key
));
420 *(char const **) &probe
.tag
= key
;
421 entry
= hash_lookup (symbol_table
, &probe
);
425 /* First insertion in the hash. */
426 entry
= symbol_new (key
, loc
);
427 hash_insert (symbol_table
, entry
);
433 /*------------------------------------------------------------------.
434 | Generate a dummy nonterminal, whose name cannot conflict with the |
436 `------------------------------------------------------------------*/
439 dummy_symbol_get (location loc
)
441 /* Incremented for each generated symbol. */
442 static int dummy_count
= 0;
443 static char buf
[256];
447 sprintf (buf
, "@%d", ++dummy_count
);
448 sym
= symbol_get (buf
, loc
);
449 sym
->class = nterm_sym
;
450 sym
->number
= nvars
++;
455 /*-------------------.
456 | Free the symbols. |
457 `-------------------*/
462 hash_free (symbol_table
);
467 /*---------------------------------------------------------------.
468 | Look for undefined symbols, report an error, and consider them |
470 `---------------------------------------------------------------*/
473 symbols_do (symbol_processor processor
, void *processor_data
)
475 hash_do_for_each (symbol_table
,
476 (Hash_processor
) processor
,
481 /*--------------------------------------------------------------.
482 | Check that all the symbols are defined. Report any undefined |
483 | symbols and consider them nonterminals. |
484 `--------------------------------------------------------------*/
487 symbols_check_defined (void)
489 symbols_do (symbol_check_defined
, NULL
);
492 /*------------------------------------------------------------------.
493 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
495 `------------------------------------------------------------------*/
498 symbols_token_translations_init (void)
500 bool num_256_available_p
= true;
503 /* Find the highest user token number, and whether 256, the POSIX
504 preferred user token number for the error token, is used. */
505 max_user_token_number
= 0;
506 for (i
= 0; i
< ntokens
; ++i
)
508 symbol
*this = symbols
[i
];
509 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
511 if (this->user_token_number
> max_user_token_number
)
512 max_user_token_number
= this->user_token_number
;
513 if (this->user_token_number
== 256)
514 num_256_available_p
= false;
518 /* If 256 is not used, assign it to error, to follow POSIX. */
519 if (num_256_available_p
520 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
521 errtoken
->user_token_number
= 256;
523 /* Set the missing user numbers. */
524 if (max_user_token_number
< 256)
525 max_user_token_number
= 256;
527 for (i
= 0; i
< ntokens
; ++i
)
529 symbol
*this = symbols
[i
];
530 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
531 this->user_token_number
= ++max_user_token_number
;
532 if (this->user_token_number
> max_user_token_number
)
533 max_user_token_number
= this->user_token_number
;
536 token_translations
= XCALLOC (symbol_number
, max_user_token_number
+ 1);
538 /* Initialize all entries for literal tokens to 2, the internal
539 token number for $undefined, which represents all invalid inputs.
541 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
542 token_translations
[i
] = undeftoken
->number
;
543 symbols_do (symbol_translation
, NULL
);
547 /*----------------------------------------------------------------.
548 | Assign symbol numbers, and write definition of token names into |
549 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
550 `----------------------------------------------------------------*/
555 symbols
= XCALLOC (symbol
*, nsyms
);
557 symbols_do (symbol_check_alias_consistence
, NULL
);
558 symbols_do (symbol_pack
, NULL
);
560 symbols_token_translations_init ();
562 if (startsymbol
->class == unknown_sym
)
563 fatal_at (startsymbol_location
,
564 _("the start symbol %s is undefined"),
566 else if (startsymbol
->class == token_sym
)
567 fatal_at (startsymbol_location
,
568 _("the start symbol %s is a token"),