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