1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000-2002, 2004-2014 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
);
103 /* If needed, swap first and second so that first has the earliest
104 location (according to location_cmp).
106 Many symbol features (e.g., user token numbers) are not assigned
107 during the parsing, but in a second step, via a traversal of the
108 symbol table sorted on tag.
110 However, error messages make more sense if we keep the first
115 void symbols_sort (symbol
**first
, symbol
**second
)
117 if (0 < location_cmp ((*first
)->location
, (*second
)->location
))
119 symbol
* tmp
= *first
;
125 /* Likewise, for locations. */
128 void locations_sort (location
*first
, location
*second
)
130 if (0 < location_cmp (*first
, *second
))
132 location tmp
= *first
;
139 code_props_type_string (code_props_type kind
)
144 return "%destructor";
151 /*----------------------------------------.
152 | Create a new semantic type, named TAG. |
153 `----------------------------------------*/
155 static semantic_type
*
156 semantic_type_new (uniqstr tag
, const location
*loc
)
158 semantic_type
*res
= xmalloc (sizeof *res
);
160 uniqstr_assert (tag
);
162 res
->location
= loc
? *loc
: empty_location
;
163 res
->status
= undeclared
;
166 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
167 code_props_none_init (&res
->props
[i
]);
178 #define SYMBOL_ATTR_PRINT(Attr) \
180 fprintf (f, " %s { %s }", #Attr, s->Attr)
182 #define SYMBOL_CODE_PRINT(Attr) \
183 if (s->props[Attr].code) \
184 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
187 symbol_print (symbol
const *s
, FILE *f
)
192 SYMBOL_ATTR_PRINT (type_name
);
193 SYMBOL_CODE_PRINT (destructor
);
194 SYMBOL_CODE_PRINT (printer
);
200 #undef SYMBOL_ATTR_PRINT
201 #undef SYMBOL_CODE_PRINT
204 /*----------------------------------.
205 | Whether S is a valid identifier. |
206 `----------------------------------*/
209 is_identifier (uniqstr s
)
211 static char const alphanum
[26 + 26 + 1 + 10] =
212 "abcdefghijklmnopqrstuvwxyz"
213 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
216 if (!s
|| ! memchr (alphanum
, *s
, sizeof alphanum
- 10))
219 if (! memchr (alphanum
, *s
, sizeof alphanum
))
225 /*-----------------------------------------------.
226 | Get the identifier associated to this symbol. |
227 `-----------------------------------------------*/
229 symbol_id_get (symbol
const *sym
)
231 aver (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
);
234 return is_identifier (sym
->tag
) ? sym
->tag
: 0;
238 /*------------------------------------------------------------------.
239 | Complain that S's WHAT is redeclared at SECOND, and was first set |
241 `------------------------------------------------------------------*/
244 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
248 locations_sort (&first
, &second
);
249 complain_indent (&second
, complaint
, &i
,
250 _("%s redeclaration for %s"), what
, s
->tag
);
252 complain_indent (&first
, complaint
, &i
,
253 _("previous declaration"));
257 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
261 locations_sort (&first
, &second
);
262 complain_indent (&second
, complaint
, &i
,
263 _("%s redeclaration for <%s>"), what
, s
->tag
);
265 complain_indent (&first
, complaint
, &i
,
266 _("previous declaration"));
271 /*-----------------------------------------------------------------.
272 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
274 `-----------------------------------------------------------------*/
277 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
282 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
285 uniqstr_assert (type_name
);
286 sym
->type_name
= type_name
;
287 sym
->type_location
= loc
;
292 /*--------------------------------------------------------.
293 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
294 `--------------------------------------------------------*/
297 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
298 code_props
const *code
)
300 if (sym
->props
[kind
].code
)
301 symbol_redeclaration (sym
, code_props_type_string (kind
),
302 sym
->props
[kind
].location
,
305 sym
->props
[kind
] = *code
;
308 /*-----------------------------------------------------.
309 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
310 `-----------------------------------------------------*/
313 semantic_type_code_props_set (semantic_type
*type
,
314 code_props_type kind
,
315 code_props
const *code
)
317 if (type
->props
[kind
].code
)
318 semantic_type_redeclaration (type
, code_props_type_string (kind
),
319 type
->props
[kind
].location
,
322 type
->props
[kind
] = *code
;
325 /*---------------------------------------------------.
326 | Get the computed %destructor or %printer for SYM. |
327 `---------------------------------------------------*/
330 symbol_code_props_get (symbol
*sym
, code_props_type kind
)
332 /* Per-symbol code props. */
333 if (sym
->props
[kind
].code
)
334 return &sym
->props
[kind
];
336 /* Per-type code props. */
340 &semantic_type_get (sym
->type_name
, NULL
)->props
[kind
];
345 /* Apply default code props's only to user-defined symbols. */
346 if (sym
->tag
[0] != '$' && sym
!= errtoken
)
349 &semantic_type_get (sym
->type_name
? "*" : "", NULL
)->props
[kind
];
353 return &code_props_none
;
356 /*-----------------------------------------------------------------.
357 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
358 | with UNDEF_ASSOC as ASSOC. |
359 `-----------------------------------------------------------------*/
362 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
364 if (a
!= undef_assoc
)
367 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
373 sym
->prec_location
= loc
;
377 /* Only terminals have a precedence. */
378 symbol_class_set (sym
, token_sym
, loc
, false);
382 /*------------------------------------.
383 | Set the CLASS associated with SYM. |
384 `------------------------------------*/
387 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
390 if (sym
->class != unknown_sym
&& sym
->class != class)
392 complain (&loc
, complaint
, _("symbol %s redefined"), sym
->tag
);
393 /* Don't report both "redefined" and "redeclared". */
397 if (class == nterm_sym
&& sym
->class != nterm_sym
)
398 sym
->number
= nvars
++;
399 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
400 sym
->number
= ntokens
++;
406 if (sym
->status
== declared
&& !warned
)
407 complain (&loc
, Wother
, _("symbol %s redeclared"), sym
->tag
);
409 sym
->status
= declared
;
414 /*------------------------------------------------.
415 | Set the USER_TOKEN_NUMBER associated with SYM. |
416 `------------------------------------------------*/
419 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
421 int *user_token_numberp
;
423 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
424 user_token_numberp
= &sym
->user_token_number
;
426 user_token_numberp
= &sym
->alias
->user_token_number
;
427 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
428 && *user_token_numberp
!= user_token_number
)
429 complain (&loc
, complaint
, _("redefining user token number of %s"),
432 *user_token_numberp
= user_token_number
;
433 /* User defined $end token? */
434 if (user_token_number
== 0)
437 /* It is always mapped to 0, so it was already counted in
439 if (endtoken
->number
!= NUMBER_UNDEFINED
)
441 endtoken
->number
= 0;
446 /*----------------------------------------------------------.
447 | If SYM is not defined, report an error, and consider it a |
449 `----------------------------------------------------------*/
452 symbol_check_defined (symbol
*sym
)
454 if (sym
->class == unknown_sym
)
456 assert (sym
->status
!= declared
);
457 complain (&sym
->location
,
458 sym
->status
== needed
? complaint
: Wother
,
459 _("symbol %s is used, but is not defined as a token"
460 " and has no rules"),
462 sym
->class = nterm_sym
;
463 sym
->number
= nvars
++;
468 for (i
= 0; i
< 2; ++i
)
469 symbol_code_props_get (sym
, i
)->is_used
= true;
472 /* Set the semantic type status associated to the current symbol to
473 'declared' so that we could check semantic types unnecessary uses. */
476 semantic_type
*sem_type
= semantic_type_get (sym
->type_name
, NULL
);
478 sem_type
->status
= declared
;
485 semantic_type_check_defined (semantic_type
*sem_type
)
487 /* <*> and <> do not have to be "declared". */
488 if (sem_type
->status
== declared
490 || STREQ (sem_type
->tag
, "*"))
493 for (i
= 0; i
< 2; ++i
)
494 if (sem_type
->props
[i
].kind
!= CODE_PROPS_NONE
495 && ! sem_type
->props
[i
].is_used
)
496 complain (&sem_type
->location
, Wother
,
497 _("useless %s for type <%s>"),
498 code_props_type_string (i
), sem_type
->tag
);
501 complain (&sem_type
->location
, Wother
,
502 _("type <%s> is used, but is not associated to any symbol"),
509 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
511 return symbol_check_defined (sym
);
515 semantic_type_check_defined_processor (void *sem_type
,
516 void *null ATTRIBUTE_UNUSED
)
518 return semantic_type_check_defined (sem_type
);
523 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
526 complain (&loc
, Wother
,
527 _("symbol %s used more than once as a literal string"), str
->tag
);
529 complain (&loc
, Wother
,
530 _("symbol %s given more than one literal string"), sym
->tag
);
533 str
->class = token_sym
;
534 str
->user_token_number
= sym
->user_token_number
;
535 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
538 str
->number
= sym
->number
;
539 symbol_type_set (str
, sym
->type_name
, loc
);
544 /*---------------------------------------------------------.
545 | Check that THIS, and its alias, have same precedence and |
547 `---------------------------------------------------------*/
550 symbol_check_alias_consistency (symbol
*this)
553 symbol
*str
= this->alias
;
555 /* Check only the symbol in the symbol-string pair. */
557 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
560 if (str
->type_name
!= sym
->type_name
)
563 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
565 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
571 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
572 if (str
->props
[i
].code
)
573 symbol_code_props_set (sym
, i
, &str
->props
[i
]);
574 else if (sym
->props
[i
].code
)
575 symbol_code_props_set (str
, i
, &sym
->props
[i
]);
578 if (sym
->prec
|| str
->prec
)
581 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
584 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
590 symbol_check_alias_consistency_processor (void *this,
591 void *null ATTRIBUTE_UNUSED
)
593 symbol_check_alias_consistency (this);
598 /*-------------------------------------------------------------------.
599 | Assign a symbol number, and write the definition of the token name |
600 | into FDEFINES. Put in SYMBOLS. |
601 `-------------------------------------------------------------------*/
604 symbol_pack (symbol
*this)
606 aver (this->number
!= NUMBER_UNDEFINED
);
607 if (this->class == nterm_sym
)
608 this->number
+= ntokens
;
609 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
612 symbols
[this->number
] = this;
617 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
619 return symbol_pack (this);
624 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
627 symbols_sort (&first
, &second
);
628 complain_indent (&second
->location
, complaint
, &i
,
629 _("user token number %d redeclaration for %s"),
632 complain_indent (&first
->location
, complaint
, &i
,
633 _("previous declaration for %s"),
637 /*--------------------------------------------------.
638 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
639 `--------------------------------------------------*/
642 symbol_translation (symbol
*this)
645 if (this->class == token_sym
646 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
648 /* A token which translation has already been set? */
649 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
650 user_token_number_redeclaration
651 (this->user_token_number
,
652 symbols
[token_translations
[this->user_token_number
]],
655 token_translations
[this->user_token_number
] = this->number
;
662 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
664 return symbol_translation (this);
668 /*---------------------------------------.
669 | Symbol and semantic type hash tables. |
670 `---------------------------------------*/
672 /* Initial capacity of symbol and semantic type hash table. */
673 #define HT_INITIAL_CAPACITY 257
675 static struct hash_table
*symbol_table
= NULL
;
676 static struct hash_table
*semantic_type_table
= NULL
;
679 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
681 /* Since tags are unique, we can compare the pointers themselves. */
682 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
686 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
688 /* Since names are unique, we can compare the pointers themselves. */
689 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
693 hash_symbol_comparator (void const *m1
, void const *m2
)
695 return hash_compare_symbol (m1
, m2
);
699 hash_semantic_type_comparator (void const *m1
, void const *m2
)
701 return hash_compare_semantic_type (m1
, m2
);
705 hash_symbol (const symbol
*m
, size_t tablesize
)
707 /* Since tags are unique, we can hash the pointer itself. */
708 return ((uintptr_t) m
->tag
) % tablesize
;
712 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
714 /* Since names are unique, we can hash the pointer itself. */
715 return ((uintptr_t) m
->tag
) % tablesize
;
719 hash_symbol_hasher (void const *m
, size_t tablesize
)
721 return hash_symbol (m
, tablesize
);
725 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
727 return hash_semantic_type (m
, tablesize
);
730 /*-------------------------------.
731 | Create the symbol hash table. |
732 `-------------------------------*/
737 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
740 hash_symbol_comparator
,
742 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
744 hash_semantic_type_hasher
,
745 hash_semantic_type_comparator
,
750 /*----------------------------------------------------------------.
751 | Find the symbol named KEY, and return it. If it does not exist |
753 `----------------------------------------------------------------*/
756 symbol_from_uniqstr (const uniqstr key
, location loc
)
762 entry
= hash_lookup (symbol_table
, &probe
);
766 /* First insertion in the hash. */
767 aver (!symbols_sorted
);
768 entry
= symbol_new (key
, loc
);
769 if (!hash_insert (symbol_table
, entry
))
776 /*-----------------------------------------------------------------------.
777 | Find the semantic type named KEY, and return it. If it does not exist |
779 `-----------------------------------------------------------------------*/
782 semantic_type_from_uniqstr (const uniqstr key
, const location
*loc
)
785 semantic_type
*entry
;
788 entry
= hash_lookup (semantic_type_table
, &probe
);
792 /* First insertion in the hash. */
793 entry
= semantic_type_new (key
, loc
);
794 if (!hash_insert (semantic_type_table
, entry
))
801 /*----------------------------------------------------------------.
802 | Find the symbol named KEY, and return it. If it does not exist |
804 `----------------------------------------------------------------*/
807 symbol_get (const char *key
, location loc
)
809 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
813 /*-----------------------------------------------------------------------.
814 | Find the semantic type named KEY, and return it. If it does not exist |
816 `-----------------------------------------------------------------------*/
819 semantic_type_get (const char *key
, const location
*loc
)
821 return semantic_type_from_uniqstr (uniqstr_new (key
), loc
);
825 /*------------------------------------------------------------------.
826 | Generate a dummy nonterminal, whose name cannot conflict with the |
828 `------------------------------------------------------------------*/
831 dummy_symbol_get (location loc
)
833 /* Incremented for each generated symbol. */
834 static int dummy_count
= 0;
835 static char buf
[256];
839 sprintf (buf
, "$@%d", ++dummy_count
);
840 sym
= symbol_get (buf
, loc
);
841 sym
->class = nterm_sym
;
842 sym
->number
= nvars
++;
847 symbol_is_dummy (const symbol
*sym
)
849 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
852 /*-------------------.
853 | Free the symbols. |
854 `-------------------*/
859 hash_free (symbol_table
);
860 hash_free (semantic_type_table
);
862 free (symbols_sorted
);
863 free (semantic_types_sorted
);
867 /*---------------------------------------------------------------.
868 | Look for undefined symbols, report an error, and consider them |
870 `---------------------------------------------------------------*/
873 symbols_cmp (symbol
const *a
, symbol
const *b
)
875 return strcmp (a
->tag
, b
->tag
);
879 symbols_cmp_qsort (void const *a
, void const *b
)
881 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
885 symbols_do (Hash_processor processor
, void *processor_data
,
886 struct hash_table
*table
, symbol
***sorted
)
888 size_t count
= hash_get_n_entries (table
);
891 *sorted
= xnmalloc (count
, sizeof **sorted
);
892 hash_get_entries (table
, (void**)*sorted
, count
);
893 qsort (*sorted
, count
, sizeof **sorted
, symbols_cmp_qsort
);
897 for (i
= 0; i
< count
; ++i
)
898 processor ((*sorted
)[i
], processor_data
);
902 /*--------------------------------------------------------------.
903 | Check that all the symbols are defined. Report any undefined |
904 | symbols and consider them nonterminals. |
905 `--------------------------------------------------------------*/
908 symbols_check_defined (void)
910 symbols_do (symbol_check_defined_processor
, NULL
,
911 symbol_table
, &symbols_sorted
);
912 symbols_do (semantic_type_check_defined_processor
, NULL
,
913 semantic_type_table
, &semantic_types_sorted
);
916 /*------------------------------------------------------------------.
917 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
919 `------------------------------------------------------------------*/
922 symbols_token_translations_init (void)
924 bool num_256_available_p
= true;
927 /* Find the highest user token number, and whether 256, the POSIX
928 preferred user token number for the error token, is used. */
929 max_user_token_number
= 0;
930 for (i
= 0; i
< ntokens
; ++i
)
932 symbol
*this = symbols
[i
];
933 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
935 if (this->user_token_number
> max_user_token_number
)
936 max_user_token_number
= this->user_token_number
;
937 if (this->user_token_number
== 256)
938 num_256_available_p
= false;
942 /* If 256 is not used, assign it to error, to follow POSIX. */
943 if (num_256_available_p
944 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
945 errtoken
->user_token_number
= 256;
947 /* Set the missing user numbers. */
948 if (max_user_token_number
< 256)
949 max_user_token_number
= 256;
951 for (i
= 0; i
< ntokens
; ++i
)
953 symbol
*this = symbols
[i
];
954 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
955 this->user_token_number
= ++max_user_token_number
;
956 if (this->user_token_number
> max_user_token_number
)
957 max_user_token_number
= this->user_token_number
;
960 token_translations
= xnmalloc (max_user_token_number
+ 1,
961 sizeof *token_translations
);
963 /* Initialize all entries for literal tokens to the internal token
964 number for $undefined, which represents all invalid inputs. */
965 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
966 token_translations
[i
] = undeftoken
->number
;
967 symbols_do (symbol_translation_processor
, NULL
,
968 symbol_table
, &symbols_sorted
);
972 /*----------------------------------------------------------------.
973 | Assign symbol numbers, and write definition of token names into |
974 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
975 `----------------------------------------------------------------*/
980 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
981 symbol_table
, &symbols_sorted
);
983 symbols
= xcalloc (nsyms
, sizeof *symbols
);
984 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, &symbols_sorted
);
986 /* Aliases leave empty slots in symbols, so remove them. */
990 int nsyms_old
= nsyms
;
991 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
993 if (symbols
[readi
] == NULL
)
1000 symbols
[writei
] = symbols
[readi
];
1001 symbols
[writei
]->number
= writei
;
1002 if (symbols
[writei
]->alias
)
1003 symbols
[writei
]->alias
->number
= writei
;
1008 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
1010 symbols_token_translations_init ();
1012 if (startsymbol
->class == unknown_sym
)
1013 complain (&startsymbol_location
, fatal
,
1014 _("the start symbol %s is undefined"),
1016 else if (startsymbol
->class == token_sym
)
1017 complain (&startsymbol_location
, fatal
,
1018 _("the start symbol %s is a token"),
1022 /*---------------------------------.
1023 | Initialize relation graph nodes. |
1024 `---------------------------------*/
1027 init_prec_nodes (void)
1030 prec_nodes
= xcalloc (nsyms
, sizeof *prec_nodes
);
1031 for (i
= 0; i
< nsyms
; ++i
)
1033 prec_nodes
[i
] = xmalloc (sizeof *prec_nodes
[i
]);
1034 symgraph
*s
= prec_nodes
[i
];
1045 static symgraphlink
*
1046 symgraphlink_new (graphid id
, symgraphlink
*next
)
1048 symgraphlink
*l
= xmalloc (sizeof *l
);
1055 /*------------------------------------------------------------------.
1056 | Register the second symbol of the precedence relation, and return |
1057 | whether this relation is new. Use only in register_precedence. |
1058 `------------------------------------------------------------------*/
1061 register_precedence_second_symbol (symgraphlink
**first
, graphid sym
)
1063 if (!*first
|| sym
< (*first
)->id
)
1064 *first
= symgraphlink_new (sym
, *first
);
1067 symgraphlink
*slist
= *first
;
1069 while (slist
->next
&& slist
->next
->id
<= sym
)
1070 slist
= slist
->next
;
1072 if (slist
->id
== sym
)
1073 /* Relation already present. */
1076 slist
->next
= symgraphlink_new (sym
, slist
->next
);
1081 /*------------------------------------------------------------------.
1082 | Register a new relation between symbols as used. The first symbol |
1083 | has a greater precedence than the second one. |
1084 `------------------------------------------------------------------*/
1087 register_precedence (graphid first
, graphid snd
)
1091 register_precedence_second_symbol (&(prec_nodes
[first
]->succ
), snd
);
1092 register_precedence_second_symbol (&(prec_nodes
[snd
]->pred
), first
);
1096 /*---------------------------------------.
1097 | Deep clear a linked / adjacency list). |
1098 `---------------------------------------*/
1101 linkedlist_free (symgraphlink
*node
)
1107 symgraphlink
*tmp
= node
->next
;
1115 /*----------------------------------------------.
1116 | Clear and destroy association tracking table. |
1117 `----------------------------------------------*/
1123 for (i
= 0; i
< nsyms
; ++i
)
1125 linkedlist_free (prec_nodes
[i
]->pred
);
1126 linkedlist_free (prec_nodes
[i
]->succ
);
1127 free (prec_nodes
[i
]);
1132 /*---------------------------------------.
1133 | Initialize association tracking table. |
1134 `---------------------------------------*/
1140 used_assoc
= xcalloc (nsyms
, sizeof *used_assoc
);
1141 for (i
= 0; i
< nsyms
; ++i
)
1142 used_assoc
[i
] = false;
1145 /*------------------------------------------------------------------.
1146 | Test if the associativity for the symbols is defined and useless. |
1147 `------------------------------------------------------------------*/
1150 is_assoc_useless (symbol
*s
)
1153 && s
->assoc
!= undef_assoc
1154 && s
->assoc
!= precedence_assoc
1155 && !used_assoc
[s
->number
];
1158 /*-------------------------------.
1159 | Register a used associativity. |
1160 `-------------------------------*/
1163 register_assoc (graphid i
, graphid j
)
1167 used_assoc
[i
] = true;
1168 used_assoc
[j
] = true;
1171 /*--------------------------------------------------.
1172 | Print a warning for unused precedence relations. |
1173 `--------------------------------------------------*/
1176 print_precedence_warnings (void)
1183 for (i
= 0; i
< nsyms
; ++i
)
1185 symbol
*s
= symbols
[i
];
1188 && !prec_nodes
[i
]->pred
1189 && !prec_nodes
[i
]->succ
)
1191 if (is_assoc_useless (s
))
1192 complain (&s
->prec_location
, Wprecedence
,
1193 _("useless precedence and associativity for %s"), s
->tag
);
1194 else if (s
->assoc
== precedence_assoc
)
1195 complain (&s
->prec_location
, Wprecedence
,
1196 _("useless precedence for %s"), s
->tag
);
1198 else if (is_assoc_useless (s
))
1199 complain (&s
->prec_location
, Wprecedence
,
1200 _("useless associativity for %s, use %%precedence"), s
->tag
);