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 | Create a new symbol, named TAG. |
57 `---------------------------------*/
60 symbol_new (uniqstr tag
, location loc
)
62 symbol
*res
= xmalloc (sizeof *res
);
65 /* If the tag is not a string (starts with a double quote), check
66 that it is valid for Yacc. */
67 if (tag
[0] != '\"' && tag
[0] != '\'' && strchr (tag
, '-'))
68 complain (&loc
, Wyacc
,
69 _("POSIX Yacc forbids dashes in symbol names: %s"), tag
);
74 res
->type_name
= NULL
;
77 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
78 code_props_none_init (&res
->props
[i
]);
81 res
->number
= NUMBER_UNDEFINED
;
83 res
->assoc
= undef_assoc
;
84 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
87 res
->class = unknown_sym
;
88 res
->status
= undeclared
;
90 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
91 complain (NULL
, fatal
, _("too many symbols in input grammar (limit is %d)"),
92 SYMBOL_NUMBER_MAXIMUM
);
98 code_props_type_string (code_props_type kind
)
103 return "%destructor";
110 /*----------------------------------------.
111 | Create a new semantic type, named TAG. |
112 `----------------------------------------*/
114 static semantic_type
*
115 semantic_type_new (uniqstr tag
, const location
*loc
)
117 semantic_type
*res
= xmalloc (sizeof *res
);
119 uniqstr_assert (tag
);
121 res
->location
= loc
? *loc
: empty_location
;
122 res
->status
= undeclared
;
125 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
126 code_props_none_init (&res
->props
[i
]);
137 #define SYMBOL_ATTR_PRINT(Attr) \
139 fprintf (f, " %s { %s }", #Attr, s->Attr)
141 #define SYMBOL_CODE_PRINT(Attr) \
142 if (s->props[Attr].code) \
143 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
146 symbol_print (symbol
const *s
, FILE *f
)
150 fprintf (f
, "\"%s\"", s
->tag
);
151 SYMBOL_ATTR_PRINT (type_name
);
152 SYMBOL_CODE_PRINT (destructor
);
153 SYMBOL_CODE_PRINT (printer
);
156 fprintf (f
, "<NULL>");
159 #undef SYMBOL_ATTR_PRINT
160 #undef SYMBOL_CODE_PRINT
163 /*----------------------------------.
164 | Whether S is a valid identifier. |
165 `----------------------------------*/
168 is_identifier (uniqstr s
)
170 static char const alphanum
[26 + 26 + 1 + 10] =
171 "abcdefghijklmnopqrstuvwxyz"
172 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
175 if (!s
|| ! memchr (alphanum
, *s
, sizeof alphanum
- 10))
178 if (! memchr (alphanum
, *s
, sizeof alphanum
))
184 /*-----------------------------------------------.
185 | Get the identifier associated to this symbol. |
186 `-----------------------------------------------*/
188 symbol_id_get (symbol
const *sym
)
190 aver (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
);
193 return is_identifier (sym
->tag
) ? sym
->tag
: 0;
197 /*------------------------------------------------------------------.
198 | Complain that S's WHAT is redeclared at SECOND, and was first set |
200 `------------------------------------------------------------------*/
203 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
207 complain_indent (&second
, complaint
, &i
,
208 _("%s redeclaration for %s"), what
, s
->tag
);
210 complain_indent (&first
, complaint
, &i
,
211 _("previous declaration"));
215 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
219 complain_indent (&second
, complaint
, &i
,
220 _("%s redeclaration for <%s>"), what
, s
->tag
);
222 complain_indent (&first
, complaint
, &i
,
223 _("previous declaration"));
228 /*-----------------------------------------------------------------.
229 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
231 `-----------------------------------------------------------------*/
234 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
239 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
240 uniqstr_assert (type_name
);
241 sym
->type_name
= type_name
;
242 sym
->type_location
= loc
;
246 /*--------------------------------------------------------.
247 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
248 `--------------------------------------------------------*/
251 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
252 code_props
const *code
)
254 if (sym
->props
[kind
].code
)
255 symbol_redeclaration (sym
, code_props_type_string (kind
),
256 sym
->props
[kind
].location
,
258 sym
->props
[kind
] = *code
;
261 /*-----------------------------------------------------.
262 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
263 `-----------------------------------------------------*/
266 semantic_type_code_props_set (semantic_type
*type
,
267 code_props_type kind
,
268 code_props
const *code
)
270 if (type
->props
[kind
].code
)
271 semantic_type_redeclaration (type
, code_props_type_string (kind
),
272 type
->props
[kind
].location
,
274 type
->props
[kind
] = *code
;
277 /*---------------------------------------------------.
278 | Get the computed %destructor or %printer for SYM. |
279 `---------------------------------------------------*/
282 symbol_code_props_get (symbol
*sym
, code_props_type kind
)
284 /* Per-symbol code props. */
285 if (sym
->props
[kind
].code
)
286 return &sym
->props
[kind
];
288 /* Per-type code props. */
292 &semantic_type_get (sym
->type_name
, NULL
)->props
[kind
];
297 /* Apply default code props's only to user-defined symbols. */
298 if (sym
->tag
[0] != '$' && sym
!= errtoken
)
301 &semantic_type_get (sym
->type_name
? "*" : "", NULL
)->props
[kind
];
305 return &code_props_none
;
308 /*-----------------------------------------------------------------.
309 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
310 | with UNDEF_ASSOC as ASSOC. |
311 `-----------------------------------------------------------------*/
314 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
316 if (a
!= undef_assoc
)
319 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
323 sym
->prec_location
= loc
;
326 /* Only terminals have a precedence. */
327 symbol_class_set (sym
, token_sym
, loc
, false);
331 /*------------------------------------.
332 | Set the CLASS associated with SYM. |
333 `------------------------------------*/
336 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
339 if (sym
->class != unknown_sym
&& sym
->class != class)
341 complain (&loc
, complaint
, _("symbol %s redefined"), sym
->tag
);
342 /* Don't report both "redefined" and "redeclared". */
346 if (class == nterm_sym
&& sym
->class != nterm_sym
)
347 sym
->number
= nvars
++;
348 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
349 sym
->number
= ntokens
++;
355 if (sym
->status
== declared
&& !warned
)
356 complain (&loc
, Wother
, _("symbol %s redeclared"), sym
->tag
);
357 sym
->status
= declared
;
362 /*------------------------------------------------.
363 | Set the USER_TOKEN_NUMBER associated with SYM. |
364 `------------------------------------------------*/
367 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
369 int *user_token_numberp
;
371 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
372 user_token_numberp
= &sym
->user_token_number
;
374 user_token_numberp
= &sym
->alias
->user_token_number
;
375 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
376 && *user_token_numberp
!= user_token_number
)
377 complain (&loc
, complaint
, _("redefining user token number of %s"),
380 *user_token_numberp
= user_token_number
;
381 /* User defined $end token? */
382 if (user_token_number
== 0)
385 /* It is always mapped to 0, so it was already counted in
387 if (endtoken
->number
!= NUMBER_UNDEFINED
)
389 endtoken
->number
= 0;
394 /*----------------------------------------------------------.
395 | If SYM is not defined, report an error, and consider it a |
397 `----------------------------------------------------------*/
400 symbol_check_defined (symbol
*sym
)
402 if (sym
->class == unknown_sym
)
404 assert (sym
->status
!= declared
);
405 complain (&sym
->location
,
406 sym
->status
== needed
? complaint
: Wother
,
407 _("symbol %s is used, but is not defined as a token"
408 " and has no rules"),
410 sym
->class = nterm_sym
;
411 sym
->number
= nvars
++;
416 for (i
= 0; i
< 2; ++i
)
417 symbol_code_props_get (sym
, i
)->is_used
= true;
420 /* Set the semantic type status associated to the current symbol to
421 'declared' so that we could check semantic types unnecessary uses. */
424 semantic_type
*sem_type
= semantic_type_get (sym
->type_name
, NULL
);
426 sem_type
->status
= declared
;
433 semantic_type_check_defined (semantic_type
*sem_type
)
435 /* <*> and <> do not have to be "declared". */
436 if (sem_type
->status
== declared
438 || STREQ(sem_type
->tag
, "*"))
441 for (i
= 0; i
< 2; ++i
)
442 if (sem_type
->props
[i
].kind
!= CODE_PROPS_NONE
443 && ! sem_type
->props
[i
].is_used
)
444 complain (&sem_type
->location
, Wother
,
445 _("useless %s for type <%s>"),
446 code_props_type_string (i
), sem_type
->tag
);
449 complain (&sem_type
->location
, Wother
,
450 _("type <%s> is used, but is not associated to any symbol"),
457 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
459 return symbol_check_defined (sym
);
463 semantic_type_check_defined_processor (void *sem_type
,
464 void *null ATTRIBUTE_UNUSED
)
466 return semantic_type_check_defined (sem_type
);
471 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
474 complain (&loc
, Wother
,
475 _("symbol %s used more than once as a literal string"), str
->tag
);
477 complain (&loc
, Wother
,
478 _("symbol %s given more than one literal string"), sym
->tag
);
481 str
->class = token_sym
;
482 str
->user_token_number
= sym
->user_token_number
;
483 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
486 str
->number
= sym
->number
;
487 symbol_type_set (str
, sym
->type_name
, loc
);
492 /*---------------------------------------------------------.
493 | Check that THIS, and its alias, have same precedence and |
495 `---------------------------------------------------------*/
498 symbol_check_alias_consistency (symbol
*this)
501 symbol
*str
= this->alias
;
503 /* Check only the symbol in the symbol-string pair. */
505 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
508 if (str
->type_name
!= sym
->type_name
)
511 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
513 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
519 for (i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
520 if (str
->props
[i
].code
)
521 symbol_code_props_set (sym
, i
, &str
->props
[i
]);
522 else if (sym
->props
[i
].code
)
523 symbol_code_props_set (str
, i
, &sym
->props
[i
]);
526 if (sym
->prec
|| str
->prec
)
529 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
532 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
538 symbol_check_alias_consistency_processor (void *this,
539 void *null ATTRIBUTE_UNUSED
)
541 symbol_check_alias_consistency (this);
546 /*-------------------------------------------------------------------.
547 | Assign a symbol number, and write the definition of the token name |
548 | into FDEFINES. Put in SYMBOLS. |
549 `-------------------------------------------------------------------*/
552 symbol_pack (symbol
*this)
554 aver (this->number
!= NUMBER_UNDEFINED
);
555 if (this->class == nterm_sym
)
556 this->number
+= ntokens
;
557 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
560 symbols
[this->number
] = this;
565 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
567 return symbol_pack (this);
572 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
575 /* User token numbers are not assigned during the parsing, but in a
576 second step, via a traversal of the symbol table sorted on tag.
578 However, error messages make more sense if we keep the first
579 declaration first. */
580 if (location_cmp (first
->location
, second
->location
) > 0)
586 complain_indent (&second
->location
, complaint
, &i
,
587 _("user token number %d redeclaration for %s"),
590 complain_indent (&first
->location
, complaint
, &i
,
591 _("previous declaration for %s"),
595 /*--------------------------------------------------.
596 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
597 `--------------------------------------------------*/
600 symbol_translation (symbol
*this)
603 if (this->class == token_sym
604 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
606 /* A token which translation has already been set? */
607 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
608 user_token_number_redeclaration
609 (this->user_token_number
,
610 symbols
[token_translations
[this->user_token_number
]],
613 token_translations
[this->user_token_number
] = this->number
;
620 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
622 return symbol_translation (this);
626 /*---------------------------------------.
627 | Symbol and semantic type hash tables. |
628 `---------------------------------------*/
630 /* Initial capacity of symbol and semantic type hash table. */
631 #define HT_INITIAL_CAPACITY 257
633 static struct hash_table
*symbol_table
= NULL
;
634 static struct hash_table
*semantic_type_table
= NULL
;
637 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
639 /* Since tags are unique, we can compare the pointers themselves. */
640 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
644 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
646 /* Since names are unique, we can compare the pointers themselves. */
647 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
651 hash_symbol_comparator (void const *m1
, void const *m2
)
653 return hash_compare_symbol (m1
, m2
);
657 hash_semantic_type_comparator (void const *m1
, void const *m2
)
659 return hash_compare_semantic_type (m1
, m2
);
663 hash_symbol (const symbol
*m
, size_t tablesize
)
665 /* Since tags are unique, we can hash the pointer itself. */
666 return ((uintptr_t) m
->tag
) % tablesize
;
670 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
672 /* Since names are unique, we can hash the pointer itself. */
673 return ((uintptr_t) m
->tag
) % tablesize
;
677 hash_symbol_hasher (void const *m
, size_t tablesize
)
679 return hash_symbol (m
, tablesize
);
683 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
685 return hash_semantic_type (m
, tablesize
);
688 /*-------------------------------.
689 | Create the symbol hash table. |
690 `-------------------------------*/
695 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
698 hash_symbol_comparator
,
700 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
702 hash_semantic_type_hasher
,
703 hash_semantic_type_comparator
,
708 /*----------------------------------------------------------------.
709 | Find the symbol named KEY, and return it. If it does not exist |
711 `----------------------------------------------------------------*/
714 symbol_from_uniqstr (const uniqstr key
, location loc
)
720 entry
= hash_lookup (symbol_table
, &probe
);
724 /* First insertion in the hash. */
725 aver (!symbols_sorted
);
726 entry
= symbol_new (key
, loc
);
727 if (!hash_insert (symbol_table
, entry
))
734 /*-----------------------------------------------------------------------.
735 | Find the semantic type named KEY, and return it. If it does not exist |
737 `-----------------------------------------------------------------------*/
740 semantic_type_from_uniqstr (const uniqstr key
, const location
*loc
)
743 semantic_type
*entry
;
746 entry
= hash_lookup (semantic_type_table
, &probe
);
750 /* First insertion in the hash. */
751 entry
= semantic_type_new (key
, loc
);
752 if (!hash_insert (semantic_type_table
, entry
))
759 /*----------------------------------------------------------------.
760 | Find the symbol named KEY, and return it. If it does not exist |
762 `----------------------------------------------------------------*/
765 symbol_get (const char *key
, location loc
)
767 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
771 /*-----------------------------------------------------------------------.
772 | Find the semantic type named KEY, and return it. If it does not exist |
774 `-----------------------------------------------------------------------*/
777 semantic_type_get (const char *key
, const location
*loc
)
779 return semantic_type_from_uniqstr (uniqstr_new (key
), loc
);
783 /*------------------------------------------------------------------.
784 | Generate a dummy nonterminal, whose name cannot conflict with the |
786 `------------------------------------------------------------------*/
789 dummy_symbol_get (location loc
)
791 /* Incremented for each generated symbol. */
792 static int dummy_count
= 0;
793 static char buf
[256];
797 sprintf (buf
, "$@%d", ++dummy_count
);
798 sym
= symbol_get (buf
, loc
);
799 sym
->class = nterm_sym
;
800 sym
->number
= nvars
++;
805 symbol_is_dummy (const symbol
*sym
)
807 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
810 /*-------------------.
811 | Free the symbols. |
812 `-------------------*/
817 hash_free (symbol_table
);
818 hash_free (semantic_type_table
);
820 free (symbols_sorted
);
821 free (semantic_types_sorted
);
825 /*---------------------------------------------------------------.
826 | Look for undefined symbols, report an error, and consider them |
828 `---------------------------------------------------------------*/
831 symbols_cmp (symbol
const *a
, symbol
const *b
)
833 return strcmp (a
->tag
, b
->tag
);
837 symbols_cmp_qsort (void const *a
, void const *b
)
839 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
843 symbols_do (Hash_processor processor
, void *processor_data
,
844 struct hash_table
*table
, symbol
***sorted
)
846 size_t count
= hash_get_n_entries (table
);
849 *sorted
= xnmalloc (count
, sizeof **sorted
);
850 hash_get_entries (table
, (void**)*sorted
, count
);
851 qsort (*sorted
, count
, sizeof **sorted
, symbols_cmp_qsort
);
855 for (i
= 0; i
< count
; ++i
)
856 processor ((*sorted
)[i
], processor_data
);
860 /*--------------------------------------------------------------.
861 | Check that all the symbols are defined. Report any undefined |
862 | symbols and consider them nonterminals. |
863 `--------------------------------------------------------------*/
866 symbols_check_defined (void)
868 symbols_do (symbol_check_defined_processor
, NULL
,
869 symbol_table
, &symbols_sorted
);
870 symbols_do (semantic_type_check_defined_processor
, NULL
,
871 semantic_type_table
, &semantic_types_sorted
);
874 /*------------------------------------------------------------------.
875 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
877 `------------------------------------------------------------------*/
880 symbols_token_translations_init (void)
882 bool num_256_available_p
= true;
885 /* Find the highest user token number, and whether 256, the POSIX
886 preferred user token number for the error token, is used. */
887 max_user_token_number
= 0;
888 for (i
= 0; i
< ntokens
; ++i
)
890 symbol
*this = symbols
[i
];
891 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
893 if (this->user_token_number
> max_user_token_number
)
894 max_user_token_number
= this->user_token_number
;
895 if (this->user_token_number
== 256)
896 num_256_available_p
= false;
900 /* If 256 is not used, assign it to error, to follow POSIX. */
901 if (num_256_available_p
902 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
903 errtoken
->user_token_number
= 256;
905 /* Set the missing user numbers. */
906 if (max_user_token_number
< 256)
907 max_user_token_number
= 256;
909 for (i
= 0; i
< ntokens
; ++i
)
911 symbol
*this = symbols
[i
];
912 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
913 this->user_token_number
= ++max_user_token_number
;
914 if (this->user_token_number
> max_user_token_number
)
915 max_user_token_number
= this->user_token_number
;
918 token_translations
= xnmalloc (max_user_token_number
+ 1,
919 sizeof *token_translations
);
921 /* Initialize all entries for literal tokens to the internal token
922 number for $undefined, which represents all invalid inputs. */
923 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
924 token_translations
[i
] = undeftoken
->number
;
925 symbols_do (symbol_translation_processor
, NULL
,
926 symbol_table
, &symbols_sorted
);
930 /*----------------------------------------------------------------.
931 | Assign symbol numbers, and write definition of token names into |
932 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
933 `----------------------------------------------------------------*/
938 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
939 symbol_table
, &symbols_sorted
);
941 symbols
= xcalloc (nsyms
, sizeof *symbols
);
942 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, &symbols_sorted
);
944 /* Aliases leave empty slots in symbols, so remove them. */
948 int nsyms_old
= nsyms
;
949 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
951 if (symbols
[readi
] == NULL
)
958 symbols
[writei
] = symbols
[readi
];
959 symbols
[writei
]->number
= writei
;
960 if (symbols
[writei
]->alias
)
961 symbols
[writei
]->alias
->number
= writei
;
966 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
968 symbols_token_translations_init ();
970 if (startsymbol
->class == unknown_sym
)
971 complain (&startsymbol_location
, fatal
,
972 _("the start symbol %s is undefined"),
974 else if (startsymbol
->class == token_sym
)
975 complain (&startsymbol_location
, fatal
,
976 _("the start symbol %s is a token"),
980 /*---------------------------------.
981 | Initialize relation graph nodes. |
982 `---------------------------------*/
985 init_prec_nodes (void)
988 prec_nodes
= xcalloc (nsyms
, sizeof *prec_nodes
);
989 for (i
= 0; i
< nsyms
; ++i
)
991 prec_nodes
[i
] = xmalloc (sizeof *prec_nodes
[i
]);
992 symgraph
*s
= prec_nodes
[i
];
1003 static symgraphlink
*
1004 symgraphlink_new (graphid id
, symgraphlink
*next
)
1006 symgraphlink
*l
= xmalloc (sizeof *l
);
1013 /*------------------------------------------------------------------.
1014 | Register the second symbol of the precedence relation, and return |
1015 | whether this relation is new. Use only in register_precedence. |
1016 `------------------------------------------------------------------*/
1019 register_precedence_second_symbol (symgraphlink
**first
, graphid sym
)
1021 if (!*first
|| sym
< (*first
)->id
)
1022 *first
= symgraphlink_new (sym
, *first
);
1025 symgraphlink
*slist
= *first
;
1027 while (slist
->next
&& slist
->next
->id
<= sym
)
1028 slist
= slist
->next
;
1030 if (slist
->id
== sym
)
1031 /* Relation already present. */
1034 slist
->next
= symgraphlink_new (sym
, slist
->next
);
1039 /*------------------------------------------------------------------.
1040 | Register a new relation between symbols as used. The first symbol |
1041 | has a greater precedence than the second one. |
1042 `------------------------------------------------------------------*/
1045 register_precedence (graphid first
, graphid snd
)
1049 register_precedence_second_symbol (&(prec_nodes
[first
]->succ
), snd
);
1050 register_precedence_second_symbol (&(prec_nodes
[snd
]->pred
), first
);
1054 /*--------------------------------------------------.
1055 | Print a warning for unused precedence relations. |
1056 `--------------------------------------------------*/
1059 print_precedence_warnings (void)
1064 for (i
= 0; i
< nsyms
; ++i
)
1066 symbol
*s
= symbols
[i
];
1069 && !prec_nodes
[i
]->pred
1070 && !prec_nodes
[i
]->succ
1071 && s
->assoc
== precedence_assoc
)
1072 complain (&s
->location
, Wother
,
1073 _("useless precedence for %s"), s
->tag
);