]>
git.saurik.com Git - bison.git/blob - src/print-xml.c
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 | Print rules never reduced. |
56 `-----------------------------*/
59 print_rules_never_reduced (FILE *out
, int level
)
64 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
66 if (rule_never_reduced_p (&rules
[r
]))
74 xml_puts (out
, level
, "<rules-never-reduced>");
75 grammar_rules_partial_print_xml (out
, level
- 1,
76 false, rule_never_reduced_p
);
77 xml_puts (out
, level
, "</rules-never-reduced>");
80 xml_puts (out
, level
, "<rules-never-reduced/>");
83 /*--------------------------------.
84 | Report information on a state. |
85 `--------------------------------*/
88 print_core (FILE *out
, int level
, state
*s
)
91 item_number
*sitems
= s
->items
;
92 size_t snritems
= s
->nitems
;
94 /* Output all the items of a state, not only its kernel. */
95 if (report_flag
& report_itemsets
)
97 closure (sitems
, snritems
);
103 xml_puts (out
, level
, "<itemset/>");
107 xml_puts (out
, level
, "<itemset>");
109 for (i
= 0; i
< snritems
; i
++)
111 bool printed
= false;
116 sp1
= sp
= ritem
+ sitems
[i
];
121 r
= item_number_as_rule_number (*sp
);
124 /* Display the lookahead tokens? */
125 if (report_flag
& report_lookahead_tokens
)
127 reductions
*reds
= s
->reductions
;
128 int red
= state_reduction_find (s
, &rules
[r
]);
129 /* Print item with lookaheads if there are. */
130 if (reds
->lookahead_tokens
&& red
!= -1)
132 xml_printf (out
, level
+ 1,
133 "<item rule-number=\"%d\" point=\"%d\">",
134 rules
[r
].number
, sp1
- sp
);
135 state_rule_lookahead_tokens_print_xml (s
, &rules
[r
],
137 xml_puts (out
, level
+ 1, "</item>");
144 xml_printf (out
, level
+ 1,
145 "<item rule-number=\"%d\" point=\"%d\"/>",
150 xml_puts (out
, level
, "</itemset>");
154 /*-----------------------------------------------------------.
155 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
157 `-----------------------------------------------------------*/
160 print_transitions (state
*s
, FILE *out
, int level
)
162 transitions
*trans
= s
->transitions
;
166 for (i
= 0; i
< trans
->num
; i
++)
167 if (!TRANSITION_IS_DISABLED (trans
, i
))
172 /* Nothing to report. */
174 xml_puts (out
, level
, "<transitions/>");
178 /* Report lookahead tokens and shifts. */
179 xml_puts (out
, level
, "<transitions>");
181 for (i
= 0; i
< trans
->num
; i
++)
182 if (!TRANSITION_IS_DISABLED (trans
, i
)
183 && TRANSITION_IS_SHIFT (trans
, i
))
185 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
186 char const *tag
= sym
->tag
;
187 state
*s1
= trans
->states
[i
];
189 xml_printf (out
, level
+ 1,
190 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
191 xml_escape (tag
), s1
->number
);
194 for (i
= 0; i
< trans
->num
; i
++)
195 if (!TRANSITION_IS_DISABLED (trans
, i
)
196 && !TRANSITION_IS_SHIFT (trans
, i
))
198 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
199 char const *tag
= sym
->tag
;
200 state
*s1
= trans
->states
[i
];
202 xml_printf (out
, level
+ 1,
203 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
204 xml_escape (tag
), s1
->number
);
207 xml_puts (out
, level
, "</transitions>");
211 /*--------------------------------------------------------.
212 | Report the explicit errors of S raised from %nonassoc. |
213 `--------------------------------------------------------*/
216 print_errs (FILE *out
, int level
, state
*s
)
218 errs
*errp
= s
->errs
;
222 for (i
= 0; i
< errp
->num
; ++i
)
223 if (errp
->symbols
[i
])
226 /* Nothing to report. */
228 xml_puts (out
, level
, "<errors/>");
232 /* Report lookahead tokens and errors. */
233 xml_puts (out
, level
, "<errors>");
234 for (i
= 0; i
< errp
->num
; ++i
)
235 if (errp
->symbols
[i
])
237 char const *tag
= errp
->symbols
[i
]->tag
;
238 xml_printf (out
, level
+ 1,
239 "<error symbol=\"%s\">nonassociative</error>",
242 xml_puts (out
, level
, "</errors>");
246 /*-------------------------------------------------------------------------.
247 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
248 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
250 `-------------------------------------------------------------------------*/
253 print_reduction (FILE *out
, int level
, char const *lookahead_token
,
254 rule
*r
, bool enabled
)
257 xml_printf (out
, level
,
258 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
259 xml_escape (lookahead_token
),
261 enabled
? "true" : "false");
263 xml_printf (out
, level
,
264 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
265 xml_escape (lookahead_token
),
266 enabled
? "true" : "false");
270 /*-------------------------------------------.
271 | Report on OUT the reduction actions of S. |
272 `-------------------------------------------*/
275 print_reductions (FILE *out
, int level
, state
*s
)
277 transitions
*trans
= s
->transitions
;
278 reductions
*reds
= s
->reductions
;
279 rule
*default_rule
= NULL
;
283 if (reds
->num
== 0) {
284 xml_puts (out
, level
, "<reductions/>");
288 if (yydefact
[s
->number
] != 0)
289 default_rule
= &rules
[yydefact
[s
->number
] - 1];
291 bitset_zero (no_reduce_set
);
292 FOR_EACH_SHIFT (trans
, i
)
293 bitset_set (no_reduce_set
, TRANSITION_SYMBOL (trans
, i
));
294 for (i
= 0; i
< s
->errs
->num
; ++i
)
295 if (s
->errs
->symbols
[i
])
296 bitset_set (no_reduce_set
, s
->errs
->symbols
[i
]->number
);
301 if (reds
->lookahead_tokens
)
302 for (i
= 0; i
< ntokens
; i
++)
304 bool count
= bitset_test (no_reduce_set
, i
);
306 for (j
= 0; j
< reds
->num
; ++j
)
307 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
311 if (reds
->rules
[j
] != default_rule
)
322 /* Nothing to report. */
324 xml_puts (out
, level
, "<reductions/>");
328 xml_puts (out
, level
, "<reductions>");
330 /* Report lookahead tokens (or $default) and reductions. */
331 if (reds
->lookahead_tokens
)
332 for (i
= 0; i
< ntokens
; i
++)
334 bool defaulted
= false;
335 bool count
= bitset_test (no_reduce_set
, i
);
337 for (j
= 0; j
< reds
->num
; ++j
)
338 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
342 if (reds
->rules
[j
] != default_rule
)
343 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
344 reds
->rules
[j
], true);
352 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
355 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
356 reds
->rules
[j
], false);
362 print_reduction (out
, level
+ 1,
363 "$default", default_rule
, true);
365 xml_puts (out
, level
, "</reductions>");
369 /*--------------------------------------------------------------.
370 | Report on OUT all the actions (shifts, gotos, reductions, and |
371 | explicit erros from %nonassoc) of S. |
372 `--------------------------------------------------------------*/
375 print_actions (FILE *out
, int level
, state
*s
)
377 xml_puts (out
, level
, "<actions>");
378 print_transitions (s
, out
, level
+ 1);
379 print_errs (out
, level
+ 1, s
);
380 print_reductions (out
, level
+ 1, s
);
381 xml_puts (out
, level
, "</actions>");
385 /*----------------------------------.
386 | Report all the data on S on OUT. |
387 `----------------------------------*/
390 print_state (FILE *out
, int level
, state
*s
)
393 xml_printf (out
, level
, "<state number=\"%d\">", s
->number
);
394 print_core (out
, level
+ 1, s
);
395 print_actions (out
, level
+ 1, s
);
396 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts_xml
)
398 xml_puts (out
, level
+ 1, "<solved-conflicts>");
399 fputs (s
->solved_conflicts_xml
, out
);
400 xml_puts (out
, level
+ 1, "</solved-conflicts>");
403 xml_puts (out
, level
+ 1, "<solved-conflicts/>");
404 xml_puts (out
, level
, "</state>");
408 /*-----------------------------------------.
409 | Print information on the whole grammar. |
410 `-----------------------------------------*/
413 print_grammar (FILE *out
, int level
)
418 xml_puts (out
, level
, "<grammar>");
419 grammar_rules_print_xml (out
, level
);
422 xml_puts (out
, level
+ 1, "<terminals>");
423 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
424 if (token_translations
[i
] != undeftoken
->number
)
426 char const *tag
= symbols
[token_translations
[i
]]->tag
;
430 xml_printf (out
, level
+ 2,
431 "<terminal type=\"%d\" symbol=\"%s\">",
432 i
, xml_escape (tag
));
434 for (r
= 0; r
< nrules
; r
++)
435 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
436 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
438 xml_printf (out
, level
+ 3, "<rule>%d</rule>", r
);
441 xml_puts (out
, level
+ 2, "</terminal>");
443 xml_puts (out
, level
+ 1, "</terminals>");
446 xml_puts (out
, level
+ 1, "<nonterminals>");
447 for (i
= ntokens
; i
< nsyms
; i
++)
449 int left_count
= 0, right_count
= 0;
451 char const *tag
= symbols
[i
]->tag
;
453 for (r
= 0; r
< nrules
; r
++)
456 if (rules
[r
].lhs
->number
== i
)
458 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
459 if (item_number_as_symbol_number (*rhsp
) == i
)
466 xml_printf (out
, level
+ 2,
467 "<nonterminal type=\"%d\" symbol=\"%s\">",
468 i
, xml_escape (tag
));
472 xml_puts (out
, level
+ 3, "<left>");
473 for (r
= 0; r
< nrules
; r
++)
475 if (rules
[r
].lhs
->number
== i
)
476 xml_printf (out
, level
+ 4, "<rule>%d</rule>", r
);
478 xml_puts (out
, level
+ 3, "</left>");
483 xml_puts (out
, level
+ 3, "<right>");
484 for (r
= 0; r
< nrules
; r
++)
487 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
488 if (item_number_as_symbol_number (*rhsp
) == i
)
490 xml_printf (out
, level
+ 4, "<rule>%d</rule>", r
);
494 xml_puts (out
, level
+ 3, "</right>");
497 xml_puts (out
, level
+ 2, "</nonterminal>");
499 xml_puts (out
, level
+ 1, "</nonterminals>");
500 xml_puts (out
, level
, "</grammar>");
504 xml_puts (FILE *out
, int level
, char const *s
)
507 for (i
= 0; i
< level
; i
++)
514 xml_printf (FILE *out
, int level
, char const *fmt
, ...)
519 for (i
= 0; i
< level
; i
++)
522 va_start (arglist
, fmt
);
523 vfprintf (out
, fmt
, arglist
);
530 xml_escape_string (struct escape_buf
*buf
, char const *str
)
532 size_t len
= strlen (str
);
533 size_t max_expansion
= sizeof """ - 1;
536 if (buf
->size
<= max_expansion
* len
)
538 buf
->size
= max_expansion
* len
+ 1;
539 buf
->ptr
= x2realloc (buf
->ptr
, &buf
->size
);
546 default: *p
++ = *str
; break;
547 case '&': p
= stpcpy (p
, "&" ); break;
548 case '<': p
= stpcpy (p
, "<" ); break;
549 case '>': p
= stpcpy (p
, ">" ); break;
550 case '"': p
= stpcpy (p
, """); break;
558 xml_escape_n (int n
, char const *str
)
560 return xml_escape_string (escape_bufs
+ n
, str
);
564 xml_escape (char const *str
)
566 return xml_escape_n (0, str
);
575 FILE *out
= xfopen (spec_xml_file
, "w");
577 fputs ("<?xml version=\"1.0\"?>\n\n", out
);
578 xml_printf (out
, level
, "<bison-xml-report version=\"%s\">",
579 xml_escape (VERSION
));
582 xml_printf (out
, level
+ 1, "<filename>%s</filename>",
583 xml_escape (grammar_file
));
585 /* print reductions */
586 reduce_xml (out
, level
+ 1);
588 /* print rules never reduced */
589 print_rules_never_reduced (out
, level
+ 1);
592 print_grammar (out
, level
+ 1);
594 if (report_flag
& report_itemsets
)
595 new_closure (nritems
);
596 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
598 /* print automaton */
600 xml_puts (out
, level
+ 1, "<automaton>");
601 for (i
= 0; i
< nstates
; i
++)
602 print_state (out
, level
+ 2, states
[i
]);
603 xml_puts (out
, level
+ 1, "</automaton>");
605 bitset_free (no_reduce_set
);
606 if (report_flag
& report_itemsets
)
609 xml_puts (out
, 0, "</bison-xml-report>");
611 free (escape_bufs
[0].ptr
);
612 free (escape_bufs
[1].ptr
);