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
);
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
;
126 code_props_type_string (code_props_type kind
)
131 return "%destructor";
138 /*----------------------------------------.
139 | Create a new semantic type, named TAG. |
140 `----------------------------------------*/
142 static semantic_type
*
143 semantic_type_new (uniqstr tag
, const location
*loc
)
145 semantic_type
*res
= xmalloc (sizeof *res
);
147 uniqstr_assert (tag
);
149 res
->location
= loc
? *loc
: empty_location
;
150 res
->status
= undeclared
;
153 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
154 code_props_none_init (&res
->props
[i
]);
165 #define SYMBOL_ATTR_PRINT(Attr) \
167 fprintf (f, " %s { %s }", #Attr, s->Attr)
169 #define SYMBOL_CODE_PRINT(Attr) \
170 if (s->props[Attr].code) \
171 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
174 symbol_print (symbol
const *s
, FILE *f
)
179 SYMBOL_ATTR_PRINT (type_name
);
180 SYMBOL_CODE_PRINT (destructor
);
181 SYMBOL_CODE_PRINT (printer
);
187 #undef SYMBOL_ATTR_PRINT
188 #undef SYMBOL_CODE_PRINT
191 /*----------------------------------.
192 | Whether S is a valid identifier. |
193 `----------------------------------*/
196 is_identifier (uniqstr s
)
198 static char const alphanum
[26 + 26 + 1 + 10] =
199 "abcdefghijklmnopqrstuvwxyz"
200 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
203 if (!s
|| ! memchr (alphanum
, *s
, sizeof alphanum
- 10))
206 if (! memchr (alphanum
, *s
, sizeof alphanum
))
212 /*-----------------------------------------------.
213 | Get the identifier associated to this symbol. |
214 `-----------------------------------------------*/
216 symbol_id_get (symbol
const *sym
)
218 aver (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
);
221 return is_identifier (sym
->tag
) ? sym
->tag
: 0;
225 /*------------------------------------------------------------------.
226 | Complain that S's WHAT is redeclared at SECOND, and was first set |
228 `------------------------------------------------------------------*/
231 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
235 complain_indent (&second
, complaint
, &i
,
236 _("%s redeclaration for %s"), what
, s
->tag
);
238 complain_indent (&first
, complaint
, &i
,
239 _("previous declaration"));
243 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
247 complain_indent (&second
, complaint
, &i
,
248 _("%s redeclaration for <%s>"), what
, s
->tag
);
250 complain_indent (&first
, complaint
, &i
,
251 _("previous declaration"));
256 /*-----------------------------------------------------------------.
257 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
259 `-----------------------------------------------------------------*/
262 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
267 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
270 uniqstr_assert (type_name
);
271 sym
->type_name
= type_name
;
272 sym
->type_location
= loc
;
277 /*--------------------------------------------------------.
278 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
279 `--------------------------------------------------------*/
282 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
283 code_props
const *code
)
285 if (sym
->props
[kind
].code
)
286 symbol_redeclaration (sym
, code_props_type_string (kind
),
287 sym
->props
[kind
].location
,
290 sym
->props
[kind
] = *code
;
293 /*-----------------------------------------------------.
294 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
295 `-----------------------------------------------------*/
298 semantic_type_code_props_set (semantic_type
*type
,
299 code_props_type kind
,
300 code_props
const *code
)
302 if (type
->props
[kind
].code
)
303 semantic_type_redeclaration (type
, code_props_type_string (kind
),
304 type
->props
[kind
].location
,
307 type
->props
[kind
] = *code
;
310 /*---------------------------------------------------.
311 | Get the computed %destructor or %printer for SYM. |
312 `---------------------------------------------------*/
315 symbol_code_props_get (symbol
*sym
, code_props_type kind
)
317 /* Per-symbol code props. */
318 if (sym
->props
[kind
].code
)
319 return &sym
->props
[kind
];
321 /* Per-type code props. */
325 &semantic_type_get (sym
->type_name
, NULL
)->props
[kind
];
330 /* Apply default code props's only to user-defined symbols. */
331 if (sym
->tag
[0] != '$' && sym
!= errtoken
)
334 &semantic_type_get (sym
->type_name
? "*" : "", NULL
)->props
[kind
];
338 return &code_props_none
;
341 /*-----------------------------------------------------------------.
342 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
343 | with UNDEF_ASSOC as ASSOC. |
344 `-----------------------------------------------------------------*/
347 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
349 if (a
!= undef_assoc
)
352 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
358 sym
->prec_location
= loc
;
362 /* Only terminals have a precedence. */
363 symbol_class_set (sym
, token_sym
, loc
, false);
367 /*------------------------------------.
368 | Set the CLASS associated with SYM. |
369 `------------------------------------*/
372 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
375 if (sym
->class != unknown_sym
&& sym
->class != class)
377 complain (&loc
, complaint
, _("symbol %s redefined"), sym
->tag
);
378 /* Don't report both "redefined" and "redeclared". */
382 if (class == nterm_sym
&& sym
->class != nterm_sym
)
383 sym
->number
= nvars
++;
384 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
385 sym
->number
= ntokens
++;
391 if (sym
->status
== declared
&& !warned
)
392 complain (&loc
, Wother
, _("symbol %s redeclared"), sym
->tag
);
394 sym
->status
= declared
;
399 /*------------------------------------------------.
400 | Set the USER_TOKEN_NUMBER associated with SYM. |
401 `------------------------------------------------*/
404 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
406 int *user_token_numberp
;
408 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
409 user_token_numberp
= &sym
->user_token_number
;
411 user_token_numberp
= &sym
->alias
->user_token_number
;
412 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
413 && *user_token_numberp
!= user_token_number
)
414 complain (&loc
, complaint
, _("redefining user token number of %s"),
417 *user_token_numberp
= user_token_number
;
418 /* User defined $end token? */
419 if (user_token_number
== 0)
422 /* It is always mapped to 0, so it was already counted in
424 if (endtoken
->number
!= NUMBER_UNDEFINED
)
426 endtoken
->number
= 0;
431 /*----------------------------------------------------------.
432 | If SYM is not defined, report an error, and consider it a |
434 `----------------------------------------------------------*/
437 symbol_check_defined (symbol
*sym
)
439 if (sym
->class == unknown_sym
)
441 assert (sym
->status
!= declared
);
442 complain (&sym
->location
,
443 sym
->status
== needed
? complaint
: Wother
,
444 _("symbol %s is used, but is not defined as a token"
445 " and has no rules"),
447 sym
->class = nterm_sym
;
448 sym
->number
= nvars
++;
453 for (i
= 0; i
< 2; ++i
)
454 symbol_code_props_get (sym
, i
)->is_used
= true;
457 /* Set the semantic type status associated to the current symbol to
458 'declared' so that we could check semantic types unnecessary uses. */
461 semantic_type
*sem_type
= semantic_type_get (sym
->type_name
, NULL
);
463 sem_type
->status
= declared
;
470 semantic_type_check_defined (semantic_type
*sem_type
)
472 /* <*> and <> do not have to be "declared". */
473 if (sem_type
->status
== declared
475 || STREQ (sem_type
->tag
, "*"))
478 for (i
= 0; i
< 2; ++i
)
479 if (sem_type
->props
[i
].kind
!= CODE_PROPS_NONE
480 && ! sem_type
->props
[i
].is_used
)
481 complain (&sem_type
->location
, Wother
,
482 _("useless %s for type <%s>"),
483 code_props_type_string (i
), sem_type
->tag
);
486 complain (&sem_type
->location
, Wother
,
487 _("type <%s> is used, but is not associated to any symbol"),
494 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
496 return symbol_check_defined (sym
);
500 semantic_type_check_defined_processor (void *sem_type
,
501 void *null ATTRIBUTE_UNUSED
)
503 return semantic_type_check_defined (sem_type
);
508 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
511 complain (&loc
, Wother
,
512 _("symbol %s used more than once as a literal string"), str
->tag
);
514 complain (&loc
, Wother
,
515 _("symbol %s given more than one literal string"), sym
->tag
);
518 str
->class = token_sym
;
519 str
->user_token_number
= sym
->user_token_number
;
520 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
523 str
->number
= sym
->number
;
524 symbol_type_set (str
, sym
->type_name
, loc
);
529 /*---------------------------------------------------------.
530 | Check that THIS, and its alias, have same precedence and |
532 `---------------------------------------------------------*/
535 symbol_check_alias_consistency (symbol
*this)
538 symbol
*str
= this->alias
;
540 /* Check only the symbol in the symbol-string pair. */
542 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
545 if (str
->type_name
!= sym
->type_name
)
548 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
550 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
556 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
557 if (str
->props
[i
].code
)
558 symbol_code_props_set (sym
, i
, &str
->props
[i
]);
559 else if (sym
->props
[i
].code
)
560 symbol_code_props_set (str
, i
, &sym
->props
[i
]);
563 if (sym
->prec
|| str
->prec
)
566 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
569 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
575 symbol_check_alias_consistency_processor (void *this,
576 void *null ATTRIBUTE_UNUSED
)
578 symbol_check_alias_consistency (this);
583 /*-------------------------------------------------------------------.
584 | Assign a symbol number, and write the definition of the token name |
585 | into FDEFINES. Put in SYMBOLS. |
586 `-------------------------------------------------------------------*/
589 symbol_pack (symbol
*this)
591 aver (this->number
!= NUMBER_UNDEFINED
);
592 if (this->class == nterm_sym
)
593 this->number
+= ntokens
;
594 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
597 symbols
[this->number
] = this;
602 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
604 return symbol_pack (this);
609 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
612 symbols_sort (&first
, &second
);
613 complain_indent (&second
->location
, complaint
, &i
,
614 _("user token number %d redeclaration for %s"),
617 complain_indent (&first
->location
, complaint
, &i
,
618 _("previous declaration for %s"),
622 /*--------------------------------------------------.
623 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
624 `--------------------------------------------------*/
627 symbol_translation (symbol
*this)
630 if (this->class == token_sym
631 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
633 /* A token which translation has already been set? */
634 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
635 user_token_number_redeclaration
636 (this->user_token_number
,
637 symbols
[token_translations
[this->user_token_number
]],
640 token_translations
[this->user_token_number
] = this->number
;
647 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
649 return symbol_translation (this);
653 /*---------------------------------------.
654 | Symbol and semantic type hash tables. |
655 `---------------------------------------*/
657 /* Initial capacity of symbol and semantic type hash table. */
658 #define HT_INITIAL_CAPACITY 257
660 static struct hash_table
*symbol_table
= NULL
;
661 static struct hash_table
*semantic_type_table
= NULL
;
664 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
666 /* Since tags are unique, we can compare the pointers themselves. */
667 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
671 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
673 /* Since names are unique, we can compare the pointers themselves. */
674 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
678 hash_symbol_comparator (void const *m1
, void const *m2
)
680 return hash_compare_symbol (m1
, m2
);
684 hash_semantic_type_comparator (void const *m1
, void const *m2
)
686 return hash_compare_semantic_type (m1
, m2
);
690 hash_symbol (const symbol
*m
, size_t tablesize
)
692 /* Since tags are unique, we can hash the pointer itself. */
693 return ((uintptr_t) m
->tag
) % tablesize
;
697 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
699 /* Since names are unique, we can hash the pointer itself. */
700 return ((uintptr_t) m
->tag
) % tablesize
;
704 hash_symbol_hasher (void const *m
, size_t tablesize
)
706 return hash_symbol (m
, tablesize
);
710 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
712 return hash_semantic_type (m
, tablesize
);
715 /*-------------------------------.
716 | Create the symbol hash table. |
717 `-------------------------------*/
722 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
725 hash_symbol_comparator
,
727 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
729 hash_semantic_type_hasher
,
730 hash_semantic_type_comparator
,
735 /*----------------------------------------------------------------.
736 | Find the symbol named KEY, and return it. If it does not exist |
738 `----------------------------------------------------------------*/
741 symbol_from_uniqstr (const uniqstr key
, location loc
)
747 entry
= hash_lookup (symbol_table
, &probe
);
751 /* First insertion in the hash. */
752 aver (!symbols_sorted
);
753 entry
= symbol_new (key
, loc
);
754 if (!hash_insert (symbol_table
, entry
))
761 /*-----------------------------------------------------------------------.
762 | Find the semantic type named KEY, and return it. If it does not exist |
764 `-----------------------------------------------------------------------*/
767 semantic_type_from_uniqstr (const uniqstr key
, const location
*loc
)
770 semantic_type
*entry
;
773 entry
= hash_lookup (semantic_type_table
, &probe
);
777 /* First insertion in the hash. */
778 entry
= semantic_type_new (key
, loc
);
779 if (!hash_insert (semantic_type_table
, entry
))
786 /*----------------------------------------------------------------.
787 | Find the symbol named KEY, and return it. If it does not exist |
789 `----------------------------------------------------------------*/
792 symbol_get (const char *key
, location loc
)
794 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
798 /*-----------------------------------------------------------------------.
799 | Find the semantic type named KEY, and return it. If it does not exist |
801 `-----------------------------------------------------------------------*/
804 semantic_type_get (const char *key
, const location
*loc
)
806 return semantic_type_from_uniqstr (uniqstr_new (key
), loc
);
810 /*------------------------------------------------------------------.
811 | Generate a dummy nonterminal, whose name cannot conflict with the |
813 `------------------------------------------------------------------*/
816 dummy_symbol_get (location loc
)
818 /* Incremented for each generated symbol. */
819 static int dummy_count
= 0;
820 static char buf
[256];
824 sprintf (buf
, "$@%d", ++dummy_count
);
825 sym
= symbol_get (buf
, loc
);
826 sym
->class = nterm_sym
;
827 sym
->number
= nvars
++;
832 symbol_is_dummy (const symbol
*sym
)
834 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
837 /*-------------------.
838 | Free the symbols. |
839 `-------------------*/
844 hash_free (symbol_table
);
845 hash_free (semantic_type_table
);
847 free (symbols_sorted
);
848 free (semantic_types_sorted
);
852 /*---------------------------------------------------------------.
853 | Look for undefined symbols, report an error, and consider them |
855 `---------------------------------------------------------------*/
858 symbols_cmp (symbol
const *a
, symbol
const *b
)
860 return strcmp (a
->tag
, b
->tag
);
864 symbols_cmp_qsort (void const *a
, void const *b
)
866 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
870 symbols_do (Hash_processor processor
, void *processor_data
,
871 struct hash_table
*table
, symbol
***sorted
)
873 size_t count
= hash_get_n_entries (table
);
876 *sorted
= xnmalloc (count
, sizeof **sorted
);
877 hash_get_entries (table
, (void**)*sorted
, count
);
878 qsort (*sorted
, count
, sizeof **sorted
, symbols_cmp_qsort
);
882 for (i
= 0; i
< count
; ++i
)
883 processor ((*sorted
)[i
], processor_data
);
887 /*--------------------------------------------------------------.
888 | Check that all the symbols are defined. Report any undefined |
889 | symbols and consider them nonterminals. |
890 `--------------------------------------------------------------*/
893 symbols_check_defined (void)
895 symbols_do (symbol_check_defined_processor
, NULL
,
896 symbol_table
, &symbols_sorted
);
897 symbols_do (semantic_type_check_defined_processor
, NULL
,
898 semantic_type_table
, &semantic_types_sorted
);
901 /*------------------------------------------------------------------.
902 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
904 `------------------------------------------------------------------*/
907 symbols_token_translations_init (void)
909 bool num_256_available_p
= true;
912 /* Find the highest user token number, and whether 256, the POSIX
913 preferred user token number for the error token, is used. */
914 max_user_token_number
= 0;
915 for (i
= 0; i
< ntokens
; ++i
)
917 symbol
*this = symbols
[i
];
918 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
920 if (this->user_token_number
> max_user_token_number
)
921 max_user_token_number
= this->user_token_number
;
922 if (this->user_token_number
== 256)
923 num_256_available_p
= false;
927 /* If 256 is not used, assign it to error, to follow POSIX. */
928 if (num_256_available_p
929 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
930 errtoken
->user_token_number
= 256;
932 /* Set the missing user numbers. */
933 if (max_user_token_number
< 256)
934 max_user_token_number
= 256;
936 for (i
= 0; i
< ntokens
; ++i
)
938 symbol
*this = symbols
[i
];
939 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
940 this->user_token_number
= ++max_user_token_number
;
941 if (this->user_token_number
> max_user_token_number
)
942 max_user_token_number
= this->user_token_number
;
945 token_translations
= xnmalloc (max_user_token_number
+ 1,
946 sizeof *token_translations
);
948 /* Initialize all entries for literal tokens to the internal token
949 number for $undefined, which represents all invalid inputs. */
950 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
951 token_translations
[i
] = undeftoken
->number
;
952 symbols_do (symbol_translation_processor
, NULL
,
953 symbol_table
, &symbols_sorted
);
957 /*----------------------------------------------------------------.
958 | Assign symbol numbers, and write definition of token names into |
959 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
960 `----------------------------------------------------------------*/
965 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
966 symbol_table
, &symbols_sorted
);
968 symbols
= xcalloc (nsyms
, sizeof *symbols
);
969 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, &symbols_sorted
);
971 /* Aliases leave empty slots in symbols, so remove them. */
975 int nsyms_old
= nsyms
;
976 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
978 if (symbols
[readi
] == NULL
)
985 symbols
[writei
] = symbols
[readi
];
986 symbols
[writei
]->number
= writei
;
987 if (symbols
[writei
]->alias
)
988 symbols
[writei
]->alias
->number
= writei
;
993 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
995 symbols_token_translations_init ();
997 if (startsymbol
->class == unknown_sym
)
998 complain (&startsymbol_location
, fatal
,
999 _("the start symbol %s is undefined"),
1001 else if (startsymbol
->class == token_sym
)
1002 complain (&startsymbol_location
, fatal
,
1003 _("the start symbol %s is a token"),
1007 /*---------------------------------.
1008 | Initialize relation graph nodes. |
1009 `---------------------------------*/
1012 init_prec_nodes (void)
1015 prec_nodes
= xcalloc (nsyms
, sizeof *prec_nodes
);
1016 for (i
= 0; i
< nsyms
; ++i
)
1018 prec_nodes
[i
] = xmalloc (sizeof *prec_nodes
[i
]);
1019 symgraph
*s
= prec_nodes
[i
];
1030 static symgraphlink
*
1031 symgraphlink_new (graphid id
, symgraphlink
*next
)
1033 symgraphlink
*l
= xmalloc (sizeof *l
);
1040 /*------------------------------------------------------------------.
1041 | Register the second symbol of the precedence relation, and return |
1042 | whether this relation is new. Use only in register_precedence. |
1043 `------------------------------------------------------------------*/
1046 register_precedence_second_symbol (symgraphlink
**first
, graphid sym
)
1048 if (!*first
|| sym
< (*first
)->id
)
1049 *first
= symgraphlink_new (sym
, *first
);
1052 symgraphlink
*slist
= *first
;
1054 while (slist
->next
&& slist
->next
->id
<= sym
)
1055 slist
= slist
->next
;
1057 if (slist
->id
== sym
)
1058 /* Relation already present. */
1061 slist
->next
= symgraphlink_new (sym
, slist
->next
);
1066 /*------------------------------------------------------------------.
1067 | Register a new relation between symbols as used. The first symbol |
1068 | has a greater precedence than the second one. |
1069 `------------------------------------------------------------------*/
1072 register_precedence (graphid first
, graphid snd
)
1076 register_precedence_second_symbol (&(prec_nodes
[first
]->succ
), snd
);
1077 register_precedence_second_symbol (&(prec_nodes
[snd
]->pred
), first
);
1081 /*---------------------------------------.
1082 | Deep clear a linked / adjacency list). |
1083 `---------------------------------------*/
1086 linkedlist_free (symgraphlink
*node
)
1092 symgraphlink
*tmp
= node
->next
;
1100 /*----------------------------------------------.
1101 | Clear and destroy association tracking table. |
1102 `----------------------------------------------*/
1108 for (i
= 0; i
< nsyms
; ++i
)
1110 linkedlist_free (prec_nodes
[i
]->pred
);
1111 linkedlist_free (prec_nodes
[i
]->succ
);
1112 free (prec_nodes
[i
]);
1117 /*---------------------------------------.
1118 | Initialize association tracking table. |
1119 `---------------------------------------*/
1125 used_assoc
= xcalloc (nsyms
, sizeof *used_assoc
);
1126 for (i
= 0; i
< nsyms
; ++i
)
1127 used_assoc
[i
] = false;
1130 /*------------------------------------------------------------------.
1131 | Test if the associativity for the symbols is defined and useless. |
1132 `------------------------------------------------------------------*/
1135 is_assoc_useless (symbol
*s
)
1138 && s
->assoc
!= undef_assoc
1139 && s
->assoc
!= precedence_assoc
1140 && !used_assoc
[s
->number
];
1143 /*-------------------------------.
1144 | Register a used associativity. |
1145 `-------------------------------*/
1148 register_assoc (graphid i
, graphid j
)
1152 used_assoc
[i
] = true;
1153 used_assoc
[j
] = true;
1156 /*--------------------------------------------------.
1157 | Print a warning for unused precedence relations. |
1158 `--------------------------------------------------*/
1161 print_precedence_warnings (void)
1168 for (i
= 0; i
< nsyms
; ++i
)
1170 symbol
*s
= symbols
[i
];
1173 && !prec_nodes
[i
]->pred
1174 && !prec_nodes
[i
]->succ
)
1176 if (is_assoc_useless (s
))
1177 complain (&s
->prec_location
, Wprecedence
,
1178 _("useless precedence and associativity for %s"), s
->tag
);
1179 else if (s
->assoc
== precedence_assoc
)
1180 complain (&s
->prec_location
, Wprecedence
,
1181 _("useless precedence for %s"), s
->tag
);
1183 else if (is_assoc_useless (s
))
1184 complain (&s
->prec_location
, Wprecedence
,
1185 _("useless associativity for %s, use %%precedence"), s
->tag
);