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