]>
Commit | Line | Data |
---|---|---|
40675e7c | 1 | /* Symbol table manager for Bison, |
72a23c97 | 2 | Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
40675e7c | 3 | |
95e36146 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
40675e7c | 5 | |
95e36146 AD |
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) | |
9 | any later version. | |
40675e7c | 10 | |
95e36146 AD |
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. | |
40675e7c | 15 | |
95e36146 AD |
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. */ | |
40675e7c DM |
20 | |
21 | ||
40675e7c | 22 | #include "system.h" |
6b98e4b5 | 23 | #include "quotearg.h" |
72a23c97 | 24 | #include "hash.h" |
2f1afb73 | 25 | #include "complain.h" |
40675e7c DM |
26 | #include "symtab.h" |
27 | #include "gram.h" | |
28 | ||
2f1afb73 AD |
29 | /*------------------------. |
30 | | Distinguished symbols. | | |
31 | `------------------------*/ | |
32 | ||
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; | |
8efe435c | 38 | location_t startsymbol_location; |
2f1afb73 | 39 | |
72a23c97 AD |
40 | /*---------------------------------. |
41 | | Create a new symbol, named TAG. | | |
42 | `---------------------------------*/ | |
1e9798d5 | 43 | |
db8837cb | 44 | static symbol_t * |
ee000ba4 | 45 | symbol_new (const char *tag, location_t location) |
1e9798d5 | 46 | { |
db8837cb | 47 | symbol_t *res = XMALLOC (symbol_t, 1); |
1e9798d5 | 48 | |
1e9798d5 | 49 | res->tag = xstrdup (tag); |
24c0aad7 AD |
50 | res->location = location; |
51 | ||
1e9798d5 | 52 | res->type_name = NULL; |
9280d3ef | 53 | res->destructor = NULL; |
260008e5 | 54 | res->printer = NULL; |
24c0aad7 | 55 | |
5fbb0954 | 56 | res->number = NUMBER_UNDEFINED; |
1e9798d5 AD |
57 | res->prec = 0; |
58 | res->assoc = right_assoc; | |
b87f8b21 | 59 | res->user_token_number = USER_NUMBER_UNDEFINED; |
260008e5 | 60 | |
1e9798d5 AD |
61 | res->alias = NULL; |
62 | res->class = unknown_sym; | |
63 | ||
64 | nsyms++; | |
1e9798d5 AD |
65 | return res; |
66 | } | |
67 | ||
40675e7c | 68 | |
e9955c83 AD |
69 | /*------------------------------------------------------------------. |
70 | | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 | | |
71 | | as TYPE_NAME. | | |
72 | `------------------------------------------------------------------*/ | |
3ae2b51f AD |
73 | |
74 | void | |
1a31ed21 | 75 | symbol_type_set (symbol_t *symbol, char *type_name, location_t location) |
3ae2b51f | 76 | { |
e9955c83 AD |
77 | if (type_name) |
78 | { | |
79 | if (symbol->type_name) | |
1e0bab92 | 80 | complain_at (location, |
97650f4e | 81 | _("type redeclaration for %s"), symbol->tag); |
e9955c83 AD |
82 | symbol->type_name = type_name; |
83 | } | |
3ae2b51f AD |
84 | } |
85 | ||
86 | ||
9280d3ef | 87 | /*-------------------------------------------------------------------. |
366eea36 | 88 | | Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. | |
9280d3ef AD |
89 | `-------------------------------------------------------------------*/ |
90 | ||
91 | void | |
1a31ed21 | 92 | symbol_destructor_set (symbol_t *symbol, char *destructor, location_t location) |
9280d3ef AD |
93 | { |
94 | if (destructor) | |
95 | { | |
96 | if (symbol->destructor) | |
97 | complain_at (location, | |
366eea36 | 98 | _("%s redeclaration for %s"), |
97650f4e | 99 | "%destructor", symbol->tag); |
9280d3ef | 100 | symbol->destructor = destructor; |
24c0aad7 | 101 | symbol->destructor_location = location; |
9280d3ef AD |
102 | } |
103 | } | |
104 | ||
105 | ||
366eea36 AD |
106 | /*----------------------------------------------------------------. |
107 | | Set the PRITNER associated to SYMBOL. Do nothing if passed 0. | | |
108 | `----------------------------------------------------------------*/ | |
109 | ||
110 | void | |
111 | symbol_printer_set (symbol_t *symbol, char *printer, location_t location) | |
112 | { | |
113 | if (printer) | |
114 | { | |
115 | if (symbol->printer) | |
116 | complain_at (location, | |
117 | _("%s redeclaration for %s"), | |
97650f4e | 118 | "%printer", symbol->tag); |
366eea36 AD |
119 | symbol->printer = printer; |
120 | symbol->printer_location = location; | |
121 | } | |
122 | } | |
123 | ||
124 | ||
e9955c83 AD |
125 | /*------------------------------------------------------------------. |
126 | | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked | | |
127 | | with UNDEF_ASSOC as ASSOC. | | |
128 | `------------------------------------------------------------------*/ | |
3ae2b51f AD |
129 | |
130 | void | |
1a31ed21 AD |
131 | symbol_precedence_set (symbol_t *symbol, |
132 | int prec, associativity assoc, location_t location) | |
3ae2b51f | 133 | { |
e9955c83 AD |
134 | if (assoc != undef_assoc) |
135 | { | |
136 | if (symbol->prec != 0) | |
1e0bab92 | 137 | complain_at (location, |
dafdc66f | 138 | _("redefining precedence of %s"), |
97650f4e | 139 | symbol->tag); |
e9955c83 AD |
140 | symbol->prec = prec; |
141 | symbol->assoc = assoc; | |
142 | } | |
143 | ||
144 | /* Only terminals have a precedence. */ | |
e776192e | 145 | symbol_class_set (symbol, token_sym, location); |
3ae2b51f AD |
146 | } |
147 | ||
148 | ||
44536b35 AD |
149 | /*-------------------------------------. |
150 | | Set the CLASS associated to SYMBOL. | | |
151 | `-------------------------------------*/ | |
152 | ||
153 | void | |
e776192e | 154 | symbol_class_set (symbol_t *symbol, symbol_class class, location_t location) |
44536b35 AD |
155 | { |
156 | if (symbol->class != unknown_sym && symbol->class != class) | |
97650f4e | 157 | complain_at (location, _("symbol %s redefined"), symbol->tag); |
44536b35 AD |
158 | |
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++; | |
163 | ||
164 | symbol->class = class; | |
165 | } | |
166 | ||
167 | ||
168 | /*-------------------------------------------------. | |
169 | | Set the USER_TOKEN_NUMBER associated to SYMBOL. | | |
170 | `-------------------------------------------------*/ | |
171 | ||
172 | void | |
e776192e AD |
173 | symbol_user_token_number_set (symbol_t *symbol, |
174 | int user_token_number, location_t location) | |
44536b35 AD |
175 | { |
176 | assert (symbol->class == token_sym); | |
177 | ||
5e424082 AD |
178 | if (symbol->user_token_number != USER_NUMBER_UNDEFINED |
179 | && symbol->user_token_number != user_token_number) | |
e776192e | 180 | complain_at (location, _("redefining user token number of %s"), |
97650f4e | 181 | symbol->tag); |
44536b35 AD |
182 | |
183 | symbol->user_token_number = user_token_number; | |
184 | /* User defined EOF token? */ | |
185 | if (user_token_number == 0) | |
186 | { | |
187 | eoftoken = symbol; | |
188 | eoftoken->number = 0; | |
189 | /* It is always mapped to 0, so it was already counted in | |
190 | NTOKENS. */ | |
191 | --ntokens; | |
192 | } | |
193 | } | |
194 | ||
195 | ||
72a23c97 AD |
196 | /*------------. |
197 | | Free THIS. | | |
198 | `------------*/ | |
199 | ||
200 | static void | |
db8837cb | 201 | symbol_free (symbol_t *this) |
40675e7c | 202 | { |
72a23c97 AD |
203 | #if 0 |
204 | /* This causes crashes because one string can appear more | |
205 | than once. */ | |
206 | XFREE (this->type_name); | |
207 | #endif | |
208 | XFREE (this->tag); | |
209 | XFREE (this); | |
210 | } | |
211 | ||
212 | ||
2f1afb73 AD |
213 | /*-----------------------------------------------------------. |
214 | | If THIS is not defined, report an error, and consider it a | | |
215 | | nonterminal. | | |
216 | `-----------------------------------------------------------*/ | |
217 | ||
218 | static bool | |
219 | symbol_check_defined (symbol_t *this) | |
220 | { | |
221 | if (this->class == unknown_sym) | |
222 | { | |
ee000ba4 AD |
223 | complain_at |
224 | (this->location, | |
225 | _("symbol %s is used, but is not defined as a token and has no rules"), | |
97650f4e | 226 | this->tag); |
2f1afb73 AD |
227 | this->class = nterm_sym; |
228 | this->number = nvars++; | |
229 | } | |
230 | ||
231 | return TRUE; | |
232 | } | |
233 | ||
234 | ||
235 | /*-------------------------------------------------------------------. | |
236 | | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them | | |
237 | | with TYPENAME. | | |
238 | `-------------------------------------------------------------------*/ | |
239 | ||
240 | void | |
e9955c83 | 241 | symbol_make_alias (symbol_t *symbol, symbol_t *symval) |
2f1afb73 AD |
242 | { |
243 | if (symval->alias) | |
244 | warn (_("symbol `%s' used more than once as a literal string"), | |
97650f4e | 245 | symval->tag); |
2f1afb73 AD |
246 | else if (symbol->alias) |
247 | warn (_("symbol `%s' given more than one literal string"), | |
97650f4e | 248 | symbol->tag); |
2f1afb73 AD |
249 | else |
250 | { | |
251 | symval->class = token_sym; | |
2f1afb73 AD |
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 */ | |
257 | nsyms--; | |
258 | ntokens--; | |
259 | assert (ntokens == symbol->number || ntokens == symval->number); | |
260 | symbol->number = symval->number = | |
261 | (symval->number < symbol->number) ? symval->number : symbol->number; | |
262 | } | |
263 | } | |
264 | ||
265 | ||
266 | /*---------------------------------------------------------. | |
267 | | Check that THIS, and its alias, have same precedence and | | |
268 | | associativity. | | |
269 | `---------------------------------------------------------*/ | |
270 | ||
271 | static bool | |
272 | symbol_check_alias_consistence (symbol_t *this) | |
273 | { | |
274 | /* Check only those who _are_ the aliases. */ | |
275 | if (this->alias && this->user_token_number == USER_NUMBER_ALIAS) | |
276 | { | |
277 | if (this->prec != this->alias->prec) | |
278 | { | |
279 | if (this->prec != 0 && this->alias->prec != 0) | |
280 | complain (_("conflicting precedences for %s and %s"), | |
97650f4e | 281 | this->tag, this->alias->tag); |
2f1afb73 AD |
282 | if (this->prec != 0) |
283 | this->alias->prec = this->prec; | |
284 | else | |
285 | this->prec = this->alias->prec; | |
286 | } | |
287 | ||
288 | if (this->assoc != this->alias->assoc) | |
289 | { | |
e9955c83 AD |
290 | /* FIXME: For some reason (probably the S/R => keep the S), |
291 | the right assoc is chosen has the ``not set''. This is | |
292 | not nice, fix this! */ | |
293 | if (this->assoc != right_assoc | |
294 | && this->alias->assoc != right_assoc) | |
295 | complain (_("conflicting associativities for %s and %s"), | |
97650f4e | 296 | this->tag, this->alias->tag); |
2f1afb73 AD |
297 | if (this->assoc != 0) |
298 | this->alias->assoc = this->assoc; | |
299 | else | |
300 | this->assoc = this->alias->assoc; | |
301 | } | |
302 | } | |
303 | return TRUE; | |
304 | } | |
305 | ||
306 | ||
307 | /*-------------------------------------------------------------------. | |
308 | | Assign a symbol number, and write the definition of the token name | | |
309 | | into FDEFINES. Put in SYMBOLS. | | |
310 | `-------------------------------------------------------------------*/ | |
311 | ||
312 | static bool | |
313 | symbol_pack (symbol_t *this) | |
314 | { | |
315 | if (this->class == nterm_sym) | |
316 | { | |
317 | this->number += ntokens; | |
318 | } | |
319 | else if (this->alias) | |
320 | { | |
321 | /* This symbol and its alias are a single token defn. | |
322 | Allocate a tokno, and assign to both check agreement of | |
323 | prec and assoc fields and make both the same */ | |
324 | if (this->number == NUMBER_UNDEFINED) | |
325 | { | |
326 | if (this == eoftoken || this->alias == eoftoken) | |
327 | this->number = this->alias->number = 0; | |
328 | else | |
329 | { | |
330 | assert (this->alias->number != NUMBER_UNDEFINED); | |
331 | this->number = this->alias->number; | |
332 | } | |
333 | } | |
334 | /* Do not do processing below for USER_NUMBER_ALIASs. */ | |
335 | if (this->user_token_number == USER_NUMBER_ALIAS) | |
336 | return TRUE; | |
337 | } | |
338 | else /* this->class == token_sym */ | |
339 | { | |
340 | assert (this->number != NUMBER_UNDEFINED); | |
341 | } | |
342 | ||
343 | symbols[this->number] = this; | |
344 | return TRUE; | |
345 | } | |
346 | ||
347 | ||
348 | ||
349 | ||
350 | /*--------------------------------------------------. | |
351 | | Put THIS in TOKEN_TRANSLATIONS if it is a token. | | |
352 | `--------------------------------------------------*/ | |
353 | ||
354 | static bool | |
355 | symbol_translation (symbol_t *this) | |
356 | { | |
357 | /* Non-terminal? */ | |
358 | if (this->class == token_sym | |
359 | && this->user_token_number != USER_NUMBER_ALIAS) | |
360 | { | |
361 | /* A token which translation has already been set? */ | |
362 | if (token_translations[this->user_token_number] != undeftoken->number) | |
363 | complain (_("tokens %s and %s both assigned number %d"), | |
97650f4e AD |
364 | symbols[token_translations[this->user_token_number]]->tag, |
365 | this->tag, this->user_token_number); | |
2f1afb73 AD |
366 | |
367 | token_translations[this->user_token_number] = this->number; | |
368 | } | |
369 | ||
370 | return TRUE; | |
371 | } | |
372 | ||
72a23c97 AD |
373 | |
374 | /*----------------------. | |
2f1afb73 | 375 | | A symbol hash table. | |
72a23c97 | 376 | `----------------------*/ |
40675e7c | 377 | |
db8837cb | 378 | /* Initial capacity of symbols hash table. */ |
72a23c97 AD |
379 | #define HT_INITIAL_CAPACITY 257 |
380 | ||
db8837cb | 381 | static struct hash_table *symbol_table = NULL; |
72a23c97 AD |
382 | |
383 | static bool | |
db8837cb | 384 | hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2) |
72a23c97 AD |
385 | { |
386 | return strcmp (m1->tag, m2->tag) ? FALSE : TRUE; | |
387 | } | |
388 | ||
389 | static unsigned int | |
db8837cb | 390 | hash_symbol_t (const symbol_t *m, unsigned int tablesize) |
72a23c97 AD |
391 | { |
392 | return hash_string (m->tag, tablesize); | |
393 | } | |
394 | ||
395 | ||
396 | /*-------------------------------. | |
2f1afb73 | 397 | | Create the symbol hash table. | |
72a23c97 AD |
398 | `-------------------------------*/ |
399 | ||
400 | void | |
db8837cb | 401 | symbols_new (void) |
72a23c97 | 402 | { |
db8837cb | 403 | symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
72a23c97 | 404 | NULL, |
db8837cb AD |
405 | (Hash_hasher) hash_symbol_t, |
406 | (Hash_comparator) hash_compare_symbol_t, | |
407 | (Hash_data_freer) symbol_free); | |
40675e7c DM |
408 | } |
409 | ||
410 | ||
1e9798d5 AD |
411 | /*----------------------------------------------------------------. |
412 | | Find the symbol named KEY, and return it. If it does not exist | | |
413 | | yet, create it. | | |
414 | `----------------------------------------------------------------*/ | |
415 | ||
db8837cb | 416 | symbol_t * |
39f41916 | 417 | symbol_get (const char *key, location_t location) |
40675e7c | 418 | { |
db8837cb AD |
419 | symbol_t probe; |
420 | symbol_t *entry; | |
40675e7c | 421 | |
97650f4e AD |
422 | /* Keep the symbol in a printable form. */ |
423 | key = quotearg_style (escape_quoting_style, key); | |
72a23c97 | 424 | (const char *) probe.tag = key; |
db8837cb | 425 | entry = hash_lookup (symbol_table, &probe); |
40675e7c | 426 | |
72a23c97 | 427 | if (!entry) |
40675e7c | 428 | { |
72a23c97 | 429 | /* First insertion in the hash. */ |
ee000ba4 | 430 | entry = symbol_new (key, location); |
db8837cb | 431 | hash_insert (symbol_table, entry); |
40675e7c | 432 | } |
72a23c97 AD |
433 | return entry; |
434 | } | |
40675e7c | 435 | |
40675e7c | 436 | |
39f41916 AD |
437 | /*------------------------------------------------------------------. |
438 | | Generate a dummy nonterminal, whose name cannot conflict with the | | |
439 | | user's names. | | |
440 | `------------------------------------------------------------------*/ | |
441 | ||
442 | symbol_t * | |
443 | dummy_symbol_get (location_t location) | |
444 | { | |
445 | /* Incremented for each generated symbol. */ | |
446 | static int dummy_count = 0; | |
447 | static char buf[256]; | |
448 | ||
449 | symbol_t *sym; | |
450 | ||
451 | sprintf (buf, "@%d", ++dummy_count); | |
452 | sym = symbol_get (buf, location); | |
453 | sym->class = nterm_sym; | |
454 | sym->number = nvars++; | |
455 | return sym; | |
456 | } | |
457 | ||
458 | ||
72a23c97 | 459 | /*-------------------. |
db8837cb | 460 | | Free the symbols. | |
72a23c97 AD |
461 | `-------------------*/ |
462 | ||
463 | void | |
db8837cb | 464 | symbols_free (void) |
72a23c97 | 465 | { |
db8837cb | 466 | hash_free (symbol_table); |
536545f3 | 467 | free (symbols); |
40675e7c DM |
468 | } |
469 | ||
470 | ||
72a23c97 | 471 | /*---------------------------------------------------------------. |
db8837cb | 472 | | Look for undefined symbols, report an error, and consider them | |
72a23c97 AD |
473 | | terminals. | |
474 | `---------------------------------------------------------------*/ | |
475 | ||
40675e7c | 476 | void |
db8837cb | 477 | symbols_do (symbol_processor processor, void *processor_data) |
40675e7c | 478 | { |
db8837cb | 479 | hash_do_for_each (symbol_table, |
72a23c97 AD |
480 | (Hash_processor) processor, |
481 | processor_data); | |
40675e7c | 482 | } |
2f1afb73 AD |
483 | |
484 | ||
485 | /*--------------------------------------------------------------. | |
486 | | Check that all the symbols are defined. Report any undefined | | |
487 | | symbols and consider them nonterminals. | | |
488 | `--------------------------------------------------------------*/ | |
489 | ||
490 | void | |
491 | symbols_check_defined (void) | |
492 | { | |
493 | symbols_do (symbol_check_defined, NULL); | |
494 | } | |
495 | ||
496 | /*------------------------------------------------------------------. | |
497 | | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same | | |
498 | | number. | | |
499 | `------------------------------------------------------------------*/ | |
500 | ||
501 | static void | |
502 | symbols_token_translations_init (void) | |
503 | { | |
504 | int num_256_available_p = TRUE; | |
505 | int i; | |
506 | ||
507 | /* Find the highest user token number, and whether 256, the POSIX | |
508 | preferred user token number for the error token, is used. */ | |
509 | max_user_token_number = 0; | |
510 | for (i = 0; i < ntokens; ++i) | |
511 | { | |
512 | symbol_t *this = symbols[i]; | |
513 | if (this->user_token_number != USER_NUMBER_UNDEFINED) | |
514 | { | |
515 | if (this->user_token_number > max_user_token_number) | |
516 | max_user_token_number = this->user_token_number; | |
517 | if (this->user_token_number == 256) | |
518 | num_256_available_p = FALSE; | |
519 | } | |
520 | } | |
521 | ||
522 | /* If 256 is not used, assign it to error, to follow POSIX. */ | |
523 | if (num_256_available_p | |
524 | && errtoken->user_token_number == USER_NUMBER_UNDEFINED) | |
525 | errtoken->user_token_number = 256; | |
526 | ||
527 | /* Set the missing user numbers. */ | |
528 | if (max_user_token_number < 256) | |
529 | max_user_token_number = 256; | |
530 | ||
531 | for (i = 0; i < ntokens; ++i) | |
532 | { | |
533 | symbol_t *this = symbols[i]; | |
534 | if (this->user_token_number == USER_NUMBER_UNDEFINED) | |
535 | this->user_token_number = ++max_user_token_number; | |
536 | if (this->user_token_number > max_user_token_number) | |
537 | max_user_token_number = this->user_token_number; | |
538 | } | |
539 | ||
540 | token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1); | |
541 | ||
542 | /* Initialize all entries for literal tokens to 2, the internal | |
543 | token number for $undefined., which represents all invalid | |
544 | inputs. */ | |
545 | for (i = 0; i < max_user_token_number + 1; i++) | |
546 | token_translations[i] = undeftoken->number; | |
547 | symbols_do (symbol_translation, NULL); | |
548 | } | |
549 | ||
550 | ||
551 | /*----------------------------------------------------------------. | |
552 | | Assign symbol numbers, and write definition of token names into | | |
553 | | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. | | |
554 | `----------------------------------------------------------------*/ | |
555 | ||
556 | void | |
557 | symbols_pack (void) | |
558 | { | |
559 | symbols = XCALLOC (symbol_t *, nsyms); | |
560 | ||
561 | symbols_do (symbol_check_alias_consistence, NULL); | |
562 | symbols_do (symbol_pack, NULL); | |
563 | ||
564 | symbols_token_translations_init (); | |
565 | ||
566 | if (startsymbol->class == unknown_sym) | |
ee000ba4 | 567 | fatal_at (startsymbol_location, |
dafdc66f | 568 | _("the start symbol %s is undefined"), |
97650f4e | 569 | startsymbol->tag); |
2f1afb73 | 570 | else if (startsymbol->class == token_sym) |
ee000ba4 | 571 | fatal_at (startsymbol_location, |
dafdc66f | 572 | _("the start symbol %s is a token"), |
97650f4e | 573 | startsymbol->tag); |
2f1afb73 | 574 | } |