]> git.saurik.com Git - bison.git/blame - src/print-xml.c
* data/xslt/bison.xsl (bison:ruleNumber): Rename to...
[bison.git] / src / print-xml.c
CommitLineData
41d7a5f2
PE
1/* Print an xml on generated parser, for Bison,
2
3 Copyright (C) 2007 Free Software Foundation, Inc.
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};
51static struct escape_buf escape_bufs[2];
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;
247 rule *default_rule = NULL;
248 int report = false;
249 int i, j;
250
251 if (reds->num == 0) {
252 xml_puts (out, level, "<reductions/>");
253 return;
254 }
255
256 if (yydefact[s->number] != 0)
257 default_rule = &rules[yydefact[s->number] - 1];
258
259 bitset_zero (no_reduce_set);
260 FOR_EACH_SHIFT (trans, i)
261 bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i));
262 for (i = 0; i < s->errs->num; ++i)
263 if (s->errs->symbols[i])
264 bitset_set (no_reduce_set, s->errs->symbols[i]->number);
265
266 if (default_rule)
267 report = true;
268
269 if (reds->lookahead_tokens)
270 for (i = 0; i < ntokens; i++)
271 {
272 bool count = bitset_test (no_reduce_set, i);
273
274 for (j = 0; j < reds->num; ++j)
275 if (bitset_test (reds->lookahead_tokens[j], i))
276 {
277 if (! count)
278 {
279 if (reds->rules[j] != default_rule)
280 report = true;
281 count = true;
282 }
283 else
284 {
285 report = true;
286 }
287 }
288 }
289
290 /* Nothing to report. */
291 if (!report) {
292 xml_puts (out, level, "<reductions/>");
293 return;
294 }
295
296 xml_puts (out, level, "<reductions>");
297
298 /* Report lookahead tokens (or $default) and reductions. */
299 if (reds->lookahead_tokens)
300 for (i = 0; i < ntokens; i++)
301 {
302 bool defaulted = false;
303 bool count = bitset_test (no_reduce_set, i);
304
305 for (j = 0; j < reds->num; ++j)
306 if (bitset_test (reds->lookahead_tokens[j], i))
307 {
308 if (! count)
309 {
310 if (reds->rules[j] != default_rule)
311 print_reduction (out, level + 1, symbols[i]->tag,
312 reds->rules[j], true);
313 else
314 defaulted = true;
315 count = true;
316 }
317 else
318 {
319 if (defaulted)
320 print_reduction (out, level + 1, symbols[i]->tag,
321 default_rule, true);
322 defaulted = false;
323 print_reduction (out, level + 1, symbols[i]->tag,
324 reds->rules[j], false);
325 }
326 }
327 }
328
329 if (default_rule)
330 print_reduction (out, level + 1,
331 "$default", default_rule, true);
332
333 xml_puts (out, level, "</reductions>");
334}
335
336
337/*--------------------------------------------------------------.
338| Report on OUT all the actions (shifts, gotos, reductions, and |
339| explicit erros from %nonassoc) of S. |
340`--------------------------------------------------------------*/
341
342static void
343print_actions (FILE *out, int level, state *s)
344{
345 xml_puts (out, level, "<actions>");
346 print_transitions (s, out, level + 1);
347 print_errs (out, level + 1, s);
348 print_reductions (out, level + 1, s);
349 xml_puts (out, level, "</actions>");
350}
351
352
353/*----------------------------------.
354| Report all the data on S on OUT. |
355`----------------------------------*/
356
357static void
358print_state (FILE *out, int level, state *s)
359{
360 fputc ('\n', out);
361 xml_printf (out, level, "<state number=\"%d\">", s->number);
362 print_core (out, level + 1, s);
363 print_actions (out, level + 1, s);
ef1b4273 364 if (s->solved_conflicts_xml)
41d7a5f2
PE
365 {
366 xml_puts (out, level + 1, "<solved-conflicts>");
367 fputs (s->solved_conflicts_xml, out);
368 xml_puts (out, level + 1, "</solved-conflicts>");
369 }
370 else
371 xml_puts (out, level + 1, "<solved-conflicts/>");
372 xml_puts (out, level, "</state>");
373}
374
375
376/*-----------------------------------------.
377| Print information on the whole grammar. |
378`-----------------------------------------*/
379
380static void
381print_grammar (FILE *out, int level)
382{
383 symbol_number i;
384
385 fputc ('\n', out);
386 xml_puts (out, level, "<grammar>");
387 grammar_rules_print_xml (out, level);
388
389 /* Terminals */
390 xml_puts (out, level + 1, "<terminals>");
391 for (i = 0; i < max_user_token_number + 1; i++)
392 if (token_translations[i] != undeftoken->number)
393 {
394 char const *tag = symbols[token_translations[i]]->tag;
41d7a5f2 395 xml_printf (out, level + 2,
32f19b6b 396 "<terminal symbol-number=\"%d\" token-number=\"%d\""
d4a26c48 397 " name=\"%s\" usefulness=\"%s\"/>",
d80fb37a
JD
398 token_translations[i], i, xml_escape (tag),
399 reduce_token_unused_in_grammar (token_translations[i])
400 ? "unused-in-grammar" : "useful");
41d7a5f2
PE
401 }
402 xml_puts (out, level + 1, "</terminals>");
403
404 /* Nonterminals */
405 xml_puts (out, level + 1, "<nonterminals>");
d80fb37a 406 for (i = ntokens; i < nsyms + nuseless_nonterminals; i++)
41d7a5f2 407 {
41d7a5f2 408 char const *tag = symbols[i]->tag;
41d7a5f2 409 xml_printf (out, level + 2,
d80fb37a 410 "<nonterminal symbol-number=\"%d\" name=\"%s\""
d4a26c48 411 " usefulness=\"%s\"/>",
d80fb37a
JD
412 i, xml_escape (tag),
413 reduce_nonterminal_useless_in_grammar (i)
414 ? "useless-in-grammar" : "useful");
41d7a5f2
PE
415 }
416 xml_puts (out, level + 1, "</nonterminals>");
417 xml_puts (out, level, "</grammar>");
418}
419
420void
ad5feac4 421xml_puts (FILE *out, int level, char const *s)
41d7a5f2
PE
422{
423 int i;
41d7a5f2 424 for (i = 0; i < level; i++)
a4f75309 425 fputs (" ", out);
41d7a5f2
PE
426 fputs (s, out);
427 fputc ('\n', out);
428}
429
430void
431xml_printf (FILE *out, int level, char const *fmt, ...)
432{
433 int i;
434 va_list arglist;
435
436 for (i = 0; i < level; i++)
a4f75309 437 fputs (" ", out);
41d7a5f2
PE
438
439 va_start (arglist, fmt);
440 vfprintf (out, fmt, arglist);
441 va_end (arglist);
442
443 fputc ('\n', out);
444}
445
41d7a5f2
PE
446static char const *
447xml_escape_string (struct escape_buf *buf, char const *str)
448{
449 size_t len = strlen (str);
450 size_t max_expansion = sizeof "&quot;" - 1;
451 char *p;
452
453 if (buf->size <= max_expansion * len)
454 {
455 buf->size = max_expansion * len + 1;
456 buf->ptr = x2realloc (buf->ptr, &buf->size);
457 }
458 p = buf->ptr;
459
460 for (; *str; str++)
461 switch (*str)
462 {
463 default: *p++ = *str; break;
464 case '&': p = stpcpy (p, "&amp;" ); break;
465 case '<': p = stpcpy (p, "&lt;" ); break;
466 case '>': p = stpcpy (p, "&gt;" ); break;
467 case '"': p = stpcpy (p, "&quot;"); break;
468 }
469
470 *p = '\0';
471 return buf->ptr;
472}
473
474char const *
475xml_escape_n (int n, char const *str)
476{
34cdeddf 477 return xml_escape_string (escape_bufs + n, str);
41d7a5f2
PE
478}
479
480char const *
481xml_escape (char const *str)
482{
483 return xml_escape_n (0, str);
484}
485
486void
487print_xml (void)
488{
489 state_number i;
490 int level = 0;
491
492 FILE *out = xfopen (spec_xml_file, "w");
493
494 fputs ("<?xml version=\"1.0\"?>\n\n", out);
495 xml_printf (out, level, "<bison-xml-report version=\"%s\">",
496 xml_escape (VERSION));
497
498 fputc ('\n', out);
499 xml_printf (out, level + 1, "<filename>%s</filename>",
500 xml_escape (grammar_file));
501
41d7a5f2
PE
502 /* print grammar */
503 print_grammar (out, level + 1);
504
ef1b4273 505 new_closure (nritems);
41d7a5f2
PE
506 no_reduce_set = bitset_create (ntokens, BITSET_FIXED);
507
508 /* print automaton */
509 fputc ('\n', out);
510 xml_puts (out, level + 1, "<automaton>");
511 for (i = 0; i < nstates; i++)
512 print_state (out, level + 2, states[i]);
513 xml_puts (out, level + 1, "</automaton>");
514
515 bitset_free (no_reduce_set);
ef1b4273 516 free_closure ();
41d7a5f2
PE
517
518 xml_puts (out, 0, "</bison-xml-report>");
519
34cdeddf
JD
520 free (escape_bufs[0].ptr);
521 free (escape_bufs[1].ptr);
522
41d7a5f2
PE
523 xfclose (out);
524}