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