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