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