]> git.saurik.com Git - bison.git/blame - src/symtab.c
* src/symtab.c (symbol_new): Initialize the `printer' member.
[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;
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
6b98e4b5
AD
69/*-----------------------------------------------------------------.
70| Return the tag of this SYMBOL in a printable form. Warning: use |
71| the first QUOTEARG slot: 0. |
72`-----------------------------------------------------------------*/
73
74const char *
75symbol_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
86const char *
87symbol_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
97void
98symbol_tag_print (symbol_t *symbol, FILE *out)
99{
100 fputs (symbol_tag_get (symbol), out);
101}
102
103
e9955c83
AD
104/*------------------------------------------------------------------.
105| Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 |
106| as TYPE_NAME. |
107`------------------------------------------------------------------*/
3ae2b51f
AD
108
109void
1a31ed21 110symbol_type_set (symbol_t *symbol, char *type_name, location_t location)
3ae2b51f 111{
e9955c83
AD
112 if (type_name)
113 {
114 if (symbol->type_name)
1e0bab92 115 complain_at (location,
dafdc66f 116 _("type redeclaration for %s"), symbol_tag_get (symbol));
e9955c83
AD
117 symbol->type_name = type_name;
118 }
3ae2b51f
AD
119}
120
121
9280d3ef 122/*-------------------------------------------------------------------.
366eea36 123| Set the DESTRUCTOR associated to SYMBOL. Do nothing if passed 0. |
9280d3ef
AD
124`-------------------------------------------------------------------*/
125
126void
1a31ed21 127symbol_destructor_set (symbol_t *symbol, char *destructor, location_t location)
9280d3ef
AD
128{
129 if (destructor)
130 {
131 if (symbol->destructor)
132 complain_at (location,
366eea36
AD
133 _("%s redeclaration for %s"),
134 "%destructor", symbol_tag_get (symbol));
9280d3ef 135 symbol->destructor = destructor;
24c0aad7 136 symbol->destructor_location = location;
9280d3ef
AD
137 }
138}
139
140
366eea36
AD
141/*----------------------------------------------------------------.
142| Set the PRITNER associated to SYMBOL. Do nothing if passed 0. |
143`----------------------------------------------------------------*/
144
145void
146symbol_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
e9955c83
AD
160/*------------------------------------------------------------------.
161| Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
162| with UNDEF_ASSOC as ASSOC. |
163`------------------------------------------------------------------*/
3ae2b51f
AD
164
165void
1a31ed21
AD
166symbol_precedence_set (symbol_t *symbol,
167 int prec, associativity assoc, location_t location)
3ae2b51f 168{
e9955c83
AD
169 if (assoc != undef_assoc)
170 {
171 if (symbol->prec != 0)
1e0bab92 172 complain_at (location,
dafdc66f
AD
173 _("redefining precedence of %s"),
174 symbol_tag_get (symbol));
e9955c83
AD
175 symbol->prec = prec;
176 symbol->assoc = assoc;
177 }
178
179 /* Only terminals have a precedence. */
e776192e 180 symbol_class_set (symbol, token_sym, location);
3ae2b51f
AD
181}
182
183
44536b35
AD
184/*-------------------------------------.
185| Set the CLASS associated to SYMBOL. |
186`-------------------------------------*/
187
188void
e776192e 189symbol_class_set (symbol_t *symbol, symbol_class class, location_t location)
44536b35
AD
190{
191 if (symbol->class != unknown_sym && symbol->class != class)
e776192e 192 complain_at (location, _("symbol %s redefined"), symbol_tag_get (symbol));
44536b35
AD
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
207void
e776192e
AD
208symbol_user_token_number_set (symbol_t *symbol,
209 int user_token_number, location_t location)
44536b35
AD
210{
211 assert (symbol->class == token_sym);
212
5e424082
AD
213 if (symbol->user_token_number != USER_NUMBER_UNDEFINED
214 && symbol->user_token_number != user_token_number)
e776192e
AD
215 complain_at (location, _("redefining user token number of %s"),
216 symbol_tag_get (symbol));
44536b35
AD
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
72a23c97
AD
231/*------------.
232| Free THIS. |
233`------------*/
234
235static void
db8837cb 236symbol_free (symbol_t *this)
40675e7c 237{
72a23c97
AD
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
2f1afb73
AD
248/*-----------------------------------------------------------.
249| If THIS is not defined, report an error, and consider it a |
250| nonterminal. |
251`-----------------------------------------------------------*/
252
253static bool
254symbol_check_defined (symbol_t *this)
255{
256 if (this->class == unknown_sym)
257 {
ee000ba4
AD
258 complain_at
259 (this->location,
260 _("symbol %s is used, but is not defined as a token and has no rules"),
dafdc66f 261 symbol_tag_get (this));
2f1afb73
AD
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
275void
e9955c83 276symbol_make_alias (symbol_t *symbol, symbol_t *symval)
2f1afb73
AD
277{
278 if (symval->alias)
279 warn (_("symbol `%s' used more than once as a literal string"),
dafdc66f 280 symbol_tag_get (symval));
2f1afb73
AD
281 else if (symbol->alias)
282 warn (_("symbol `%s' given more than one literal string"),
dafdc66f 283 symbol_tag_get (symbol));
2f1afb73
AD
284 else
285 {
286 symval->class = token_sym;
2f1afb73
AD
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
306static bool
307symbol_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"),
dafdc66f 316 symbol_tag_get (this), symbol_tag_get (this->alias));
2f1afb73
AD
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 {
e9955c83
AD
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"),
dafdc66f 331 symbol_tag_get (this), symbol_tag_get (this->alias));
2f1afb73
AD
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
347static bool
348symbol_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
389static bool
390symbol_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"),
dafdc66f
AD
399 symbol_tag_get (symbols[token_translations[this->user_token_number]]),
400 symbol_tag_get (this), this->user_token_number);
2f1afb73
AD
401
402 token_translations[this->user_token_number] = this->number;
403 }
404
405 return TRUE;
406}
407
72a23c97
AD
408
409/*----------------------.
2f1afb73 410| A symbol hash table. |
72a23c97 411`----------------------*/
40675e7c 412
db8837cb 413/* Initial capacity of symbols hash table. */
72a23c97
AD
414#define HT_INITIAL_CAPACITY 257
415
db8837cb 416static struct hash_table *symbol_table = NULL;
72a23c97
AD
417
418static bool
db8837cb 419hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
72a23c97
AD
420{
421 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
422}
423
424static unsigned int
db8837cb 425hash_symbol_t (const symbol_t *m, unsigned int tablesize)
72a23c97
AD
426{
427 return hash_string (m->tag, tablesize);
428}
429
430
431/*-------------------------------.
2f1afb73 432| Create the symbol hash table. |
72a23c97
AD
433`-------------------------------*/
434
435void
db8837cb 436symbols_new (void)
72a23c97 437{
db8837cb 438 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
72a23c97 439 NULL,
db8837cb
AD
440 (Hash_hasher) hash_symbol_t,
441 (Hash_comparator) hash_compare_symbol_t,
442 (Hash_data_freer) symbol_free);
40675e7c
DM
443}
444
445
1e9798d5
AD
446/*----------------------------------------------------------------.
447| Find the symbol named KEY, and return it. If it does not exist |
448| yet, create it. |
449`----------------------------------------------------------------*/
450
db8837cb 451symbol_t *
39f41916 452symbol_get (const char *key, location_t location)
40675e7c 453{
db8837cb
AD
454 symbol_t probe;
455 symbol_t *entry;
40675e7c 456
72a23c97 457 (const char *) probe.tag = key;
db8837cb 458 entry = hash_lookup (symbol_table, &probe);
40675e7c 459
72a23c97 460 if (!entry)
40675e7c 461 {
72a23c97 462 /* First insertion in the hash. */
ee000ba4 463 entry = symbol_new (key, location);
db8837cb 464 hash_insert (symbol_table, entry);
40675e7c 465 }
72a23c97
AD
466 return entry;
467}
40675e7c 468
40675e7c 469
39f41916
AD
470/*------------------------------------------------------------------.
471| Generate a dummy nonterminal, whose name cannot conflict with the |
472| user's names. |
473`------------------------------------------------------------------*/
474
475symbol_t *
476dummy_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
72a23c97 492/*-------------------.
db8837cb 493| Free the symbols. |
72a23c97
AD
494`-------------------*/
495
496void
db8837cb 497symbols_free (void)
72a23c97 498{
db8837cb 499 hash_free (symbol_table);
40675e7c
DM
500}
501
502
72a23c97 503/*---------------------------------------------------------------.
db8837cb 504| Look for undefined symbols, report an error, and consider them |
72a23c97
AD
505| terminals. |
506`---------------------------------------------------------------*/
507
40675e7c 508void
db8837cb 509symbols_do (symbol_processor processor, void *processor_data)
40675e7c 510{
db8837cb 511 hash_do_for_each (symbol_table,
72a23c97
AD
512 (Hash_processor) processor,
513 processor_data);
40675e7c 514}
2f1afb73
AD
515
516
517/*--------------------------------------------------------------.
518| Check that all the symbols are defined. Report any undefined |
519| symbols and consider them nonterminals. |
520`--------------------------------------------------------------*/
521
522void
523symbols_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
533static void
534symbols_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
588void
589symbols_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)
ee000ba4 599 fatal_at (startsymbol_location,
dafdc66f
AD
600 _("the start symbol %s is undefined"),
601 symbol_tag_get (startsymbol));
2f1afb73 602 else if (startsymbol->class == token_sym)
ee000ba4 603 fatal_at (startsymbol_location,
dafdc66f
AD
604 _("the start symbol %s is a token"),
605 symbol_tag_get (startsymbol));
2f1afb73 606}