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