1 /* Print an xml on generated parser, for Bison,
3 Copyright (C) 2007, 2009-2015, 2018-2020 Free Software Foundation,
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/>. */
22 #include "print-xml.h"
30 #include "conflicts.h"
43 static bitset no_reduce_set
;
49 enum { num_escape_bufs
= 3 };
50 static struct escape_buf escape_bufs
[num_escape_bufs
];
53 /*--------------------------------.
54 | Report information on a state. |
55 `--------------------------------*/
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
);
70 xml_puts (out
, level
, "<itemset/>");
74 xml_puts (out
, level
, "<itemset>");
76 for (size_t i
= 0; i
< snritems
; i
++)
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\">",
94 state_rule_lookaheads_print_xml (s
, r
,
96 xml_puts (out
, level
+ 1, "</item>");
102 xml_printf (out
, level
+ 1,
103 "<item rule-number=\"%d\" dot=\"%d\"/>",
107 xml_puts (out
, level
, "</itemset>");
111 /*-----------------------------------------------------------.
112 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
114 `-----------------------------------------------------------*/
117 print_transitions (state
*s
, FILE *out
, int level
)
119 transitions
*trans
= s
->transitions
;
123 for (i
= 0; i
< trans
->num
; i
++)
124 if (!TRANSITION_IS_DISABLED (trans
, i
))
129 /* Nothing to report. */
132 xml_puts (out
, level
, "<transitions/>");
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 `--------------------------------------------------------*/
174 print_errs (FILE *out
, int level
, state
*s
)
176 errs
*errp
= s
->errs
;
180 for (i
= 0; i
< errp
->num
; ++i
)
181 if (errp
->symbols
[i
])
184 /* Nothing to report. */
187 xml_puts (out
, level
, "<errors/>");
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>",
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 |
209 `-------------------------------------------------------------------*/
212 print_reduction (FILE *out
, int level
, char const *lookahead
,
213 rule
*r
, bool enabled
)
216 xml_printf (out
, level
,
217 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
218 xml_escape (lookahead
),
220 enabled
? "true" : "false");
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 `-------------------------------------------*/
234 print_reductions (FILE *out
, int level
, state
*s
)
236 transitions
*trans
= s
->transitions
;
237 reductions
*reds
= s
->reductions
;
238 rule
*default_reduction
= NULL
;
244 xml_puts (out
, level
, "<reductions/>");
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
)
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
))
271 if (reds
->rules
[j
] != default_reduction
)
282 /* Nothing to report. */
285 xml_puts (out
, level
, "<reductions/>");
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
))
303 if (reds
->rules
[j
] != default_reduction
)
304 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
305 reds
->rules
[j
], true);
313 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
314 default_reduction
, true);
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 `--------------------------------------------------------------*/
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 `----------------------------------*/
351 print_state (FILE *out
, int level
, state
*s
)
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>");
364 xml_puts (out
, level
+ 1, "<solved-conflicts/>");
365 xml_puts (out
, level
, "</state>");
369 /*-----------------------------------------.
370 | Print information on the whole grammar. |
371 `-----------------------------------------*/
374 print_grammar (FILE *out
, int level
)
377 xml_puts (out
, level
, "<grammar>");
378 grammar_rules_print_xml (out
, level
);
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);
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");
399 fprintf (out
, " prec=\"%d\"", precedence
);
400 if (associativity
!= undef_assoc
)
401 fprintf (out
, " assoc=\"%s\"", assoc_to_string (associativity
) + 1);
404 xml_puts (out
, level
+ 1, "</terminals>");
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\""
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>");
427 xml_indent (FILE *out
, int level
)
429 for (int i
= 0; i
< level
; i
++)
434 xml_puts (FILE *out
, int level
, char const *s
)
436 xml_indent (out
, level
);
442 xml_printf (FILE *out
, int level
, char const *fmt
, ...)
446 xml_indent (out
, level
);
448 va_start (arglist
, fmt
);
449 vfprintf (out
, fmt
, arglist
);
456 xml_escape_string (struct escape_buf
*buf
, char const *str
)
458 size_t len
= strlen (str
);
459 size_t max_expansion
= sizeof """ - 1;
461 if (buf
->size
<= max_expansion
* len
)
463 buf
->size
= max_expansion
* len
+ 1;
464 buf
->ptr
= x2realloc (buf
->ptr
, &buf
->size
);
471 default: *p
++ = *str
; break;
472 case '&': p
= stpcpy (p
, "&" ); break;
473 case '<': p
= stpcpy (p
, "<" ); break;
474 case '>': p
= stpcpy (p
, ">" ); break;
475 case '"': p
= stpcpy (p
, """); break;
483 xml_escape_n (int n
, char const *str
)
485 return xml_escape_string (escape_bufs
+ n
, str
);
489 xml_escape (char const *str
)
491 return xml_escape_n (0, str
);
497 FILE *out
= xfopen (spec_xml_file
, "w");
499 fputs ("<?xml version=\"1.0\"?>\n\n", out
);
502 xml_printf (out
, level
,
503 "<bison-xml-report version=\"%s\" bug-report=\"%s\""
505 xml_escape_n (0, VERSION
),
506 xml_escape_n (1, PACKAGE_BUGREPORT
),
507 xml_escape_n (2, PACKAGE_URL
));
510 xml_printf (out
, level
+ 1, "<filename>%s</filename>",
511 xml_escape (grammar_file
));
514 print_grammar (out
, level
+ 1);
516 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
518 /* print automaton */
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
);