]> git.saurik.com Git - bison.git/blob - src/symtab.c
* src/conflicts.c (log_resolution): Accept the rule involved in
[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, char *type_name)
106 {
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 }
113 }
114
115
116 /*------------------------------------------------------------------.
117 | Set the PRECEDENCE associated to SYMBOL. Does nothing if invoked |
118 | with UNDEF_ASSOC as ASSOC. |
119 `------------------------------------------------------------------*/
120
121 void
122 symbol_precedence_set (symbol_t *symbol,
123 int prec, associativity assoc)
124 {
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);
135 }
136
137
138 /*-------------------------------------.
139 | Set the CLASS associated to SYMBOL. |
140 `-------------------------------------*/
141
142 void
143 symbol_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
161 void
162 symbol_user_token_number_set (symbol_t *symbol, int user_token_number)
163 {
164 assert (symbol->class == token_sym);
165
166 if (symbol->user_token_number != USER_NUMBER_UNDEFINED
167 && symbol->user_token_number != user_token_number)
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
183 /*------------.
184 | Free THIS. |
185 `------------*/
186
187 static void
188 symbol_free (symbol_t *this)
189 {
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
200 /*-----------------------------------------------------------.
201 | If THIS is not defined, report an error, and consider it a |
202 | nonterminal. |
203 `-----------------------------------------------------------*/
204
205 static bool
206 symbol_check_defined (symbol_t *this)
207 {
208 if (this->class == unknown_sym)
209 {
210 complain_at
211 (this->location,
212 _("symbol %s is used, but is not defined as a token and has no rules"),
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
227 void
228 symbol_make_alias (symbol_t *symbol, symbol_t *symval)
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;
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
258 static bool
259 symbol_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 {
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"),
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
299 static bool
300 symbol_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
341 static bool
342 symbol_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
360
361 /*----------------------.
362 | A symbol hash table. |
363 `----------------------*/
364
365 /* Initial capacity of symbols hash table. */
366 #define HT_INITIAL_CAPACITY 257
367
368 static struct hash_table *symbol_table = NULL;
369
370 static bool
371 hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
372 {
373 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
374 }
375
376 static unsigned int
377 hash_symbol_t (const symbol_t *m, unsigned int tablesize)
378 {
379 return hash_string (m->tag, tablesize);
380 }
381
382
383 /*-------------------------------.
384 | Create the symbol hash table. |
385 `-------------------------------*/
386
387 void
388 symbols_new (void)
389 {
390 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
391 NULL,
392 (Hash_hasher) hash_symbol_t,
393 (Hash_comparator) hash_compare_symbol_t,
394 (Hash_data_freer) symbol_free);
395 }
396
397
398 /*----------------------------------------------------------------.
399 | Find the symbol named KEY, and return it. If it does not exist |
400 | yet, create it. |
401 `----------------------------------------------------------------*/
402
403 symbol_t *
404 getsym (const char *key, location_t location)
405 {
406 symbol_t probe;
407 symbol_t *entry;
408
409 (const char *) probe.tag = key;
410 entry = hash_lookup (symbol_table, &probe);
411
412 if (!entry)
413 {
414 /* First insertion in the hash. */
415 entry = symbol_new (key, location);
416 hash_insert (symbol_table, entry);
417 }
418 return entry;
419 }
420
421
422 /*-------------------.
423 | Free the symbols. |
424 `-------------------*/
425
426 void
427 symbols_free (void)
428 {
429 hash_free (symbol_table);
430 }
431
432
433 /*---------------------------------------------------------------.
434 | Look for undefined symbols, report an error, and consider them |
435 | terminals. |
436 `---------------------------------------------------------------*/
437
438 void
439 symbols_do (symbol_processor processor, void *processor_data)
440 {
441 hash_do_for_each (symbol_table,
442 (Hash_processor) processor,
443 processor_data);
444 }
445
446
447 /*--------------------------------------------------------------.
448 | Check that all the symbols are defined. Report any undefined |
449 | symbols and consider them nonterminals. |
450 `--------------------------------------------------------------*/
451
452 void
453 symbols_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
463 static void
464 symbols_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
518 void
519 symbols_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)
529 fatal_at (startsymbol_location,
530 _("the start symbol %s is undefined"), startsymbol->tag);
531 else if (startsymbol->class == token_sym)
532 fatal_at (startsymbol_location,
533 _("the start symbol %s is a token"), startsymbol->tag);
534 }