]> git.saurik.com Git - bison.git/blob - src/symtab.c
Make it easier to write deterministic tests.
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison.
2
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007,
4 2008, 2009
5 Free Software Foundation, Inc.
6
7 This file is part of Bison, the GNU Compiler Compiler.
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include <hash.h>
26 #include <quotearg.h>
27
28 #include "complain.h"
29 #include "gram.h"
30 #include "symtab.h"
31
32 /*-------------------------------------------------------------------.
33 | Symbols sorted by tag. Allocated by the first invocation of |
34 | symbols_do, after which no more symbols should be created. |
35 `-------------------------------------------------------------------*/
36
37 static symbol **symbols_sorted = NULL;
38
39 /*------------------------.
40 | Distinguished symbols. |
41 `------------------------*/
42
43 symbol *errtoken = NULL;
44 symbol *undeftoken = NULL;
45 symbol *endtoken = NULL;
46 symbol *accept = NULL;
47 symbol *startsymbol = NULL;
48 location startsymbol_location;
49
50 /*---------------------------------------.
51 | Default %destructor's and %printer's. |
52 `---------------------------------------*/
53
54 static code_props default_tagged_destructor = CODE_PROPS_NONE_INIT;
55 static code_props default_tagless_destructor = CODE_PROPS_NONE_INIT;
56 static code_props default_tagged_printer = CODE_PROPS_NONE_INIT;
57 static code_props default_tagless_printer = CODE_PROPS_NONE_INIT;
58
59 /*---------------------------------.
60 | Create a new symbol, named TAG. |
61 `---------------------------------*/
62
63 static symbol *
64 symbol_new (uniqstr tag, location loc)
65 {
66 symbol *res = xmalloc (sizeof *res);
67
68 uniqstr_assert (tag);
69
70 /* If the tag is not a string (starts with a double quote), check
71 that it is valid for Yacc. */
72 if (tag[0] != '\"' && tag[0] != '\'' && strchr (tag, '-'))
73 yacc_at (loc, _("POSIX Yacc forbids dashes in symbol names: %s"),
74 tag);
75
76 res->tag = tag;
77 res->location = loc;
78
79 res->type_name = NULL;
80 code_props_none_init (&res->destructor);
81 code_props_none_init (&res->printer);
82
83 res->number = NUMBER_UNDEFINED;
84 res->prec = 0;
85 res->assoc = undef_assoc;
86 res->user_token_number = USER_NUMBER_UNDEFINED;
87
88 res->alias = NULL;
89 res->class = unknown_sym;
90 res->declared = false;
91
92 if (nsyms == SYMBOL_NUMBER_MAXIMUM)
93 fatal (_("too many symbols in input grammar (limit is %d)"),
94 SYMBOL_NUMBER_MAXIMUM);
95 nsyms++;
96 return res;
97 }
98
99 /*----------------------------------------.
100 | Create a new semantic type, named TAG. |
101 `----------------------------------------*/
102
103 static semantic_type *
104 semantic_type_new (uniqstr tag)
105 {
106 semantic_type *res = xmalloc (sizeof *res);
107
108 uniqstr_assert (tag);
109 res->tag = tag;
110 code_props_none_init (&res->destructor);
111 code_props_none_init (&res->printer);
112
113 return res;
114 }
115
116
117 /*-----------------.
118 | Print a symbol. |
119 `-----------------*/
120
121 #define SYMBOL_ATTR_PRINT(Attr) \
122 if (s->Attr) \
123 fprintf (f, " %s { %s }", #Attr, s->Attr)
124
125 #define SYMBOL_CODE_PRINT(Attr) \
126 if (s->Attr.code) \
127 fprintf (f, " %s { %s }", #Attr, s->Attr.code)
128
129 void
130 symbol_print (symbol *s, FILE *f)
131 {
132 if (s)
133 {
134 fprintf (f, "\"%s\"", s->tag);
135 SYMBOL_ATTR_PRINT (type_name);
136 SYMBOL_CODE_PRINT (destructor);
137 SYMBOL_CODE_PRINT (printer);
138 }
139 else
140 fprintf (f, "<NULL>");
141 }
142
143 #undef SYMBOL_ATTR_PRINT
144 #undef SYMBOL_CODE_PRINT
145
146 /*------------------------------------------------------------------.
147 | Complain that S's WHAT is redeclared at SECOND, and was first set |
148 | at FIRST. |
149 `------------------------------------------------------------------*/
150
151 static void
152 symbol_redeclaration (symbol *s, const char *what, location first,
153 location second)
154 {
155 complain_at (second, _("%s redeclaration for %s"), what, s->tag);
156 complain_at (first, _("previous declaration"));
157 }
158
159 static void
160 semantic_type_redeclaration (semantic_type *s, const char *what, location first,
161 location second)
162 {
163 complain_at (second, _("%s redeclaration for <%s>"), what, s->tag);
164 complain_at (first, _("previous declaration"));
165 }
166
167
168
169 /*-----------------------------------------------------------------.
170 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
171 | as TYPE_NAME. |
172 `-----------------------------------------------------------------*/
173
174 void
175 symbol_type_set (symbol *sym, uniqstr type_name, location loc)
176 {
177 if (type_name)
178 {
179 if (sym->type_name)
180 symbol_redeclaration (sym, "%type", sym->type_location, loc);
181 uniqstr_assert (type_name);
182 sym->type_name = type_name;
183 sym->type_location = loc;
184 }
185 }
186
187 /*-----------------------------------.
188 | Get the CLASS associated with SYM. |
189 `-----------------------------------*/
190
191 const char *
192 symbol_class_get_string (symbol *sym)
193 {
194 if (sym->class)
195 {
196 if (sym->class == token_sym)
197 return "terminal";
198 else if (sym->class == nterm_sym)
199 return "nonterminal";
200 }
201 return "unknown";
202 }
203
204
205 /*-----------------------------------------.
206 | Set the DESTRUCTOR associated with SYM. |
207 `-----------------------------------------*/
208
209 void
210 symbol_destructor_set (symbol *sym, code_props const *destructor)
211 {
212 if (sym->destructor.code)
213 symbol_redeclaration (sym, "%destructor", sym->destructor.location,
214 destructor->location);
215 sym->destructor = *destructor;
216 }
217
218 /*------------------------------------------.
219 | Set the DESTRUCTOR associated with TYPE. |
220 `------------------------------------------*/
221
222 void
223 semantic_type_destructor_set (semantic_type *type,
224 code_props const *destructor)
225 {
226 if (type->destructor.code)
227 semantic_type_redeclaration (type, "%destructor",
228 type->destructor.location,
229 destructor->location);
230 type->destructor = *destructor;
231 }
232
233 /*---------------------------------------.
234 | Get the computed %destructor for SYM. |
235 `---------------------------------------*/
236
237 code_props const *
238 symbol_destructor_get (symbol const *sym)
239 {
240 /* Per-symbol %destructor. */
241 if (sym->destructor.code)
242 return &sym->destructor;
243
244 /* Per-type %destructor. */
245 if (sym->type_name)
246 {
247 code_props const *destructor =
248 &semantic_type_get (sym->type_name)->destructor;
249 if (destructor->code)
250 return destructor;
251 }
252
253 /* Apply default %destructor's only to user-defined symbols. */
254 if (sym->tag[0] == '$' || sym == errtoken)
255 return &code_props_none;
256
257 if (sym->type_name)
258 return &default_tagged_destructor;
259 return &default_tagless_destructor;
260 }
261
262 /*--------------------------------------.
263 | Set the PRINTER associated with SYM. |
264 `--------------------------------------*/
265
266 void
267 symbol_printer_set (symbol *sym, code_props const *printer)
268 {
269 if (sym->printer.code)
270 symbol_redeclaration (sym, "%printer",
271 sym->printer.location, printer->location);
272 sym->printer = *printer;
273 }
274
275 /*---------------------------------------.
276 | Set the PRINTER associated with TYPE. |
277 `---------------------------------------*/
278
279 void
280 semantic_type_printer_set (semantic_type *type, code_props const *printer)
281 {
282 if (type->printer.code)
283 semantic_type_redeclaration (type, "%printer",
284 type->printer.location, printer->location);
285 type->printer = *printer;
286 }
287
288 /*------------------------------------.
289 | Get the computed %printer for SYM. |
290 `------------------------------------*/
291
292 code_props const *
293 symbol_printer_get (symbol const *sym)
294 {
295 /* Per-symbol %printer. */
296 if (sym->printer.code)
297 return &sym->printer;
298
299 /* Per-type %printer. */
300 if (sym->type_name)
301 {
302 code_props const *printer = &semantic_type_get (sym->type_name)->printer;
303 if (printer->code)
304 return printer;
305 }
306
307 /* Apply the default %printer only to user-defined symbols. */
308 if (sym->tag[0] == '$' || sym == errtoken)
309 return &code_props_none;
310
311 if (sym->type_name)
312 return &default_tagged_printer;
313 return &default_tagless_printer;
314 }
315
316 /*-----------------------------------------------------------------.
317 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
318 | with UNDEF_ASSOC as ASSOC. |
319 `-----------------------------------------------------------------*/
320
321 void
322 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
323 {
324 if (a != undef_assoc)
325 {
326 if (sym->prec != 0)
327 symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location,
328 loc);
329 sym->prec = prec;
330 sym->assoc = a;
331 sym->prec_location = loc;
332 }
333
334 /* Only terminals have a precedence. */
335 symbol_class_set (sym, token_sym, loc, false);
336 }
337
338
339 /*------------------------------------.
340 | Set the CLASS associated with SYM. |
341 `------------------------------------*/
342
343 void
344 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
345 {
346 if (sym->class != unknown_sym && sym->class != class)
347 {
348 complain_at (loc, _("symbol %s redefined"), sym->tag);
349 sym->declared = false;
350 }
351
352 if (class == nterm_sym && sym->class != nterm_sym)
353 sym->number = nvars++;
354 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
355 sym->number = ntokens++;
356
357 sym->class = class;
358
359 if (declaring)
360 {
361 if (sym->declared)
362 warn_at (loc, _("symbol %s redeclared"), sym->tag);
363 sym->declared = true;
364 }
365 }
366
367
368 /*------------------------------------------------.
369 | Set the USER_TOKEN_NUMBER associated with SYM. |
370 `------------------------------------------------*/
371
372 void
373 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
374 {
375 int *user_token_numberp;
376
377 if (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
378 user_token_numberp = &sym->user_token_number;
379 else
380 user_token_numberp = &sym->alias->user_token_number;
381 if (*user_token_numberp != USER_NUMBER_UNDEFINED
382 && *user_token_numberp != user_token_number)
383 complain_at (loc, _("redefining user token number of %s"), sym->tag);
384
385 *user_token_numberp = user_token_number;
386 /* User defined $end token? */
387 if (user_token_number == 0)
388 {
389 endtoken = sym;
390 endtoken->number = 0;
391 /* It is always mapped to 0, so it was already counted in
392 NTOKENS. */
393 --ntokens;
394 }
395 }
396
397
398 /*----------------------------------------------------------.
399 | If SYM is not defined, report an error, and consider it a |
400 | nonterminal. |
401 `----------------------------------------------------------*/
402
403 static inline bool
404 symbol_check_defined (symbol *sym)
405 {
406 if (sym->class == unknown_sym)
407 {
408 complain_at
409 (sym->location,
410 _("symbol %s is used, but is not defined as a token and has no rules"),
411 sym->tag);
412 sym->class = nterm_sym;
413 sym->number = nvars++;
414 }
415
416 return true;
417 }
418
419 static bool
420 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
421 {
422 return symbol_check_defined (sym);
423 }
424
425
426 void
427 symbol_make_alias (symbol *sym, symbol *str, location loc)
428 {
429 if (str->alias)
430 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
431 str->tag);
432 else if (sym->alias)
433 warn_at (loc, _("symbol `%s' given more than one literal string"),
434 sym->tag);
435 else
436 {
437 str->class = token_sym;
438 str->user_token_number = sym->user_token_number;
439 sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
440 str->alias = sym;
441 sym->alias = str;
442 str->number = sym->number;
443 symbol_type_set (str, sym->type_name, loc);
444 }
445 }
446
447
448 /*---------------------------------------------------------.
449 | Check that THIS, and its alias, have same precedence and |
450 | associativity. |
451 `---------------------------------------------------------*/
452
453 static inline void
454 symbol_check_alias_consistency (symbol *this)
455 {
456 symbol *sym = this;
457 symbol *str = this->alias;
458
459 /* Check only the symbol in the symbol-string pair. */
460 if (!(this->alias
461 && this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
462 return;
463
464 if (str->type_name != sym->type_name)
465 {
466 if (str->type_name)
467 symbol_type_set (sym, str->type_name, str->type_location);
468 else
469 symbol_type_set (str, sym->type_name, sym->type_location);
470 }
471
472
473 if (str->destructor.code || sym->destructor.code)
474 {
475 if (str->destructor.code)
476 symbol_destructor_set (sym, &str->destructor);
477 else
478 symbol_destructor_set (str, &sym->destructor);
479 }
480
481 if (str->printer.code || sym->printer.code)
482 {
483 if (str->printer.code)
484 symbol_printer_set (sym, &str->printer);
485 else
486 symbol_printer_set (str, &sym->printer);
487 }
488
489 if (sym->prec || str->prec)
490 {
491 if (str->prec)
492 symbol_precedence_set (sym, str->prec, str->assoc,
493 str->prec_location);
494 else
495 symbol_precedence_set (str, sym->prec, sym->assoc,
496 sym->prec_location);
497 }
498 }
499
500 static bool
501 symbol_check_alias_consistency_processor (void *this,
502 void *null ATTRIBUTE_UNUSED)
503 {
504 symbol_check_alias_consistency (this);
505 return true;
506 }
507
508
509 /*-------------------------------------------------------------------.
510 | Assign a symbol number, and write the definition of the token name |
511 | into FDEFINES. Put in SYMBOLS. |
512 `-------------------------------------------------------------------*/
513
514 static inline bool
515 symbol_pack (symbol *this)
516 {
517 if (this->class == nterm_sym)
518 {
519 this->number += ntokens;
520 }
521 else if (this->alias)
522 {
523 /* This symbol and its alias are a single token defn.
524 Allocate a tokno, and assign to both check agreement of
525 prec and assoc fields and make both the same */
526 if (this->number == NUMBER_UNDEFINED)
527 {
528 if (this == endtoken || this->alias == endtoken)
529 this->number = this->alias->number = 0;
530 else
531 {
532 aver (this->alias->number != NUMBER_UNDEFINED);
533 this->number = this->alias->number;
534 }
535 }
536 /* Do not do processing below for USER_NUMBER_HAS_STRING_ALIASes. */
537 if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
538 return true;
539 }
540 else /* this->class == token_sym */
541 aver (this->number != NUMBER_UNDEFINED);
542
543 symbols[this->number] = this;
544 return true;
545 }
546
547 static bool
548 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
549 {
550 return symbol_pack (this);
551 }
552
553
554 static void
555 user_token_number_redeclaration (int num, symbol *first, symbol *second)
556 {
557 /* User token numbers are not assigned during the parsing, but in a
558 second step, via a traversal of the symbol table sorted on tag.
559
560 However, error messages make more sense if we keep the first
561 declaration first. */
562 if (location_cmp (first->location, second->location) > 0)
563 {
564 symbol* tmp = first;
565 first = second;
566 second = tmp;
567 }
568 complain_at (second->location,
569 _("user token number %d redeclaration for %s"),
570 num, second->tag);
571 complain_at (first->location, _("previous declaration for %s"),
572 first->tag);
573 }
574
575 /*--------------------------------------------------.
576 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
577 `--------------------------------------------------*/
578
579 static inline bool
580 symbol_translation (symbol *this)
581 {
582 /* Non-terminal? */
583 if (this->class == token_sym
584 && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
585 {
586 /* A token which translation has already been set? */
587 if (token_translations[this->user_token_number] != undeftoken->number)
588 user_token_number_redeclaration
589 (this->user_token_number,
590 symbols[token_translations[this->user_token_number]],
591 this);
592
593 token_translations[this->user_token_number] = this->number;
594 }
595
596 return true;
597 }
598
599 static bool
600 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
601 {
602 return symbol_translation (this);
603 }
604
605
606 /*---------------------------------------.
607 | Symbol and semantic type hash tables. |
608 `---------------------------------------*/
609
610 /* Initial capacity of symbol and semantic type hash table. */
611 #define HT_INITIAL_CAPACITY 257
612
613 static struct hash_table *symbol_table = NULL;
614 static struct hash_table *semantic_type_table = NULL;
615
616 static inline bool
617 hash_compare_symbol (const symbol *m1, const symbol *m2)
618 {
619 /* Since tags are unique, we can compare the pointers themselves. */
620 return UNIQSTR_EQ (m1->tag, m2->tag);
621 }
622
623 static inline bool
624 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
625 {
626 /* Since names are unique, we can compare the pointers themselves. */
627 return UNIQSTR_EQ (m1->tag, m2->tag);
628 }
629
630 static bool
631 hash_symbol_comparator (void const *m1, void const *m2)
632 {
633 return hash_compare_symbol (m1, m2);
634 }
635
636 static bool
637 hash_semantic_type_comparator (void const *m1, void const *m2)
638 {
639 return hash_compare_semantic_type (m1, m2);
640 }
641
642 static inline size_t
643 hash_symbol (const symbol *m, size_t tablesize)
644 {
645 /* Since tags are unique, we can hash the pointer itself. */
646 return ((uintptr_t) m->tag) % tablesize;
647 }
648
649 static inline size_t
650 hash_semantic_type (const semantic_type *m, size_t tablesize)
651 {
652 /* Since names are unique, we can hash the pointer itself. */
653 return ((uintptr_t) m->tag) % tablesize;
654 }
655
656 static size_t
657 hash_symbol_hasher (void const *m, size_t tablesize)
658 {
659 return hash_symbol (m, tablesize);
660 }
661
662 static size_t
663 hash_semantic_type_hasher (void const *m, size_t tablesize)
664 {
665 return hash_semantic_type (m, tablesize);
666 }
667
668 /*-------------------------------.
669 | Create the symbol hash table. |
670 `-------------------------------*/
671
672 void
673 symbols_new (void)
674 {
675 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
676 NULL,
677 hash_symbol_hasher,
678 hash_symbol_comparator,
679 free);
680 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
681 NULL,
682 hash_semantic_type_hasher,
683 hash_semantic_type_comparator,
684 free);
685 }
686
687
688 /*----------------------------------------------------------------.
689 | Find the symbol named KEY, and return it. If it does not exist |
690 | yet, create it. |
691 `----------------------------------------------------------------*/
692
693 symbol *
694 symbol_from_uniqstr (const uniqstr key, location loc)
695 {
696 symbol probe;
697 symbol *entry;
698
699 probe.tag = key;
700 entry = hash_lookup (symbol_table, &probe);
701
702 if (!entry)
703 {
704 /* First insertion in the hash. */
705 aver (!symbols_sorted);
706 entry = symbol_new (key, loc);
707 if (!hash_insert (symbol_table, entry))
708 xalloc_die ();
709 }
710 return entry;
711 }
712
713
714 /*-----------------------------------------------------------------------.
715 | Find the semantic type named KEY, and return it. If it does not exist |
716 | yet, create it. |
717 `-----------------------------------------------------------------------*/
718
719 semantic_type *
720 semantic_type_from_uniqstr (const uniqstr key)
721 {
722 semantic_type probe;
723 semantic_type *entry;
724
725 probe.tag = key;
726 entry = hash_lookup (semantic_type_table, &probe);
727
728 if (!entry)
729 {
730 /* First insertion in the hash. */
731 entry = semantic_type_new (key);
732 if (!hash_insert (semantic_type_table, entry))
733 xalloc_die ();
734 }
735 return entry;
736 }
737
738
739 /*----------------------------------------------------------------.
740 | Find the symbol named KEY, and return it. If it does not exist |
741 | yet, create it. |
742 `----------------------------------------------------------------*/
743
744 symbol *
745 symbol_get (const char *key, location loc)
746 {
747 return symbol_from_uniqstr (uniqstr_new (key), loc);
748 }
749
750
751 /*-----------------------------------------------------------------------.
752 | Find the semantic type named KEY, and return it. If it does not exist |
753 | yet, create it. |
754 `-----------------------------------------------------------------------*/
755
756 semantic_type *
757 semantic_type_get (const char *key)
758 {
759 return semantic_type_from_uniqstr (uniqstr_new (key));
760 }
761
762
763 /*------------------------------------------------------------------.
764 | Generate a dummy nonterminal, whose name cannot conflict with the |
765 | user's names. |
766 `------------------------------------------------------------------*/
767
768 symbol *
769 dummy_symbol_get (location loc)
770 {
771 /* Incremented for each generated symbol. */
772 static int dummy_count = 0;
773 static char buf[256];
774
775 symbol *sym;
776
777 sprintf (buf, "$@%d", ++dummy_count);
778 sym = symbol_get (buf, loc);
779 sym->class = nterm_sym;
780 sym->number = nvars++;
781 return sym;
782 }
783
784 bool
785 symbol_is_dummy (const symbol *sym)
786 {
787 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
788 }
789
790 /*-------------------.
791 | Free the symbols. |
792 `-------------------*/
793
794 void
795 symbols_free (void)
796 {
797 hash_free (symbol_table);
798 hash_free (semantic_type_table);
799 free (symbols);
800 free (symbols_sorted);
801 }
802
803
804 /*---------------------------------------------------------------.
805 | Look for undefined symbols, report an error, and consider them |
806 | terminals. |
807 `---------------------------------------------------------------*/
808
809 static int
810 symbols_cmp (symbol const *a, symbol const *b)
811 {
812 return strcmp (a->tag, b->tag);
813 }
814
815 static int
816 symbols_cmp_qsort (void const *a, void const *b)
817 {
818 return symbols_cmp (*(symbol * const *)a, *(symbol * const *)b);
819 }
820
821 static void
822 symbols_do (Hash_processor processor, void *processor_data)
823 {
824 size_t count = hash_get_n_entries (symbol_table);
825 if (!symbols_sorted)
826 {
827 symbols_sorted = xnmalloc (count, sizeof *symbols_sorted);
828 hash_get_entries (symbol_table, (void**)symbols_sorted, count);
829 qsort (symbols_sorted, count, sizeof *symbols_sorted,
830 symbols_cmp_qsort);
831 }
832 {
833 size_t i;
834 for (i = 0; i < count; ++i)
835 processor (symbols_sorted[i], processor_data);
836 }
837 }
838
839 /*--------------------------------------------------------------.
840 | Check that all the symbols are defined. Report any undefined |
841 | symbols and consider them nonterminals. |
842 `--------------------------------------------------------------*/
843
844 void
845 symbols_check_defined (void)
846 {
847 symbols_do (symbol_check_defined_processor, NULL);
848 }
849
850 /*------------------------------------------------------------------.
851 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
852 | number. |
853 `------------------------------------------------------------------*/
854
855 static void
856 symbols_token_translations_init (void)
857 {
858 bool num_256_available_p = true;
859 int i;
860
861 /* Find the highest user token number, and whether 256, the POSIX
862 preferred user token number for the error token, is used. */
863 max_user_token_number = 0;
864 for (i = 0; i < ntokens; ++i)
865 {
866 symbol *this = symbols[i];
867 if (this->user_token_number != USER_NUMBER_UNDEFINED)
868 {
869 if (this->user_token_number > max_user_token_number)
870 max_user_token_number = this->user_token_number;
871 if (this->user_token_number == 256)
872 num_256_available_p = false;
873 }
874 }
875
876 /* If 256 is not used, assign it to error, to follow POSIX. */
877 if (num_256_available_p
878 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
879 errtoken->user_token_number = 256;
880
881 /* Set the missing user numbers. */
882 if (max_user_token_number < 256)
883 max_user_token_number = 256;
884
885 for (i = 0; i < ntokens; ++i)
886 {
887 symbol *this = symbols[i];
888 if (this->user_token_number == USER_NUMBER_UNDEFINED)
889 this->user_token_number = ++max_user_token_number;
890 if (this->user_token_number > max_user_token_number)
891 max_user_token_number = this->user_token_number;
892 }
893
894 token_translations = xnmalloc (max_user_token_number + 1,
895 sizeof *token_translations);
896
897 /* Initialize all entries for literal tokens to 2, the internal
898 token number for $undefined, which represents all invalid inputs.
899 */
900 for (i = 0; i < max_user_token_number + 1; i++)
901 token_translations[i] = undeftoken->number;
902 symbols_do (symbol_translation_processor, NULL);
903 }
904
905
906 /*----------------------------------------------------------------.
907 | Assign symbol numbers, and write definition of token names into |
908 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
909 `----------------------------------------------------------------*/
910
911 void
912 symbols_pack (void)
913 {
914 symbols_do (symbol_check_alias_consistency_processor, NULL);
915
916 symbols = xcalloc (nsyms, sizeof *symbols);
917 symbols_do (symbol_pack_processor, NULL);
918
919 /* Aliases leave empty slots in symbols, so remove them. */
920 {
921 int writei;
922 int readi;
923 int nsyms_old = nsyms;
924 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
925 {
926 if (symbols[readi] == NULL)
927 {
928 nsyms -= 1;
929 ntokens -= 1;
930 }
931 else
932 {
933 symbols[writei] = symbols[readi];
934 symbols[writei]->number = writei;
935 if (symbols[writei]->alias)
936 symbols[writei]->alias->number = writei;
937 writei += 1;
938 }
939 }
940 }
941 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
942
943 symbols_token_translations_init ();
944
945 if (startsymbol->class == unknown_sym)
946 fatal_at (startsymbol_location,
947 _("the start symbol %s is undefined"),
948 startsymbol->tag);
949 else if (startsymbol->class == token_sym)
950 fatal_at (startsymbol_location,
951 _("the start symbol %s is a token"),
952 startsymbol->tag);
953 }
954
955
956 /*--------------------------------------------------.
957 | Set default tagged/tagless %destructor/%printer. |
958 `--------------------------------------------------*/
959
960 void
961 default_tagged_destructor_set (code_props const *destructor)
962 {
963 if (default_tagged_destructor.code)
964 {
965 complain_at (destructor->location,
966 _("redeclaration for default tagged %%destructor"));
967 complain_at (default_tagged_destructor.location,
968 _("previous declaration"));
969 }
970 default_tagged_destructor = *destructor;
971 }
972
973 void
974 default_tagless_destructor_set (code_props const *destructor)
975 {
976 if (default_tagless_destructor.code)
977 {
978 complain_at (destructor->location,
979 _("redeclaration for default tagless %%destructor"));
980 complain_at (default_tagless_destructor.location,
981 _("previous declaration"));
982 }
983 default_tagless_destructor = *destructor;
984 }
985
986 void
987 default_tagged_printer_set (code_props const *printer)
988 {
989 if (default_tagged_printer.code)
990 {
991 complain_at (printer->location,
992 _("redeclaration for default tagged %%printer"));
993 complain_at (default_tagged_printer.location,
994 _("previous declaration"));
995 }
996 default_tagged_printer = *printer;
997 }
998
999 void
1000 default_tagless_printer_set (code_props const *printer)
1001 {
1002 if (default_tagless_printer.code)
1003 {
1004 complain_at (printer->location,
1005 _("redeclaration for default tagless %%printer"));
1006 complain_at (default_tagless_printer.location,
1007 _("previous declaration"));
1008 }
1009 default_tagless_printer = *printer;
1010 }