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