]> git.saurik.com Git - bison.git/blob - src/symtab.c
NEWS: prepare for 2.7.91
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison.
2
3 Copyright (C) 1984, 1989, 2000-2002, 2004-2013 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 static symbol **semantic_types_sorted = NULL;
37
38 /*------------------------.
39 | Distinguished symbols. |
40 `------------------------*/
41
42 symbol *errtoken = NULL;
43 symbol *undeftoken = NULL;
44 symbol *endtoken = NULL;
45 symbol *accept = NULL;
46 symbol *startsymbol = NULL;
47 location startsymbol_location;
48
49 /*---------------------------.
50 | Precedence relation graph. |
51 `---------------------------*/
52
53 static symgraph **prec_nodes;
54
55 /*-----------------------------------.
56 | Store which associativity is used. |
57 `-----------------------------------*/
58
59 bool *used_assoc = NULL;
60
61 /*---------------------------------.
62 | Create a new symbol, named TAG. |
63 `---------------------------------*/
64
65 static symbol *
66 symbol_new (uniqstr tag, location loc)
67 {
68 symbol *res = xmalloc (sizeof *res);
69 uniqstr_assert (tag);
70
71 /* If the tag is not a string (starts with a double quote), check
72 that it is valid for Yacc. */
73 if (tag[0] != '\"' && tag[0] != '\'' && strchr (tag, '-'))
74 complain (&loc, Wyacc,
75 _("POSIX Yacc forbids dashes in symbol names: %s"), tag);
76
77 res->tag = tag;
78 res->location = loc;
79
80 res->type_name = NULL;
81 {
82 int i;
83 for (i = 0; i < CODE_PROPS_SIZE; ++i)
84 code_props_none_init (&res->props[i]);
85 }
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 complain (NULL, 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, const location *loc)
122 {
123 semantic_type *res = xmalloc (sizeof *res);
124
125 uniqstr_assert (tag);
126 res->tag = tag;
127 res->location = loc ? *loc : empty_location;
128 res->status = undeclared;
129 {
130 int i;
131 for (i = 0; i < CODE_PROPS_SIZE; ++i)
132 code_props_none_init (&res->props[i]);
133 }
134
135 return res;
136 }
137
138
139 /*-----------------.
140 | Print a symbol. |
141 `-----------------*/
142
143 #define SYMBOL_ATTR_PRINT(Attr) \
144 if (s->Attr) \
145 fprintf (f, " %s { %s }", #Attr, s->Attr)
146
147 #define SYMBOL_CODE_PRINT(Attr) \
148 if (s->props[Attr].code) \
149 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
150
151 void
152 symbol_print (symbol const *s, FILE *f)
153 {
154 if (s)
155 {
156 fputs (s->tag, f);
157 SYMBOL_ATTR_PRINT (type_name);
158 SYMBOL_CODE_PRINT (destructor);
159 SYMBOL_CODE_PRINT (printer);
160 }
161 else
162 fputs ("<NULL>", f);
163 }
164
165 #undef SYMBOL_ATTR_PRINT
166 #undef SYMBOL_CODE_PRINT
167
168
169 /*----------------------------------.
170 | Whether S is a valid identifier. |
171 `----------------------------------*/
172
173 static bool
174 is_identifier (uniqstr s)
175 {
176 static char const alphanum[26 + 26 + 1 + 10] =
177 "abcdefghijklmnopqrstuvwxyz"
178 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
179 "_"
180 "0123456789";
181 if (!s || ! memchr (alphanum, *s, sizeof alphanum - 10))
182 return false;
183 for (++s; *s; ++s)
184 if (! memchr (alphanum, *s, sizeof alphanum))
185 return false;
186 return true;
187 }
188
189
190 /*-----------------------------------------------.
191 | Get the identifier associated to this symbol. |
192 `-----------------------------------------------*/
193 uniqstr
194 symbol_id_get (symbol const *sym)
195 {
196 aver (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS);
197 if (sym->alias)
198 sym = sym->alias;
199 return is_identifier (sym->tag) ? sym->tag : 0;
200 }
201
202
203 /*------------------------------------------------------------------.
204 | Complain that S's WHAT is redeclared at SECOND, and was first set |
205 | at FIRST. |
206 `------------------------------------------------------------------*/
207
208 static void
209 symbol_redeclaration (symbol *s, const char *what, location first,
210 location second)
211 {
212 unsigned i = 0;
213 complain_indent (&second, complaint, &i,
214 _("%s redeclaration for %s"), what, s->tag);
215 i += SUB_INDENT;
216 complain_indent (&first, complaint, &i,
217 _("previous declaration"));
218 }
219
220 static void
221 semantic_type_redeclaration (semantic_type *s, const char *what, location first,
222 location second)
223 {
224 unsigned i = 0;
225 complain_indent (&second, complaint, &i,
226 _("%s redeclaration for <%s>"), what, s->tag);
227 i += SUB_INDENT;
228 complain_indent (&first, complaint, &i,
229 _("previous declaration"));
230 }
231
232
233
234 /*-----------------------------------------------------------------.
235 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
236 | as TYPE_NAME. |
237 `-----------------------------------------------------------------*/
238
239 void
240 symbol_type_set (symbol *sym, uniqstr type_name, location loc)
241 {
242 if (type_name)
243 {
244 if (sym->type_name)
245 symbol_redeclaration (sym, "%type", sym->type_location, loc);
246 else
247 {
248 uniqstr_assert (type_name);
249 sym->type_name = type_name;
250 sym->type_location = loc;
251 }
252 }
253 }
254
255 /*--------------------------------------------------------.
256 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
257 `--------------------------------------------------------*/
258
259 void
260 symbol_code_props_set (symbol *sym, code_props_type kind,
261 code_props const *code)
262 {
263 if (sym->props[kind].code)
264 symbol_redeclaration (sym, code_props_type_string (kind),
265 sym->props[kind].location,
266 code->location);
267 else
268 sym->props[kind] = *code;
269 }
270
271 /*-----------------------------------------------------.
272 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
273 `-----------------------------------------------------*/
274
275 void
276 semantic_type_code_props_set (semantic_type *type,
277 code_props_type kind,
278 code_props const *code)
279 {
280 if (type->props[kind].code)
281 semantic_type_redeclaration (type, code_props_type_string (kind),
282 type->props[kind].location,
283 code->location);
284 else
285 type->props[kind] = *code;
286 }
287
288 /*---------------------------------------------------.
289 | Get the computed %destructor or %printer for SYM. |
290 `---------------------------------------------------*/
291
292 code_props *
293 symbol_code_props_get (symbol *sym, code_props_type kind)
294 {
295 /* Per-symbol code props. */
296 if (sym->props[kind].code)
297 return &sym->props[kind];
298
299 /* Per-type code props. */
300 if (sym->type_name)
301 {
302 code_props *code =
303 &semantic_type_get (sym->type_name, NULL)->props[kind];
304 if (code->code)
305 return code;
306 }
307
308 /* Apply default code props's only to user-defined symbols. */
309 if (sym->tag[0] != '$' && sym != errtoken)
310 {
311 code_props *code =
312 &semantic_type_get (sym->type_name ? "*" : "", NULL)->props[kind];
313 if (code->code)
314 return code;
315 }
316 return &code_props_none;
317 }
318
319 /*-----------------------------------------------------------------.
320 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
321 | with UNDEF_ASSOC as ASSOC. |
322 `-----------------------------------------------------------------*/
323
324 void
325 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
326 {
327 if (a != undef_assoc)
328 {
329 if (sym->prec)
330 symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location,
331 loc);
332 else
333 {
334 sym->prec = prec;
335 sym->assoc = a;
336 sym->prec_location = loc;
337 }
338 }
339
340 /* Only terminals have a precedence. */
341 symbol_class_set (sym, token_sym, loc, false);
342 }
343
344
345 /*------------------------------------.
346 | Set the CLASS associated with SYM. |
347 `------------------------------------*/
348
349 void
350 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
351 {
352 bool warned = false;
353 if (sym->class != unknown_sym && sym->class != class)
354 {
355 complain (&loc, complaint, _("symbol %s redefined"), sym->tag);
356 /* Don't report both "redefined" and "redeclared". */
357 warned = true;
358 }
359
360 if (class == nterm_sym && sym->class != nterm_sym)
361 sym->number = nvars++;
362 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
363 sym->number = ntokens++;
364
365 sym->class = class;
366
367 if (declaring)
368 {
369 if (sym->status == declared && !warned)
370 complain (&loc, Wother, _("symbol %s redeclared"), sym->tag);
371 else
372 sym->status = declared;
373 }
374 }
375
376
377 /*------------------------------------------------.
378 | Set the USER_TOKEN_NUMBER associated with SYM. |
379 `------------------------------------------------*/
380
381 void
382 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
383 {
384 int *user_token_numberp;
385
386 if (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
387 user_token_numberp = &sym->user_token_number;
388 else
389 user_token_numberp = &sym->alias->user_token_number;
390 if (*user_token_numberp != USER_NUMBER_UNDEFINED
391 && *user_token_numberp != user_token_number)
392 complain (&loc, complaint, _("redefining user token number of %s"),
393 sym->tag);
394
395 *user_token_numberp = user_token_number;
396 /* User defined $end token? */
397 if (user_token_number == 0)
398 {
399 endtoken = sym;
400 /* It is always mapped to 0, so it was already counted in
401 NTOKENS. */
402 if (endtoken->number != NUMBER_UNDEFINED)
403 --ntokens;
404 endtoken->number = 0;
405 }
406 }
407
408
409 /*----------------------------------------------------------.
410 | If SYM is not defined, report an error, and consider it a |
411 | nonterminal. |
412 `----------------------------------------------------------*/
413
414 static inline bool
415 symbol_check_defined (symbol *sym)
416 {
417 if (sym->class == unknown_sym)
418 {
419 assert (sym->status != declared);
420 complain (&sym->location,
421 sym->status == needed ? complaint : Wother,
422 _("symbol %s is used, but is not defined as a token"
423 " and has no rules"),
424 sym->tag);
425 sym->class = nterm_sym;
426 sym->number = nvars++;
427 }
428
429 {
430 int i;
431 for (i = 0; i < 2; ++i)
432 symbol_code_props_get (sym, i)->is_used = true;
433 }
434
435 /* Set the semantic type status associated to the current symbol to
436 'declared' so that we could check semantic types unnecessary uses. */
437 if (sym->type_name)
438 {
439 semantic_type *sem_type = semantic_type_get (sym->type_name, NULL);
440 if (sem_type)
441 sem_type->status = declared;
442 }
443
444 return true;
445 }
446
447 static inline bool
448 semantic_type_check_defined (semantic_type *sem_type)
449 {
450 /* <*> and <> do not have to be "declared". */
451 if (sem_type->status == declared
452 || !*sem_type->tag
453 || STREQ (sem_type->tag, "*"))
454 {
455 int i;
456 for (i = 0; i < 2; ++i)
457 if (sem_type->props[i].kind != CODE_PROPS_NONE
458 && ! sem_type->props[i].is_used)
459 complain (&sem_type->location, Wother,
460 _("useless %s for type <%s>"),
461 code_props_type_string (i), sem_type->tag);
462 }
463 else
464 complain (&sem_type->location, Wother,
465 _("type <%s> is used, but is not associated to any symbol"),
466 sem_type->tag);
467
468 return true;
469 }
470
471 static bool
472 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
473 {
474 return symbol_check_defined (sym);
475 }
476
477 static bool
478 semantic_type_check_defined_processor (void *sem_type,
479 void *null ATTRIBUTE_UNUSED)
480 {
481 return semantic_type_check_defined (sem_type);
482 }
483
484
485 void
486 symbol_make_alias (symbol *sym, symbol *str, location loc)
487 {
488 if (str->alias)
489 complain (&loc, Wother,
490 _("symbol %s used more than once as a literal string"), str->tag);
491 else if (sym->alias)
492 complain (&loc, Wother,
493 _("symbol %s given more than one literal string"), sym->tag);
494 else
495 {
496 str->class = token_sym;
497 str->user_token_number = sym->user_token_number;
498 sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
499 str->alias = sym;
500 sym->alias = str;
501 str->number = sym->number;
502 symbol_type_set (str, sym->type_name, loc);
503 }
504 }
505
506
507 /*---------------------------------------------------------.
508 | Check that THIS, and its alias, have same precedence and |
509 | associativity. |
510 `---------------------------------------------------------*/
511
512 static inline void
513 symbol_check_alias_consistency (symbol *this)
514 {
515 symbol *sym = this;
516 symbol *str = this->alias;
517
518 /* Check only the symbol in the symbol-string pair. */
519 if (!(this->alias
520 && this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
521 return;
522
523 if (str->type_name != sym->type_name)
524 {
525 if (str->type_name)
526 symbol_type_set (sym, str->type_name, str->type_location);
527 else
528 symbol_type_set (str, sym->type_name, sym->type_location);
529 }
530
531
532 {
533 int i;
534 for (i = 0; i < CODE_PROPS_SIZE; ++i)
535 if (str->props[i].code)
536 symbol_code_props_set (sym, i, &str->props[i]);
537 else if (sym->props[i].code)
538 symbol_code_props_set (str, i, &sym->props[i]);
539 }
540
541 if (sym->prec || str->prec)
542 {
543 if (str->prec)
544 symbol_precedence_set (sym, str->prec, str->assoc,
545 str->prec_location);
546 else
547 symbol_precedence_set (str, sym->prec, sym->assoc,
548 sym->prec_location);
549 }
550 }
551
552 static bool
553 symbol_check_alias_consistency_processor (void *this,
554 void *null ATTRIBUTE_UNUSED)
555 {
556 symbol_check_alias_consistency (this);
557 return true;
558 }
559
560
561 /*-------------------------------------------------------------------.
562 | Assign a symbol number, and write the definition of the token name |
563 | into FDEFINES. Put in SYMBOLS. |
564 `-------------------------------------------------------------------*/
565
566 static inline bool
567 symbol_pack (symbol *this)
568 {
569 aver (this->number != NUMBER_UNDEFINED);
570 if (this->class == nterm_sym)
571 this->number += ntokens;
572 else if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
573 return true;
574
575 symbols[this->number] = this;
576 return true;
577 }
578
579 static bool
580 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
581 {
582 return symbol_pack (this);
583 }
584
585
586 static void
587 user_token_number_redeclaration (int num, symbol *first, symbol *second)
588 {
589 unsigned i = 0;
590 /* User token numbers are not assigned during the parsing, but in a
591 second step, via a traversal of the symbol table sorted on tag.
592
593 However, error messages make more sense if we keep the first
594 declaration first. */
595 if (location_cmp (first->location, second->location) > 0)
596 {
597 symbol* tmp = first;
598 first = second;
599 second = tmp;
600 }
601 complain_indent (&second->location, complaint, &i,
602 _("user token number %d redeclaration for %s"),
603 num, second->tag);
604 i += SUB_INDENT;
605 complain_indent (&first->location, complaint, &i,
606 _("previous declaration for %s"),
607 first->tag);
608 }
609
610 /*--------------------------------------------------.
611 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
612 `--------------------------------------------------*/
613
614 static inline bool
615 symbol_translation (symbol *this)
616 {
617 /* Non-terminal? */
618 if (this->class == token_sym
619 && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
620 {
621 /* A token which translation has already been set? */
622 if (token_translations[this->user_token_number] != undeftoken->number)
623 user_token_number_redeclaration
624 (this->user_token_number,
625 symbols[token_translations[this->user_token_number]],
626 this);
627 else
628 token_translations[this->user_token_number] = this->number;
629 }
630
631 return true;
632 }
633
634 static bool
635 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
636 {
637 return symbol_translation (this);
638 }
639
640
641 /*---------------------------------------.
642 | Symbol and semantic type hash tables. |
643 `---------------------------------------*/
644
645 /* Initial capacity of symbol and semantic type hash table. */
646 #define HT_INITIAL_CAPACITY 257
647
648 static struct hash_table *symbol_table = NULL;
649 static struct hash_table *semantic_type_table = NULL;
650
651 static inline bool
652 hash_compare_symbol (const symbol *m1, const symbol *m2)
653 {
654 /* Since tags are unique, we can compare the pointers themselves. */
655 return UNIQSTR_EQ (m1->tag, m2->tag);
656 }
657
658 static inline bool
659 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
660 {
661 /* Since names are unique, we can compare the pointers themselves. */
662 return UNIQSTR_EQ (m1->tag, m2->tag);
663 }
664
665 static bool
666 hash_symbol_comparator (void const *m1, void const *m2)
667 {
668 return hash_compare_symbol (m1, m2);
669 }
670
671 static bool
672 hash_semantic_type_comparator (void const *m1, void const *m2)
673 {
674 return hash_compare_semantic_type (m1, m2);
675 }
676
677 static inline size_t
678 hash_symbol (const symbol *m, size_t tablesize)
679 {
680 /* Since tags are unique, we can hash the pointer itself. */
681 return ((uintptr_t) m->tag) % tablesize;
682 }
683
684 static inline size_t
685 hash_semantic_type (const semantic_type *m, size_t tablesize)
686 {
687 /* Since names are unique, we can hash the pointer itself. */
688 return ((uintptr_t) m->tag) % tablesize;
689 }
690
691 static size_t
692 hash_symbol_hasher (void const *m, size_t tablesize)
693 {
694 return hash_symbol (m, tablesize);
695 }
696
697 static size_t
698 hash_semantic_type_hasher (void const *m, size_t tablesize)
699 {
700 return hash_semantic_type (m, tablesize);
701 }
702
703 /*-------------------------------.
704 | Create the symbol hash table. |
705 `-------------------------------*/
706
707 void
708 symbols_new (void)
709 {
710 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
711 NULL,
712 hash_symbol_hasher,
713 hash_symbol_comparator,
714 free);
715 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
716 NULL,
717 hash_semantic_type_hasher,
718 hash_semantic_type_comparator,
719 free);
720 }
721
722
723 /*----------------------------------------------------------------.
724 | Find the symbol named KEY, and return it. If it does not exist |
725 | yet, create it. |
726 `----------------------------------------------------------------*/
727
728 symbol *
729 symbol_from_uniqstr (const uniqstr key, location loc)
730 {
731 symbol probe;
732 symbol *entry;
733
734 probe.tag = key;
735 entry = hash_lookup (symbol_table, &probe);
736
737 if (!entry)
738 {
739 /* First insertion in the hash. */
740 aver (!symbols_sorted);
741 entry = symbol_new (key, loc);
742 if (!hash_insert (symbol_table, entry))
743 xalloc_die ();
744 }
745 return entry;
746 }
747
748
749 /*-----------------------------------------------------------------------.
750 | Find the semantic type named KEY, and return it. If it does not exist |
751 | yet, create it. |
752 `-----------------------------------------------------------------------*/
753
754 semantic_type *
755 semantic_type_from_uniqstr (const uniqstr key, const location *loc)
756 {
757 semantic_type probe;
758 semantic_type *entry;
759
760 probe.tag = key;
761 entry = hash_lookup (semantic_type_table, &probe);
762
763 if (!entry)
764 {
765 /* First insertion in the hash. */
766 entry = semantic_type_new (key, loc);
767 if (!hash_insert (semantic_type_table, entry))
768 xalloc_die ();
769 }
770 return entry;
771 }
772
773
774 /*----------------------------------------------------------------.
775 | Find the symbol named KEY, and return it. If it does not exist |
776 | yet, create it. |
777 `----------------------------------------------------------------*/
778
779 symbol *
780 symbol_get (const char *key, location loc)
781 {
782 return symbol_from_uniqstr (uniqstr_new (key), loc);
783 }
784
785
786 /*-----------------------------------------------------------------------.
787 | Find the semantic type named KEY, and return it. If it does not exist |
788 | yet, create it. |
789 `-----------------------------------------------------------------------*/
790
791 semantic_type *
792 semantic_type_get (const char *key, const location *loc)
793 {
794 return semantic_type_from_uniqstr (uniqstr_new (key), loc);
795 }
796
797
798 /*------------------------------------------------------------------.
799 | Generate a dummy nonterminal, whose name cannot conflict with the |
800 | user's names. |
801 `------------------------------------------------------------------*/
802
803 symbol *
804 dummy_symbol_get (location loc)
805 {
806 /* Incremented for each generated symbol. */
807 static int dummy_count = 0;
808 static char buf[256];
809
810 symbol *sym;
811
812 sprintf (buf, "$@%d", ++dummy_count);
813 sym = symbol_get (buf, loc);
814 sym->class = nterm_sym;
815 sym->number = nvars++;
816 return sym;
817 }
818
819 bool
820 symbol_is_dummy (const symbol *sym)
821 {
822 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
823 }
824
825 /*-------------------.
826 | Free the symbols. |
827 `-------------------*/
828
829 void
830 symbols_free (void)
831 {
832 hash_free (symbol_table);
833 hash_free (semantic_type_table);
834 free (symbols);
835 free (symbols_sorted);
836 free (semantic_types_sorted);
837 }
838
839
840 /*---------------------------------------------------------------.
841 | Look for undefined symbols, report an error, and consider them |
842 | terminals. |
843 `---------------------------------------------------------------*/
844
845 static int
846 symbols_cmp (symbol const *a, symbol const *b)
847 {
848 return strcmp (a->tag, b->tag);
849 }
850
851 static int
852 symbols_cmp_qsort (void const *a, void const *b)
853 {
854 return symbols_cmp (*(symbol * const *)a, *(symbol * const *)b);
855 }
856
857 static void
858 symbols_do (Hash_processor processor, void *processor_data,
859 struct hash_table *table, symbol ***sorted)
860 {
861 size_t count = hash_get_n_entries (table);
862 if (!*sorted)
863 {
864 *sorted = xnmalloc (count, sizeof **sorted);
865 hash_get_entries (table, (void**)*sorted, count);
866 qsort (*sorted, count, sizeof **sorted, symbols_cmp_qsort);
867 }
868 {
869 size_t i;
870 for (i = 0; i < count; ++i)
871 processor ((*sorted)[i], processor_data);
872 }
873 }
874
875 /*--------------------------------------------------------------.
876 | Check that all the symbols are defined. Report any undefined |
877 | symbols and consider them nonterminals. |
878 `--------------------------------------------------------------*/
879
880 void
881 symbols_check_defined (void)
882 {
883 symbols_do (symbol_check_defined_processor, NULL,
884 symbol_table, &symbols_sorted);
885 symbols_do (semantic_type_check_defined_processor, NULL,
886 semantic_type_table, &semantic_types_sorted);
887 }
888
889 /*------------------------------------------------------------------.
890 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
891 | number. |
892 `------------------------------------------------------------------*/
893
894 static void
895 symbols_token_translations_init (void)
896 {
897 bool num_256_available_p = true;
898 int i;
899
900 /* Find the highest user token number, and whether 256, the POSIX
901 preferred user token number for the error token, is used. */
902 max_user_token_number = 0;
903 for (i = 0; i < ntokens; ++i)
904 {
905 symbol *this = symbols[i];
906 if (this->user_token_number != USER_NUMBER_UNDEFINED)
907 {
908 if (this->user_token_number > max_user_token_number)
909 max_user_token_number = this->user_token_number;
910 if (this->user_token_number == 256)
911 num_256_available_p = false;
912 }
913 }
914
915 /* If 256 is not used, assign it to error, to follow POSIX. */
916 if (num_256_available_p
917 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
918 errtoken->user_token_number = 256;
919
920 /* Set the missing user numbers. */
921 if (max_user_token_number < 256)
922 max_user_token_number = 256;
923
924 for (i = 0; i < ntokens; ++i)
925 {
926 symbol *this = symbols[i];
927 if (this->user_token_number == USER_NUMBER_UNDEFINED)
928 this->user_token_number = ++max_user_token_number;
929 if (this->user_token_number > max_user_token_number)
930 max_user_token_number = this->user_token_number;
931 }
932
933 token_translations = xnmalloc (max_user_token_number + 1,
934 sizeof *token_translations);
935
936 /* Initialize all entries for literal tokens to the internal token
937 number for $undefined, which represents all invalid inputs. */
938 for (i = 0; i < max_user_token_number + 1; i++)
939 token_translations[i] = undeftoken->number;
940 symbols_do (symbol_translation_processor, NULL,
941 symbol_table, &symbols_sorted);
942 }
943
944
945 /*----------------------------------------------------------------.
946 | Assign symbol numbers, and write definition of token names into |
947 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
948 `----------------------------------------------------------------*/
949
950 void
951 symbols_pack (void)
952 {
953 symbols_do (symbol_check_alias_consistency_processor, NULL,
954 symbol_table, &symbols_sorted);
955
956 symbols = xcalloc (nsyms, sizeof *symbols);
957 symbols_do (symbol_pack_processor, NULL, symbol_table, &symbols_sorted);
958
959 /* Aliases leave empty slots in symbols, so remove them. */
960 {
961 int writei;
962 int readi;
963 int nsyms_old = nsyms;
964 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
965 {
966 if (symbols[readi] == NULL)
967 {
968 nsyms -= 1;
969 ntokens -= 1;
970 }
971 else
972 {
973 symbols[writei] = symbols[readi];
974 symbols[writei]->number = writei;
975 if (symbols[writei]->alias)
976 symbols[writei]->alias->number = writei;
977 writei += 1;
978 }
979 }
980 }
981 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
982
983 symbols_token_translations_init ();
984
985 if (startsymbol->class == unknown_sym)
986 complain (&startsymbol_location, fatal,
987 _("the start symbol %s is undefined"),
988 startsymbol->tag);
989 else if (startsymbol->class == token_sym)
990 complain (&startsymbol_location, fatal,
991 _("the start symbol %s is a token"),
992 startsymbol->tag);
993 }
994
995 /*---------------------------------.
996 | Initialize relation graph nodes. |
997 `---------------------------------*/
998
999 static void
1000 init_prec_nodes (void)
1001 {
1002 int i;
1003 prec_nodes = xcalloc (nsyms, sizeof *prec_nodes);
1004 for (i = 0; i < nsyms; ++i)
1005 {
1006 prec_nodes[i] = xmalloc (sizeof *prec_nodes[i]);
1007 symgraph *s = prec_nodes[i];
1008 s->id = i;
1009 s->succ = 0;
1010 s->pred = 0;
1011 }
1012 }
1013
1014 /*----------------.
1015 | Create a link. |
1016 `----------------*/
1017
1018 static symgraphlink *
1019 symgraphlink_new (graphid id, symgraphlink *next)
1020 {
1021 symgraphlink *l = xmalloc (sizeof *l);
1022 l->id = id;
1023 l->next = next;
1024 return l;
1025 }
1026
1027
1028 /*------------------------------------------------------------------.
1029 | Register the second symbol of the precedence relation, and return |
1030 | whether this relation is new. Use only in register_precedence. |
1031 `------------------------------------------------------------------*/
1032
1033 static bool
1034 register_precedence_second_symbol (symgraphlink **first, graphid sym)
1035 {
1036 if (!*first || sym < (*first)->id)
1037 *first = symgraphlink_new (sym, *first);
1038 else
1039 {
1040 symgraphlink *slist = *first;
1041
1042 while (slist->next && slist->next->id <= sym)
1043 slist = slist->next;
1044
1045 if (slist->id == sym)
1046 /* Relation already present. */
1047 return false;
1048
1049 slist->next = symgraphlink_new (sym, slist->next);
1050 }
1051 return true;
1052 }
1053
1054 /*------------------------------------------------------------------.
1055 | Register a new relation between symbols as used. The first symbol |
1056 | has a greater precedence than the second one. |
1057 `------------------------------------------------------------------*/
1058
1059 void
1060 register_precedence (graphid first, graphid snd)
1061 {
1062 if (!prec_nodes)
1063 init_prec_nodes ();
1064 register_precedence_second_symbol (&(prec_nodes[first]->succ), snd);
1065 register_precedence_second_symbol (&(prec_nodes[snd]->pred), first);
1066 }
1067
1068
1069 /*---------------------------------------.
1070 | Deep clear a linked / adjacency list). |
1071 `---------------------------------------*/
1072
1073 static void
1074 linkedlist_free (symgraphlink *node)
1075 {
1076 if (node)
1077 {
1078 while (node->next)
1079 {
1080 symgraphlink *tmp = node->next;
1081 free (node);
1082 node = tmp;
1083 }
1084 free (node);
1085 }
1086 }
1087
1088 /*----------------------------------------------.
1089 | Clear and destroy association tracking table. |
1090 `----------------------------------------------*/
1091
1092 static void
1093 assoc_free (void)
1094 {
1095 int i;
1096 for (i = 0; i < nsyms; ++i)
1097 {
1098 linkedlist_free (prec_nodes[i]->pred);
1099 linkedlist_free (prec_nodes[i]->succ);
1100 free (prec_nodes[i]);
1101 }
1102 free (prec_nodes);
1103 }
1104
1105 /*---------------------------------------.
1106 | Initialize association tracking table. |
1107 `---------------------------------------*/
1108
1109 static void
1110 init_assoc (void)
1111 {
1112 graphid i;
1113 used_assoc = xcalloc (nsyms, sizeof *used_assoc);
1114 for (i = 0; i < nsyms; ++i)
1115 used_assoc[i] = false;
1116 }
1117
1118 /*------------------------------------------------------------------.
1119 | Test if the associativity for the symbols is defined and useless. |
1120 `------------------------------------------------------------------*/
1121
1122 static inline bool
1123 is_assoc_useless (symbol *s)
1124 {
1125 return s
1126 && s->assoc != undef_assoc
1127 && s->assoc != precedence_assoc
1128 && !used_assoc[s->number];
1129 }
1130
1131 /*-------------------------------.
1132 | Register a used associativity. |
1133 `-------------------------------*/
1134
1135 void
1136 register_assoc (graphid i, graphid j)
1137 {
1138 if (!used_assoc)
1139 init_assoc ();
1140 used_assoc[i] = true;
1141 used_assoc[j] = true;
1142 }
1143
1144 /*--------------------------------------------------.
1145 | Print a warning for unused precedence relations. |
1146 `--------------------------------------------------*/
1147
1148 void
1149 print_precedence_warnings (void)
1150 {
1151 int i;
1152 if (!prec_nodes)
1153 init_prec_nodes ();
1154 if (!used_assoc)
1155 init_assoc ();
1156 for (i = 0; i < nsyms; ++i)
1157 {
1158 symbol *s = symbols[i];
1159 if (s
1160 && s->prec != 0
1161 && !prec_nodes[i]->pred
1162 && !prec_nodes[i]->succ)
1163 {
1164 if (is_assoc_useless (s))
1165 complain (&s->prec_location, Wprecedence,
1166 _("useless precedence and associativity for %s"), s->tag);
1167 else if (s->assoc == precedence_assoc)
1168 complain (&s->prec_location, Wprecedence,
1169 _("useless precedence for %s"), s->tag);
1170 }
1171 else if (is_assoc_useless (s))
1172 complain (&s->prec_location, Wprecedence,
1173 _("useless associativity for %s, use %%precedence"), s->tag);
1174 }
1175 free (used_assoc);
1176 assoc_free ();
1177 }