]> git.saurik.com Git - bison.git/blame - src/print-xml.c
Add -Wconflicts-sr and -Wconflicts-rr.
[bison.git] / src / print-xml.c
CommitLineData
41d7a5f2
PE
1/* Print an xml on generated parser, for Bison,
2
575619af 3 Copyright (C) 2007, 2009-2011 Free Software Foundation, Inc.
41d7a5f2
PE
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22#include <config.h>
23#include "system.h"
24
25#include <stdarg.h>
26
27#include <bitset.h>
28#include <quotearg.h>
29
30#include "LR0.h"
31#include "closure.h"
32#include "conflicts.h"
33#include "files.h"
34#include "getargs.h"
35#include "gram.h"
36#include "lalr.h"
37#include "print.h"
38#include "print-xml.h"
39#include "reader.h"
40#include "reduce.h"
41#include "state.h"
42#include "symtab.h"
43#include "tables.h"
44
45static bitset no_reduce_set;
34cdeddf
JD
46struct escape_buf
47{
48 char *ptr;
49 size_t size;
50};
bc81de36 51static struct escape_buf escape_bufs[3];
41d7a5f2
PE
52
53
41d7a5f2
PE
54/*--------------------------------.
55| Report information on a state. |
56`--------------------------------*/
57
58static void
59print_core (FILE *out, int level, state *s)
60{
61 size_t i;
62 item_number *sitems = s->items;
63 size_t snritems = s->nitems;
64
65 /* Output all the items of a state, not only its kernel. */
ef1b4273
JD
66 closure (sitems, snritems);
67 sitems = itemset;
68 snritems = nitemset;
41d7a5f2
PE
69
70 if (!snritems) {
71 xml_puts (out, level, "<itemset/>");
72 return;
73 }
74
75 xml_puts (out, level, "<itemset>");
76
77 for (i = 0; i < snritems; i++)
78 {
25f6da67 79 bool printed = false;
41d7a5f2
PE
80 item_number *sp;
81 item_number *sp1;
82 rule_number r;
83
84 sp1 = sp = ritem + sitems[i];
85
86 while (*sp >= 0)
87 sp++;
88
89 r = item_number_as_rule_number (*sp);
25f6da67 90 sp = rules[r].rhs;
41d7a5f2
PE
91
92 /* Display the lookahead tokens? */
ef1b4273 93 if (item_number_is_rule_number (*sp1))
25f6da67
WP
94 {
95 reductions *reds = s->reductions;
96 int red = state_reduction_find (s, &rules[r]);
97 /* Print item with lookaheads if there are. */
98 if (reds->lookahead_tokens && red != -1)
99 {
100 xml_printf (out, level + 1,
101 "<item rule-number=\"%d\" point=\"%d\">",
102 rules[r].number, sp1 - sp);
103 state_rule_lookahead_tokens_print_xml (s, &rules[r],
104 out, level + 2);
105 xml_puts (out, level + 1, "</item>");
106 printed = true;
107 }
108 }
41d7a5f2 109
25f6da67
WP
110 if (!printed)
111 {
112 xml_printf (out, level + 1,
113 "<item rule-number=\"%d\" point=\"%d\"/>",
114 rules[r].number,
115 sp1 - sp);
116 }
41d7a5f2
PE
117 }
118 xml_puts (out, level, "</itemset>");
119}
120
121
122/*-----------------------------------------------------------.
123| Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
124| OUT. |
125`-----------------------------------------------------------*/
126
127static void
128print_transitions (state *s, FILE *out, int level)
129{
130 transitions *trans = s->transitions;
131 int n = 0;
132 int i;
133
134 for (i = 0; i < trans->num; i++)
135 if (!TRANSITION_IS_DISABLED (trans, i))
136 {
137 n++;
138 }
139
140 /* Nothing to report. */
141 if (!n) {
142 xml_puts (out, level, "<transitions/>");
143 return;
144 }
145
146 /* Report lookahead tokens and shifts. */
147 xml_puts (out, level, "<transitions>");
148
149 for (i = 0; i < trans->num; i++)
150 if (!TRANSITION_IS_DISABLED (trans, i)
151 && TRANSITION_IS_SHIFT (trans, i))
152 {
153 symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
154 char const *tag = sym->tag;
155 state *s1 = trans->states[i];
156
157 xml_printf (out, level + 1,
158 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
159 xml_escape (tag), s1->number);
160 }
161
162 for (i = 0; i < trans->num; i++)
163 if (!TRANSITION_IS_DISABLED (trans, i)
164 && !TRANSITION_IS_SHIFT (trans, i))
165 {
166 symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
167 char const *tag = sym->tag;
168 state *s1 = trans->states[i];
169
170 xml_printf (out, level + 1,
171 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
172 xml_escape (tag), s1->number);
173 }
174
175 xml_puts (out, level, "</transitions>");
176}
177
178
179/*--------------------------------------------------------.
180| Report the explicit errors of S raised from %nonassoc. |
181`--------------------------------------------------------*/
182
183static void
184print_errs (FILE *out, int level, state *s)
185{
186 errs *errp = s->errs;
187 bool count = false;
188 int i;
189
190 for (i = 0; i < errp->num; ++i)
191 if (errp->symbols[i])
192 count = true;
193
194 /* Nothing to report. */
195 if (!count) {
196 xml_puts (out, level, "<errors/>");
197 return;
198 }
199
200 /* Report lookahead tokens and errors. */
201 xml_puts (out, level, "<errors>");
202 for (i = 0; i < errp->num; ++i)
203 if (errp->symbols[i])
204 {
205 char const *tag = errp->symbols[i]->tag;
206 xml_printf (out, level + 1,
207 "<error symbol=\"%s\">nonassociative</error>",
208 xml_escape (tag));
209 }
210 xml_puts (out, level, "</errors>");
211}
212
213
214/*-------------------------------------------------------------------------.
215| Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
216| If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
217| R/R conflicts). |
218`-------------------------------------------------------------------------*/
219
220static void
221print_reduction (FILE *out, int level, char const *lookahead_token,
222 rule *r, bool enabled)
223{
224 if (r->number)
225 xml_printf (out, level,
226 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
227 xml_escape (lookahead_token),
228 r->number,
229 enabled ? "true" : "false");
230 else
231 xml_printf (out, level,
232 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
233 xml_escape (lookahead_token),
234 enabled ? "true" : "false");
235}
236
237
238/*-------------------------------------------.
239| Report on OUT the reduction actions of S. |
240`-------------------------------------------*/
241
242static void
243print_reductions (FILE *out, int level, state *s)
244{
245 transitions *trans = s->transitions;
246 reductions *reds = s->reductions;
110ef36a 247 rule *default_reduction = NULL;
41d7a5f2
PE
248 int report = false;
249 int i, j;
250
41976786
AD
251 if (reds->num == 0)
252 {
253 xml_puts (out, level, "<reductions/>");
254 return;
255 }
41d7a5f2
PE
256
257 if (yydefact[s->number] != 0)
110ef36a 258 default_reduction = &rules[yydefact[s->number] - 1];
41d7a5f2
PE
259
260 bitset_zero (no_reduce_set);
261 FOR_EACH_SHIFT (trans, i)
262 bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i));
263 for (i = 0; i < s->errs->num; ++i)
264 if (s->errs->symbols[i])
265 bitset_set (no_reduce_set, s->errs->symbols[i]->number);
266
110ef36a 267 if (default_reduction)
41d7a5f2
PE
268 report = true;
269
270 if (reds->lookahead_tokens)
271 for (i = 0; i < ntokens; i++)
272 {
273 bool count = bitset_test (no_reduce_set, i);
274
275 for (j = 0; j < reds->num; ++j)
276 if (bitset_test (reds->lookahead_tokens[j], i))
277 {
278 if (! count)
279 {
110ef36a 280 if (reds->rules[j] != default_reduction)
41d7a5f2
PE
281 report = true;
282 count = true;
283 }
284 else
285 {
286 report = true;
287 }
288 }
289 }
290
291 /* Nothing to report. */
292 if (!report) {
293 xml_puts (out, level, "<reductions/>");
294 return;
295 }
296
297 xml_puts (out, level, "<reductions>");
298
299 /* Report lookahead tokens (or $default) and reductions. */
300 if (reds->lookahead_tokens)
301 for (i = 0; i < ntokens; i++)
302 {
303 bool defaulted = false;
304 bool count = bitset_test (no_reduce_set, i);
305
306 for (j = 0; j < reds->num; ++j)
307 if (bitset_test (reds->lookahead_tokens[j], i))
308 {
309 if (! count)
310 {
110ef36a 311 if (reds->rules[j] != default_reduction)
41d7a5f2
PE
312 print_reduction (out, level + 1, symbols[i]->tag,
313 reds->rules[j], true);
314 else
315 defaulted = true;
316 count = true;
317 }
318 else
319 {
320 if (defaulted)
321 print_reduction (out, level + 1, symbols[i]->tag,
110ef36a 322 default_reduction, true);
41d7a5f2
PE
323 defaulted = false;
324 print_reduction (out, level + 1, symbols[i]->tag,
325 reds->rules[j], false);
326 }
327 }
328 }
329
110ef36a 330 if (default_reduction)
41d7a5f2 331 print_reduction (out, level + 1,
110ef36a 332 "$default", default_reduction, true);
41d7a5f2
PE
333
334 xml_puts (out, level, "</reductions>");
335}
336
337
338/*--------------------------------------------------------------.
339| Report on OUT all the actions (shifts, gotos, reductions, and |
340| explicit erros from %nonassoc) of S. |
341`--------------------------------------------------------------*/
342
343static void
344print_actions (FILE *out, int level, state *s)
345{
346 xml_puts (out, level, "<actions>");
347 print_transitions (s, out, level + 1);
348 print_errs (out, level + 1, s);
349 print_reductions (out, level + 1, s);
350 xml_puts (out, level, "</actions>");
351}
352
353
354/*----------------------------------.
355| Report all the data on S on OUT. |
356`----------------------------------*/
357
358static void
359print_state (FILE *out, int level, state *s)
360{
361 fputc ('\n', out);
362 xml_printf (out, level, "<state number=\"%d\">", s->number);
363 print_core (out, level + 1, s);
364 print_actions (out, level + 1, s);
ef1b4273 365 if (s->solved_conflicts_xml)
41d7a5f2
PE
366 {
367 xml_puts (out, level + 1, "<solved-conflicts>");
368 fputs (s->solved_conflicts_xml, out);
369 xml_puts (out, level + 1, "</solved-conflicts>");
370 }
371 else
372 xml_puts (out, level + 1, "<solved-conflicts/>");
373 xml_puts (out, level, "</state>");
374}
375
376
377/*-----------------------------------------.
378| Print information on the whole grammar. |
379`-----------------------------------------*/
380
381static void
382print_grammar (FILE *out, int level)
383{
384 symbol_number i;
385
386 fputc ('\n', out);
387 xml_puts (out, level, "<grammar>");
388 grammar_rules_print_xml (out, level);
389
390 /* Terminals */
391 xml_puts (out, level + 1, "<terminals>");
392 for (i = 0; i < max_user_token_number + 1; i++)
393 if (token_translations[i] != undeftoken->number)
394 {
395 char const *tag = symbols[token_translations[i]]->tag;
408476bc
JD
396 int precedence = symbols[token_translations[i]]->prec;
397 assoc associativity = symbols[token_translations[i]]->assoc;
398 xml_indent (out, level + 2);
399 fprintf (out,
400 "<terminal symbol-number=\"%d\" token-number=\"%d\""
401 " name=\"%s\" usefulness=\"%s\"",
402 token_translations[i], i, xml_escape (tag),
403 reduce_token_unused_in_grammar (token_translations[i])
404 ? "unused-in-grammar" : "useful");
405 if (precedence)
406 fprintf (out, " prec=\"%d\"", precedence);
407 if (associativity != undef_assoc)
408 fprintf (out, " assoc=\"%s\"", assoc_to_string (associativity) + 1);
409 fputs ("/>\n", out);
41d7a5f2
PE
410 }
411 xml_puts (out, level + 1, "</terminals>");
412
413 /* Nonterminals */
414 xml_puts (out, level + 1, "<nonterminals>");
d80fb37a 415 for (i = ntokens; i < nsyms + nuseless_nonterminals; i++)
41d7a5f2 416 {
41d7a5f2 417 char const *tag = symbols[i]->tag;
41d7a5f2 418 xml_printf (out, level + 2,
d80fb37a 419 "<nonterminal symbol-number=\"%d\" name=\"%s\""
d4a26c48 420 " usefulness=\"%s\"/>",
d80fb37a
JD
421 i, xml_escape (tag),
422 reduce_nonterminal_useless_in_grammar (i)
423 ? "useless-in-grammar" : "useful");
41d7a5f2
PE
424 }
425 xml_puts (out, level + 1, "</nonterminals>");
426 xml_puts (out, level, "</grammar>");
427}
428
429void
408476bc 430xml_indent (FILE *out, int level)
41d7a5f2
PE
431{
432 int i;
41d7a5f2 433 for (i = 0; i < level; i++)
a4f75309 434 fputs (" ", out);
408476bc
JD
435}
436
437void
438xml_puts (FILE *out, int level, char const *s)
439{
440 xml_indent (out, level);
41d7a5f2
PE
441 fputs (s, out);
442 fputc ('\n', out);
443}
444
445void
446xml_printf (FILE *out, int level, char const *fmt, ...)
447{
41d7a5f2
PE
448 va_list arglist;
449
408476bc 450 xml_indent (out, level);
41d7a5f2
PE
451
452 va_start (arglist, fmt);
453 vfprintf (out, fmt, arglist);
454 va_end (arglist);
455
456 fputc ('\n', out);
457}
458
41d7a5f2
PE
459static char const *
460xml_escape_string (struct escape_buf *buf, char const *str)
461{
462 size_t len = strlen (str);
463 size_t max_expansion = sizeof "&quot;" - 1;
464 char *p;
465
466 if (buf->size <= max_expansion * len)
467 {
468 buf->size = max_expansion * len + 1;
469 buf->ptr = x2realloc (buf->ptr, &buf->size);
470 }
471 p = buf->ptr;
472
473 for (; *str; str++)
474 switch (*str)
475 {
476 default: *p++ = *str; break;
477 case '&': p = stpcpy (p, "&amp;" ); break;
478 case '<': p = stpcpy (p, "&lt;" ); break;
479 case '>': p = stpcpy (p, "&gt;" ); break;
480 case '"': p = stpcpy (p, "&quot;"); break;
481 }
482
483 *p = '\0';
484 return buf->ptr;
485}
486
487char const *
488xml_escape_n (int n, char const *str)
489{
34cdeddf 490 return xml_escape_string (escape_bufs + n, str);
41d7a5f2
PE
491}
492
493char const *
494xml_escape (char const *str)
495{
496 return xml_escape_n (0, str);
497}
498
499void
500print_xml (void)
501{
502 state_number i;
503 int level = 0;
504
505 FILE *out = xfopen (spec_xml_file, "w");
506
507 fputs ("<?xml version=\"1.0\"?>\n\n", out);
bc81de36
JD
508 xml_printf (out, level,
509 "<bison-xml-report version=\"%s\" bug-report=\"%s\""
510 " url=\"%s\">",
511 xml_escape_n (0, VERSION),
512 xml_escape_n (1, PACKAGE_BUGREPORT),
513 xml_escape_n (2, PACKAGE_URL));
41d7a5f2
PE
514
515 fputc ('\n', out);
516 xml_printf (out, level + 1, "<filename>%s</filename>",
517 xml_escape (grammar_file));
518
41d7a5f2
PE
519 /* print grammar */
520 print_grammar (out, level + 1);
521
ef1b4273 522 new_closure (nritems);
41d7a5f2
PE
523 no_reduce_set = bitset_create (ntokens, BITSET_FIXED);
524
525 /* print automaton */
526 fputc ('\n', out);
527 xml_puts (out, level + 1, "<automaton>");
528 for (i = 0; i < nstates; i++)
529 print_state (out, level + 2, states[i]);
530 xml_puts (out, level + 1, "</automaton>");
531
532 bitset_free (no_reduce_set);
ef1b4273 533 free_closure ();
41d7a5f2
PE
534
535 xml_puts (out, 0, "</bison-xml-report>");
536
34cdeddf
JD
537 free (escape_bufs[0].ptr);
538 free (escape_bufs[1].ptr);
539
41d7a5f2
PE
540 xfclose (out);
541}