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
;
55 res
->number
= NUMBER_UNDEFINED
;
57 res
->assoc
= right_assoc
;
58 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
60 res
->class = unknown_sym
;
67 /*-----------------------------------------------------------------.
68 | Return the tag of this SYMBOL in a printable form. Warning: use |
69 | the first QUOTEARG slot: 0. |
70 `-----------------------------------------------------------------*/
73 symbol_tag_get (symbol_t
*symbol
)
75 return quotearg_style (escape_quoting_style
, symbol
->tag
);
79 /*------------------------------------------------------------.
80 | Return the tag of this SYMBOL in a printable form. Use the |
81 | QUOTEARG slot number N. |
82 `------------------------------------------------------------*/
85 symbol_tag_get_n (symbol_t
*symbol
, int n
)
87 return quotearg_n_style (n
, escape_quoting_style
, symbol
->tag
);
91 /*-------------------------------.
92 | Print the tag of this SYMBOL. |
93 `-------------------------------*/
96 symbol_tag_print (symbol_t
*symbol
, FILE *out
)
98 fputs (symbol_tag_get (symbol
), out
);
102 /*------------------------------------------------------------------.
103 | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
105 `------------------------------------------------------------------*/
108 symbol_type_set (symbol_t
*symbol
, location_t location
, char *type_name
)
112 if (symbol
->type_name
)
113 complain_at (location
,
114 _("type redeclaration for %s"), symbol_tag_get (symbol
));
115 symbol
->type_name
= type_name
;
120 /*-------------------------------------------------------------------.
121 | Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
122 `-------------------------------------------------------------------*/
125 symbol_destructor_set (symbol_t
*symbol
, location_t location
, char *destructor
)
129 if (symbol
->destructor
)
130 complain_at (location
,
131 _("%s redeclaration for %s"),
132 "%destructor", symbol_tag_get (symbol
));
133 symbol
->destructor
= destructor
;
134 symbol
->destructor_location
= location
;
139 /*----------------------------------------------------------------.
140 | Set the PRITNER associated to SYMBOL. Do nothing if passed 0. |
141 `----------------------------------------------------------------*/
144 symbol_printer_set (symbol_t
*symbol
, char *printer
, location_t location
)
149 complain_at (location
,
150 _("%s redeclaration for %s"),
151 "%printer", symbol_tag_get (symbol
));
152 symbol
->printer
= printer
;
153 symbol
->printer_location
= location
;
158 /*------------------------------------------------------------------.
159 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
160 | with UNDEF_ASSOC as ASSOC. |
161 `------------------------------------------------------------------*/
164 symbol_precedence_set (symbol_t
*symbol
, location_t location
,
165 int prec
, associativity assoc
)
167 if (assoc
!= undef_assoc
)
169 if (symbol
->prec
!= 0)
170 complain_at (location
,
171 _("redefining precedence of %s"),
172 symbol_tag_get (symbol
));
174 symbol
->assoc
= assoc
;
177 /* Only terminals have a precedence. */
178 symbol_class_set (symbol
, token_sym
);
182 /*-------------------------------------.
183 | Set the CLASS associated to SYMBOL. |
184 `-------------------------------------*/
187 symbol_class_set (symbol_t
*symbol
, symbol_class
class)
189 if (symbol
->class != unknown_sym
&& symbol
->class != class)
190 complain (_("symbol %s redefined"), symbol_tag_get (symbol
));
192 if (class == nterm_sym
&& symbol
->class != nterm_sym
)
193 symbol
->number
= nvars
++;
194 else if (class == token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
195 symbol
->number
= ntokens
++;
197 symbol
->class = class;
201 /*-------------------------------------------------.
202 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
203 `-------------------------------------------------*/
206 symbol_user_token_number_set (symbol_t
*symbol
, int user_token_number
)
208 assert (symbol
->class == token_sym
);
210 if (symbol
->user_token_number
!= USER_NUMBER_UNDEFINED
211 && symbol
->user_token_number
!= user_token_number
)
212 complain (_("redefining user token number of %s"),
213 symbol_tag_get (symbol
));
215 symbol
->user_token_number
= user_token_number
;
216 /* User defined EOF token? */
217 if (user_token_number
== 0)
220 eoftoken
->number
= 0;
221 /* It is always mapped to 0, so it was already counted in
233 symbol_free (symbol_t
*this)
236 /* This causes crashes because one string can appear more
238 XFREE (this->type_name
);
245 /*-----------------------------------------------------------.
246 | If THIS is not defined, report an error, and consider it a |
248 `-----------------------------------------------------------*/
251 symbol_check_defined (symbol_t
*this)
253 if (this->class == unknown_sym
)
257 _("symbol %s is used, but is not defined as a token and has no rules"),
258 symbol_tag_get (this));
259 this->class = nterm_sym
;
260 this->number
= nvars
++;
267 /*-------------------------------------------------------------------.
268 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
270 `-------------------------------------------------------------------*/
273 symbol_make_alias (symbol_t
*symbol
, symbol_t
*symval
)
276 warn (_("symbol `%s' used more than once as a literal string"),
277 symbol_tag_get (symval
));
278 else if (symbol
->alias
)
279 warn (_("symbol `%s' given more than one literal string"),
280 symbol_tag_get (symbol
));
283 symval
->class = token_sym
;
284 symval
->user_token_number
= symbol
->user_token_number
;
285 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
286 symval
->alias
= symbol
;
287 symbol
->alias
= symval
;
288 /* symbol and symval combined are only one symbol */
291 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
292 symbol
->number
= symval
->number
=
293 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
298 /*---------------------------------------------------------.
299 | Check that THIS, and its alias, have same precedence and |
301 `---------------------------------------------------------*/
304 symbol_check_alias_consistence (symbol_t
*this)
306 /* Check only those who _are_ the aliases. */
307 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
309 if (this->prec
!= this->alias
->prec
)
311 if (this->prec
!= 0 && this->alias
->prec
!= 0)
312 complain (_("conflicting precedences for %s and %s"),
313 symbol_tag_get (this), symbol_tag_get (this->alias
));
315 this->alias
->prec
= this->prec
;
317 this->prec
= this->alias
->prec
;
320 if (this->assoc
!= this->alias
->assoc
)
322 /* FIXME: For some reason (probably the S/R => keep the S),
323 the right assoc is chosen has the ``not set''. This is
324 not nice, fix this! */
325 if (this->assoc
!= right_assoc
326 && this->alias
->assoc
!= right_assoc
)
327 complain (_("conflicting associativities for %s and %s"),
328 symbol_tag_get (this), symbol_tag_get (this->alias
));
329 if (this->assoc
!= 0)
330 this->alias
->assoc
= this->assoc
;
332 this->assoc
= this->alias
->assoc
;
339 /*-------------------------------------------------------------------.
340 | Assign a symbol number, and write the definition of the token name |
341 | into FDEFINES. Put in SYMBOLS. |
342 `-------------------------------------------------------------------*/
345 symbol_pack (symbol_t
*this)
347 if (this->class == nterm_sym
)
349 this->number
+= ntokens
;
351 else if (this->alias
)
353 /* This symbol and its alias are a single token defn.
354 Allocate a tokno, and assign to both check agreement of
355 prec and assoc fields and make both the same */
356 if (this->number
== NUMBER_UNDEFINED
)
358 if (this == eoftoken
|| this->alias
== eoftoken
)
359 this->number
= this->alias
->number
= 0;
362 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
363 this->number
= this->alias
->number
;
366 /* Do not do processing below for USER_NUMBER_ALIASs. */
367 if (this->user_token_number
== USER_NUMBER_ALIAS
)
370 else /* this->class == token_sym */
372 assert (this->number
!= NUMBER_UNDEFINED
);
375 symbols
[this->number
] = this;
382 /*--------------------------------------------------.
383 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
384 `--------------------------------------------------*/
387 symbol_translation (symbol_t
*this)
390 if (this->class == token_sym
391 && this->user_token_number
!= USER_NUMBER_ALIAS
)
393 /* A token which translation has already been set? */
394 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
395 complain (_("tokens %s and %s both assigned number %d"),
396 symbol_tag_get (symbols
[token_translations
[this->user_token_number
]]),
397 symbol_tag_get (this), this->user_token_number
);
399 token_translations
[this->user_token_number
] = this->number
;
406 /*----------------------.
407 | A symbol hash table. |
408 `----------------------*/
410 /* Initial capacity of symbols hash table. */
411 #define HT_INITIAL_CAPACITY 257
413 static struct hash_table
*symbol_table
= NULL
;
416 hash_compare_symbol_t (const symbol_t
*m1
, const symbol_t
*m2
)
418 return strcmp (m1
->tag
, m2
->tag
) ? FALSE
: TRUE
;
422 hash_symbol_t (const symbol_t
*m
, unsigned int tablesize
)
424 return hash_string (m
->tag
, tablesize
);
428 /*-------------------------------.
429 | Create the symbol hash table. |
430 `-------------------------------*/
435 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
437 (Hash_hasher
) hash_symbol_t
,
438 (Hash_comparator
) hash_compare_symbol_t
,
439 (Hash_data_freer
) symbol_free
);
443 /*----------------------------------------------------------------.
444 | Find the symbol named KEY, and return it. If it does not exist |
446 `----------------------------------------------------------------*/
449 getsym (const char *key
, location_t location
)
454 (const char *) probe
.tag
= key
;
455 entry
= hash_lookup (symbol_table
, &probe
);
459 /* First insertion in the hash. */
460 entry
= symbol_new (key
, location
);
461 hash_insert (symbol_table
, entry
);
467 /*-------------------.
468 | Free the symbols. |
469 `-------------------*/
474 hash_free (symbol_table
);
478 /*---------------------------------------------------------------.
479 | Look for undefined symbols, report an error, and consider them |
481 `---------------------------------------------------------------*/
484 symbols_do (symbol_processor processor
, void *processor_data
)
486 hash_do_for_each (symbol_table
,
487 (Hash_processor
) processor
,
492 /*--------------------------------------------------------------.
493 | Check that all the symbols are defined. Report any undefined |
494 | symbols and consider them nonterminals. |
495 `--------------------------------------------------------------*/
498 symbols_check_defined (void)
500 symbols_do (symbol_check_defined
, NULL
);
503 /*------------------------------------------------------------------.
504 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
506 `------------------------------------------------------------------*/
509 symbols_token_translations_init (void)
511 int num_256_available_p
= TRUE
;
514 /* Find the highest user token number, and whether 256, the POSIX
515 preferred user token number for the error token, is used. */
516 max_user_token_number
= 0;
517 for (i
= 0; i
< ntokens
; ++i
)
519 symbol_t
*this = symbols
[i
];
520 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
522 if (this->user_token_number
> max_user_token_number
)
523 max_user_token_number
= this->user_token_number
;
524 if (this->user_token_number
== 256)
525 num_256_available_p
= FALSE
;
529 /* If 256 is not used, assign it to error, to follow POSIX. */
530 if (num_256_available_p
531 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
532 errtoken
->user_token_number
= 256;
534 /* Set the missing user numbers. */
535 if (max_user_token_number
< 256)
536 max_user_token_number
= 256;
538 for (i
= 0; i
< ntokens
; ++i
)
540 symbol_t
*this = symbols
[i
];
541 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
542 this->user_token_number
= ++max_user_token_number
;
543 if (this->user_token_number
> max_user_token_number
)
544 max_user_token_number
= this->user_token_number
;
547 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
549 /* Initialize all entries for literal tokens to 2, the internal
550 token number for $undefined., which represents all invalid
552 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
553 token_translations
[i
] = undeftoken
->number
;
554 symbols_do (symbol_translation
, NULL
);
558 /*----------------------------------------------------------------.
559 | Assign symbol numbers, and write definition of token names into |
560 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
561 `----------------------------------------------------------------*/
566 symbols
= XCALLOC (symbol_t
*, nsyms
);
568 symbols_do (symbol_check_alias_consistence
, NULL
);
569 symbols_do (symbol_pack
, NULL
);
571 symbols_token_translations_init ();
573 if (startsymbol
->class == unknown_sym
)
574 fatal_at (startsymbol_location
,
575 _("the start symbol %s is undefined"),
576 symbol_tag_get (startsymbol
));
577 else if (startsymbol
->class == token_sym
)
578 fatal_at (startsymbol_location
,
579 _("the start symbol %s is a token"),
580 symbol_tag_get (startsymbol
));