]> git.saurik.com Git - bison.git/blob - src/symtab.c
warnings: used but undeclared symbols are warnings
[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] != '\'' && strchr (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->status = needed;
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->status = needed;
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->status == declared)
377 warn_at (loc, _("symbol %s redeclared"), sym->tag);
378 sym->status = declared;
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 if (sym->status == needed)
425 complain_at
426 (sym->location,
427 _("symbol %s is used, but is not defined as a token and has no"
428 " rules"),
429 sym->tag);
430 else
431 warn_at
432 (sym->location,
433 _("symbol %s is used, but is not defined as a token and has no"
434 " rules"),
435 sym->tag);
436
437 sym->class = nterm_sym;
438 sym->number = nvars++;
439 }
440
441 return true;
442 }
443
444 static bool
445 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
446 {
447 return symbol_check_defined (sym);
448 }
449
450
451 void
452 symbol_make_alias (symbol *sym, symbol *str, location loc)
453 {
454 if (str->alias)
455 warn_at (loc, _("symbol %s used more than once as a literal string"),
456 str->tag);
457 else if (sym->alias)
458 warn_at (loc, _("symbol %s given more than one literal string"),
459 sym->tag);
460 else
461 {
462 str->class = token_sym;
463 str->user_token_number = sym->user_token_number;
464 sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
465 str->alias = sym;
466 sym->alias = str;
467 str->number = sym->number;
468 symbol_type_set (str, sym->type_name, loc);
469 }
470 }
471
472
473 /*---------------------------------------------------------.
474 | Check that THIS, and its alias, have same precedence and |
475 | associativity. |
476 `---------------------------------------------------------*/
477
478 static inline void
479 symbol_check_alias_consistency (symbol *this)
480 {
481 symbol *sym = this;
482 symbol *str = this->alias;
483
484 /* Check only the symbol in the symbol-string pair. */
485 if (!(this->alias
486 && this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
487 return;
488
489 if (str->type_name != sym->type_name)
490 {
491 if (str->type_name)
492 symbol_type_set (sym, str->type_name, str->type_location);
493 else
494 symbol_type_set (str, sym->type_name, sym->type_location);
495 }
496
497
498 if (str->destructor.code || sym->destructor.code)
499 {
500 if (str->destructor.code)
501 symbol_destructor_set (sym, &str->destructor);
502 else
503 symbol_destructor_set (str, &sym->destructor);
504 }
505
506 if (str->printer.code || sym->printer.code)
507 {
508 if (str->printer.code)
509 symbol_printer_set (sym, &str->printer);
510 else
511 symbol_printer_set (str, &sym->printer);
512 }
513
514 if (sym->prec || str->prec)
515 {
516 if (str->prec)
517 symbol_precedence_set (sym, str->prec, str->assoc,
518 str->prec_location);
519 else
520 symbol_precedence_set (str, sym->prec, sym->assoc,
521 sym->prec_location);
522 }
523 }
524
525 static bool
526 symbol_check_alias_consistency_processor (void *this,
527 void *null ATTRIBUTE_UNUSED)
528 {
529 symbol_check_alias_consistency (this);
530 return true;
531 }
532
533
534 /*-------------------------------------------------------------------.
535 | Assign a symbol number, and write the definition of the token name |
536 | into FDEFINES. Put in SYMBOLS. |
537 `-------------------------------------------------------------------*/
538
539 static inline bool
540 symbol_pack (symbol *this)
541 {
542 aver (this->number != NUMBER_UNDEFINED);
543 if (this->class == nterm_sym)
544 this->number += ntokens;
545 else if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
546 return true;
547
548 symbols[this->number] = this;
549 return true;
550 }
551
552 static bool
553 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
554 {
555 return symbol_pack (this);
556 }
557
558
559 static void
560 user_token_number_redeclaration (int num, symbol *first, symbol *second)
561 {
562 /* User token numbers are not assigned during the parsing, but in a
563 second step, via a traversal of the symbol table sorted on tag.
564
565 However, error messages make more sense if we keep the first
566 declaration first. */
567 if (location_cmp (first->location, second->location) > 0)
568 {
569 symbol* tmp = first;
570 first = second;
571 second = tmp;
572 }
573 complain_at (second->location,
574 _("user token number %d redeclaration for %s"),
575 num, second->tag);
576 complain_at (first->location, _("previous declaration for %s"),
577 first->tag);
578 }
579
580 /*--------------------------------------------------.
581 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
582 `--------------------------------------------------*/
583
584 static inline bool
585 symbol_translation (symbol *this)
586 {
587 /* Non-terminal? */
588 if (this->class == token_sym
589 && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
590 {
591 /* A token which translation has already been set? */
592 if (token_translations[this->user_token_number] != undeftoken->number)
593 user_token_number_redeclaration
594 (this->user_token_number,
595 symbols[token_translations[this->user_token_number]],
596 this);
597
598 token_translations[this->user_token_number] = this->number;
599 }
600
601 return true;
602 }
603
604 static bool
605 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
606 {
607 return symbol_translation (this);
608 }
609
610
611 /*---------------------------------------.
612 | Symbol and semantic type hash tables. |
613 `---------------------------------------*/
614
615 /* Initial capacity of symbol and semantic type hash table. */
616 #define HT_INITIAL_CAPACITY 257
617
618 static struct hash_table *symbol_table = NULL;
619 static struct hash_table *semantic_type_table = NULL;
620
621 static inline bool
622 hash_compare_symbol (const symbol *m1, const symbol *m2)
623 {
624 /* Since tags are unique, we can compare the pointers themselves. */
625 return UNIQSTR_EQ (m1->tag, m2->tag);
626 }
627
628 static inline bool
629 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
630 {
631 /* Since names are unique, we can compare the pointers themselves. */
632 return UNIQSTR_EQ (m1->tag, m2->tag);
633 }
634
635 static bool
636 hash_symbol_comparator (void const *m1, void const *m2)
637 {
638 return hash_compare_symbol (m1, m2);
639 }
640
641 static bool
642 hash_semantic_type_comparator (void const *m1, void const *m2)
643 {
644 return hash_compare_semantic_type (m1, m2);
645 }
646
647 static inline size_t
648 hash_symbol (const symbol *m, size_t tablesize)
649 {
650 /* Since tags are unique, we can hash the pointer itself. */
651 return ((uintptr_t) m->tag) % tablesize;
652 }
653
654 static inline size_t
655 hash_semantic_type (const semantic_type *m, size_t tablesize)
656 {
657 /* Since names are unique, we can hash the pointer itself. */
658 return ((uintptr_t) m->tag) % tablesize;
659 }
660
661 static size_t
662 hash_symbol_hasher (void const *m, size_t tablesize)
663 {
664 return hash_symbol (m, tablesize);
665 }
666
667 static size_t
668 hash_semantic_type_hasher (void const *m, size_t tablesize)
669 {
670 return hash_semantic_type (m, tablesize);
671 }
672
673 /*-------------------------------.
674 | Create the symbol hash table. |
675 `-------------------------------*/
676
677 void
678 symbols_new (void)
679 {
680 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
681 NULL,
682 hash_symbol_hasher,
683 hash_symbol_comparator,
684 free);
685 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
686 NULL,
687 hash_semantic_type_hasher,
688 hash_semantic_type_comparator,
689 free);
690 }
691
692
693 /*----------------------------------------------------------------.
694 | Find the symbol named KEY, and return it. If it does not exist |
695 | yet, create it. |
696 `----------------------------------------------------------------*/
697
698 symbol *
699 symbol_from_uniqstr (const uniqstr key, location loc)
700 {
701 symbol probe;
702 symbol *entry;
703
704 probe.tag = key;
705 entry = hash_lookup (symbol_table, &probe);
706
707 if (!entry)
708 {
709 /* First insertion in the hash. */
710 aver (!symbols_sorted);
711 entry = symbol_new (key, loc);
712 if (!hash_insert (symbol_table, entry))
713 xalloc_die ();
714 }
715 return entry;
716 }
717
718
719 /*-----------------------------------------------------------------------.
720 | Find the semantic type named KEY, and return it. If it does not exist |
721 | yet, create it. |
722 `-----------------------------------------------------------------------*/
723
724 semantic_type *
725 semantic_type_from_uniqstr (const uniqstr key)
726 {
727 semantic_type probe;
728 semantic_type *entry;
729
730 probe.tag = key;
731 entry = hash_lookup (semantic_type_table, &probe);
732
733 if (!entry)
734 {
735 /* First insertion in the hash. */
736 entry = semantic_type_new (key);
737 if (!hash_insert (semantic_type_table, entry))
738 xalloc_die ();
739 }
740 return entry;
741 }
742
743
744 /*----------------------------------------------------------------.
745 | Find the symbol named KEY, and return it. If it does not exist |
746 | yet, create it. |
747 `----------------------------------------------------------------*/
748
749 symbol *
750 symbol_get (const char *key, location loc)
751 {
752 return symbol_from_uniqstr (uniqstr_new (key), loc);
753 }
754
755
756 /*-----------------------------------------------------------------------.
757 | Find the semantic type named KEY, and return it. If it does not exist |
758 | yet, create it. |
759 `-----------------------------------------------------------------------*/
760
761 semantic_type *
762 semantic_type_get (const char *key)
763 {
764 return semantic_type_from_uniqstr (uniqstr_new (key));
765 }
766
767
768 /*------------------------------------------------------------------.
769 | Generate a dummy nonterminal, whose name cannot conflict with the |
770 | user's names. |
771 `------------------------------------------------------------------*/
772
773 symbol *
774 dummy_symbol_get (location loc)
775 {
776 /* Incremented for each generated symbol. */
777 static int dummy_count = 0;
778 static char buf[256];
779
780 symbol *sym;
781
782 sprintf (buf, "$@%d", ++dummy_count);
783 sym = symbol_get (buf, loc);
784 sym->class = nterm_sym;
785 sym->number = nvars++;
786 return sym;
787 }
788
789 bool
790 symbol_is_dummy (const symbol *sym)
791 {
792 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
793 }
794
795 /*-------------------.
796 | Free the symbols. |
797 `-------------------*/
798
799 void
800 symbols_free (void)
801 {
802 hash_free (symbol_table);
803 hash_free (semantic_type_table);
804 free (symbols);
805 free (symbols_sorted);
806 }
807
808
809 /*---------------------------------------------------------------.
810 | Look for undefined symbols, report an error, and consider them |
811 | terminals. |
812 `---------------------------------------------------------------*/
813
814 static int
815 symbols_cmp (symbol const *a, symbol const *b)
816 {
817 return strcmp (a->tag, b->tag);
818 }
819
820 static int
821 symbols_cmp_qsort (void const *a, void const *b)
822 {
823 return symbols_cmp (*(symbol * const *)a, *(symbol * const *)b);
824 }
825
826 static void
827 symbols_do (Hash_processor processor, void *processor_data)
828 {
829 size_t count = hash_get_n_entries (symbol_table);
830 if (!symbols_sorted)
831 {
832 symbols_sorted = xnmalloc (count, sizeof *symbols_sorted);
833 hash_get_entries (symbol_table, (void**)symbols_sorted, count);
834 qsort (symbols_sorted, count, sizeof *symbols_sorted,
835 symbols_cmp_qsort);
836 }
837 {
838 size_t i;
839 for (i = 0; i < count; ++i)
840 processor (symbols_sorted[i], processor_data);
841 }
842 }
843
844 /*--------------------------------------------------------------.
845 | Check that all the symbols are defined. Report any undefined |
846 | symbols and consider them nonterminals. |
847 `--------------------------------------------------------------*/
848
849 void
850 symbols_check_defined (void)
851 {
852 symbols_do (symbol_check_defined_processor, NULL);
853 }
854
855 /*------------------------------------------------------------------.
856 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
857 | number. |
858 `------------------------------------------------------------------*/
859
860 static void
861 symbols_token_translations_init (void)
862 {
863 bool num_256_available_p = true;
864 int i;
865
866 /* Find the highest user token number, and whether 256, the POSIX
867 preferred user token number for the error token, is used. */
868 max_user_token_number = 0;
869 for (i = 0; i < ntokens; ++i)
870 {
871 symbol *this = symbols[i];
872 if (this->user_token_number != USER_NUMBER_UNDEFINED)
873 {
874 if (this->user_token_number > max_user_token_number)
875 max_user_token_number = this->user_token_number;
876 if (this->user_token_number == 256)
877 num_256_available_p = false;
878 }
879 }
880
881 /* If 256 is not used, assign it to error, to follow POSIX. */
882 if (num_256_available_p
883 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
884 errtoken->user_token_number = 256;
885
886 /* Set the missing user numbers. */
887 if (max_user_token_number < 256)
888 max_user_token_number = 256;
889
890 for (i = 0; i < ntokens; ++i)
891 {
892 symbol *this = symbols[i];
893 if (this->user_token_number == USER_NUMBER_UNDEFINED)
894 this->user_token_number = ++max_user_token_number;
895 if (this->user_token_number > max_user_token_number)
896 max_user_token_number = this->user_token_number;
897 }
898
899 token_translations = xnmalloc (max_user_token_number + 1,
900 sizeof *token_translations);
901
902 /* Initialize all entries for literal tokens to the internal token
903 number for $undefined, which represents all invalid inputs. */
904 for (i = 0; i < max_user_token_number + 1; i++)
905 token_translations[i] = undeftoken->number;
906 symbols_do (symbol_translation_processor, NULL);
907 }
908
909
910 /*----------------------------------------------------------------.
911 | Assign symbol numbers, and write definition of token names into |
912 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
913 `----------------------------------------------------------------*/
914
915 void
916 symbols_pack (void)
917 {
918 symbols_do (symbol_check_alias_consistency_processor, NULL);
919
920 symbols = xcalloc (nsyms, sizeof *symbols);
921 symbols_do (symbol_pack_processor, NULL);
922
923 /* Aliases leave empty slots in symbols, so remove them. */
924 {
925 int writei;
926 int readi;
927 int nsyms_old = nsyms;
928 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
929 {
930 if (symbols[readi] == NULL)
931 {
932 nsyms -= 1;
933 ntokens -= 1;
934 }
935 else
936 {
937 symbols[writei] = symbols[readi];
938 symbols[writei]->number = writei;
939 if (symbols[writei]->alias)
940 symbols[writei]->alias->number = writei;
941 writei += 1;
942 }
943 }
944 }
945 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
946
947 symbols_token_translations_init ();
948
949 if (startsymbol->class == unknown_sym)
950 fatal_at (startsymbol_location,
951 _("the start symbol %s is undefined"),
952 startsymbol->tag);
953 else if (startsymbol->class == token_sym)
954 fatal_at (startsymbol_location,
955 _("the start symbol %s is a token"),
956 startsymbol->tag);
957 }
958
959
960 /*--------------------------------------------------.
961 | Set default tagged/tagless %destructor/%printer. |
962 `--------------------------------------------------*/
963
964 void
965 default_tagged_destructor_set (code_props const *destructor)
966 {
967 if (default_tagged_destructor.code)
968 {
969 complain_at (destructor->location,
970 _("redeclaration for default tagged %%destructor"));
971 complain_at (default_tagged_destructor.location,
972 _("previous declaration"));
973 }
974 default_tagged_destructor = *destructor;
975 }
976
977 void
978 default_tagless_destructor_set (code_props const *destructor)
979 {
980 if (default_tagless_destructor.code)
981 {
982 complain_at (destructor->location,
983 _("redeclaration for default tagless %%destructor"));
984 complain_at (default_tagless_destructor.location,
985 _("previous declaration"));
986 }
987 default_tagless_destructor = *destructor;
988 }
989
990 void
991 default_tagged_printer_set (code_props const *printer)
992 {
993 if (default_tagged_printer.code)
994 {
995 complain_at (printer->location,
996 _("redeclaration for default tagged %%printer"));
997 complain_at (default_tagged_printer.location,
998 _("previous declaration"));
999 }
1000 default_tagged_printer = *printer;
1001 }
1002
1003 void
1004 default_tagless_printer_set (code_props const *printer)
1005 {
1006 if (default_tagless_printer.code)
1007 {
1008 complain_at (printer->location,
1009 _("redeclaration for default tagless %%printer"));
1010 complain_at (default_tagless_printer.location,
1011 _("previous declaration"));
1012 }
1013 default_tagless_printer = *printer;
1014 }