/* Print an xml on generated parser, for Bison,
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007, 2009 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
#include "tables.h"
static bitset no_reduce_set;
-
-
-/*----------------------------.
-| Print rules never reduced. |
-`-----------------------------*/
-
-static void
-print_rules_never_reduced (FILE *out, int level)
+struct escape_buf
{
- rule_number r;
- bool count = false;
-
- for (r = 0; r < nrules + nuseless_productions; r++)
- {
- if (rule_never_reduced_p (&rules[r]))
- {
- count = true;
- break;
- }
- }
+ char *ptr;
+ size_t size;
+};
+static struct escape_buf escape_bufs[2];
- if (count) {
- xml_puts (out, level, "<rules-never-reduced>");
- grammar_rules_partial_print_xml (out, level - 1,
- false, rule_never_reduced_p);
- xml_puts (out, level, "</rules-never-reduced>");
- }
- else
- xml_puts (out, level, "<rules-never-reduced/>");
-}
/*--------------------------------.
| Report information on a state. |
size_t snritems = s->nitems;
/* Output all the items of a state, not only its kernel. */
- if (report_flag & report_itemsets)
- {
- closure (sitems, snritems);
- sitems = itemset;
- snritems = nitemset;
- }
+ closure (sitems, snritems);
+ sitems = itemset;
+ snritems = nitemset;
if (!snritems) {
xml_puts (out, level, "<itemset/>");
for (i = 0; i < snritems; i++)
{
+ bool printed = false;
item_number *sp;
item_number *sp1;
rule_number r;
sp++;
r = item_number_as_rule_number (*sp);
-
- xml_printf (out, level + 1, "<rule number=\"%d\">",
- rules[r].number);
-
- rule_lhs_print_xml (&rules[r], out, level + 2);
-
- xml_puts (out, level + 2, "<rhs>");
- for (sp = rules[r].rhs; sp < sp1; sp++)
- xml_printf (out, level + 3, "<symbol class=\"%s\">%s</symbol>",
- symbol_class_get_string (symbols[*sp]),
- xml_escape (symbols[*sp]->tag));
- xml_puts (out, level + 3, "<point/>");
- for (/* Nothing */; *sp >= 0; ++sp)
- xml_printf (out, level + 3, "<symbol class=\"%s\">%s</symbol>",
- symbol_class_get_string (symbols[*sp]),
- xml_escape (symbols[*sp]->tag));
-
- xml_puts (out, level + 2, "</rhs>");
+ sp = rules[r].rhs;
/* Display the lookahead tokens? */
- if (report_flag & report_lookahead_tokens)
- state_rule_lookahead_tokens_print_xml (s, &rules[r], out, level + 2);
+ if (item_number_is_rule_number (*sp1))
+ {
+ reductions *reds = s->reductions;
+ int red = state_reduction_find (s, &rules[r]);
+ /* Print item with lookaheads if there are. */
+ if (reds->lookahead_tokens && red != -1)
+ {
+ xml_printf (out, level + 1,
+ "<item rule-number=\"%d\" point=\"%d\">",
+ rules[r].number, sp1 - sp);
+ state_rule_lookahead_tokens_print_xml (s, &rules[r],
+ out, level + 2);
+ xml_puts (out, level + 1, "</item>");
+ printed = true;
+ }
+ }
- xml_puts (out, level + 1, "</rule>");
+ if (!printed)
+ {
+ xml_printf (out, level + 1,
+ "<item rule-number=\"%d\" point=\"%d\"/>",
+ rules[r].number,
+ sp1 - sp);
+ }
}
xml_puts (out, level, "</itemset>");
}
{
transitions *trans = s->transitions;
reductions *reds = s->reductions;
- rule *default_rule = NULL;
+ rule *default_reduction = NULL;
int report = false;
int i, j;
- if (reds->num == 0) {
- xml_puts (out, level, "<reductions/>");
- return;
- }
+ if (reds->num == 0)
+ {
+ xml_puts (out, level, "<reductions/>");
+ return;
+ }
if (yydefact[s->number] != 0)
- default_rule = &rules[yydefact[s->number] - 1];
+ default_reduction = &rules[yydefact[s->number] - 1];
bitset_zero (no_reduce_set);
FOR_EACH_SHIFT (trans, i)
if (s->errs->symbols[i])
bitset_set (no_reduce_set, s->errs->symbols[i]->number);
- if (default_rule)
+ if (default_reduction)
report = true;
if (reds->lookahead_tokens)
{
if (! count)
{
- if (reds->rules[j] != default_rule)
+ if (reds->rules[j] != default_reduction)
report = true;
count = true;
}
{
if (! count)
{
- if (reds->rules[j] != default_rule)
+ if (reds->rules[j] != default_reduction)
print_reduction (out, level + 1, symbols[i]->tag,
reds->rules[j], true);
else
{
if (defaulted)
print_reduction (out, level + 1, symbols[i]->tag,
- default_rule, true);
+ default_reduction, true);
defaulted = false;
print_reduction (out, level + 1, symbols[i]->tag,
reds->rules[j], false);
}
}
- if (default_rule)
+ if (default_reduction)
print_reduction (out, level + 1,
- "$default", default_rule, true);
+ "$default", default_reduction, true);
xml_puts (out, level, "</reductions>");
}
xml_printf (out, level, "<state number=\"%d\">", s->number);
print_core (out, level + 1, s);
print_actions (out, level + 1, s);
- if ((report_flag & report_solved_conflicts) && s->solved_conflicts_xml)
+ if (s->solved_conflicts_xml)
{
xml_puts (out, level + 1, "<solved-conflicts>");
fputs (s->solved_conflicts_xml, out);
if (token_translations[i] != undeftoken->number)
{
char const *tag = symbols[token_translations[i]]->tag;
- rule_number r;
- item_number *rhsp;
-
- xml_printf (out, level + 2,
- "<terminal type=\"%d\" symbol=\"%s\">",
- i, xml_escape (tag));
-
- for (r = 0; r < nrules; r++)
- for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
- if (item_number_as_symbol_number (*rhsp) == token_translations[i])
- {
- xml_printf (out, level + 3, "<rule>%d</rule>", r);
- break;
- }
- xml_puts (out, level + 2, "</terminal>");
+ int precedence = symbols[token_translations[i]]->prec;
+ assoc associativity = symbols[token_translations[i]]->assoc;
+ xml_indent (out, level + 2);
+ fprintf (out,
+ "<terminal symbol-number=\"%d\" token-number=\"%d\""
+ " name=\"%s\" usefulness=\"%s\"",
+ token_translations[i], i, xml_escape (tag),
+ reduce_token_unused_in_grammar (token_translations[i])
+ ? "unused-in-grammar" : "useful");
+ if (precedence)
+ fprintf (out, " prec=\"%d\"", precedence);
+ if (associativity != undef_assoc)
+ fprintf (out, " assoc=\"%s\"", assoc_to_string (associativity) + 1);
+ fputs ("/>\n", out);
}
xml_puts (out, level + 1, "</terminals>");
/* Nonterminals */
xml_puts (out, level + 1, "<nonterminals>");
- for (i = ntokens; i < nsyms; i++)
+ for (i = ntokens; i < nsyms + nuseless_nonterminals; i++)
{
- int left_count = 0, right_count = 0;
- rule_number r;
char const *tag = symbols[i]->tag;
-
- for (r = 0; r < nrules; r++)
- {
- item_number *rhsp;
- if (rules[r].lhs->number == i)
- left_count++;
- for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
- if (item_number_as_symbol_number (*rhsp) == i)
- {
- right_count++;
- break;
- }
- }
-
xml_printf (out, level + 2,
- "<nonterminal type=\"%d\" symbol=\"%s\">",
- i, xml_escape (tag));
-
- if (left_count > 0)
- {
- xml_puts (out, level + 3, "<left>");
- for (r = 0; r < nrules; r++)
- {
- if (rules[r].lhs->number == i)
- xml_printf (out, level + 4, "<rule>%d</rule>", r);
- }
- xml_puts (out, level + 3, "</left>");
- }
-
- if (right_count > 0)
- {
- xml_puts (out, level + 3, "<right>");
- for (r = 0; r < nrules; r++)
- {
- item_number *rhsp;
- for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
- if (item_number_as_symbol_number (*rhsp) == i)
- {
- xml_printf (out, level + 4, "<rule>%d</rule>", r);
- break;
- }
- }
- xml_puts (out, level + 3, "</right>");
- }
-
- xml_puts (out, level + 2, "</nonterminal>");
+ "<nonterminal symbol-number=\"%d\" name=\"%s\""
+ " usefulness=\"%s\"/>",
+ i, xml_escape (tag),
+ reduce_nonterminal_useless_in_grammar (i)
+ ? "useless-in-grammar" : "useful");
}
xml_puts (out, level + 1, "</nonterminals>");
xml_puts (out, level, "</grammar>");
}
void
-xml_puts (FILE *out, int level, char const *s)
+xml_indent (FILE *out, int level)
{
int i;
- level *= 2;
for (i = 0; i < level; i++)
- fputc (' ', out);
+ fputs (" ", out);
+}
+
+void
+xml_puts (FILE *out, int level, char const *s)
+{
+ xml_indent (out, level);
fputs (s, out);
fputc ('\n', out);
}
void
xml_printf (FILE *out, int level, char const *fmt, ...)
{
- int i;
va_list arglist;
- for (i = 0; i < level; i++)
- fputc (' ', out);
+ xml_indent (out, level);
va_start (arglist, fmt);
vfprintf (out, fmt, arglist);
fputc ('\n', out);
}
-struct escape_buf
-{
- char *ptr;
- size_t size;
-};
-
static char const *
xml_escape_string (struct escape_buf *buf, char const *str)
{
char const *
xml_escape_n (int n, char const *str)
{
- static struct escape_buf buf[2];
- return xml_escape_string (buf + n, str);
+ return xml_escape_string (escape_bufs + n, str);
}
char const *
xml_printf (out, level + 1, "<filename>%s</filename>",
xml_escape (grammar_file));
- /* print reductions */
- reduce_xml (out, level + 1);
-
- /* print rules never reduced */
- print_rules_never_reduced (out, level + 1);
-
- /* print conflicts */
- conflicts_output_xml (out, level + 1);
-
/* print grammar */
print_grammar (out, level + 1);
- if (report_flag & report_itemsets)
- new_closure (nritems);
+ new_closure (nritems);
no_reduce_set = bitset_create (ntokens, BITSET_FIXED);
/* print automaton */
xml_puts (out, level + 1, "</automaton>");
bitset_free (no_reduce_set);
- if (report_flag & report_itemsets)
- free_closure ();
+ free_closure ();
xml_puts (out, 0, "</bison-xml-report>");
+ free (escape_bufs[0].ptr);
+ free (escape_bufs[1].ptr);
+
xfclose (out);
}