1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2015, 2018-2020 Free
4 Software Foundation, Inc.
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/>. */
28 #include "print-xml.h"
33 /* Comments for these variables are in gram.h. */
35 item_number
*ritem
= NULL
;
39 rule_number nrules
= 0;
41 symbol
**symbols
= NULL
;
46 symbol_number
*token_translations
= NULL
;
50 int required_version
= 0;
53 item_print (item_number
*item
, rule
const *previous_rule
, FILE *out
)
55 rule
const *r
= item_rule (item
);
56 rule_lhs_print (r
, previous_rule
? previous_rule
->lhs
: NULL
, out
);
58 for (item_number
*sp
= r
->rhs
; sp
< item
; sp
++)
59 fprintf (out
, " %s", symbols
[*sp
]->tag
);
60 fprintf (out
, " %s", dot
);
62 for (item_number
*sp
= item
; 0 <= *sp
; ++sp
)
63 fprintf (out
, " %s", symbols
[*sp
]->tag
);
65 fprintf (out
, " %%empty");
70 rule_useful_in_grammar_p (rule
const *r
)
72 return r
->number
< nrules
;
76 rule_useless_in_grammar_p (rule
const *r
)
78 return !rule_useful_in_grammar_p (r
);
82 rule_useless_in_parser_p (rule
const *r
)
84 return !r
->useful
&& rule_useful_in_grammar_p (r
);
88 rule_useless_chain_p (rule
const *r
)
90 return rule_rhs_length (r
) == 1 && !r
->action
;
94 rule_lhs_print (rule
const *r
, sym_content
const *previous_lhs
, FILE *out
)
96 fprintf (out
, " %3d ", r
->number
);
97 if (previous_lhs
!= r
->lhs
)
98 fprintf (out
, "%s:", r
->lhs
->symbol
->tag
);
100 fprintf (out
, "%*s|", (int) strlen (previous_lhs
->symbol
->tag
), "");
104 rule_lhs_print_xml (rule
const *r
, FILE *out
, int level
)
106 xml_printf (out
, level
, "<lhs>%s</lhs>", r
->lhs
->symbol
->tag
);
110 rule_rhs_length (rule
const *r
)
113 for (item_number
*rhsp
= r
->rhs
; 0 <= *rhsp
; ++rhsp
)
119 rule_rhs_print (rule
const *r
, FILE *out
)
122 for (item_number
*rhsp
= r
->rhs
; 0 <= *rhsp
; ++rhsp
)
123 fprintf (out
, " %s", symbols
[*rhsp
]->tag
);
125 fputs (" %empty", out
);
129 rule_rhs_print_xml (rule
const *r
, FILE *out
, int level
)
133 xml_puts (out
, level
, "<rhs>");
134 for (item_number
*rhsp
= r
->rhs
; 0 <= *rhsp
; ++rhsp
)
135 xml_printf (out
, level
+ 1, "<symbol>%s</symbol>",
136 xml_escape (symbols
[*rhsp
]->tag
));
137 xml_puts (out
, level
, "</rhs>");
141 xml_puts (out
, level
, "<rhs>");
142 xml_puts (out
, level
+ 1, "<empty/>");
143 xml_puts (out
, level
, "</rhs>");
148 rule_print (rule
const *r
, rule
const *prev_rule
, FILE *out
)
150 rule_lhs_print (r
, prev_rule
? prev_rule
->lhs
: NULL
, out
);
151 rule_rhs_print (r
, out
);
155 ritem_print (FILE *out
)
157 fputs ("RITEM\n", out
);
158 for (int i
= 0; i
< nritems
; ++i
)
160 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
162 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
167 ritem_longest_rhs (void)
170 for (rule_number r
= 0; r
< nrules
; ++r
)
172 size_t length
= rule_rhs_length (&rules
[r
]);
181 grammar_rules_partial_print (FILE *out
, const char *title
,
185 rule
*previous_rule
= NULL
;
187 /* rule # : LHS -> RHS */
188 for (rule_number r
= 0; r
< nrules
+ nuseless_productions
; r
++)
190 if (filter
&& !filter (&rules
[r
]))
193 fprintf (out
, "%s\n\n", title
);
194 else if (previous_rule
&& previous_rule
->lhs
!= rules
[r
].lhs
)
197 rule_print (&rules
[r
], previous_rule
, out
);
199 previous_rule
= &rules
[r
];
206 grammar_rules_print (FILE *out
)
208 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_in_grammar_p
);
212 grammar_rules_print_xml (FILE *out
, int level
)
216 for (rule_number r
= 0; r
< nrules
+ nuseless_productions
; r
++)
219 xml_puts (out
, level
+ 1, "<rules>");
222 char const *usefulness
223 = rule_useless_in_grammar_p (&rules
[r
]) ? "useless-in-grammar"
224 : rule_useless_in_parser_p (&rules
[r
]) ? "useless-in-parser"
226 xml_indent (out
, level
+ 2);
227 fprintf (out
, "<rule number=\"%d\" usefulness=\"%s\"",
228 rules
[r
].number
, usefulness
);
229 if (rules
[r
].precsym
)
230 fprintf (out
, " percent_prec=\"%s\"",
231 xml_escape (rules
[r
].precsym
->symbol
->tag
));
234 rule_lhs_print_xml (&rules
[r
], out
, level
+ 3);
235 rule_rhs_print_xml (&rules
[r
], out
, level
+ 3);
236 xml_puts (out
, level
+ 2, "</rule>");
239 xml_puts (out
, level
+ 1, "</rules>");
241 xml_puts (out
, level
+ 1, "<rules/>");
245 section (FILE *out
, const char *s
)
247 fprintf (out
, "%s\n", s
);
248 for (int i
= strlen (s
); 0 < i
; --i
)
255 grammar_dump (FILE *out
, const char *title
)
257 fprintf (out
, "%s\n\n", title
);
259 "ntokens = %d, nnterms = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
260 ntokens
, nnterms
, nsyms
, nrules
, nritems
);
262 section (out
, "Tokens");
264 fprintf (out
, "Value Sprec Sassoc Tag\n");
266 for (symbol_number i
= 0; i
< ntokens
; i
++)
267 fprintf (out
, "%5d %5d %5d %s\n",
269 symbols
[i
]->content
->prec
, symbols
[i
]->content
->assoc
,
271 fprintf (out
, "\n\n");
274 section (out
, "Nonterminals");
276 fprintf (out
, "Value Tag\n");
278 for (symbol_number i
= ntokens
; i
< nsyms
; i
++)
279 fprintf (out
, "%5d %s\n",
281 fprintf (out
, "\n\n");
284 section (out
, "Rules");
287 "Num (Prec, Assoc, Useful, UselessChain) Lhs"
288 " -> (Ritem Range) Rhs\n");
289 for (rule_number i
= 0; i
< nrules
+ nuseless_productions
; ++i
)
291 rule
const *rule_i
= &rules
[i
];
292 int const rhs_itemno
= rule_i
->rhs
- ritem
;
293 int length
= rule_rhs_length (rule_i
);
294 aver (item_number_as_rule_number (rule_i
->rhs
[length
]) == i
);
295 fprintf (out
, "%3d (%2d, %2d, %2s, %2s) %2d -> (%2u-%2u)",
297 rule_i
->prec
? rule_i
->prec
->prec
: 0,
298 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
299 rule_i
->useful
? "t" : "f",
300 rule_useless_chain_p (rule_i
) ? "t" : "f",
302 rhs_itemno
, rhs_itemno
+ length
- 1);
303 /* Dumped the RHS. */
304 for (item_number
*rhsp
= rule_i
->rhs
; 0 <= *rhsp
; ++rhsp
)
305 fprintf (out
, " %3d", *rhsp
);
309 fprintf (out
, "\n\n");
311 section (out
, "Rules interpreted");
312 for (rule_number r
= 0; r
< nrules
+ nuseless_productions
; ++r
)
314 fprintf (out
, "%-5d %s:", r
, rules
[r
].lhs
->symbol
->tag
);
315 rule_rhs_print (&rules
[r
], out
);
318 fprintf (out
, "\n\n");
322 grammar_rules_useless_report (const char *message
)
324 for (rule_number r
= 0; r
< nrules
; ++r
)
325 /* Don't complain about rules whose LHS is useless, we already
326 complained about it. */
327 if (!reduce_nonterminal_useless_in_grammar (rules
[r
].lhs
)
329 complain (&rules
[r
].location
, Wother
, "%s", message
);
338 free (token_translations
);
339 /* Free the symbol table data structure. */
341 free_merger_functions ();