]> git.saurik.com Git - bison.git/blame - src/symtab.c
* src/LR0.c (save_reductions): Remove, replaced by...
[bison.git] / src / symtab.c
CommitLineData
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
33symbol_t *errtoken = NULL;
34symbol_t *undeftoken = NULL;
35symbol_t *eoftoken = NULL;
36symbol_t *axiom = NULL;
37symbol_t *startsymbol = NULL;
8efe435c 38location_t startsymbol_location;
2f1afb73 39
72a23c97
AD
40/*---------------------------------.
41| Create a new symbol, named TAG. |
42`---------------------------------*/
1e9798d5 43
db8837cb 44static symbol_t *
ee000ba4 45symbol_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
72const char *
73symbol_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
84const char *
85symbol_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
95void
96symbol_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
107void
1a31ed21 108symbol_type_set (symbol_t *symbol, char *type_name, location_t location)
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 120/*-------------------------------------------------------------------.
366eea36 121| Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
9280d3ef
AD
122`-------------------------------------------------------------------*/
123
124void
1a31ed21 125symbol_destructor_set (symbol_t *symbol, char *destructor, location_t location)
9280d3ef
AD
126{
127 if (destructor)
128 {
129 if (symbol->destructor)
130 complain_at (location,
366eea36
AD
131 _("%s redeclaration for %s"),
132 "%destructor", symbol_tag_get (symbol));
9280d3ef 133 symbol->destructor = destructor;
24c0aad7 134 symbol->destructor_location = location;
9280d3ef
AD
135 }
136}
137
138
366eea36
AD
139/*----------------------------------------------------------------.
140| Set the PRITNER associated to SYMBOL. Do nothing if passed 0. |
141`----------------------------------------------------------------*/
142
143void
144symbol_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
e9955c83
AD
158/*------------------------------------------------------------------.
159| Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
160| with UNDEF_ASSOC as ASSOC. |
161`------------------------------------------------------------------*/
3ae2b51f
AD
162
163void
1a31ed21
AD
164symbol_precedence_set (symbol_t *symbol,
165 int prec, associativity assoc, location_t location)
3ae2b51f 166{
e9955c83
AD
167 if (assoc != undef_assoc)
168 {
169 if (symbol->prec != 0)
1e0bab92 170 complain_at (location,
dafdc66f
AD
171 _("redefining precedence of %s"),
172 symbol_tag_get (symbol));
e9955c83
AD
173 symbol->prec = prec;
174 symbol->assoc = assoc;
175 }
176
177 /* Only terminals have a precedence. */
e776192e 178 symbol_class_set (symbol, token_sym, location);
3ae2b51f
AD
179}
180
181
44536b35
AD
182/*-------------------------------------.
183| Set the CLASS associated to SYMBOL. |
184`-------------------------------------*/
185
186void
e776192e 187symbol_class_set (symbol_t *symbol, symbol_class class, location_t location)
44536b35
AD
188{
189 if (symbol->class != unknown_sym && symbol->class != class)
e776192e 190 complain_at (location, _("symbol %s redefined"), symbol_tag_get (symbol));
44536b35
AD
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
205void
e776192e
AD
206symbol_user_token_number_set (symbol_t *symbol,
207 int user_token_number, location_t location)
44536b35
AD
208{
209 assert (symbol->class == token_sym);
210
5e424082
AD
211 if (symbol->user_token_number != USER_NUMBER_UNDEFINED
212 && symbol->user_token_number != user_token_number)
e776192e
AD
213 complain_at (location, _("redefining user token number of %s"),
214 symbol_tag_get (symbol));
44536b35
AD
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
72a23c97
AD
229/*------------.
230| Free THIS. |
231`------------*/
232
233static void
db8837cb 234symbol_free (symbol_t *this)
40675e7c 235{
72a23c97
AD
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
2f1afb73
AD
246/*-----------------------------------------------------------.
247| If THIS is not defined, report an error, and consider it a |
248| nonterminal. |
249`-----------------------------------------------------------*/
250
251static bool
252symbol_check_defined (symbol_t *this)
253{
254 if (this->class == unknown_sym)
255 {
ee000ba4
AD
256 complain_at
257 (this->location,
258 _("symbol %s is used, but is not defined as a token and has no rules"),
dafdc66f 259 symbol_tag_get (this));
2f1afb73
AD
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
273void
e9955c83 274symbol_make_alias (symbol_t *symbol, symbol_t *symval)
2f1afb73
AD
275{
276 if (symval->alias)
277 warn (_("symbol `%s' used more than once as a literal string"),
dafdc66f 278 symbol_tag_get (symval));
2f1afb73
AD
279 else if (symbol->alias)
280 warn (_("symbol `%s' given more than one literal string"),
dafdc66f 281 symbol_tag_get (symbol));
2f1afb73
AD
282 else
283 {
284 symval->class = token_sym;
2f1afb73
AD
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
304static bool
305symbol_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"),
dafdc66f 314 symbol_tag_get (this), symbol_tag_get (this->alias));
2f1afb73
AD
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 {
e9955c83
AD
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"),
dafdc66f 329 symbol_tag_get (this), symbol_tag_get (this->alias));
2f1afb73
AD
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
345static bool
346symbol_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
387static bool
388symbol_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"),
dafdc66f
AD
397 symbol_tag_get (symbols[token_translations[this->user_token_number]]),
398 symbol_tag_get (this), this->user_token_number);
2f1afb73
AD
399
400 token_translations[this->user_token_number] = this->number;
401 }
402
403 return TRUE;
404}
405
72a23c97
AD
406
407/*----------------------.
2f1afb73 408| A symbol hash table. |
72a23c97 409`----------------------*/
40675e7c 410
db8837cb 411/* Initial capacity of symbols hash table. */
72a23c97
AD
412#define HT_INITIAL_CAPACITY 257
413
db8837cb 414static struct hash_table *symbol_table = NULL;
72a23c97
AD
415
416static bool
db8837cb 417hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
72a23c97
AD
418{
419 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
420}
421
422static unsigned int
db8837cb 423hash_symbol_t (const symbol_t *m, unsigned int tablesize)
72a23c97
AD
424{
425 return hash_string (m->tag, tablesize);
426}
427
428
429/*-------------------------------.
2f1afb73 430| Create the symbol hash table. |
72a23c97
AD
431`-------------------------------*/
432
433void
db8837cb 434symbols_new (void)
72a23c97 435{
db8837cb 436 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
72a23c97 437 NULL,
db8837cb
AD
438 (Hash_hasher) hash_symbol_t,
439 (Hash_comparator) hash_compare_symbol_t,
440 (Hash_data_freer) symbol_free);
40675e7c
DM
441}
442
443
1e9798d5
AD
444/*----------------------------------------------------------------.
445| Find the symbol named KEY, and return it. If it does not exist |
446| yet, create it. |
447`----------------------------------------------------------------*/
448
db8837cb 449symbol_t *
39f41916 450symbol_get (const char *key, location_t location)
40675e7c 451{
db8837cb
AD
452 symbol_t probe;
453 symbol_t *entry;
40675e7c 454
72a23c97 455 (const char *) probe.tag = key;
db8837cb 456 entry = hash_lookup (symbol_table, &probe);
40675e7c 457
72a23c97 458 if (!entry)
40675e7c 459 {
72a23c97 460 /* First insertion in the hash. */
ee000ba4 461 entry = symbol_new (key, location);
db8837cb 462 hash_insert (symbol_table, entry);
40675e7c 463 }
72a23c97
AD
464 return entry;
465}
40675e7c 466
40675e7c 467
39f41916
AD
468/*------------------------------------------------------------------.
469| Generate a dummy nonterminal, whose name cannot conflict with the |
470| user's names. |
471`------------------------------------------------------------------*/
472
473symbol_t *
474dummy_symbol_get (location_t location)
475{
476 /* Incremented for each generated symbol. */
477 static int dummy_count = 0;
478 static char buf[256];
479
480 symbol_t *sym;
481
482 sprintf (buf, "@%d", ++dummy_count);
483 sym = symbol_get (buf, location);
484 sym->class = nterm_sym;
485 sym->number = nvars++;
486 return sym;
487}
488
489
72a23c97 490/*-------------------.
db8837cb 491| Free the symbols. |
72a23c97
AD
492`-------------------*/
493
494void
db8837cb 495symbols_free (void)
72a23c97 496{
db8837cb 497 hash_free (symbol_table);
40675e7c
DM
498}
499
500
72a23c97 501/*---------------------------------------------------------------.
db8837cb 502| Look for undefined symbols, report an error, and consider them |
72a23c97
AD
503| terminals. |
504`---------------------------------------------------------------*/
505
40675e7c 506void
db8837cb 507symbols_do (symbol_processor processor, void *processor_data)
40675e7c 508{
db8837cb 509 hash_do_for_each (symbol_table,
72a23c97
AD
510 (Hash_processor) processor,
511 processor_data);
40675e7c 512}
2f1afb73
AD
513
514
515/*--------------------------------------------------------------.
516| Check that all the symbols are defined. Report any undefined |
517| symbols and consider them nonterminals. |
518`--------------------------------------------------------------*/
519
520void
521symbols_check_defined (void)
522{
523 symbols_do (symbol_check_defined, NULL);
524}
525
526/*------------------------------------------------------------------.
527| Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
528| number. |
529`------------------------------------------------------------------*/
530
531static void
532symbols_token_translations_init (void)
533{
534 int num_256_available_p = TRUE;
535 int i;
536
537 /* Find the highest user token number, and whether 256, the POSIX
538 preferred user token number for the error token, is used. */
539 max_user_token_number = 0;
540 for (i = 0; i < ntokens; ++i)
541 {
542 symbol_t *this = symbols[i];
543 if (this->user_token_number != USER_NUMBER_UNDEFINED)
544 {
545 if (this->user_token_number > max_user_token_number)
546 max_user_token_number = this->user_token_number;
547 if (this->user_token_number == 256)
548 num_256_available_p = FALSE;
549 }
550 }
551
552 /* If 256 is not used, assign it to error, to follow POSIX. */
553 if (num_256_available_p
554 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
555 errtoken->user_token_number = 256;
556
557 /* Set the missing user numbers. */
558 if (max_user_token_number < 256)
559 max_user_token_number = 256;
560
561 for (i = 0; i < ntokens; ++i)
562 {
563 symbol_t *this = symbols[i];
564 if (this->user_token_number == USER_NUMBER_UNDEFINED)
565 this->user_token_number = ++max_user_token_number;
566 if (this->user_token_number > max_user_token_number)
567 max_user_token_number = this->user_token_number;
568 }
569
570 token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1);
571
572 /* Initialize all entries for literal tokens to 2, the internal
573 token number for $undefined., which represents all invalid
574 inputs. */
575 for (i = 0; i < max_user_token_number + 1; i++)
576 token_translations[i] = undeftoken->number;
577 symbols_do (symbol_translation, NULL);
578}
579
580
581/*----------------------------------------------------------------.
582| Assign symbol numbers, and write definition of token names into |
583| FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
584`----------------------------------------------------------------*/
585
586void
587symbols_pack (void)
588{
589 symbols = XCALLOC (symbol_t *, nsyms);
590
591 symbols_do (symbol_check_alias_consistence, NULL);
592 symbols_do (symbol_pack, NULL);
593
594 symbols_token_translations_init ();
595
596 if (startsymbol->class == unknown_sym)
ee000ba4 597 fatal_at (startsymbol_location,
dafdc66f
AD
598 _("the start symbol %s is undefined"),
599 symbol_tag_get (startsymbol));
2f1afb73 600 else if (startsymbol->class == token_sym)
ee000ba4 601 fatal_at (startsymbol_location,
dafdc66f
AD
602 _("the start symbol %s is a token"),
603 symbol_tag_get (startsymbol));
2f1afb73 604}