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