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
*eoftoken
= NULL
;
36 symbol_t
*axiom
= NULL
;
37 symbol_t
*startsymbol
= NULL
;
38 location_t startsymbol_location
;
40 /*---------------------------------.
41 | Create a new symbol, named TAG. |
42 `---------------------------------*/
45 symbol_new (const char *tag
, location_t location
)
47 symbol_t
*res
= XMALLOC (symbol_t
, 1);
49 res
->tag
= xstrdup (tag
);
50 res
->location
= location
;
52 res
->type_name
= NULL
;
53 res
->destructor
= NULL
;
56 res
->number
= NUMBER_UNDEFINED
;
58 res
->assoc
= right_assoc
;
59 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
62 res
->class = unknown_sym
;
69 /*------------------------------------------------------------------.
70 | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
72 `------------------------------------------------------------------*/
75 symbol_type_set (symbol_t
*symbol
, char *type_name
, location_t location
)
79 if (symbol
->type_name
)
80 complain_at (location
,
81 _("type redeclaration for %s"), symbol
->tag
);
82 symbol
->type_name
= type_name
;
87 /*-------------------------------------------------------------------.
88 | Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
89 `-------------------------------------------------------------------*/
92 symbol_destructor_set (symbol_t
*symbol
, char *destructor
, location_t location
)
96 if (symbol
->destructor
)
97 complain_at (location
,
98 _("%s redeclaration for %s"),
99 "%destructor", symbol
->tag
);
100 symbol
->destructor
= destructor
;
101 symbol
->destructor_location
= location
;
106 /*----------------------------------------------------------------.
107 | Set the PRITNER associated to SYMBOL. Do nothing if passed 0. |
108 `----------------------------------------------------------------*/
111 symbol_printer_set (symbol_t
*symbol
, char *printer
, location_t location
)
116 complain_at (location
,
117 _("%s redeclaration for %s"),
118 "%printer", symbol
->tag
);
119 symbol
->printer
= printer
;
120 symbol
->printer_location
= location
;
125 /*------------------------------------------------------------------.
126 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
127 | with UNDEF_ASSOC as ASSOC. |
128 `------------------------------------------------------------------*/
131 symbol_precedence_set (symbol_t
*symbol
,
132 int prec
, associativity assoc
, location_t location
)
134 if (assoc
!= undef_assoc
)
136 if (symbol
->prec
!= 0)
137 complain_at (location
,
138 _("redefining precedence of %s"),
141 symbol
->assoc
= assoc
;
144 /* Only terminals have a precedence. */
145 symbol_class_set (symbol
, token_sym
, location
);
149 /*-------------------------------------.
150 | Set the CLASS associated to SYMBOL. |
151 `-------------------------------------*/
154 symbol_class_set (symbol_t
*symbol
, symbol_class
class, location_t location
)
156 if (symbol
->class != unknown_sym
&& symbol
->class != class)
157 complain_at (location
, _("symbol %s redefined"), symbol
->tag
);
159 if (class == nterm_sym
&& symbol
->class != nterm_sym
)
160 symbol
->number
= nvars
++;
161 else if (class == token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
162 symbol
->number
= ntokens
++;
164 symbol
->class = class;
168 /*-------------------------------------------------.
169 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
170 `-------------------------------------------------*/
173 symbol_user_token_number_set (symbol_t
*symbol
,
174 int user_token_number
, location_t location
)
176 assert (symbol
->class == token_sym
);
178 if (symbol
->user_token_number
!= USER_NUMBER_UNDEFINED
179 && symbol
->user_token_number
!= user_token_number
)
180 complain_at (location
, _("redefining user token number of %s"),
183 symbol
->user_token_number
= user_token_number
;
184 /* User defined EOF token? */
185 if (user_token_number
== 0)
188 eoftoken
->number
= 0;
189 /* It is always mapped to 0, so it was already counted in
201 symbol_free (symbol_t
*this)
204 /* This causes crashes because one string can appear more
206 XFREE (this->type_name
);
213 /*-----------------------------------------------------------.
214 | If THIS is not defined, report an error, and consider it a |
216 `-----------------------------------------------------------*/
219 symbol_check_defined (symbol_t
*this)
221 if (this->class == unknown_sym
)
225 _("symbol %s is used, but is not defined as a token and has no rules"),
227 this->class = nterm_sym
;
228 this->number
= nvars
++;
235 /*-------------------------------------------------------------------.
236 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
238 `-------------------------------------------------------------------*/
241 symbol_make_alias (symbol_t
*symbol
, symbol_t
*symval
, location_t loc
)
244 warn_at (loc
, _("symbol `%s' used more than once as a literal string"),
246 else if (symbol
->alias
)
247 warn_at (loc
, _("symbol `%s' given more than one literal string"),
251 symval
->class = token_sym
;
252 symval
->user_token_number
= symbol
->user_token_number
;
253 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
254 symval
->alias
= symbol
;
255 symbol
->alias
= symval
;
256 /* symbol and symval combined are only one symbol */
259 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
260 symbol
->number
= symval
->number
=
261 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
266 /*---------------------------------------------------------.
267 | Check that THIS, and its alias, have same precedence and |
269 `---------------------------------------------------------*/
272 symbol_check_alias_consistence (symbol_t
*this)
274 /* Check only those who _are_ the aliases. */
275 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
277 if (this->prec
!= this->alias
->prec
)
279 if (this->prec
!= 0 && this->alias
->prec
!= 0)
280 complain_at (this->alias
->location
,
281 _("conflicting precedences for %s and %s"),
282 this->tag
, this->alias
->tag
);
284 this->alias
->prec
= this->prec
;
286 this->prec
= this->alias
->prec
;
289 if (this->assoc
!= this->alias
->assoc
)
291 /* FIXME: For some reason (probably the S/R => keep the S),
292 the right assoc is chosen has the ``not set''. This is
293 not nice, fix this! */
294 if (this->assoc
!= right_assoc
295 && this->alias
->assoc
!= right_assoc
)
296 complain_at (this->alias
->location
,
297 _("conflicting associativities for %s and %s"),
298 this->tag
, this->alias
->tag
);
299 if (this->assoc
!= 0)
300 this->alias
->assoc
= this->assoc
;
302 this->assoc
= this->alias
->assoc
;
309 /*-------------------------------------------------------------------.
310 | Assign a symbol number, and write the definition of the token name |
311 | into FDEFINES. Put in SYMBOLS. |
312 `-------------------------------------------------------------------*/
315 symbol_pack (symbol_t
*this)
317 if (this->class == nterm_sym
)
319 this->number
+= ntokens
;
321 else if (this->alias
)
323 /* This symbol and its alias are a single token defn.
324 Allocate a tokno, and assign to both check agreement of
325 prec and assoc fields and make both the same */
326 if (this->number
== NUMBER_UNDEFINED
)
328 if (this == eoftoken
|| this->alias
== eoftoken
)
329 this->number
= this->alias
->number
= 0;
332 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
333 this->number
= this->alias
->number
;
336 /* Do not do processing below for USER_NUMBER_ALIASs. */
337 if (this->user_token_number
== USER_NUMBER_ALIAS
)
340 else /* this->class == token_sym */
342 assert (this->number
!= NUMBER_UNDEFINED
);
345 symbols
[this->number
] = this;
352 /*--------------------------------------------------.
353 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
354 `--------------------------------------------------*/
357 symbol_translation (symbol_t
*this)
360 if (this->class == token_sym
361 && this->user_token_number
!= USER_NUMBER_ALIAS
)
363 /* A token which translation has already been set? */
364 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
365 complain_at (this->location
,
366 _("tokens %s and %s both assigned number %d"),
367 symbols
[token_translations
[this->user_token_number
]]->tag
,
368 this->tag
, this->user_token_number
);
370 token_translations
[this->user_token_number
] = this->number
;
377 /*----------------------.
378 | A symbol hash table. |
379 `----------------------*/
381 /* Initial capacity of symbols hash table. */
382 #define HT_INITIAL_CAPACITY 257
384 static struct hash_table
*symbol_table
= NULL
;
387 hash_compare_symbol_t (const symbol_t
*m1
, const symbol_t
*m2
)
389 return strcmp (m1
->tag
, m2
->tag
) ? FALSE
: TRUE
;
393 hash_symbol_t (const symbol_t
*m
, unsigned int tablesize
)
395 return hash_string (m
->tag
, tablesize
);
399 /*-------------------------------.
400 | Create the symbol hash table. |
401 `-------------------------------*/
406 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
408 (Hash_hasher
) hash_symbol_t
,
409 (Hash_comparator
) hash_compare_symbol_t
,
410 (Hash_data_freer
) symbol_free
);
414 /*----------------------------------------------------------------.
415 | Find the symbol named KEY, and return it. If it does not exist |
417 `----------------------------------------------------------------*/
420 symbol_get (const char *key
, location_t location
)
425 /* Keep the symbol in a printable form. */
426 key
= quotearg_style (escape_quoting_style
, key
);
427 (const char *) probe
.tag
= key
;
428 entry
= hash_lookup (symbol_table
, &probe
);
432 /* First insertion in the hash. */
433 entry
= symbol_new (key
, location
);
434 hash_insert (symbol_table
, entry
);
440 /*------------------------------------------------------------------.
441 | Generate a dummy nonterminal, whose name cannot conflict with the |
443 `------------------------------------------------------------------*/
446 dummy_symbol_get (location_t location
)
448 /* Incremented for each generated symbol. */
449 static int dummy_count
= 0;
450 static char buf
[256];
454 sprintf (buf
, "@%d", ++dummy_count
);
455 sym
= symbol_get (buf
, location
);
456 sym
->class = nterm_sym
;
457 sym
->number
= nvars
++;
462 /*-------------------.
463 | Free the symbols. |
464 `-------------------*/
469 hash_free (symbol_table
);
474 /*---------------------------------------------------------------.
475 | Look for undefined symbols, report an error, and consider them |
477 `---------------------------------------------------------------*/
480 symbols_do (symbol_processor processor
, void *processor_data
)
482 hash_do_for_each (symbol_table
,
483 (Hash_processor
) processor
,
488 /*--------------------------------------------------------------.
489 | Check that all the symbols are defined. Report any undefined |
490 | symbols and consider them nonterminals. |
491 `--------------------------------------------------------------*/
494 symbols_check_defined (void)
496 symbols_do (symbol_check_defined
, NULL
);
499 /*------------------------------------------------------------------.
500 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
502 `------------------------------------------------------------------*/
505 symbols_token_translations_init (void)
507 int num_256_available_p
= TRUE
;
510 /* Find the highest user token number, and whether 256, the POSIX
511 preferred user token number for the error token, is used. */
512 max_user_token_number
= 0;
513 for (i
= 0; i
< ntokens
; ++i
)
515 symbol_t
*this = symbols
[i
];
516 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
518 if (this->user_token_number
> max_user_token_number
)
519 max_user_token_number
= this->user_token_number
;
520 if (this->user_token_number
== 256)
521 num_256_available_p
= FALSE
;
525 /* If 256 is not used, assign it to error, to follow POSIX. */
526 if (num_256_available_p
527 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
528 errtoken
->user_token_number
= 256;
530 /* Set the missing user numbers. */
531 if (max_user_token_number
< 256)
532 max_user_token_number
= 256;
534 for (i
= 0; i
< ntokens
; ++i
)
536 symbol_t
*this = symbols
[i
];
537 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
538 this->user_token_number
= ++max_user_token_number
;
539 if (this->user_token_number
> max_user_token_number
)
540 max_user_token_number
= this->user_token_number
;
543 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
545 /* Initialize all entries for literal tokens to 2, the internal
546 token number for $undefined., which represents all invalid
548 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
549 token_translations
[i
] = undeftoken
->number
;
550 symbols_do (symbol_translation
, NULL
);
554 /*----------------------------------------------------------------.
555 | Assign symbol numbers, and write definition of token names into |
556 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
557 `----------------------------------------------------------------*/
562 symbols
= XCALLOC (symbol_t
*, nsyms
);
564 symbols_do (symbol_check_alias_consistence
, NULL
);
565 symbols_do (symbol_pack
, NULL
);
567 symbols_token_translations_init ();
569 if (startsymbol
->class == unknown_sym
)
570 fatal_at (startsymbol_location
,
571 _("the start symbol %s is undefined"),
573 else if (startsymbol
->class == token_sym
)
574 fatal_at (startsymbol_location
,
575 _("the start symbol %s is a token"),