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