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