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