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