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
->type_name
= NULL
;
51 res
->location
= location
;
52 res
->number
= NUMBER_UNDEFINED
;
54 res
->assoc
= right_assoc
;
55 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
57 res
->class = unknown_sym
;
64 /*-----------------------------------------------------------------.
65 | Return the tag of this SYMBOL in a printable form. Warning: use |
66 | the first QUOTEARG slot: 0. |
67 `-----------------------------------------------------------------*/
70 symbol_tag_get (symbol_t
*symbol
)
72 return quotearg_style (escape_quoting_style
, symbol
->tag
);
76 /*------------------------------------------------------------.
77 | Return the tag of this SYMBOL in a printable form. Use the |
78 | QUOTEARG slot number N. |
79 `------------------------------------------------------------*/
82 symbol_tag_get_n (symbol_t
*symbol
, int n
)
84 return quotearg_n_style (n
, escape_quoting_style
, symbol
->tag
);
88 /*-------------------------------.
89 | Print the tag of this SYMBOL. |
90 `-------------------------------*/
93 symbol_tag_print (symbol_t
*symbol
, FILE *out
)
95 fputs (symbol_tag_get (symbol
), out
);
99 /*------------------------------------------------------------------.
100 | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
102 `------------------------------------------------------------------*/
105 symbol_type_set (symbol_t
*symbol
, char *type_name
)
109 if (symbol
->type_name
)
110 complain (_("type redeclaration for %s"), symbol
->tag
);
111 symbol
->type_name
= type_name
;
116 /*------------------------------------------------------------------.
117 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
118 | with UNDEF_ASSOC as ASSOC. |
119 `------------------------------------------------------------------*/
122 symbol_precedence_set (symbol_t
*symbol
,
123 int prec
, associativity assoc
)
125 if (assoc
!= undef_assoc
)
127 if (symbol
->prec
!= 0)
128 complain (_("redefining precedence of %s"), symbol
->tag
);
130 symbol
->assoc
= assoc
;
133 /* Only terminals have a precedence. */
134 symbol_class_set (symbol
, token_sym
);
138 /*-------------------------------------.
139 | Set the CLASS associated to SYMBOL. |
140 `-------------------------------------*/
143 symbol_class_set (symbol_t
*symbol
, symbol_class
class)
145 if (symbol
->class != unknown_sym
&& symbol
->class != class)
146 complain (_("symbol %s redefined"), symbol
->tag
);
148 if (class == nterm_sym
&& symbol
->class != nterm_sym
)
149 symbol
->number
= nvars
++;
150 else if (class == token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
151 symbol
->number
= ntokens
++;
153 symbol
->class = class;
157 /*-------------------------------------------------.
158 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
159 `-------------------------------------------------*/
162 symbol_user_token_number_set (symbol_t
*symbol
, int user_token_number
)
164 assert (symbol
->class == token_sym
);
166 if (symbol
->user_token_number
!= USER_NUMBER_UNDEFINED
167 && symbol
->user_token_number
!= user_token_number
)
168 complain (_("redefining user token number of %s"), symbol
->tag
);
170 symbol
->user_token_number
= user_token_number
;
171 /* User defined EOF token? */
172 if (user_token_number
== 0)
175 eoftoken
->number
= 0;
176 /* It is always mapped to 0, so it was already counted in
188 symbol_free (symbol_t
*this)
191 /* This causes crashes because one string can appear more
193 XFREE (this->type_name
);
200 /*-----------------------------------------------------------.
201 | If THIS is not defined, report an error, and consider it a |
203 `-----------------------------------------------------------*/
206 symbol_check_defined (symbol_t
*this)
208 if (this->class == unknown_sym
)
212 _("symbol %s is used, but is not defined as a token and has no rules"),
214 this->class = nterm_sym
;
215 this->number
= nvars
++;
222 /*-------------------------------------------------------------------.
223 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
225 `-------------------------------------------------------------------*/
228 symbol_make_alias (symbol_t
*symbol
, symbol_t
*symval
)
231 warn (_("symbol `%s' used more than once as a literal string"),
233 else if (symbol
->alias
)
234 warn (_("symbol `%s' given more than one literal string"),
238 symval
->class = token_sym
;
239 symval
->user_token_number
= symbol
->user_token_number
;
240 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
241 symval
->alias
= symbol
;
242 symbol
->alias
= symval
;
243 /* symbol and symval combined are only one symbol */
246 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
247 symbol
->number
= symval
->number
=
248 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
253 /*---------------------------------------------------------.
254 | Check that THIS, and its alias, have same precedence and |
256 `---------------------------------------------------------*/
259 symbol_check_alias_consistence (symbol_t
*this)
261 /* Check only those who _are_ the aliases. */
262 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
264 if (this->prec
!= this->alias
->prec
)
266 if (this->prec
!= 0 && this->alias
->prec
!= 0)
267 complain (_("conflicting precedences for %s and %s"),
268 this->tag
, this->alias
->tag
);
270 this->alias
->prec
= this->prec
;
272 this->prec
= this->alias
->prec
;
275 if (this->assoc
!= this->alias
->assoc
)
277 /* FIXME: For some reason (probably the S/R => keep the S),
278 the right assoc is chosen has the ``not set''. This is
279 not nice, fix this! */
280 if (this->assoc
!= right_assoc
281 && this->alias
->assoc
!= right_assoc
)
282 complain (_("conflicting associativities for %s and %s"),
283 this->tag
, this->alias
->tag
);
284 if (this->assoc
!= 0)
285 this->alias
->assoc
= this->assoc
;
287 this->assoc
= this->alias
->assoc
;
294 /*-------------------------------------------------------------------.
295 | Assign a symbol number, and write the definition of the token name |
296 | into FDEFINES. Put in SYMBOLS. |
297 `-------------------------------------------------------------------*/
300 symbol_pack (symbol_t
*this)
302 if (this->class == nterm_sym
)
304 this->number
+= ntokens
;
306 else if (this->alias
)
308 /* This symbol and its alias are a single token defn.
309 Allocate a tokno, and assign to both check agreement of
310 prec and assoc fields and make both the same */
311 if (this->number
== NUMBER_UNDEFINED
)
313 if (this == eoftoken
|| this->alias
== eoftoken
)
314 this->number
= this->alias
->number
= 0;
317 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
318 this->number
= this->alias
->number
;
321 /* Do not do processing below for USER_NUMBER_ALIASs. */
322 if (this->user_token_number
== USER_NUMBER_ALIAS
)
325 else /* this->class == token_sym */
327 assert (this->number
!= NUMBER_UNDEFINED
);
330 symbols
[this->number
] = this;
337 /*--------------------------------------------------.
338 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
339 `--------------------------------------------------*/
342 symbol_translation (symbol_t
*this)
345 if (this->class == token_sym
346 && this->user_token_number
!= USER_NUMBER_ALIAS
)
348 /* A token which translation has already been set? */
349 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
350 complain (_("tokens %s and %s both assigned number %d"),
351 symbols
[token_translations
[this->user_token_number
]]->tag
,
352 this->tag
, this->user_token_number
);
354 token_translations
[this->user_token_number
] = this->number
;
361 /*----------------------.
362 | A symbol hash table. |
363 `----------------------*/
365 /* Initial capacity of symbols hash table. */
366 #define HT_INITIAL_CAPACITY 257
368 static struct hash_table
*symbol_table
= NULL
;
371 hash_compare_symbol_t (const symbol_t
*m1
, const symbol_t
*m2
)
373 return strcmp (m1
->tag
, m2
->tag
) ? FALSE
: TRUE
;
377 hash_symbol_t (const symbol_t
*m
, unsigned int tablesize
)
379 return hash_string (m
->tag
, tablesize
);
383 /*-------------------------------.
384 | Create the symbol hash table. |
385 `-------------------------------*/
390 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
392 (Hash_hasher
) hash_symbol_t
,
393 (Hash_comparator
) hash_compare_symbol_t
,
394 (Hash_data_freer
) symbol_free
);
398 /*----------------------------------------------------------------.
399 | Find the symbol named KEY, and return it. If it does not exist |
401 `----------------------------------------------------------------*/
404 getsym (const char *key
, location_t location
)
409 (const char *) probe
.tag
= key
;
410 entry
= hash_lookup (symbol_table
, &probe
);
414 /* First insertion in the hash. */
415 entry
= symbol_new (key
, location
);
416 hash_insert (symbol_table
, entry
);
422 /*-------------------.
423 | Free the symbols. |
424 `-------------------*/
429 hash_free (symbol_table
);
433 /*---------------------------------------------------------------.
434 | Look for undefined symbols, report an error, and consider them |
436 `---------------------------------------------------------------*/
439 symbols_do (symbol_processor processor
, void *processor_data
)
441 hash_do_for_each (symbol_table
,
442 (Hash_processor
) processor
,
447 /*--------------------------------------------------------------.
448 | Check that all the symbols are defined. Report any undefined |
449 | symbols and consider them nonterminals. |
450 `--------------------------------------------------------------*/
453 symbols_check_defined (void)
455 symbols_do (symbol_check_defined
, NULL
);
458 /*------------------------------------------------------------------.
459 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
461 `------------------------------------------------------------------*/
464 symbols_token_translations_init (void)
466 int num_256_available_p
= TRUE
;
469 /* Find the highest user token number, and whether 256, the POSIX
470 preferred user token number for the error token, is used. */
471 max_user_token_number
= 0;
472 for (i
= 0; i
< ntokens
; ++i
)
474 symbol_t
*this = symbols
[i
];
475 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
477 if (this->user_token_number
> max_user_token_number
)
478 max_user_token_number
= this->user_token_number
;
479 if (this->user_token_number
== 256)
480 num_256_available_p
= FALSE
;
484 /* If 256 is not used, assign it to error, to follow POSIX. */
485 if (num_256_available_p
486 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
487 errtoken
->user_token_number
= 256;
489 /* Set the missing user numbers. */
490 if (max_user_token_number
< 256)
491 max_user_token_number
= 256;
493 for (i
= 0; i
< ntokens
; ++i
)
495 symbol_t
*this = symbols
[i
];
496 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
497 this->user_token_number
= ++max_user_token_number
;
498 if (this->user_token_number
> max_user_token_number
)
499 max_user_token_number
= this->user_token_number
;
502 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
504 /* Initialize all entries for literal tokens to 2, the internal
505 token number for $undefined., which represents all invalid
507 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
508 token_translations
[i
] = undeftoken
->number
;
509 symbols_do (symbol_translation
, NULL
);
513 /*----------------------------------------------------------------.
514 | Assign symbol numbers, and write definition of token names into |
515 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
516 `----------------------------------------------------------------*/
521 symbols
= XCALLOC (symbol_t
*, nsyms
);
523 symbols_do (symbol_check_alias_consistence
, NULL
);
524 symbols_do (symbol_pack
, NULL
);
526 symbols_token_translations_init ();
528 if (startsymbol
->class == unknown_sym
)
529 fatal_at (startsymbol_location
,
530 _("the start symbol %s is undefined"), startsymbol
->tag
);
531 else if (startsymbol
->class == token_sym
)
532 fatal_at (startsymbol_location
,
533 _("the start symbol %s is a token"), startsymbol
->tag
);