]>
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-2013 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program 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 3 of the License, or
10 (at your option) any later version.
12 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "conflicts.h"
35 #include "print-xml.h"
42 static bitset no_reduce_set
;
48 static struct escape_buf escape_bufs
[3];
51 /*--------------------------------.
52 | Report information on a state. |
53 `--------------------------------*/
56 print_core (FILE *out
, int level
, state
*s
)
59 item_number
*sitems
= s
->items
;
60 size_t snritems
= s
->nitems
;
62 /* Output all the items of a state, not only its kernel. */
63 closure (sitems
, snritems
);
68 xml_puts (out
, level
, "<itemset/>");
72 xml_puts (out
, level
, "<itemset>");
74 for (i
= 0; i
< snritems
; i
++)
81 sp1
= sp
= ritem
+ sitems
[i
];
86 r
= item_number_as_rule_number (*sp
);
89 /* Display the lookahead tokens? */
90 if (item_number_is_rule_number (*sp1
))
92 reductions
*reds
= s
->reductions
;
93 int red
= state_reduction_find (s
, &rules
[r
]);
94 /* Print item with lookaheads if there are. */
95 if (reds
->lookahead_tokens
&& red
!= -1)
97 xml_printf (out
, level
+ 1,
98 "<item rule-number=\"%d\" point=\"%d\">",
99 rules
[r
].number
, sp1
- sp
);
100 state_rule_lookahead_tokens_print_xml (s
, &rules
[r
],
102 xml_puts (out
, level
+ 1, "</item>");
109 xml_printf (out
, level
+ 1,
110 "<item rule-number=\"%d\" point=\"%d\"/>",
115 xml_puts (out
, level
, "</itemset>");
119 /*-----------------------------------------------------------.
120 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
122 `-----------------------------------------------------------*/
125 print_transitions (state
*s
, FILE *out
, int level
)
127 transitions
*trans
= s
->transitions
;
131 for (i
= 0; i
< trans
->num
; i
++)
132 if (!TRANSITION_IS_DISABLED (trans
, i
))
137 /* Nothing to report. */
139 xml_puts (out
, level
, "<transitions/>");
143 /* Report lookahead tokens and shifts. */
144 xml_puts (out
, level
, "<transitions>");
146 for (i
= 0; i
< trans
->num
; i
++)
147 if (!TRANSITION_IS_DISABLED (trans
, i
)
148 && TRANSITION_IS_SHIFT (trans
, i
))
150 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
151 char const *tag
= sym
->tag
;
152 state
*s1
= trans
->states
[i
];
154 xml_printf (out
, level
+ 1,
155 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
156 xml_escape (tag
), s1
->number
);
159 for (i
= 0; i
< trans
->num
; i
++)
160 if (!TRANSITION_IS_DISABLED (trans
, i
)
161 && !TRANSITION_IS_SHIFT (trans
, i
))
163 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
164 char const *tag
= sym
->tag
;
165 state
*s1
= trans
->states
[i
];
167 xml_printf (out
, level
+ 1,
168 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
169 xml_escape (tag
), s1
->number
);
172 xml_puts (out
, level
, "</transitions>");
176 /*--------------------------------------------------------.
177 | Report the explicit errors of S raised from %nonassoc. |
178 `--------------------------------------------------------*/
181 print_errs (FILE *out
, int level
, state
*s
)
183 errs
*errp
= s
->errs
;
187 for (i
= 0; i
< errp
->num
; ++i
)
188 if (errp
->symbols
[i
])
191 /* Nothing to report. */
193 xml_puts (out
, level
, "<errors/>");
197 /* Report lookahead tokens and errors. */
198 xml_puts (out
, level
, "<errors>");
199 for (i
= 0; i
< errp
->num
; ++i
)
200 if (errp
->symbols
[i
])
202 char const *tag
= errp
->symbols
[i
]->tag
;
203 xml_printf (out
, level
+ 1,
204 "<error symbol=\"%s\">nonassociative</error>",
207 xml_puts (out
, level
, "</errors>");
211 /*-------------------------------------------------------------------------.
212 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
213 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
215 `-------------------------------------------------------------------------*/
218 print_reduction (FILE *out
, int level
, char const *lookahead_token
,
219 rule
*r
, bool enabled
)
222 xml_printf (out
, level
,
223 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
224 xml_escape (lookahead_token
),
226 enabled
? "true" : "false");
228 xml_printf (out
, level
,
229 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
230 xml_escape (lookahead_token
),
231 enabled
? "true" : "false");
235 /*-------------------------------------------.
236 | Report on OUT the reduction actions of S. |
237 `-------------------------------------------*/
240 print_reductions (FILE *out
, int level
, state
*s
)
242 transitions
*trans
= s
->transitions
;
243 reductions
*reds
= s
->reductions
;
244 rule
*default_reduction
= NULL
;
250 xml_puts (out
, level
, "<reductions/>");
254 if (yydefact
[s
->number
] != 0)
255 default_reduction
= &rules
[yydefact
[s
->number
] - 1];
257 bitset_zero (no_reduce_set
);
258 FOR_EACH_SHIFT (trans
, i
)
259 bitset_set (no_reduce_set
, TRANSITION_SYMBOL (trans
, i
));
260 for (i
= 0; i
< s
->errs
->num
; ++i
)
261 if (s
->errs
->symbols
[i
])
262 bitset_set (no_reduce_set
, s
->errs
->symbols
[i
]->number
);
264 if (default_reduction
)
267 if (reds
->lookahead_tokens
)
268 for (i
= 0; i
< ntokens
; i
++)
270 bool count
= bitset_test (no_reduce_set
, i
);
272 for (j
= 0; j
< reds
->num
; ++j
)
273 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
277 if (reds
->rules
[j
] != default_reduction
)
288 /* Nothing to report. */
290 xml_puts (out
, level
, "<reductions/>");
294 xml_puts (out
, level
, "<reductions>");
296 /* Report lookahead tokens (or $default) and reductions. */
297 if (reds
->lookahead_tokens
)
298 for (i
= 0; i
< ntokens
; i
++)
300 bool defaulted
= false;
301 bool count
= bitset_test (no_reduce_set
, i
);
303 for (j
= 0; j
< reds
->num
; ++j
)
304 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
308 if (reds
->rules
[j
] != default_reduction
)
309 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
310 reds
->rules
[j
], true);
318 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
319 default_reduction
, true);
321 print_reduction (out
, level
+ 1, symbols
[i
]->tag
,
322 reds
->rules
[j
], false);
327 if (default_reduction
)
328 print_reduction (out
, level
+ 1,
329 "$default", default_reduction
, true);
331 xml_puts (out
, level
, "</reductions>");
335 /*--------------------------------------------------------------.
336 | Report on OUT all the actions (shifts, gotos, reductions, and |
337 | explicit erros from %nonassoc) of S. |
338 `--------------------------------------------------------------*/
341 print_actions (FILE *out
, int level
, state
*s
)
343 xml_puts (out
, level
, "<actions>");
344 print_transitions (s
, out
, level
+ 1);
345 print_errs (out
, level
+ 1, s
);
346 print_reductions (out
, level
+ 1, s
);
347 xml_puts (out
, level
, "</actions>");
351 /*----------------------------------.
352 | Report all the data on S on OUT. |
353 `----------------------------------*/
356 print_state (FILE *out
, int level
, state
*s
)
359 xml_printf (out
, level
, "<state number=\"%d\">", s
->number
);
360 print_core (out
, level
+ 1, s
);
361 print_actions (out
, level
+ 1, s
);
362 if (s
->solved_conflicts_xml
)
364 xml_puts (out
, level
+ 1, "<solved-conflicts>");
365 fputs (s
->solved_conflicts_xml
, out
);
366 xml_puts (out
, level
+ 1, "</solved-conflicts>");
369 xml_puts (out
, level
+ 1, "<solved-conflicts/>");
370 xml_puts (out
, level
, "</state>");
374 /*-----------------------------------------.
375 | Print information on the whole grammar. |
376 `-----------------------------------------*/
379 print_grammar (FILE *out
, int level
)
384 xml_puts (out
, level
, "<grammar>");
385 grammar_rules_print_xml (out
, level
);
388 xml_puts (out
, level
+ 1, "<terminals>");
389 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
390 if (token_translations
[i
] != undeftoken
->number
)
392 char const *tag
= symbols
[token_translations
[i
]]->tag
;
393 int precedence
= symbols
[token_translations
[i
]]->prec
;
394 assoc associativity
= symbols
[token_translations
[i
]]->assoc
;
395 xml_indent (out
, level
+ 2);
397 "<terminal symbol-number=\"%d\" token-number=\"%d\""
398 " name=\"%s\" usefulness=\"%s\"",
399 token_translations
[i
], i
, xml_escape (tag
),
400 reduce_token_unused_in_grammar (token_translations
[i
])
401 ? "unused-in-grammar" : "useful");
403 fprintf (out
, " prec=\"%d\"", precedence
);
404 if (associativity
!= undef_assoc
)
405 fprintf (out
, " assoc=\"%s\"", assoc_to_string (associativity
) + 1);
408 xml_puts (out
, level
+ 1, "</terminals>");
411 xml_puts (out
, level
+ 1, "<nonterminals>");
412 for (i
= ntokens
; i
< nsyms
+ nuseless_nonterminals
; i
++)
414 char const *tag
= symbols
[i
]->tag
;
415 xml_printf (out
, level
+ 2,
416 "<nonterminal symbol-number=\"%d\" name=\"%s\""
417 " usefulness=\"%s\"/>",
419 reduce_nonterminal_useless_in_grammar (i
)
420 ? "useless-in-grammar" : "useful");
422 xml_puts (out
, level
+ 1, "</nonterminals>");
423 xml_puts (out
, level
, "</grammar>");
427 xml_indent (FILE *out
, int level
)
430 for (i
= 0; i
< level
; i
++)
435 xml_puts (FILE *out
, int level
, char const *s
)
437 xml_indent (out
, level
);
443 xml_printf (FILE *out
, int level
, char const *fmt
, ...)
447 xml_indent (out
, level
);
449 va_start (arglist
, fmt
);
450 vfprintf (out
, fmt
, arglist
);
457 xml_escape_string (struct escape_buf
*buf
, char const *str
)
459 size_t len
= strlen (str
);
460 size_t max_expansion
= sizeof """ - 1;
463 if (buf
->size
<= max_expansion
* len
)
465 buf
->size
= max_expansion
* len
+ 1;
466 buf
->ptr
= x2realloc (buf
->ptr
, &buf
->size
);
473 default: *p
++ = *str
; break;
474 case '&': p
= stpcpy (p
, "&" ); break;
475 case '<': p
= stpcpy (p
, "<" ); break;
476 case '>': p
= stpcpy (p
, ">" ); break;
477 case '"': p
= stpcpy (p
, """); break;
485 xml_escape_n (int n
, char const *str
)
487 return xml_escape_string (escape_bufs
+ n
, str
);
491 xml_escape (char const *str
)
493 return xml_escape_n (0, str
);
502 FILE *out
= xfopen (spec_xml_file
, "w");
504 fputs ("<?xml version=\"1.0\"?>\n\n", out
);
505 xml_printf (out
, level
,
506 "<bison-xml-report version=\"%s\" bug-report=\"%s\""
508 xml_escape_n (0, VERSION
),
509 xml_escape_n (1, PACKAGE_BUGREPORT
),
510 xml_escape_n (2, PACKAGE_URL
));
513 xml_printf (out
, level
+ 1, "<filename>%s</filename>",
514 xml_escape (grammar_file
));
517 print_grammar (out
, level
+ 1);
519 new_closure (nritems
);
520 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
522 /* print automaton */
524 xml_puts (out
, level
+ 1, "<automaton>");
525 for (i
= 0; i
< nstates
; i
++)
526 print_state (out
, level
+ 2, states
[i
]);
527 xml_puts (out
, level
+ 1, "</automaton>");
529 bitset_free (no_reduce_set
);
532 xml_puts (out
, 0, "</bison-xml-report>");
534 free (escape_bufs
[0].ptr
);
535 free (escape_bufs
[1].ptr
);