]>
git.saurik.com Git - bison.git/blob - src/print-xml.c
1 /* Print an xml on generated parser, for Bison,
3 Copyright (C) 2007, 2009 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 closure (sitems
, snritems
);
71 xml_puts (out
, level
, "<itemset/>");
75 xml_puts (out
, level
, "<itemset>");
77 for (i
= 0; i
< snritems
; i
++)
84 sp1
= sp
= ritem
+ sitems
[i
];
89 r
= item_number_as_rule_number (*sp
);
92 /* Display the lookahead tokens? */
93 if (item_number_is_rule_number (*sp1
))
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)
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
],
105 xml_puts (out
, level
+ 1, "</item>");
112 xml_printf (out
, level
+ 1,
113 "<item rule-number=\"%d\" point=\"%d\"/>",
118 xml_puts (out
, level
, "</itemset>");
122 /*-----------------------------------------------------------.
123 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
125 `-----------------------------------------------------------*/
128 print_transitions (state
*s
, FILE *out
, int level
)
130 transitions
*trans
= s
->transitions
;
134 for (i
= 0; i
< trans
->num
; i
++)
135 if (!TRANSITION_IS_DISABLED (trans
, i
))
140 /* Nothing to report. */
142 xml_puts (out
, level
, "<transitions/>");
146 /* Report lookahead tokens and shifts. */
147 xml_puts (out
, level
, "<transitions>");
149 for (i
= 0; i
< trans
->num
; i
++)
150 if (!TRANSITION_IS_DISABLED (trans
, i
)
151 && TRANSITION_IS_SHIFT (trans
, i
))
153 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
154 char const *tag
= sym
->tag
;
155 state
*s1
= trans
->states
[i
];
157 xml_printf (out
, level
+ 1,
158 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
159 xml_escape (tag
), s1
->number
);
162 for (i
= 0; i
< trans
->num
; i
++)
163 if (!TRANSITION_IS_DISABLED (trans
, i
)
164 && !TRANSITION_IS_SHIFT (trans
, i
))
166 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
167 char const *tag
= sym
->tag
;
168 state
*s1
= trans
->states
[i
];
170 xml_printf (out
, level
+ 1,
171 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
172 xml_escape (tag
), s1
->number
);
175 xml_puts (out
, level
, "</transitions>");
179 /*--------------------------------------------------------.
180 | Report the explicit errors of S raised from %nonassoc. |
181 `--------------------------------------------------------*/
184 print_errs (FILE *out
, int level
, state
*s
)
186 errs
*errp
= s
->errs
;
190 for (i
= 0; i
< errp
->num
; ++i
)
191 if (errp
->symbols
[i
])
194 /* Nothing to report. */
196 xml_puts (out
, level
, "<errors/>");
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
])
205 char const *tag
= errp
->symbols
[i
]->tag
;
206 xml_printf (out
, level
+ 1,
207 "<error symbol=\"%s\">nonassociative</error>",
210 xml_puts (out
, level
, "</errors>");
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 |
218 `-------------------------------------------------------------------------*/
221 print_reduction (FILE *out
, int level
, char const *lookahead_token
,
222 rule
*r
, bool enabled
)
225 xml_printf (out
, level
,
226 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
227 xml_escape (lookahead_token
),
229 enabled
? "true" : "false");
231 xml_printf (out
, level
,
232 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
233 xml_escape (lookahead_token
),
234 enabled
? "true" : "false");
238 /*-------------------------------------------.
239 | Report on OUT the reduction actions of S. |
240 `-------------------------------------------*/
243 print_reductions (FILE *out
, int level
, state
*s
)
245 transitions
*trans
= s
->transitions
;
246 reductions
*reds
= s
->reductions
;
247 rule
*default_reduction
= NULL
;
251 if (reds
->num
== 0) {
252 xml_puts (out
, level
, "<reductions/>");
256 if (yydefact
[s
->number
] != 0)
257 default_reduction
= &rules
[yydefact
[s
->number
] - 1];
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
);
266 if (default_reduction
)
269 if (reds
->lookahead_tokens
)
270 for (i
= 0; i
< ntokens
; i
++)
272 bool count
= bitset_test (no_reduce_set
, i
);
274 for (j
= 0; j
< reds
->num
; ++j
)
275 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
279 if (reds
->rules
[j
] != default_reduction
)
290 /* Nothing to report. */
292 xml_puts (out
, level
, "<reductions/>");
296 xml_puts (out
, level
, "<reductions>");
298 /* Report lookahead tokens (or $default) and reductions. */
299 if (reds
->lookahead_tokens
)
300 for (i
= 0; i
< ntokens
; i
++)
302 bool defaulted
= false;
303 bool count
= bitset_test (no_reduce_set
, i
);
305 for (j
= 0; j
< reds
->num
; ++j
)
306 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
310 if (reds
->rules
[j
] != default_reduction
)
311 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
312 reds
->rules
[j
], true);
320 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
321 default_reduction
, true);
323 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
324 reds
->rules
[j
], false);
329 if (default_reduction
)
330 print_reduction (out
, level
+ 1,
331 "$default", default_reduction
, true);
333 xml_puts (out
, level
, "</reductions>");
337 /*--------------------------------------------------------------.
338 | Report on OUT all the actions (shifts, gotos, reductions, and |
339 | explicit erros from %nonassoc) of S. |
340 `--------------------------------------------------------------*/
343 print_actions (FILE *out
, int level
, state
*s
)
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>");
353 /*----------------------------------.
354 | Report all the data on S on OUT. |
355 `----------------------------------*/
358 print_state (FILE *out
, int level
, state
*s
)
361 xml_printf (out
, level
, "<state number=\"%d\">", s
->number
);
362 print_core (out
, level
+ 1, s
);
363 print_actions (out
, level
+ 1, s
);
364 if (s
->solved_conflicts_xml
)
366 xml_puts (out
, level
+ 1, "<solved-conflicts>");
367 fputs (s
->solved_conflicts_xml
, out
);
368 xml_puts (out
, level
+ 1, "</solved-conflicts>");
371 xml_puts (out
, level
+ 1, "<solved-conflicts/>");
372 xml_puts (out
, level
, "</state>");
376 /*-----------------------------------------.
377 | Print information on the whole grammar. |
378 `-----------------------------------------*/
381 print_grammar (FILE *out
, int level
)
386 xml_puts (out
, level
, "<grammar>");
387 grammar_rules_print_xml (out
, level
);
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
)
394 char const *tag
= symbols
[token_translations
[i
]]->tag
;
395 int precedence
= symbols
[token_translations
[i
]]->prec
;
396 assoc associativity
= symbols
[token_translations
[i
]]->assoc
;
397 xml_indent (out
, level
+ 2);
399 "<terminal symbol-number=\"%d\" token-number=\"%d\""
400 " name=\"%s\" usefulness=\"%s\"",
401 token_translations
[i
], i
, xml_escape (tag
),
402 reduce_token_unused_in_grammar (token_translations
[i
])
403 ? "unused-in-grammar" : "useful");
405 fprintf (out
, " prec=\"%d\"", precedence
);
406 if (associativity
!= undef_assoc
)
407 fprintf (out
, " assoc=\"%s\"", assoc_to_string (associativity
) + 1);
410 xml_puts (out
, level
+ 1, "</terminals>");
413 xml_puts (out
, level
+ 1, "<nonterminals>");
414 for (i
= ntokens
; i
< nsyms
+ nuseless_nonterminals
; i
++)
416 char const *tag
= symbols
[i
]->tag
;
417 xml_printf (out
, level
+ 2,
418 "<nonterminal symbol-number=\"%d\" name=\"%s\""
419 " usefulness=\"%s\"/>",
421 reduce_nonterminal_useless_in_grammar (i
)
422 ? "useless-in-grammar" : "useful");
424 xml_puts (out
, level
+ 1, "</nonterminals>");
425 xml_puts (out
, level
, "</grammar>");
429 xml_indent (FILE *out
, int level
)
432 for (i
= 0; i
< level
; i
++)
437 xml_puts (FILE *out
, int level
, char const *s
)
439 xml_indent (out
, level
);
445 xml_printf (FILE *out
, int level
, char const *fmt
, ...)
449 xml_indent (out
, level
);
451 va_start (arglist
, fmt
);
452 vfprintf (out
, fmt
, arglist
);
459 xml_escape_string (struct escape_buf
*buf
, char const *str
)
461 size_t len
= strlen (str
);
462 size_t max_expansion
= sizeof """ - 1;
465 if (buf
->size
<= max_expansion
* len
)
467 buf
->size
= max_expansion
* len
+ 1;
468 buf
->ptr
= x2realloc (buf
->ptr
, &buf
->size
);
475 default: *p
++ = *str
; break;
476 case '&': p
= stpcpy (p
, "&" ); break;
477 case '<': p
= stpcpy (p
, "<" ); break;
478 case '>': p
= stpcpy (p
, ">" ); break;
479 case '"': p
= stpcpy (p
, """); break;
487 xml_escape_n (int n
, char const *str
)
489 return xml_escape_string (escape_bufs
+ n
, str
);
493 xml_escape (char const *str
)
495 return xml_escape_n (0, str
);
504 FILE *out
= xfopen (spec_xml_file
, "w");
506 fputs ("<?xml version=\"1.0\"?>\n\n", out
);
507 xml_printf (out
, level
, "<bison-xml-report version=\"%s\">",
508 xml_escape (VERSION
));
511 xml_printf (out
, level
+ 1, "<filename>%s</filename>",
512 xml_escape (grammar_file
));
515 print_grammar (out
, level
+ 1);
517 new_closure (nritems
);
518 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
520 /* print automaton */
522 xml_puts (out
, level
+ 1, "<automaton>");
523 for (i
= 0; i
< nstates
; i
++)
524 print_state (out
, level
+ 2, states
[i
]);
525 xml_puts (out
, level
+ 1, "</automaton>");
527 bitset_free (no_reduce_set
);
530 xml_puts (out
, 0, "</bison-xml-report>");
532 free (escape_bufs
[0].ptr
);
533 free (escape_bufs
[1].ptr
);