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. */
29 /*------------------------.
30 | Distinguished symbols. |
31 `------------------------*/
33 symbol_t
*errtoken
= NULL
;
34 symbol_t
*undeftoken
= NULL
;
35 symbol_t
*endtoken
= NULL
;
36 symbol_t
*accept
= NULL
;
37 symbol_t
*startsymbol
= NULL
;
38 location_t startsymbol_location
;
40 /*---------------------------------.
41 | Create a new symbol, named TAG. |
42 `---------------------------------*/
45 symbol_new (struniq_t tag
, location_t location
)
47 symbol_t
*res
= XMALLOC (symbol_t
, 1);
51 res
->location
= location
;
53 res
->type_name
= NULL
;
54 res
->destructor
= NULL
;
57 res
->number
= NUMBER_UNDEFINED
;
59 res
->assoc
= undef_assoc
;
60 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
63 res
->class = unknown_sym
;
70 /*------------------------------------------------------------------.
71 | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
73 `------------------------------------------------------------------*/
76 symbol_type_set (symbol_t
*symbol
, struniq_t type_name
, location_t location
)
80 if (symbol
->type_name
)
81 complain_at (location
,
82 _("type redeclaration for %s"), symbol
->tag
);
83 struniq_assert (type_name
);
84 symbol
->type_name
= type_name
;
89 /*-------------------------------------------------------------------.
90 | Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
91 `-------------------------------------------------------------------*/
94 symbol_destructor_set (symbol_t
*symbol
, char *destructor
, location_t location
)
98 if (symbol
->destructor
)
99 complain_at (location
,
100 _("%s redeclaration for %s"),
101 "%destructor", symbol
->tag
);
102 symbol
->destructor
= destructor
;
103 symbol
->destructor_location
= location
;
108 /*----------------------------------------------------------------.
109 | Set the PRINTER associated to SYMBOL. Do nothing if passed 0. |
110 `----------------------------------------------------------------*/
113 symbol_printer_set (symbol_t
*symbol
, char *printer
, location_t location
)
118 complain_at (location
,
119 _("%s redeclaration for %s"),
120 "%printer", symbol
->tag
);
121 symbol
->printer
= printer
;
122 symbol
->printer_location
= location
;
127 /*------------------------------------------------------------------.
128 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
129 | with UNDEF_ASSOC as ASSOC. |
130 `------------------------------------------------------------------*/
133 symbol_precedence_set (symbol_t
*symbol
,
134 int prec
, assoc_t assoc
, location_t location
)
136 if (assoc
!= undef_assoc
)
138 if (symbol
->prec
!= 0)
139 complain_at (location
,
140 _("redefining precedence of %s"),
143 symbol
->assoc
= assoc
;
146 /* Only terminals have a precedence. */
147 symbol_class_set (symbol
, token_sym
, location
);
151 /*-------------------------------------.
152 | Set the CLASS associated to SYMBOL. |
153 `-------------------------------------*/
156 symbol_class_set (symbol_t
*symbol
, symbol_class
class, location_t location
)
158 if (symbol
->class != unknown_sym
&& symbol
->class != class)
159 complain_at (location
, _("symbol %s redefined"), symbol
->tag
);
161 if (class == nterm_sym
&& symbol
->class != nterm_sym
)
162 symbol
->number
= nvars
++;
163 else if (class == token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
164 symbol
->number
= ntokens
++;
166 symbol
->class = class;
170 /*-------------------------------------------------.
171 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
172 `-------------------------------------------------*/
175 symbol_user_token_number_set (symbol_t
*symbol
,
176 int user_token_number
, location_t location
)
178 assert (symbol
->class == token_sym
);
180 if (symbol
->user_token_number
!= USER_NUMBER_UNDEFINED
181 && symbol
->user_token_number
!= user_token_number
)
182 complain_at (location
, _("redefining user token number of %s"),
185 symbol
->user_token_number
= user_token_number
;
186 /* User defined $end token? */
187 if (user_token_number
== 0)
190 endtoken
->number
= 0;
191 /* It is always mapped to 0, so it was already counted in
203 symbol_free (symbol_t
*this)
209 /*-----------------------------------------------------------.
210 | If THIS is not defined, report an error, and consider it a |
212 `-----------------------------------------------------------*/
215 symbol_check_defined (symbol_t
*this)
217 if (this->class == unknown_sym
)
221 _("symbol %s is used, but is not defined as a token and has no rules"),
223 this->class = nterm_sym
;
224 this->number
= nvars
++;
231 /*-------------------------------------------------------------------.
232 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
234 `-------------------------------------------------------------------*/
237 symbol_make_alias (symbol_t
*symbol
, symbol_t
*symval
, location_t loc
)
240 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
242 else if (symbol
->alias
)
243 warn_at (loc
, _("symbol `%s' given more than one literal string"),
247 symval
->class = token_sym
;
248 symval
->user_token_number
= symbol
->user_token_number
;
249 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
250 symval
->alias
= symbol
;
251 symbol
->alias
= symval
;
252 /* symbol and symval combined are only one symbol */
255 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
256 symbol
->number
= symval
->number
=
257 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
262 /*---------------------------------------------------------.
263 | Check that THIS, and its alias, have same precedence and |
265 `---------------------------------------------------------*/
268 symbol_check_alias_consistence (symbol_t
*this)
270 /* Check only those who _are_ the aliases. */
271 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
273 if (this->prec
!= this->alias
->prec
)
275 if (this->prec
!= 0 && this->alias
->prec
!= 0)
276 complain_at (this->alias
->location
,
277 _("conflicting precedences for %s and %s"),
278 this->tag
, this->alias
->tag
);
280 this->alias
->prec
= this->prec
;
282 this->prec
= this->alias
->prec
;
285 if (this->assoc
!= this->alias
->assoc
)
287 if (this->assoc
!= undef_assoc
&& this->alias
->assoc
!= undef_assoc
)
288 complain_at (this->alias
->location
,
289 _("conflicting associativities for %s (%s) and %s (%s)"),
290 this->tag
, assoc_to_string (this->assoc
),
291 this->alias
->tag
, assoc_to_string (this->alias
->assoc
));
292 if (this->assoc
!= undef_assoc
)
293 this->alias
->assoc
= this->assoc
;
295 this->assoc
= this->alias
->assoc
;
302 /*-------------------------------------------------------------------.
303 | Assign a symbol number, and write the definition of the token name |
304 | into FDEFINES. Put in SYMBOLS. |
305 `-------------------------------------------------------------------*/
308 symbol_pack (symbol_t
*this)
310 if (this->class == nterm_sym
)
312 this->number
+= ntokens
;
314 else if (this->alias
)
316 /* This symbol and its alias are a single token defn.
317 Allocate a tokno, and assign to both check agreement of
318 prec and assoc fields and make both the same */
319 if (this->number
== NUMBER_UNDEFINED
)
321 if (this == endtoken
|| this->alias
== endtoken
)
322 this->number
= this->alias
->number
= 0;
325 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
326 this->number
= this->alias
->number
;
329 /* Do not do processing below for USER_NUMBER_ALIASes. */
330 if (this->user_token_number
== USER_NUMBER_ALIAS
)
333 else /* this->class == token_sym */
335 assert (this->number
!= NUMBER_UNDEFINED
);
338 symbols
[this->number
] = this;
345 /*--------------------------------------------------.
346 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
347 `--------------------------------------------------*/
350 symbol_translation (symbol_t
*this)
353 if (this->class == token_sym
354 && this->user_token_number
!= USER_NUMBER_ALIAS
)
356 /* A token which translation has already been set? */
357 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
358 complain_at (this->location
,
359 _("tokens %s and %s both assigned number %d"),
360 symbols
[token_translations
[this->user_token_number
]]->tag
,
361 this->tag
, this->user_token_number
);
363 token_translations
[this->user_token_number
] = this->number
;
370 /*----------------------.
371 | A symbol hash table. |
372 `----------------------*/
374 /* Initial capacity of symbols hash table. */
375 #define HT_INITIAL_CAPACITY 257
377 static struct hash_table
*symbol_table
= NULL
;
380 hash_compare_symbol (const symbol_t
*m1
, const symbol_t
*m2
)
382 /* Since tags are unique, we can compare the pointers themselves. */
383 return STRUNIQ_EQ (m1
->tag
, m2
->tag
);
387 hash_symbol (const symbol_t
*m
, unsigned int tablesize
)
389 /* Since tags are unique, we can hash the pointer itself. */
390 return ((size_t) m
->tag
) % tablesize
;
394 /*-------------------------------.
395 | Create the symbol hash table. |
396 `-------------------------------*/
401 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
403 (Hash_hasher
) hash_symbol
,
404 (Hash_comparator
) hash_compare_symbol
,
405 (Hash_data_freer
) symbol_free
);
409 /*----------------------------------------------------------------.
410 | Find the symbol named KEY, and return it. If it does not exist |
412 `----------------------------------------------------------------*/
415 symbol_get (const char *key
, location_t location
)
420 /* Keep the symbol in a printable form. */
421 key
= struniq_new (quotearg_style (escape_quoting_style
, key
));
422 *(char const **) &probe
.tag
= key
;
423 entry
= hash_lookup (symbol_table
, &probe
);
427 /* First insertion in the hash. */
428 entry
= symbol_new (key
, location
);
429 hash_insert (symbol_table
, entry
);
435 /*------------------------------------------------------------------.
436 | Generate a dummy nonterminal, whose name cannot conflict with the |
438 `------------------------------------------------------------------*/
441 dummy_symbol_get (location_t location
)
443 /* Incremented for each generated symbol. */
444 static int dummy_count
= 0;
445 static char buf
[256];
449 sprintf (buf
, "@%d", ++dummy_count
);
450 sym
= symbol_get (buf
, location
);
451 sym
->class = nterm_sym
;
452 sym
->number
= nvars
++;
457 /*-------------------.
458 | Free the symbols. |
459 `-------------------*/
464 hash_free (symbol_table
);
469 /*---------------------------------------------------------------.
470 | Look for undefined symbols, report an error, and consider them |
472 `---------------------------------------------------------------*/
475 symbols_do (symbol_processor processor
, void *processor_data
)
477 hash_do_for_each (symbol_table
,
478 (Hash_processor
) processor
,
483 /*--------------------------------------------------------------.
484 | Check that all the symbols are defined. Report any undefined |
485 | symbols and consider them nonterminals. |
486 `--------------------------------------------------------------*/
489 symbols_check_defined (void)
491 symbols_do (symbol_check_defined
, NULL
);
494 /*------------------------------------------------------------------.
495 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
497 `------------------------------------------------------------------*/
500 symbols_token_translations_init (void)
502 bool num_256_available_p
= true;
505 /* Find the highest user token number, and whether 256, the POSIX
506 preferred user token number for the error token, is used. */
507 max_user_token_number
= 0;
508 for (i
= 0; i
< ntokens
; ++i
)
510 symbol_t
*this = symbols
[i
];
511 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
513 if (this->user_token_number
> max_user_token_number
)
514 max_user_token_number
= this->user_token_number
;
515 if (this->user_token_number
== 256)
516 num_256_available_p
= false;
520 /* If 256 is not used, assign it to error, to follow POSIX. */
521 if (num_256_available_p
522 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
523 errtoken
->user_token_number
= 256;
525 /* Set the missing user numbers. */
526 if (max_user_token_number
< 256)
527 max_user_token_number
= 256;
529 for (i
= 0; i
< ntokens
; ++i
)
531 symbol_t
*this = symbols
[i
];
532 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
533 this->user_token_number
= ++max_user_token_number
;
534 if (this->user_token_number
> max_user_token_number
)
535 max_user_token_number
= this->user_token_number
;
538 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
540 /* Initialize all entries for literal tokens to 2, the internal
541 token number for $undefined, which represents all invalid inputs.
543 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
544 token_translations
[i
] = undeftoken
->number
;
545 symbols_do (symbol_translation
, NULL
);
549 /*----------------------------------------------------------------.
550 | Assign symbol numbers, and write definition of token names into |
551 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
552 `----------------------------------------------------------------*/
557 symbols
= XCALLOC (symbol_t
*, nsyms
);
559 symbols_do (symbol_check_alias_consistence
, NULL
);
560 symbols_do (symbol_pack
, NULL
);
562 symbols_token_translations_init ();
564 if (startsymbol
->class == unknown_sym
)
565 fatal_at (startsymbol_location
,
566 _("the start symbol %s is undefined"),
568 else if (startsymbol
->class == token_sym
)
569 fatal_at (startsymbol_location
,
570 _("the start symbol %s is a token"),