]>
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; |
24c0aad7 | 54 | |
5fbb0954 | 55 | res->number = NUMBER_UNDEFINED; |
1e9798d5 AD |
56 | res->prec = 0; |
57 | res->assoc = right_assoc; | |
b87f8b21 | 58 | res->user_token_number = USER_NUMBER_UNDEFINED; |
1e9798d5 AD |
59 | res->alias = NULL; |
60 | res->class = unknown_sym; | |
61 | ||
62 | nsyms++; | |
1e9798d5 AD |
63 | return res; |
64 | } | |
65 | ||
40675e7c | 66 | |
6b98e4b5 AD |
67 | /*-----------------------------------------------------------------. |
68 | | Return the tag of this SYMBOL in a printable form. Warning: use | | |
69 | | the first QUOTEARG slot: 0. | | |
70 | `-----------------------------------------------------------------*/ | |
71 | ||
72 | const char * | |
73 | symbol_tag_get (symbol_t *symbol) | |
74 | { | |
75 | return quotearg_style (escape_quoting_style, symbol->tag); | |
76 | } | |
77 | ||
78 | ||
79 | /*------------------------------------------------------------. | |
80 | | Return the tag of this SYMBOL in a printable form. Use the | | |
81 | | QUOTEARG slot number N. | | |
82 | `------------------------------------------------------------*/ | |
83 | ||
84 | const char * | |
85 | symbol_tag_get_n (symbol_t *symbol, int n) | |
86 | { | |
87 | return quotearg_n_style (n, escape_quoting_style, symbol->tag); | |
88 | } | |
89 | ||
90 | ||
91 | /*-------------------------------. | |
92 | | Print the tag of this SYMBOL. | | |
93 | `-------------------------------*/ | |
94 | ||
95 | void | |
96 | symbol_tag_print (symbol_t *symbol, FILE *out) | |
97 | { | |
98 | fputs (symbol_tag_get (symbol), out); | |
99 | } | |
100 | ||
101 | ||
e9955c83 AD |
102 | /*------------------------------------------------------------------. |
103 | | Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 | | |
104 | | as TYPE_NAME. | | |
105 | `------------------------------------------------------------------*/ | |
3ae2b51f AD |
106 | |
107 | void | |
1e0bab92 | 108 | symbol_type_set (symbol_t *symbol, location_t location, char *type_name) |
3ae2b51f | 109 | { |
e9955c83 AD |
110 | if (type_name) |
111 | { | |
112 | if (symbol->type_name) | |
1e0bab92 | 113 | complain_at (location, |
dafdc66f | 114 | _("type redeclaration for %s"), symbol_tag_get (symbol)); |
e9955c83 AD |
115 | symbol->type_name = type_name; |
116 | } | |
3ae2b51f AD |
117 | } |
118 | ||
119 | ||
9280d3ef AD |
120 | /*-------------------------------------------------------------------. |
121 | | Set the DESTRUCTOR associated to SYMBOL. Does nothing if passed 0 | | |
122 | | as DESTRUCTOR. | | |
123 | `-------------------------------------------------------------------*/ | |
124 | ||
125 | void | |
126 | symbol_destructor_set (symbol_t *symbol, location_t location, char *destructor) | |
127 | { | |
128 | if (destructor) | |
129 | { | |
130 | if (symbol->destructor) | |
131 | complain_at (location, | |
132 | _("destructor redeclaration for %s"), | |
133 | symbol_tag_get (symbol)); | |
134 | symbol->destructor = destructor; | |
24c0aad7 | 135 | symbol->destructor_location = location; |
9280d3ef AD |
136 | } |
137 | } | |
138 | ||
139 | ||
e9955c83 AD |
140 | /*------------------------------------------------------------------. |
141 | | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked | | |
142 | | with UNDEF_ASSOC as ASSOC. | | |
143 | `------------------------------------------------------------------*/ | |
3ae2b51f AD |
144 | |
145 | void | |
1e0bab92 | 146 | symbol_precedence_set (symbol_t *symbol, location_t location, |
3ae2b51f AD |
147 | int prec, associativity assoc) |
148 | { | |
e9955c83 AD |
149 | if (assoc != undef_assoc) |
150 | { | |
151 | if (symbol->prec != 0) | |
1e0bab92 | 152 | complain_at (location, |
dafdc66f AD |
153 | _("redefining precedence of %s"), |
154 | symbol_tag_get (symbol)); | |
e9955c83 AD |
155 | symbol->prec = prec; |
156 | symbol->assoc = assoc; | |
157 | } | |
158 | ||
159 | /* Only terminals have a precedence. */ | |
160 | symbol_class_set (symbol, token_sym); | |
3ae2b51f AD |
161 | } |
162 | ||
163 | ||
44536b35 AD |
164 | /*-------------------------------------. |
165 | | Set the CLASS associated to SYMBOL. | | |
166 | `-------------------------------------*/ | |
167 | ||
168 | void | |
169 | symbol_class_set (symbol_t *symbol, symbol_class class) | |
170 | { | |
171 | if (symbol->class != unknown_sym && symbol->class != class) | |
dafdc66f | 172 | complain (_("symbol %s redefined"), symbol_tag_get (symbol)); |
44536b35 AD |
173 | |
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++; | |
178 | ||
179 | symbol->class = class; | |
180 | } | |
181 | ||
182 | ||
183 | /*-------------------------------------------------. | |
184 | | Set the USER_TOKEN_NUMBER associated to SYMBOL. | | |
185 | `-------------------------------------------------*/ | |
186 | ||
187 | void | |
188 | symbol_user_token_number_set (symbol_t *symbol, int user_token_number) | |
189 | { | |
190 | assert (symbol->class == token_sym); | |
191 | ||
5e424082 AD |
192 | if (symbol->user_token_number != USER_NUMBER_UNDEFINED |
193 | && symbol->user_token_number != user_token_number) | |
dafdc66f AD |
194 | complain (_("redefining user token number of %s"), |
195 | symbol_tag_get (symbol)); | |
44536b35 AD |
196 | |
197 | symbol->user_token_number = user_token_number; | |
198 | /* User defined EOF token? */ | |
199 | if (user_token_number == 0) | |
200 | { | |
201 | eoftoken = symbol; | |
202 | eoftoken->number = 0; | |
203 | /* It is always mapped to 0, so it was already counted in | |
204 | NTOKENS. */ | |
205 | --ntokens; | |
206 | } | |
207 | } | |
208 | ||
209 | ||
72a23c97 AD |
210 | /*------------. |
211 | | Free THIS. | | |
212 | `------------*/ | |
213 | ||
214 | static void | |
db8837cb | 215 | symbol_free (symbol_t *this) |
40675e7c | 216 | { |
72a23c97 AD |
217 | #if 0 |
218 | /* This causes crashes because one string can appear more | |
219 | than once. */ | |
220 | XFREE (this->type_name); | |
221 | #endif | |
222 | XFREE (this->tag); | |
223 | XFREE (this); | |
224 | } | |
225 | ||
226 | ||
2f1afb73 AD |
227 | /*-----------------------------------------------------------. |
228 | | If THIS is not defined, report an error, and consider it a | | |
229 | | nonterminal. | | |
230 | `-----------------------------------------------------------*/ | |
231 | ||
232 | static bool | |
233 | symbol_check_defined (symbol_t *this) | |
234 | { | |
235 | if (this->class == unknown_sym) | |
236 | { | |
ee000ba4 AD |
237 | complain_at |
238 | (this->location, | |
239 | _("symbol %s is used, but is not defined as a token and has no rules"), | |
dafdc66f | 240 | symbol_tag_get (this)); |
2f1afb73 AD |
241 | this->class = nterm_sym; |
242 | this->number = nvars++; | |
243 | } | |
244 | ||
245 | return TRUE; | |
246 | } | |
247 | ||
248 | ||
249 | /*-------------------------------------------------------------------. | |
250 | | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them | | |
251 | | with TYPENAME. | | |
252 | `-------------------------------------------------------------------*/ | |
253 | ||
254 | void | |
e9955c83 | 255 | symbol_make_alias (symbol_t *symbol, symbol_t *symval) |
2f1afb73 AD |
256 | { |
257 | if (symval->alias) | |
258 | warn (_("symbol `%s' used more than once as a literal string"), | |
dafdc66f | 259 | symbol_tag_get (symval)); |
2f1afb73 AD |
260 | else if (symbol->alias) |
261 | warn (_("symbol `%s' given more than one literal string"), | |
dafdc66f | 262 | symbol_tag_get (symbol)); |
2f1afb73 AD |
263 | else |
264 | { | |
265 | symval->class = token_sym; | |
2f1afb73 AD |
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 */ | |
271 | nsyms--; | |
272 | ntokens--; | |
273 | assert (ntokens == symbol->number || ntokens == symval->number); | |
274 | symbol->number = symval->number = | |
275 | (symval->number < symbol->number) ? symval->number : symbol->number; | |
276 | } | |
277 | } | |
278 | ||
279 | ||
280 | /*---------------------------------------------------------. | |
281 | | Check that THIS, and its alias, have same precedence and | | |
282 | | associativity. | | |
283 | `---------------------------------------------------------*/ | |
284 | ||
285 | static bool | |
286 | symbol_check_alias_consistence (symbol_t *this) | |
287 | { | |
288 | /* Check only those who _are_ the aliases. */ | |
289 | if (this->alias && this->user_token_number == USER_NUMBER_ALIAS) | |
290 | { | |
291 | if (this->prec != this->alias->prec) | |
292 | { | |
293 | if (this->prec != 0 && this->alias->prec != 0) | |
294 | complain (_("conflicting precedences for %s and %s"), | |
dafdc66f | 295 | symbol_tag_get (this), symbol_tag_get (this->alias)); |
2f1afb73 AD |
296 | if (this->prec != 0) |
297 | this->alias->prec = this->prec; | |
298 | else | |
299 | this->prec = this->alias->prec; | |
300 | } | |
301 | ||
302 | if (this->assoc != this->alias->assoc) | |
303 | { | |
e9955c83 AD |
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"), | |
dafdc66f | 310 | symbol_tag_get (this), symbol_tag_get (this->alias)); |
2f1afb73 AD |
311 | if (this->assoc != 0) |
312 | this->alias->assoc = this->assoc; | |
313 | else | |
314 | this->assoc = this->alias->assoc; | |
315 | } | |
316 | } | |
317 | return TRUE; | |
318 | } | |
319 | ||
320 | ||
321 | /*-------------------------------------------------------------------. | |
322 | | Assign a symbol number, and write the definition of the token name | | |
323 | | into FDEFINES. Put in SYMBOLS. | | |
324 | `-------------------------------------------------------------------*/ | |
325 | ||
326 | static bool | |
327 | symbol_pack (symbol_t *this) | |
328 | { | |
329 | if (this->class == nterm_sym) | |
330 | { | |
331 | this->number += ntokens; | |
332 | } | |
333 | else if (this->alias) | |
334 | { | |
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) | |
339 | { | |
340 | if (this == eoftoken || this->alias == eoftoken) | |
341 | this->number = this->alias->number = 0; | |
342 | else | |
343 | { | |
344 | assert (this->alias->number != NUMBER_UNDEFINED); | |
345 | this->number = this->alias->number; | |
346 | } | |
347 | } | |
348 | /* Do not do processing below for USER_NUMBER_ALIASs. */ | |
349 | if (this->user_token_number == USER_NUMBER_ALIAS) | |
350 | return TRUE; | |
351 | } | |
352 | else /* this->class == token_sym */ | |
353 | { | |
354 | assert (this->number != NUMBER_UNDEFINED); | |
355 | } | |
356 | ||
357 | symbols[this->number] = this; | |
358 | return TRUE; | |
359 | } | |
360 | ||
361 | ||
362 | ||
363 | ||
364 | /*--------------------------------------------------. | |
365 | | Put THIS in TOKEN_TRANSLATIONS if it is a token. | | |
366 | `--------------------------------------------------*/ | |
367 | ||
368 | static bool | |
369 | symbol_translation (symbol_t *this) | |
370 | { | |
371 | /* Non-terminal? */ | |
372 | if (this->class == token_sym | |
373 | && this->user_token_number != USER_NUMBER_ALIAS) | |
374 | { | |
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"), | |
dafdc66f AD |
378 | symbol_tag_get (symbols[token_translations[this->user_token_number]]), |
379 | symbol_tag_get (this), this->user_token_number); | |
2f1afb73 AD |
380 | |
381 | token_translations[this->user_token_number] = this->number; | |
382 | } | |
383 | ||
384 | return TRUE; | |
385 | } | |
386 | ||
72a23c97 AD |
387 | |
388 | /*----------------------. | |
2f1afb73 | 389 | | A symbol hash table. | |
72a23c97 | 390 | `----------------------*/ |
40675e7c | 391 | |
db8837cb | 392 | /* Initial capacity of symbols hash table. */ |
72a23c97 AD |
393 | #define HT_INITIAL_CAPACITY 257 |
394 | ||
db8837cb | 395 | static struct hash_table *symbol_table = NULL; |
72a23c97 AD |
396 | |
397 | static bool | |
db8837cb | 398 | hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2) |
72a23c97 AD |
399 | { |
400 | return strcmp (m1->tag, m2->tag) ? FALSE : TRUE; | |
401 | } | |
402 | ||
403 | static unsigned int | |
db8837cb | 404 | hash_symbol_t (const symbol_t *m, unsigned int tablesize) |
72a23c97 AD |
405 | { |
406 | return hash_string (m->tag, tablesize); | |
407 | } | |
408 | ||
409 | ||
410 | /*-------------------------------. | |
2f1afb73 | 411 | | Create the symbol hash table. | |
72a23c97 AD |
412 | `-------------------------------*/ |
413 | ||
414 | void | |
db8837cb | 415 | symbols_new (void) |
72a23c97 | 416 | { |
db8837cb | 417 | symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
72a23c97 | 418 | NULL, |
db8837cb AD |
419 | (Hash_hasher) hash_symbol_t, |
420 | (Hash_comparator) hash_compare_symbol_t, | |
421 | (Hash_data_freer) symbol_free); | |
40675e7c DM |
422 | } |
423 | ||
424 | ||
1e9798d5 AD |
425 | /*----------------------------------------------------------------. |
426 | | Find the symbol named KEY, and return it. If it does not exist | | |
427 | | yet, create it. | | |
428 | `----------------------------------------------------------------*/ | |
429 | ||
db8837cb | 430 | symbol_t * |
ee000ba4 | 431 | getsym (const char *key, location_t location) |
40675e7c | 432 | { |
db8837cb AD |
433 | symbol_t probe; |
434 | symbol_t *entry; | |
40675e7c | 435 | |
72a23c97 | 436 | (const char *) probe.tag = key; |
db8837cb | 437 | entry = hash_lookup (symbol_table, &probe); |
40675e7c | 438 | |
72a23c97 | 439 | if (!entry) |
40675e7c | 440 | { |
72a23c97 | 441 | /* First insertion in the hash. */ |
ee000ba4 | 442 | entry = symbol_new (key, location); |
db8837cb | 443 | hash_insert (symbol_table, entry); |
40675e7c | 444 | } |
72a23c97 AD |
445 | return entry; |
446 | } | |
40675e7c | 447 | |
40675e7c | 448 | |
72a23c97 | 449 | /*-------------------. |
db8837cb | 450 | | Free the symbols. | |
72a23c97 AD |
451 | `-------------------*/ |
452 | ||
453 | void | |
db8837cb | 454 | symbols_free (void) |
72a23c97 | 455 | { |
db8837cb | 456 | hash_free (symbol_table); |
40675e7c DM |
457 | } |
458 | ||
459 | ||
72a23c97 | 460 | /*---------------------------------------------------------------. |
db8837cb | 461 | | Look for undefined symbols, report an error, and consider them | |
72a23c97 AD |
462 | | terminals. | |
463 | `---------------------------------------------------------------*/ | |
464 | ||
40675e7c | 465 | void |
db8837cb | 466 | symbols_do (symbol_processor processor, void *processor_data) |
40675e7c | 467 | { |
db8837cb | 468 | hash_do_for_each (symbol_table, |
72a23c97 AD |
469 | (Hash_processor) processor, |
470 | processor_data); | |
40675e7c | 471 | } |
2f1afb73 AD |
472 | |
473 | ||
474 | /*--------------------------------------------------------------. | |
475 | | Check that all the symbols are defined. Report any undefined | | |
476 | | symbols and consider them nonterminals. | | |
477 | `--------------------------------------------------------------*/ | |
478 | ||
479 | void | |
480 | symbols_check_defined (void) | |
481 | { | |
482 | symbols_do (symbol_check_defined, NULL); | |
483 | } | |
484 | ||
485 | /*------------------------------------------------------------------. | |
486 | | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same | | |
487 | | number. | | |
488 | `------------------------------------------------------------------*/ | |
489 | ||
490 | static void | |
491 | symbols_token_translations_init (void) | |
492 | { | |
493 | int num_256_available_p = TRUE; | |
494 | int i; | |
495 | ||
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) | |
500 | { | |
501 | symbol_t *this = symbols[i]; | |
502 | if (this->user_token_number != USER_NUMBER_UNDEFINED) | |
503 | { | |
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; | |
508 | } | |
509 | } | |
510 | ||
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; | |
515 | ||
516 | /* Set the missing user numbers. */ | |
517 | if (max_user_token_number < 256) | |
518 | max_user_token_number = 256; | |
519 | ||
520 | for (i = 0; i < ntokens; ++i) | |
521 | { | |
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; | |
527 | } | |
528 | ||
529 | token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1); | |
530 | ||
531 | /* Initialize all entries for literal tokens to 2, the internal | |
532 | token number for $undefined., which represents all invalid | |
533 | inputs. */ | |
534 | for (i = 0; i < max_user_token_number + 1; i++) | |
535 | token_translations[i] = undeftoken->number; | |
536 | symbols_do (symbol_translation, NULL); | |
537 | } | |
538 | ||
539 | ||
540 | /*----------------------------------------------------------------. | |
541 | | Assign symbol numbers, and write definition of token names into | | |
542 | | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. | | |
543 | `----------------------------------------------------------------*/ | |
544 | ||
545 | void | |
546 | symbols_pack (void) | |
547 | { | |
548 | symbols = XCALLOC (symbol_t *, nsyms); | |
549 | ||
550 | symbols_do (symbol_check_alias_consistence, NULL); | |
551 | symbols_do (symbol_pack, NULL); | |
552 | ||
553 | symbols_token_translations_init (); | |
554 | ||
555 | if (startsymbol->class == unknown_sym) | |
ee000ba4 | 556 | fatal_at (startsymbol_location, |
dafdc66f AD |
557 | _("the start symbol %s is undefined"), |
558 | symbol_tag_get (startsymbol)); | |
2f1afb73 | 559 | else if (startsymbol->class == token_sym) |
ee000ba4 | 560 | fatal_at (startsymbol_location, |
dafdc66f AD |
561 | _("the start symbol %s is a token"), |
562 | symbol_tag_get (startsymbol)); | |
2f1afb73 | 563 | } |