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