doc: refer to cex from sections dealing with conflicts
[bison.git] / src / print-xml.c
blobc509089f9469da6c68fd5135b68837483fde25db
1 /* Print an xml on generated parser, for Bison,
3 Copyright (C) 2007, 2009-2015, 2018-2020 Free Software Foundation,
4 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/>. */
21 #include <config.h>
22 #include "print-xml.h"
24 #include "system.h"
26 #include <bitset.h>
27 #include <stdarg.h>
29 #include "closure.h"
30 #include "conflicts.h"
31 #include "files.h"
32 #include "getargs.h"
33 #include "gram.h"
34 #include "lalr.h"
35 #include "lr0.h"
36 #include "print.h"
37 #include "reader.h"
38 #include "reduce.h"
39 #include "state.h"
40 #include "symtab.h"
41 #include "tables.h"
43 static bitset no_reduce_set;
44 struct escape_buf
46 char *ptr;
47 size_t size;
49 enum { num_escape_bufs = 3 };
50 static struct escape_buf escape_bufs[num_escape_bufs];
53 /*--------------------------------.
54 | Report information on a state. |
55 `--------------------------------*/
57 static void
58 print_core (FILE *out, int level, state *s)
60 item_index *sitems = s->items;
61 size_t snritems = s->nitems;
63 /* Output all the items of a state, not only its kernel. */
64 closure (sitems, snritems);
65 sitems = itemset;
66 snritems = nitemset;
68 if (!snritems)
70 xml_puts (out, level, "<itemset/>");
71 return;
74 xml_puts (out, level, "<itemset>");
76 for (size_t i = 0; i < snritems; i++)
78 bool printed = false;
79 item_number *sp1 = ritem + sitems[i];
80 rule const *r = item_rule (sp1);
81 item_number *sp = r->rhs;
83 /* Display the lookahead tokens? */
84 if (item_number_is_rule_number (*sp1))
86 reductions *reds = s->reductions;
87 int red = state_reduction_find (s, r);
88 /* Print item with lookaheads if there are. */
89 if (reds->lookaheads && red != -1)
91 xml_printf (out, level + 1,
92 "<item rule-number=\"%d\" dot=\"%d\">",
93 r->number, sp1 - sp);
94 state_rule_lookaheads_print_xml (s, r,
95 out, level + 2);
96 xml_puts (out, level + 1, "</item>");
97 printed = true;
101 if (!printed)
102 xml_printf (out, level + 1,
103 "<item rule-number=\"%d\" dot=\"%d\"/>",
104 r->number,
105 sp1 - sp);
107 xml_puts (out, level, "</itemset>");
111 /*-----------------------------------------------------------.
112 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
113 | OUT. |
114 `-----------------------------------------------------------*/
116 static void
117 print_transitions (state *s, FILE *out, int level)
119 transitions *trans = s->transitions;
120 int n = 0;
121 int i;
123 for (i = 0; i < trans->num; i++)
124 if (!TRANSITION_IS_DISABLED (trans, i))
126 n++;
129 /* Nothing to report. */
130 if (!n)
132 xml_puts (out, level, "<transitions/>");
133 return;
136 /* Report lookahead tokens and shifts. */
137 xml_puts (out, level, "<transitions>");
139 for (i = 0; i < trans->num; i++)
140 if (!TRANSITION_IS_DISABLED (trans, i)
141 && TRANSITION_IS_SHIFT (trans, i))
143 symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
144 char const *tag = sym->tag;
145 state *s1 = trans->states[i];
147 xml_printf (out, level + 1,
148 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
149 xml_escape (tag), s1->number);
152 for (i = 0; i < trans->num; i++)
153 if (!TRANSITION_IS_DISABLED (trans, i)
154 && !TRANSITION_IS_SHIFT (trans, i))
156 symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
157 char const *tag = sym->tag;
158 state *s1 = trans->states[i];
160 xml_printf (out, level + 1,
161 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
162 xml_escape (tag), s1->number);
165 xml_puts (out, level, "</transitions>");
169 /*--------------------------------------------------------.
170 | Report the explicit errors of S raised from %nonassoc. |
171 `--------------------------------------------------------*/
173 static void
174 print_errs (FILE *out, int level, state *s)
176 errs *errp = s->errs;
177 bool count = false;
178 int i;
180 for (i = 0; i < errp->num; ++i)
181 if (errp->symbols[i])
182 count = true;
184 /* Nothing to report. */
185 if (!count)
187 xml_puts (out, level, "<errors/>");
188 return;
191 /* Report lookahead tokens and errors. */
192 xml_puts (out, level, "<errors>");
193 for (i = 0; i < errp->num; ++i)
194 if (errp->symbols[i])
196 char const *tag = errp->symbols[i]->tag;
197 xml_printf (out, level + 1,
198 "<error symbol=\"%s\">nonassociative</error>",
199 xml_escape (tag));
201 xml_puts (out, level, "</errors>");
205 /*-------------------------------------------------------------------.
206 | Report a reduction of RULE on LOOKAHEAD (which can be 'default'). |
207 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
208 | R/R conflicts). |
209 `-------------------------------------------------------------------*/
211 static void
212 print_reduction (FILE *out, int level, char const *lookahead,
213 rule *r, bool enabled)
215 if (r->number)
216 xml_printf (out, level,
217 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
218 xml_escape (lookahead),
219 r->number,
220 enabled ? "true" : "false");
221 else
222 xml_printf (out, level,
223 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
224 xml_escape (lookahead),
225 enabled ? "true" : "false");
229 /*-------------------------------------------.
230 | Report on OUT the reduction actions of S. |
231 `-------------------------------------------*/
233 static void
234 print_reductions (FILE *out, int level, state *s)
236 transitions *trans = s->transitions;
237 reductions *reds = s->reductions;
238 rule *default_reduction = NULL;
239 int report = false;
240 int i, j;
242 if (reds->num == 0)
244 xml_puts (out, level, "<reductions/>");
245 return;
248 if (yydefact[s->number] != 0)
249 default_reduction = &rules[yydefact[s->number] - 1];
251 bitset_zero (no_reduce_set);
252 FOR_EACH_SHIFT (trans, i)
253 bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i));
254 for (i = 0; i < s->errs->num; ++i)
255 if (s->errs->symbols[i])
256 bitset_set (no_reduce_set, s->errs->symbols[i]->content->number);
258 if (default_reduction)
259 report = true;
261 if (reds->lookaheads)
262 for (i = 0; i < ntokens; i++)
264 bool count = bitset_test (no_reduce_set, i);
266 for (j = 0; j < reds->num; ++j)
267 if (bitset_test (reds->lookaheads[j], i))
269 if (! count)
271 if (reds->rules[j] != default_reduction)
272 report = true;
273 count = true;
275 else
277 report = true;
282 /* Nothing to report. */
283 if (!report)
285 xml_puts (out, level, "<reductions/>");
286 return;
289 xml_puts (out, level, "<reductions>");
291 /* Report lookahead tokens (or $default) and reductions. */
292 if (reds->lookaheads)
293 for (i = 0; i < ntokens; i++)
295 bool defaulted = false;
296 bool count = bitset_test (no_reduce_set, i);
298 for (j = 0; j < reds->num; ++j)
299 if (bitset_test (reds->lookaheads[j], i))
301 if (! count)
303 if (reds->rules[j] != default_reduction)
304 print_reduction (out, level + 1, symbols[i]->tag,
305 reds->rules[j], true);
306 else
307 defaulted = true;
308 count = true;
310 else
312 if (defaulted)
313 print_reduction (out, level + 1, symbols[i]->tag,
314 default_reduction, true);
315 defaulted = false;
316 print_reduction (out, level + 1, symbols[i]->tag,
317 reds->rules[j], false);
322 if (default_reduction)
323 print_reduction (out, level + 1,
324 "$default", default_reduction, true);
326 xml_puts (out, level, "</reductions>");
330 /*--------------------------------------------------------------.
331 | Report on OUT all the actions (shifts, gotos, reductions, and |
332 | explicit errors from %nonassoc) of S. |
333 `--------------------------------------------------------------*/
335 static void
336 print_actions (FILE *out, int level, state *s)
338 xml_puts (out, level, "<actions>");
339 print_transitions (s, out, level + 1);
340 print_errs (out, level + 1, s);
341 print_reductions (out, level + 1, s);
342 xml_puts (out, level, "</actions>");
346 /*----------------------------------.
347 | Report all the data on S on OUT. |
348 `----------------------------------*/
350 static void
351 print_state (FILE *out, int level, state *s)
353 fputc ('\n', out);
354 xml_printf (out, level, "<state number=\"%d\">", s->number);
355 print_core (out, level + 1, s);
356 print_actions (out, level + 1, s);
357 if (s->solved_conflicts_xml)
359 xml_puts (out, level + 1, "<solved-conflicts>");
360 fputs (s->solved_conflicts_xml, out);
361 xml_puts (out, level + 1, "</solved-conflicts>");
363 else
364 xml_puts (out, level + 1, "<solved-conflicts/>");
365 xml_puts (out, level, "</state>");
369 /*-----------------------------------------.
370 | Print information on the whole grammar. |
371 `-----------------------------------------*/
373 static void
374 print_grammar (FILE *out, int level)
376 fputc ('\n', out);
377 xml_puts (out, level, "<grammar>");
378 grammar_rules_print_xml (out, level);
380 /* Terminals */
381 xml_puts (out, level + 1, "<terminals>");
382 for (int i = 0; i < max_code + 1; i++)
383 if (token_translations[i] != undeftoken->content->number)
385 symbol const *sym = symbols[token_translations[i]];
386 char const *tag = sym->tag;
387 char const *type = sym->content->type_name;
388 int precedence = sym->content->prec;
389 assoc associativity = sym->content->assoc;
390 xml_indent (out, level + 2);
391 fprintf (out,
392 "<terminal symbol-number=\"%d\" token-number=\"%d\""
393 " name=\"%s\" type=\"%s\" usefulness=\"%s\"",
394 token_translations[i], i, xml_escape_n (0, tag),
395 type ? xml_escape_n (1, type) : "",
396 reduce_token_unused_in_grammar (token_translations[i])
397 ? "unused-in-grammar" : "useful");
398 if (precedence)
399 fprintf (out, " prec=\"%d\"", precedence);
400 if (associativity != undef_assoc)
401 fprintf (out, " assoc=\"%s\"", assoc_to_string (associativity) + 1);
402 fputs ("/>\n", out);
404 xml_puts (out, level + 1, "</terminals>");
406 /* Nonterminals */
407 xml_puts (out, level + 1, "<nonterminals>");
408 for (symbol_number i = ntokens; i < nsyms + nuseless_nonterminals; i++)
410 symbol const *sym = symbols[i];
411 char const *tag = sym->tag;
412 char const *type = sym->content->type_name;
413 xml_printf (out, level + 2,
414 "<nonterminal symbol-number=\"%d\" name=\"%s\""
415 " type=\"%s\""
416 " usefulness=\"%s\"/>",
417 i, xml_escape_n (0, tag),
418 type ? xml_escape_n (1, type) : "",
419 reduce_nonterminal_useless_in_grammar (sym->content)
420 ? "useless-in-grammar" : "useful");
422 xml_puts (out, level + 1, "</nonterminals>");
423 xml_puts (out, level, "</grammar>");
426 void
427 xml_indent (FILE *out, int level)
429 for (int i = 0; i < level; i++)
430 fputs (" ", out);
433 void
434 xml_puts (FILE *out, int level, char const *s)
436 xml_indent (out, level);
437 fputs (s, out);
438 fputc ('\n', out);
441 void
442 xml_printf (FILE *out, int level, char const *fmt, ...)
444 va_list arglist;
446 xml_indent (out, level);
448 va_start (arglist, fmt);
449 vfprintf (out, fmt, arglist);
450 va_end (arglist);
452 fputc ('\n', out);
455 static char const *
456 xml_escape_string (struct escape_buf *buf, char const *str)
458 size_t len = strlen (str);
459 size_t max_expansion = sizeof "&quot;" - 1;
461 if (buf->size <= max_expansion * len)
463 buf->size = max_expansion * len + 1;
464 buf->ptr = x2realloc (buf->ptr, &buf->size);
466 char *p = buf->ptr;
468 for (; *str; str++)
469 switch (*str)
471 default: *p++ = *str; break;
472 case '&': p = stpcpy (p, "&amp;" ); break;
473 case '<': p = stpcpy (p, "&lt;" ); break;
474 case '>': p = stpcpy (p, "&gt;" ); break;
475 case '"': p = stpcpy (p, "&quot;"); break;
478 *p = '\0';
479 return buf->ptr;
482 char const *
483 xml_escape_n (int n, char const *str)
485 return xml_escape_string (escape_bufs + n, str);
488 char const *
489 xml_escape (char const *str)
491 return xml_escape_n (0, str);
494 void
495 print_xml (void)
497 FILE *out = xfopen (spec_xml_file, "w");
499 fputs ("<?xml version=\"1.0\"?>\n\n", out);
501 int level = 0;
502 xml_printf (out, level,
503 "<bison-xml-report version=\"%s\" bug-report=\"%s\""
504 " url=\"%s\">",
505 xml_escape_n (0, VERSION),
506 xml_escape_n (1, PACKAGE_BUGREPORT),
507 xml_escape_n (2, PACKAGE_URL));
509 fputc ('\n', out);
510 xml_printf (out, level + 1, "<filename>%s</filename>",
511 xml_escape (grammar_file));
513 /* print grammar */
514 print_grammar (out, level + 1);
516 no_reduce_set = bitset_create (ntokens, BITSET_FIXED);
518 /* print automaton */
519 fputc ('\n', out);
520 xml_puts (out, level + 1, "<automaton>");
521 for (state_number i = 0; i < nstates; i++)
522 print_state (out, level + 2, states[i]);
523 xml_puts (out, level + 1, "</automaton>");
525 bitset_free (no_reduce_set);
527 xml_puts (out, 0, "</bison-xml-report>");
529 for (int i = 0; i < num_escape_bufs; ++i)
530 free (escape_bufs[i].ptr);
532 xfclose (out);