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