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. Does nothing if passed 0 |
123 `-------------------------------------------------------------------*/
126 symbol_destructor_set (symbol_t
*symbol
, location_t location
, char *destructor
)
130 if (symbol
->destructor
)
131 complain_at (location
,
132 _("destructor redeclaration for %s"),
133 symbol_tag_get (symbol
));
134 symbol
->destructor
= destructor
;
135 symbol
->destructor_location
= location
;
140 /*------------------------------------------------------------------.
141 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
142 | with UNDEF_ASSOC as ASSOC. |
143 `------------------------------------------------------------------*/
146 symbol_precedence_set (symbol_t
*symbol
, location_t location
,
147 int prec
, associativity assoc
)
149 if (assoc
!= undef_assoc
)
151 if (symbol
->prec
!= 0)
152 complain_at (location
,
153 _("redefining precedence of %s"),
154 symbol_tag_get (symbol
));
156 symbol
->assoc
= assoc
;
159 /* Only terminals have a precedence. */
160 symbol_class_set (symbol
, token_sym
);
164 /*-------------------------------------.
165 | Set the CLASS associated to SYMBOL. |
166 `-------------------------------------*/
169 symbol_class_set (symbol_t
*symbol
, symbol_class
class)
171 if (symbol
->class != unknown_sym
&& symbol
->class != class)
172 complain (_("symbol %s redefined"), symbol_tag_get (symbol
));
174 if (class == nterm_sym
&& symbol
->class != nterm_sym
)
175 symbol
->number
= nvars
++;
176 else if (class == token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
177 symbol
->number
= ntokens
++;
179 symbol
->class = class;
183 /*-------------------------------------------------.
184 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
185 `-------------------------------------------------*/
188 symbol_user_token_number_set (symbol_t
*symbol
, int user_token_number
)
190 assert (symbol
->class == token_sym
);
192 if (symbol
->user_token_number
!= USER_NUMBER_UNDEFINED
193 && symbol
->user_token_number
!= user_token_number
)
194 complain (_("redefining user token number of %s"),
195 symbol_tag_get (symbol
));
197 symbol
->user_token_number
= user_token_number
;
198 /* User defined EOF token? */
199 if (user_token_number
== 0)
202 eoftoken
->number
= 0;
203 /* It is always mapped to 0, so it was already counted in
215 symbol_free (symbol_t
*this)
218 /* This causes crashes because one string can appear more
220 XFREE (this->type_name
);
227 /*-----------------------------------------------------------.
228 | If THIS is not defined, report an error, and consider it a |
230 `-----------------------------------------------------------*/
233 symbol_check_defined (symbol_t
*this)
235 if (this->class == unknown_sym
)
239 _("symbol %s is used, but is not defined as a token and has no rules"),
240 symbol_tag_get (this));
241 this->class = nterm_sym
;
242 this->number
= nvars
++;
249 /*-------------------------------------------------------------------.
250 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
252 `-------------------------------------------------------------------*/
255 symbol_make_alias (symbol_t
*symbol
, symbol_t
*symval
)
258 warn (_("symbol `%s' used more than once as a literal string"),
259 symbol_tag_get (symval
));
260 else if (symbol
->alias
)
261 warn (_("symbol `%s' given more than one literal string"),
262 symbol_tag_get (symbol
));
265 symval
->class = token_sym
;
266 symval
->user_token_number
= symbol
->user_token_number
;
267 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
268 symval
->alias
= symbol
;
269 symbol
->alias
= symval
;
270 /* symbol and symval combined are only one symbol */
273 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
274 symbol
->number
= symval
->number
=
275 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
280 /*---------------------------------------------------------.
281 | Check that THIS, and its alias, have same precedence and |
283 `---------------------------------------------------------*/
286 symbol_check_alias_consistence (symbol_t
*this)
288 /* Check only those who _are_ the aliases. */
289 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
291 if (this->prec
!= this->alias
->prec
)
293 if (this->prec
!= 0 && this->alias
->prec
!= 0)
294 complain (_("conflicting precedences for %s and %s"),
295 symbol_tag_get (this), symbol_tag_get (this->alias
));
297 this->alias
->prec
= this->prec
;
299 this->prec
= this->alias
->prec
;
302 if (this->assoc
!= this->alias
->assoc
)
304 /* FIXME: For some reason (probably the S/R => keep the S),
305 the right assoc is chosen has the ``not set''. This is
306 not nice, fix this! */
307 if (this->assoc
!= right_assoc
308 && this->alias
->assoc
!= right_assoc
)
309 complain (_("conflicting associativities for %s and %s"),
310 symbol_tag_get (this), symbol_tag_get (this->alias
));
311 if (this->assoc
!= 0)
312 this->alias
->assoc
= this->assoc
;
314 this->assoc
= this->alias
->assoc
;
321 /*-------------------------------------------------------------------.
322 | Assign a symbol number, and write the definition of the token name |
323 | into FDEFINES. Put in SYMBOLS. |
324 `-------------------------------------------------------------------*/
327 symbol_pack (symbol_t
*this)
329 if (this->class == nterm_sym
)
331 this->number
+= ntokens
;
333 else if (this->alias
)
335 /* This symbol and its alias are a single token defn.
336 Allocate a tokno, and assign to both check agreement of
337 prec and assoc fields and make both the same */
338 if (this->number
== NUMBER_UNDEFINED
)
340 if (this == eoftoken
|| this->alias
== eoftoken
)
341 this->number
= this->alias
->number
= 0;
344 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
345 this->number
= this->alias
->number
;
348 /* Do not do processing below for USER_NUMBER_ALIASs. */
349 if (this->user_token_number
== USER_NUMBER_ALIAS
)
352 else /* this->class == token_sym */
354 assert (this->number
!= NUMBER_UNDEFINED
);
357 symbols
[this->number
] = this;
364 /*--------------------------------------------------.
365 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
366 `--------------------------------------------------*/
369 symbol_translation (symbol_t
*this)
372 if (this->class == token_sym
373 && this->user_token_number
!= USER_NUMBER_ALIAS
)
375 /* A token which translation has already been set? */
376 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
377 complain (_("tokens %s and %s both assigned number %d"),
378 symbol_tag_get (symbols
[token_translations
[this->user_token_number
]]),
379 symbol_tag_get (this), this->user_token_number
);
381 token_translations
[this->user_token_number
] = this->number
;
388 /*----------------------.
389 | A symbol hash table. |
390 `----------------------*/
392 /* Initial capacity of symbols hash table. */
393 #define HT_INITIAL_CAPACITY 257
395 static struct hash_table
*symbol_table
= NULL
;
398 hash_compare_symbol_t (const symbol_t
*m1
, const symbol_t
*m2
)
400 return strcmp (m1
->tag
, m2
->tag
) ? FALSE
: TRUE
;
404 hash_symbol_t (const symbol_t
*m
, unsigned int tablesize
)
406 return hash_string (m
->tag
, tablesize
);
410 /*-------------------------------.
411 | Create the symbol hash table. |
412 `-------------------------------*/
417 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
419 (Hash_hasher
) hash_symbol_t
,
420 (Hash_comparator
) hash_compare_symbol_t
,
421 (Hash_data_freer
) symbol_free
);
425 /*----------------------------------------------------------------.
426 | Find the symbol named KEY, and return it. If it does not exist |
428 `----------------------------------------------------------------*/
431 getsym (const char *key
, location_t location
)
436 (const char *) probe
.tag
= key
;
437 entry
= hash_lookup (symbol_table
, &probe
);
441 /* First insertion in the hash. */
442 entry
= symbol_new (key
, location
);
443 hash_insert (symbol_table
, entry
);
449 /*-------------------.
450 | Free the symbols. |
451 `-------------------*/
456 hash_free (symbol_table
);
460 /*---------------------------------------------------------------.
461 | Look for undefined symbols, report an error, and consider them |
463 `---------------------------------------------------------------*/
466 symbols_do (symbol_processor processor
, void *processor_data
)
468 hash_do_for_each (symbol_table
,
469 (Hash_processor
) processor
,
474 /*--------------------------------------------------------------.
475 | Check that all the symbols are defined. Report any undefined |
476 | symbols and consider them nonterminals. |
477 `--------------------------------------------------------------*/
480 symbols_check_defined (void)
482 symbols_do (symbol_check_defined
, NULL
);
485 /*------------------------------------------------------------------.
486 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
488 `------------------------------------------------------------------*/
491 symbols_token_translations_init (void)
493 int num_256_available_p
= TRUE
;
496 /* Find the highest user token number, and whether 256, the POSIX
497 preferred user token number for the error token, is used. */
498 max_user_token_number
= 0;
499 for (i
= 0; i
< ntokens
; ++i
)
501 symbol_t
*this = symbols
[i
];
502 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
504 if (this->user_token_number
> max_user_token_number
)
505 max_user_token_number
= this->user_token_number
;
506 if (this->user_token_number
== 256)
507 num_256_available_p
= FALSE
;
511 /* If 256 is not used, assign it to error, to follow POSIX. */
512 if (num_256_available_p
513 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
514 errtoken
->user_token_number
= 256;
516 /* Set the missing user numbers. */
517 if (max_user_token_number
< 256)
518 max_user_token_number
= 256;
520 for (i
= 0; i
< ntokens
; ++i
)
522 symbol_t
*this = symbols
[i
];
523 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
524 this->user_token_number
= ++max_user_token_number
;
525 if (this->user_token_number
> max_user_token_number
)
526 max_user_token_number
= this->user_token_number
;
529 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
531 /* Initialize all entries for literal tokens to 2, the internal
532 token number for $undefined., which represents all invalid
534 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
535 token_translations
[i
] = undeftoken
->number
;
536 symbols_do (symbol_translation
, NULL
);
540 /*----------------------------------------------------------------.
541 | Assign symbol numbers, and write definition of token names into |
542 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
543 `----------------------------------------------------------------*/
548 symbols
= XCALLOC (symbol_t
*, nsyms
);
550 symbols_do (symbol_check_alias_consistence
, NULL
);
551 symbols_do (symbol_pack
, NULL
);
553 symbols_token_translations_init ();
555 if (startsymbol
->class == unknown_sym
)
556 fatal_at (startsymbol_location
,
557 _("the start symbol %s is undefined"),
558 symbol_tag_get (startsymbol
));
559 else if (startsymbol
->class == token_sym
)
560 fatal_at (startsymbol_location
,
561 _("the start symbol %s is a token"),
562 symbol_tag_get (startsymbol
));