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