]>
git.saurik.com Git - bison.git/blob - src/print-xml.c
ff2bacd9b25951a2833d84d1a7c656744fe71854
1 /* Print an xml on generated parser, for Bison,
3 Copyright (C) 2007 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
32 #include "conflicts.h"
38 #include "print-xml.h"
45 static bitset no_reduce_set
;
51 static struct escape_buf escape_bufs
[2];
54 /*--------------------------------.
55 | Report information on a state. |
56 `--------------------------------*/
59 print_core (FILE *out
, int level
, state
*s
)
62 item_number
*sitems
= s
->items
;
63 size_t snritems
= s
->nitems
;
65 /* Output all the items of a state, not only its kernel. */
66 if (report_flag
& report_itemsets
)
68 closure (sitems
, snritems
);
74 xml_puts (out
, level
, "<itemset/>");
78 xml_puts (out
, level
, "<itemset>");
80 for (i
= 0; i
< snritems
; i
++)
87 sp1
= sp
= ritem
+ sitems
[i
];
92 r
= item_number_as_rule_number (*sp
);
95 /* Display the lookahead tokens? */
96 if (report_flag
& report_lookahead_tokens
97 && item_number_is_rule_number (*sp1
))
99 reductions
*reds
= s
->reductions
;
100 int red
= state_reduction_find (s
, &rules
[r
]);
101 /* Print item with lookaheads if there are. */
102 if (reds
->lookahead_tokens
&& red
!= -1)
104 xml_printf (out
, level
+ 1,
105 "<item rule-number=\"%d\" point=\"%d\">",
106 rules
[r
].number
, sp1
- sp
);
107 state_rule_lookahead_tokens_print_xml (s
, &rules
[r
],
109 xml_puts (out
, level
+ 1, "</item>");
116 xml_printf (out
, level
+ 1,
117 "<item rule-number=\"%d\" point=\"%d\"/>",
122 xml_puts (out
, level
, "</itemset>");
126 /*-----------------------------------------------------------.
127 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
129 `-----------------------------------------------------------*/
132 print_transitions (state
*s
, FILE *out
, int level
)
134 transitions
*trans
= s
->transitions
;
138 for (i
= 0; i
< trans
->num
; i
++)
139 if (!TRANSITION_IS_DISABLED (trans
, i
))
144 /* Nothing to report. */
146 xml_puts (out
, level
, "<transitions/>");
150 /* Report lookahead tokens and shifts. */
151 xml_puts (out
, level
, "<transitions>");
153 for (i
= 0; i
< trans
->num
; i
++)
154 if (!TRANSITION_IS_DISABLED (trans
, i
)
155 && TRANSITION_IS_SHIFT (trans
, i
))
157 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
158 char const *tag
= sym
->tag
;
159 state
*s1
= trans
->states
[i
];
161 xml_printf (out
, level
+ 1,
162 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
163 xml_escape (tag
), s1
->number
);
166 for (i
= 0; i
< trans
->num
; i
++)
167 if (!TRANSITION_IS_DISABLED (trans
, i
)
168 && !TRANSITION_IS_SHIFT (trans
, i
))
170 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
171 char const *tag
= sym
->tag
;
172 state
*s1
= trans
->states
[i
];
174 xml_printf (out
, level
+ 1,
175 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
176 xml_escape (tag
), s1
->number
);
179 xml_puts (out
, level
, "</transitions>");
183 /*--------------------------------------------------------.
184 | Report the explicit errors of S raised from %nonassoc. |
185 `--------------------------------------------------------*/
188 print_errs (FILE *out
, int level
, state
*s
)
190 errs
*errp
= s
->errs
;
194 for (i
= 0; i
< errp
->num
; ++i
)
195 if (errp
->symbols
[i
])
198 /* Nothing to report. */
200 xml_puts (out
, level
, "<errors/>");
204 /* Report lookahead tokens and errors. */
205 xml_puts (out
, level
, "<errors>");
206 for (i
= 0; i
< errp
->num
; ++i
)
207 if (errp
->symbols
[i
])
209 char const *tag
= errp
->symbols
[i
]->tag
;
210 xml_printf (out
, level
+ 1,
211 "<error symbol=\"%s\">nonassociative</error>",
214 xml_puts (out
, level
, "</errors>");
218 /*-------------------------------------------------------------------------.
219 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
220 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
222 `-------------------------------------------------------------------------*/
225 print_reduction (FILE *out
, int level
, char const *lookahead_token
,
226 rule
*r
, bool enabled
)
229 xml_printf (out
, level
,
230 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
231 xml_escape (lookahead_token
),
233 enabled
? "true" : "false");
235 xml_printf (out
, level
,
236 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
237 xml_escape (lookahead_token
),
238 enabled
? "true" : "false");
242 /*-------------------------------------------.
243 | Report on OUT the reduction actions of S. |
244 `-------------------------------------------*/
247 print_reductions (FILE *out
, int level
, state
*s
)
249 transitions
*trans
= s
->transitions
;
250 reductions
*reds
= s
->reductions
;
251 rule
*default_rule
= NULL
;
255 if (reds
->num
== 0) {
256 xml_puts (out
, level
, "<reductions/>");
260 if (yydefact
[s
->number
] != 0)
261 default_rule
= &rules
[yydefact
[s
->number
] - 1];
263 bitset_zero (no_reduce_set
);
264 FOR_EACH_SHIFT (trans
, i
)
265 bitset_set (no_reduce_set
, TRANSITION_SYMBOL (trans
, i
));
266 for (i
= 0; i
< s
->errs
->num
; ++i
)
267 if (s
->errs
->symbols
[i
])
268 bitset_set (no_reduce_set
, s
->errs
->symbols
[i
]->number
);
273 if (reds
->lookahead_tokens
)
274 for (i
= 0; i
< ntokens
; i
++)
276 bool count
= bitset_test (no_reduce_set
, i
);
278 for (j
= 0; j
< reds
->num
; ++j
)
279 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
283 if (reds
->rules
[j
] != default_rule
)
294 /* Nothing to report. */
296 xml_puts (out
, level
, "<reductions/>");
300 xml_puts (out
, level
, "<reductions>");
302 /* Report lookahead tokens (or $default) and reductions. */
303 if (reds
->lookahead_tokens
)
304 for (i
= 0; i
< ntokens
; i
++)
306 bool defaulted
= false;
307 bool count
= bitset_test (no_reduce_set
, i
);
309 for (j
= 0; j
< reds
->num
; ++j
)
310 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
314 if (reds
->rules
[j
] != default_rule
)
315 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
316 reds
->rules
[j
], true);
324 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
327 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
328 reds
->rules
[j
], false);
334 print_reduction (out
, level
+ 1,
335 "$default", default_rule
, true);
337 xml_puts (out
, level
, "</reductions>");
341 /*--------------------------------------------------------------.
342 | Report on OUT all the actions (shifts, gotos, reductions, and |
343 | explicit erros from %nonassoc) of S. |
344 `--------------------------------------------------------------*/
347 print_actions (FILE *out
, int level
, state
*s
)
349 xml_puts (out
, level
, "<actions>");
350 print_transitions (s
, out
, level
+ 1);
351 print_errs (out
, level
+ 1, s
);
352 print_reductions (out
, level
+ 1, s
);
353 xml_puts (out
, level
, "</actions>");
357 /*----------------------------------.
358 | Report all the data on S on OUT. |
359 `----------------------------------*/
362 print_state (FILE *out
, int level
, state
*s
)
365 xml_printf (out
, level
, "<state number=\"%d\">", s
->number
);
366 print_core (out
, level
+ 1, s
);
367 print_actions (out
, level
+ 1, s
);
368 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts_xml
)
370 xml_puts (out
, level
+ 1, "<solved-conflicts>");
371 fputs (s
->solved_conflicts_xml
, out
);
372 xml_puts (out
, level
+ 1, "</solved-conflicts>");
375 xml_puts (out
, level
+ 1, "<solved-conflicts/>");
376 xml_puts (out
, level
, "</state>");
380 /*-----------------------------------------.
381 | Print information on the whole grammar. |
382 `-----------------------------------------*/
385 print_grammar (FILE *out
, int level
)
390 xml_puts (out
, level
, "<grammar>");
391 grammar_rules_print_xml (out
, level
);
394 xml_puts (out
, level
+ 1, "<terminals>");
395 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
396 if (token_translations
[i
] != undeftoken
->number
)
398 char const *tag
= symbols
[token_translations
[i
]]->tag
;
402 xml_printf (out
, level
+ 2,
403 "<terminal symbol-number=\"%d\" token-number=\"%d\""
404 " name=\"%s\" usefulness=\"%s\">",
405 token_translations
[i
], i
, xml_escape (tag
),
406 reduce_token_unused_in_grammar (token_translations
[i
])
407 ? "unused-in-grammar" : "useful");
409 for (r
= 0; r
< nrules
; r
++)
410 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
411 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
413 xml_printf (out
, level
+ 3, "<rule>%d</rule>", r
);
416 xml_puts (out
, level
+ 2, "</terminal>");
418 xml_puts (out
, level
+ 1, "</terminals>");
421 xml_puts (out
, level
+ 1, "<nonterminals>");
422 for (i
= ntokens
; i
< nsyms
+ nuseless_nonterminals
; i
++)
424 int left_count
= 0, right_count
= 0;
426 char const *tag
= symbols
[i
]->tag
;
428 for (r
= 0; r
< nrules
; r
++)
431 if (rules
[r
].lhs
->number
== i
)
433 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
434 if (item_number_as_symbol_number (*rhsp
) == i
)
441 xml_printf (out
, level
+ 2,
442 "<nonterminal symbol-number=\"%d\" name=\"%s\""
443 " usefulness=\"%s\">",
445 reduce_nonterminal_useless_in_grammar (i
)
446 ? "useless-in-grammar" : "useful");
450 xml_puts (out
, level
+ 3, "<left>");
451 for (r
= 0; r
< nrules
; r
++)
453 if (rules
[r
].lhs
->number
== i
)
454 xml_printf (out
, level
+ 4, "<rule>%d</rule>", r
);
456 xml_puts (out
, level
+ 3, "</left>");
461 xml_puts (out
, level
+ 3, "<right>");
462 for (r
= 0; r
< nrules
; r
++)
465 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
466 if (item_number_as_symbol_number (*rhsp
) == i
)
468 xml_printf (out
, level
+ 4, "<rule>%d</rule>", r
);
472 xml_puts (out
, level
+ 3, "</right>");
475 xml_puts (out
, level
+ 2, "</nonterminal>");
477 xml_puts (out
, level
+ 1, "</nonterminals>");
478 xml_puts (out
, level
, "</grammar>");
482 xml_puts (FILE *out
, int level
, char const *s
)
485 for (i
= 0; i
< level
; i
++)
492 xml_printf (FILE *out
, int level
, char const *fmt
, ...)
497 for (i
= 0; i
< level
; i
++)
500 va_start (arglist
, fmt
);
501 vfprintf (out
, fmt
, arglist
);
508 xml_escape_string (struct escape_buf
*buf
, char const *str
)
510 size_t len
= strlen (str
);
511 size_t max_expansion
= sizeof """ - 1;
514 if (buf
->size
<= max_expansion
* len
)
516 buf
->size
= max_expansion
* len
+ 1;
517 buf
->ptr
= x2realloc (buf
->ptr
, &buf
->size
);
524 default: *p
++ = *str
; break;
525 case '&': p
= stpcpy (p
, "&" ); break;
526 case '<': p
= stpcpy (p
, "<" ); break;
527 case '>': p
= stpcpy (p
, ">" ); break;
528 case '"': p
= stpcpy (p
, """); break;
536 xml_escape_n (int n
, char const *str
)
538 return xml_escape_string (escape_bufs
+ n
, str
);
542 xml_escape (char const *str
)
544 return xml_escape_n (0, str
);
553 FILE *out
= xfopen (spec_xml_file
, "w");
555 fputs ("<?xml version=\"1.0\"?>\n\n", out
);
556 xml_printf (out
, level
, "<bison-xml-report version=\"%s\">",
557 xml_escape (VERSION
));
560 xml_printf (out
, level
+ 1, "<filename>%s</filename>",
561 xml_escape (grammar_file
));
564 print_grammar (out
, level
+ 1);
566 if (report_flag
& report_itemsets
)
567 new_closure (nritems
);
568 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
570 /* print automaton */
572 xml_puts (out
, level
+ 1, "<automaton>");
573 for (i
= 0; i
< nstates
; i
++)
574 print_state (out
, level
+ 2, states
[i
]);
575 xml_puts (out
, level
+ 1, "</automaton>");
577 bitset_free (no_reduce_set
);
578 if (report_flag
& report_itemsets
)
581 xml_puts (out
, 0, "</bison-xml-report>");
583 free (escape_bufs
[0].ptr
);
584 free (escape_bufs
[1].ptr
);