1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000-2002, 2004-2013 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
30 /*-------------------------------------------------------------------.
31 | Symbols sorted by tag. Allocated by the first invocation of |
32 | symbols_do, after which no more symbols should be created. |
33 `-------------------------------------------------------------------*/
35 static symbol
**symbols_sorted
= NULL
;
36 static symbol
**semantic_types_sorted
= NULL
;
38 /*------------------------.
39 | Distinguished symbols. |
40 `------------------------*/
42 symbol
*errtoken
= NULL
;
43 symbol
*undeftoken
= NULL
;
44 symbol
*endtoken
= NULL
;
45 symbol
*accept
= NULL
;
46 symbol
*startsymbol
= NULL
;
47 location startsymbol_location
;
49 /*---------------------------.
50 | Precedence relation graph. |
51 `---------------------------*/
53 static symgraph
**prec_nodes
;
55 /*-----------------------------------.
56 | Store which associativity is used. |
57 `-----------------------------------*/
59 bool *used_assoc
= NULL
;
61 /*---------------------------------.
62 | Create a new symbol, named TAG. |
63 `---------------------------------*/
66 symbol_new (uniqstr tag
, location loc
)
68 symbol
*res
= xmalloc (sizeof *res
);
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
);
80 res
->type_name
= NULL
;
83 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
84 code_props_none_init (&res
->props
[i
]);
87 res
->number
= NUMBER_UNDEFINED
;
89 res
->assoc
= undef_assoc
;
90 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
93 res
->class = unknown_sym
;
94 res
->status
= undeclared
;
96 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
97 complain (NULL
, fatal
, _("too many symbols in input grammar (limit is %d)"),
98 SYMBOL_NUMBER_MAXIMUM
);
104 code_props_type_string (code_props_type kind
)
109 return "%destructor";
116 /*----------------------------------------.
117 | Create a new semantic type, named TAG. |
118 `----------------------------------------*/
120 static semantic_type
*
121 semantic_type_new (uniqstr tag
, const location
*loc
)
123 semantic_type
*res
= xmalloc (sizeof *res
);
125 uniqstr_assert (tag
);
127 res
->location
= loc
? *loc
: empty_location
;
128 res
->status
= undeclared
;
131 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
132 code_props_none_init (&res
->props
[i
]);
143 #define SYMBOL_ATTR_PRINT(Attr) \
145 fprintf (f, " %s { %s }", #Attr, s->Attr)
147 #define SYMBOL_CODE_PRINT(Attr) \
148 if (s->props[Attr].code) \
149 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
152 symbol_print (symbol
const *s
, FILE *f
)
157 SYMBOL_ATTR_PRINT (type_name
);
158 SYMBOL_CODE_PRINT (destructor
);
159 SYMBOL_CODE_PRINT (printer
);
165 #undef SYMBOL_ATTR_PRINT
166 #undef SYMBOL_CODE_PRINT
169 /*----------------------------------.
170 | Whether S is a valid identifier. |
171 `----------------------------------*/
174 is_identifier (uniqstr s
)
176 static char const alphanum
[26 + 26 + 1 + 10] =
177 "abcdefghijklmnopqrstuvwxyz"
178 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
181 if (!s
|| ! memchr (alphanum
, *s
, sizeof alphanum
- 10))
184 if (! memchr (alphanum
, *s
, sizeof alphanum
))
190 /*-----------------------------------------------.
191 | Get the identifier associated to this symbol. |
192 `-----------------------------------------------*/
194 symbol_id_get (symbol
const *sym
)
196 aver (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
);
199 return is_identifier (sym
->tag
) ? sym
->tag
: 0;
203 /*------------------------------------------------------------------.
204 | Complain that S's WHAT is redeclared at SECOND, and was first set |
206 `------------------------------------------------------------------*/
209 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
213 complain_indent (&second
, complaint
, &i
,
214 _("%s redeclaration for %s"), what
, s
->tag
);
216 complain_indent (&first
, complaint
, &i
,
217 _("previous declaration"));
221 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
225 complain_indent (&second
, complaint
, &i
,
226 _("%s redeclaration for <%s>"), what
, s
->tag
);
228 complain_indent (&first
, complaint
, &i
,
229 _("previous declaration"));
234 /*-----------------------------------------------------------------.
235 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
237 `-----------------------------------------------------------------*/
240 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
245 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
248 uniqstr_assert (type_name
);
249 sym
->type_name
= type_name
;
250 sym
->type_location
= loc
;
255 /*--------------------------------------------------------.
256 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
257 `--------------------------------------------------------*/
260 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
261 code_props
const *code
)
263 if (sym
->props
[kind
].code
)
264 symbol_redeclaration (sym
, code_props_type_string (kind
),
265 sym
->props
[kind
].location
,
268 sym
->props
[kind
] = *code
;
271 /*-----------------------------------------------------.
272 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
273 `-----------------------------------------------------*/
276 semantic_type_code_props_set (semantic_type
*type
,
277 code_props_type kind
,
278 code_props
const *code
)
280 if (type
->props
[kind
].code
)
281 semantic_type_redeclaration (type
, code_props_type_string (kind
),
282 type
->props
[kind
].location
,
285 type
->props
[kind
] = *code
;
288 /*---------------------------------------------------.
289 | Get the computed %destructor or %printer for SYM. |
290 `---------------------------------------------------*/
293 symbol_code_props_get (symbol
*sym
, code_props_type kind
)
295 /* Per-symbol code props. */
296 if (sym
->props
[kind
].code
)
297 return &sym
->props
[kind
];
299 /* Per-type code props. */
303 &semantic_type_get (sym
->type_name
, NULL
)->props
[kind
];
308 /* Apply default code props's only to user-defined symbols. */
309 if (sym
->tag
[0] != '$' && sym
!= errtoken
)
312 &semantic_type_get (sym
->type_name
? "*" : "", NULL
)->props
[kind
];
316 return &code_props_none
;
319 /*-----------------------------------------------------------------.
320 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
321 | with UNDEF_ASSOC as ASSOC. |
322 `-----------------------------------------------------------------*/
325 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
327 if (a
!= undef_assoc
)
330 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
336 sym
->prec_location
= loc
;
340 /* Only terminals have a precedence. */
341 symbol_class_set (sym
, token_sym
, loc
, false);
345 /*------------------------------------.
346 | Set the CLASS associated with SYM. |
347 `------------------------------------*/
350 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
353 if (sym
->class != unknown_sym
&& sym
->class != class)
355 complain (&loc
, complaint
, _("symbol %s redefined"), sym
->tag
);
356 /* Don't report both "redefined" and "redeclared". */
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
++;
369 if (sym
->status
== declared
&& !warned
)
370 complain (&loc
, Wother
, _("symbol %s redeclared"), sym
->tag
);
372 sym
->status
= declared
;
377 /*------------------------------------------------.
378 | Set the USER_TOKEN_NUMBER associated with SYM. |
379 `------------------------------------------------*/
382 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
384 int *user_token_numberp
;
386 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
387 user_token_numberp
= &sym
->user_token_number
;
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"),
395 *user_token_numberp
= user_token_number
;
396 /* User defined $end token? */
397 if (user_token_number
== 0)
400 /* It is always mapped to 0, so it was already counted in
402 if (endtoken
->number
!= NUMBER_UNDEFINED
)
404 endtoken
->number
= 0;
409 /*----------------------------------------------------------.
410 | If SYM is not defined, report an error, and consider it a |
412 `----------------------------------------------------------*/
415 symbol_check_defined (symbol
*sym
)
417 if (sym
->class == unknown_sym
)
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"),
425 sym
->class = nterm_sym
;
426 sym
->number
= nvars
++;
431 for (i
= 0; i
< 2; ++i
)
432 symbol_code_props_get (sym
, i
)->is_used
= true;
435 /* Set the semantic type status associated to the current symbol to
436 'declared' so that we could check semantic types unnecessary uses. */
439 semantic_type
*sem_type
= semantic_type_get (sym
->type_name
, NULL
);
441 sem_type
->status
= declared
;
448 semantic_type_check_defined (semantic_type
*sem_type
)
450 /* <*> and <> do not have to be "declared". */
451 if (sem_type
->status
== declared
453 || STREQ (sem_type
->tag
, "*"))
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
);
464 complain (&sem_type
->location
, Wother
,
465 _("type <%s> is used, but is not associated to any symbol"),
472 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
474 return symbol_check_defined (sym
);
478 semantic_type_check_defined_processor (void *sem_type
,
479 void *null ATTRIBUTE_UNUSED
)
481 return semantic_type_check_defined (sem_type
);
486 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
489 complain (&loc
, Wother
,
490 _("symbol %s used more than once as a literal string"), str
->tag
);
492 complain (&loc
, Wother
,
493 _("symbol %s given more than one literal string"), sym
->tag
);
496 str
->class = token_sym
;
497 str
->user_token_number
= sym
->user_token_number
;
498 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
501 str
->number
= sym
->number
;
502 symbol_type_set (str
, sym
->type_name
, loc
);
507 /*---------------------------------------------------------.
508 | Check that THIS, and its alias, have same precedence and |
510 `---------------------------------------------------------*/
513 symbol_check_alias_consistency (symbol
*this)
516 symbol
*str
= this->alias
;
518 /* Check only the symbol in the symbol-string pair. */
520 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
523 if (str
->type_name
!= sym
->type_name
)
526 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
528 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
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
]);
541 if (sym
->prec
|| str
->prec
)
544 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
547 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
553 symbol_check_alias_consistency_processor (void *this,
554 void *null ATTRIBUTE_UNUSED
)
556 symbol_check_alias_consistency (this);
561 /*-------------------------------------------------------------------.
562 | Assign a symbol number, and write the definition of the token name |
563 | into FDEFINES. Put in SYMBOLS. |
564 `-------------------------------------------------------------------*/
567 symbol_pack (symbol
*this)
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
)
575 symbols
[this->number
] = this;
580 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
582 return symbol_pack (this);
587 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
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.
593 However, error messages make more sense if we keep the first
594 declaration first. */
595 if (location_cmp (first
->location
, second
->location
) > 0)
601 complain_indent (&second
->location
, complaint
, &i
,
602 _("user token number %d redeclaration for %s"),
605 complain_indent (&first
->location
, complaint
, &i
,
606 _("previous declaration for %s"),
610 /*--------------------------------------------------.
611 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
612 `--------------------------------------------------*/
615 symbol_translation (symbol
*this)
618 if (this->class == token_sym
619 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
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
]],
628 token_translations
[this->user_token_number
] = this->number
;
635 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
637 return symbol_translation (this);
641 /*---------------------------------------.
642 | Symbol and semantic type hash tables. |
643 `---------------------------------------*/
645 /* Initial capacity of symbol and semantic type hash table. */
646 #define HT_INITIAL_CAPACITY 257
648 static struct hash_table
*symbol_table
= NULL
;
649 static struct hash_table
*semantic_type_table
= NULL
;
652 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
654 /* Since tags are unique, we can compare the pointers themselves. */
655 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
659 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
661 /* Since names are unique, we can compare the pointers themselves. */
662 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
666 hash_symbol_comparator (void const *m1
, void const *m2
)
668 return hash_compare_symbol (m1
, m2
);
672 hash_semantic_type_comparator (void const *m1
, void const *m2
)
674 return hash_compare_semantic_type (m1
, m2
);
678 hash_symbol (const symbol
*m
, size_t tablesize
)
680 /* Since tags are unique, we can hash the pointer itself. */
681 return ((uintptr_t) m
->tag
) % tablesize
;
685 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
687 /* Since names are unique, we can hash the pointer itself. */
688 return ((uintptr_t) m
->tag
) % tablesize
;
692 hash_symbol_hasher (void const *m
, size_t tablesize
)
694 return hash_symbol (m
, tablesize
);
698 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
700 return hash_semantic_type (m
, tablesize
);
703 /*-------------------------------.
704 | Create the symbol hash table. |
705 `-------------------------------*/
710 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
713 hash_symbol_comparator
,
715 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
717 hash_semantic_type_hasher
,
718 hash_semantic_type_comparator
,
723 /*----------------------------------------------------------------.
724 | Find the symbol named KEY, and return it. If it does not exist |
726 `----------------------------------------------------------------*/
729 symbol_from_uniqstr (const uniqstr key
, location loc
)
735 entry
= hash_lookup (symbol_table
, &probe
);
739 /* First insertion in the hash. */
740 aver (!symbols_sorted
);
741 entry
= symbol_new (key
, loc
);
742 if (!hash_insert (symbol_table
, entry
))
749 /*-----------------------------------------------------------------------.
750 | Find the semantic type named KEY, and return it. If it does not exist |
752 `-----------------------------------------------------------------------*/
755 semantic_type_from_uniqstr (const uniqstr key
, const location
*loc
)
758 semantic_type
*entry
;
761 entry
= hash_lookup (semantic_type_table
, &probe
);
765 /* First insertion in the hash. */
766 entry
= semantic_type_new (key
, loc
);
767 if (!hash_insert (semantic_type_table
, entry
))
774 /*----------------------------------------------------------------.
775 | Find the symbol named KEY, and return it. If it does not exist |
777 `----------------------------------------------------------------*/
780 symbol_get (const char *key
, location loc
)
782 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
786 /*-----------------------------------------------------------------------.
787 | Find the semantic type named KEY, and return it. If it does not exist |
789 `-----------------------------------------------------------------------*/
792 semantic_type_get (const char *key
, const location
*loc
)
794 return semantic_type_from_uniqstr (uniqstr_new (key
), loc
);
798 /*------------------------------------------------------------------.
799 | Generate a dummy nonterminal, whose name cannot conflict with the |
801 `------------------------------------------------------------------*/
804 dummy_symbol_get (location loc
)
806 /* Incremented for each generated symbol. */
807 static int dummy_count
= 0;
808 static char buf
[256];
812 sprintf (buf
, "$@%d", ++dummy_count
);
813 sym
= symbol_get (buf
, loc
);
814 sym
->class = nterm_sym
;
815 sym
->number
= nvars
++;
820 symbol_is_dummy (const symbol
*sym
)
822 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
825 /*-------------------.
826 | Free the symbols. |
827 `-------------------*/
832 hash_free (symbol_table
);
833 hash_free (semantic_type_table
);
835 free (symbols_sorted
);
836 free (semantic_types_sorted
);
840 /*---------------------------------------------------------------.
841 | Look for undefined symbols, report an error, and consider them |
843 `---------------------------------------------------------------*/
846 symbols_cmp (symbol
const *a
, symbol
const *b
)
848 return strcmp (a
->tag
, b
->tag
);
852 symbols_cmp_qsort (void const *a
, void const *b
)
854 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
858 symbols_do (Hash_processor processor
, void *processor_data
,
859 struct hash_table
*table
, symbol
***sorted
)
861 size_t count
= hash_get_n_entries (table
);
864 *sorted
= xnmalloc (count
, sizeof **sorted
);
865 hash_get_entries (table
, (void**)*sorted
, count
);
866 qsort (*sorted
, count
, sizeof **sorted
, symbols_cmp_qsort
);
870 for (i
= 0; i
< count
; ++i
)
871 processor ((*sorted
)[i
], processor_data
);
875 /*--------------------------------------------------------------.
876 | Check that all the symbols are defined. Report any undefined |
877 | symbols and consider them nonterminals. |
878 `--------------------------------------------------------------*/
881 symbols_check_defined (void)
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
);
889 /*------------------------------------------------------------------.
890 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
892 `------------------------------------------------------------------*/
895 symbols_token_translations_init (void)
897 bool num_256_available_p
= true;
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
)
905 symbol
*this = symbols
[i
];
906 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
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;
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;
920 /* Set the missing user numbers. */
921 if (max_user_token_number
< 256)
922 max_user_token_number
= 256;
924 for (i
= 0; i
< ntokens
; ++i
)
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
;
933 token_translations
= xnmalloc (max_user_token_number
+ 1,
934 sizeof *token_translations
);
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
);
945 /*----------------------------------------------------------------.
946 | Assign symbol numbers, and write definition of token names into |
947 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
948 `----------------------------------------------------------------*/
953 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
954 symbol_table
, &symbols_sorted
);
956 symbols
= xcalloc (nsyms
, sizeof *symbols
);
957 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, &symbols_sorted
);
959 /* Aliases leave empty slots in symbols, so remove them. */
963 int nsyms_old
= nsyms
;
964 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
966 if (symbols
[readi
] == NULL
)
973 symbols
[writei
] = symbols
[readi
];
974 symbols
[writei
]->number
= writei
;
975 if (symbols
[writei
]->alias
)
976 symbols
[writei
]->alias
->number
= writei
;
981 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
983 symbols_token_translations_init ();
985 if (startsymbol
->class == unknown_sym
)
986 complain (&startsymbol_location
, fatal
,
987 _("the start symbol %s is undefined"),
989 else if (startsymbol
->class == token_sym
)
990 complain (&startsymbol_location
, fatal
,
991 _("the start symbol %s is a token"),
995 /*---------------------------------.
996 | Initialize relation graph nodes. |
997 `---------------------------------*/
1000 init_prec_nodes (void)
1003 prec_nodes
= xcalloc (nsyms
, sizeof *prec_nodes
);
1004 for (i
= 0; i
< nsyms
; ++i
)
1006 prec_nodes
[i
] = xmalloc (sizeof *prec_nodes
[i
]);
1007 symgraph
*s
= prec_nodes
[i
];
1018 static symgraphlink
*
1019 symgraphlink_new (graphid id
, symgraphlink
*next
)
1021 symgraphlink
*l
= xmalloc (sizeof *l
);
1028 /*------------------------------------------------------------------.
1029 | Register the second symbol of the precedence relation, and return |
1030 | whether this relation is new. Use only in register_precedence. |
1031 `------------------------------------------------------------------*/
1034 register_precedence_second_symbol (symgraphlink
**first
, graphid sym
)
1036 if (!*first
|| sym
< (*first
)->id
)
1037 *first
= symgraphlink_new (sym
, *first
);
1040 symgraphlink
*slist
= *first
;
1042 while (slist
->next
&& slist
->next
->id
<= sym
)
1043 slist
= slist
->next
;
1045 if (slist
->id
== sym
)
1046 /* Relation already present. */
1049 slist
->next
= symgraphlink_new (sym
, slist
->next
);
1054 /*------------------------------------------------------------------.
1055 | Register a new relation between symbols as used. The first symbol |
1056 | has a greater precedence than the second one. |
1057 `------------------------------------------------------------------*/
1060 register_precedence (graphid first
, graphid snd
)
1064 register_precedence_second_symbol (&(prec_nodes
[first
]->succ
), snd
);
1065 register_precedence_second_symbol (&(prec_nodes
[snd
]->pred
), first
);
1069 /*---------------------------------------.
1070 | Deep clear a linked / adjacency list). |
1071 `---------------------------------------*/
1074 linkedlist_free (symgraphlink
*node
)
1080 symgraphlink
*tmp
= node
->next
;
1088 /*----------------------------------------------.
1089 | Clear and destroy association tracking table. |
1090 `----------------------------------------------*/
1096 for (i
= 0; i
< nsyms
; ++i
)
1098 linkedlist_free (prec_nodes
[i
]->pred
);
1099 linkedlist_free (prec_nodes
[i
]->succ
);
1100 free (prec_nodes
[i
]);
1105 /*---------------------------------------.
1106 | Initialize association tracking table. |
1107 `---------------------------------------*/
1113 used_assoc
= xcalloc (nsyms
, sizeof *used_assoc
);
1114 for (i
= 0; i
< nsyms
; ++i
)
1115 used_assoc
[i
] = false;
1118 /*------------------------------------------------------------------.
1119 | Test if the associativity for the symbols is defined and useless. |
1120 `------------------------------------------------------------------*/
1123 is_assoc_useless (symbol
*s
)
1126 && s
->assoc
!= undef_assoc
1127 && s
->assoc
!= precedence_assoc
1128 && !used_assoc
[s
->number
];
1131 /*-------------------------------.
1132 | Register a used associativity. |
1133 `-------------------------------*/
1136 register_assoc (graphid i
, graphid j
)
1140 used_assoc
[i
] = true;
1141 used_assoc
[j
] = true;
1144 /*--------------------------------------------------.
1145 | Print a warning for unused precedence relations. |
1146 `--------------------------------------------------*/
1149 print_precedence_warnings (void)
1156 for (i
= 0; i
< nsyms
; ++i
)
1158 symbol
*s
= symbols
[i
];
1161 && !prec_nodes
[i
]->pred
1162 && !prec_nodes
[i
]->succ
)
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
);
1171 else if (is_assoc_useless (s
))
1172 complain (&s
->prec_location
, Wprecedence
,
1173 _("useless associativity for %s, use %%precedence"), s
->tag
);