X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/ad5feac4e2cd16569d8586f2eac6a9efeeadc038..6c05543cb03b40b9728f7073c4073ce993d796c6:/src/print-xml.c diff --git a/src/print-xml.c b/src/print-xml.c index cde23665..ce71e158 100644 --- a/src/print-xml.c +++ b/src/print-xml.c @@ -1,23 +1,21 @@ /* Print an xml on generated parser, for Bison, - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2009-2012 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. - Bison is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - Bison is distributed in the hope that it will be useful, + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with Bison; see the file COPYING. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ #include #include "system.h" @@ -25,7 +23,6 @@ #include #include -#include #include "LR0.h" #include "closure.h" @@ -43,36 +40,13 @@ #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[3]; - if (count) { - xml_puts (out, level, ""); - grammar_rules_partial_print_xml (out, level - 1, - false, rule_never_reduced_p); - xml_puts (out, level, ""); - } - else - xml_puts (out, level, ""); -} /*--------------------------------. | Report information on a state. | @@ -86,12 +60,9 @@ print_core (FILE *out, int level, state *s) 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, ""); @@ -102,6 +73,7 @@ print_core (FILE *out, int level, state *s) for (i = 0; i < snritems; i++) { + bool printed = false; item_number *sp; item_number *sp1; rule_number r; @@ -109,33 +81,36 @@ print_core (FILE *out, int level, state *s) sp1 = sp = ritem + sitems[i]; while (*sp >= 0) - sp++; + sp++; r = item_number_as_rule_number (*sp); - - xml_printf (out, level + 1, "", - rules[r].number); - - rule_lhs_print_xml (&rules[r], out, level + 2); - - xml_puts (out, level + 2, ""); - for (sp = rules[r].rhs; sp < sp1; sp++) - xml_printf (out, level + 3, "%s", - symbol_class_get_string (symbols[*sp]), - xml_escape (symbols[*sp]->tag)); - xml_puts (out, level + 3, ""); - for (/* Nothing */; *sp >= 0; ++sp) - xml_printf (out, level + 3, "%s", - symbol_class_get_string (symbols[*sp]), - xml_escape (symbols[*sp]->tag)); - - xml_puts (out, level + 2, ""); + 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); - - xml_puts (out, level + 1, ""); + 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, + "", + rules[r].number, sp1 - sp); + state_rule_lookahead_tokens_print_xml (s, &rules[r], + out, level + 2); + xml_puts (out, level + 1, ""); + printed = true; + } + } + + if (!printed) + { + xml_printf (out, level + 1, + "", + rules[r].number, + sp1 - sp); + } } xml_puts (out, level, ""); } @@ -156,7 +131,7 @@ print_transitions (state *s, FILE *out, int level) for (i = 0; i < trans->num; i++) if (!TRANSITION_IS_DISABLED (trans, i)) { - n++; + n++; } /* Nothing to report. */ @@ -170,28 +145,28 @@ print_transitions (state *s, FILE *out, int level) for (i = 0; i < trans->num; i++) if (!TRANSITION_IS_DISABLED (trans, i) - && TRANSITION_IS_SHIFT (trans, i)) + && TRANSITION_IS_SHIFT (trans, i)) { - symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)]; - char const *tag = sym->tag; - state *s1 = trans->states[i]; + symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)]; + char const *tag = sym->tag; + state *s1 = trans->states[i]; - xml_printf (out, level + 1, - "", - xml_escape (tag), s1->number); + xml_printf (out, level + 1, + "", + xml_escape (tag), s1->number); } for (i = 0; i < trans->num; i++) if (!TRANSITION_IS_DISABLED (trans, i) - && !TRANSITION_IS_SHIFT (trans, i)) + && !TRANSITION_IS_SHIFT (trans, i)) { - symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)]; - char const *tag = sym->tag; - state *s1 = trans->states[i]; + symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)]; + char const *tag = sym->tag; + state *s1 = trans->states[i]; - xml_printf (out, level + 1, - "", - xml_escape (tag), s1->number); + xml_printf (out, level + 1, + "", + xml_escape (tag), s1->number); } xml_puts (out, level, ""); @@ -224,10 +199,10 @@ print_errs (FILE *out, int level, state *s) for (i = 0; i < errp->num; ++i) if (errp->symbols[i]) { - char const *tag = errp->symbols[i]->tag; - xml_printf (out, level + 1, - "nonassociative", - xml_escape (tag)); + char const *tag = errp->symbols[i]->tag; + xml_printf (out, level + 1, + "nonassociative", + xml_escape (tag)); } xml_puts (out, level, ""); } @@ -241,19 +216,19 @@ print_errs (FILE *out, int level, state *s) static void print_reduction (FILE *out, int level, char const *lookahead_token, - rule *r, bool enabled) + rule *r, bool enabled) { if (r->number) xml_printf (out, level, - "", - xml_escape (lookahead_token), - r->number, - enabled ? "true" : "false"); + "", + xml_escape (lookahead_token), + r->number, + enabled ? "true" : "false"); else xml_printf (out, level, - "", - xml_escape (lookahead_token), - enabled ? "true" : "false"); + "", + xml_escape (lookahead_token), + enabled ? "true" : "false"); } @@ -266,17 +241,18 @@ print_reductions (FILE *out, int level, state *s) { 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, ""); - return; - } + if (reds->num == 0) + { + xml_puts (out, level, ""); + 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) @@ -285,28 +261,28 @@ print_reductions (FILE *out, int level, state *s) 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) for (i = 0; i < ntokens; i++) { - bool count = bitset_test (no_reduce_set, i); - - for (j = 0; j < reds->num; ++j) - if (bitset_test (reds->lookahead_tokens[j], i)) - { - if (! count) - { - if (reds->rules[j] != default_rule) - report = true; - count = true; - } - else - { - report = true; - } - } + bool count = bitset_test (no_reduce_set, i); + + for (j = 0; j < reds->num; ++j) + if (bitset_test (reds->lookahead_tokens[j], i)) + { + if (! count) + { + if (reds->rules[j] != default_reduction) + report = true; + count = true; + } + else + { + report = true; + } + } } /* Nothing to report. */ @@ -321,36 +297,36 @@ print_reductions (FILE *out, int level, state *s) if (reds->lookahead_tokens) for (i = 0; i < ntokens; i++) { - bool defaulted = false; - bool count = bitset_test (no_reduce_set, i); - - for (j = 0; j < reds->num; ++j) - if (bitset_test (reds->lookahead_tokens[j], i)) - { - if (! count) - { - if (reds->rules[j] != default_rule) - print_reduction (out, level + 1, symbols[i]->tag, - reds->rules[j], true); - else - defaulted = true; - count = true; - } - else - { - if (defaulted) - print_reduction (out, level + 1, symbols[i]->tag, - default_rule, true); - defaulted = false; - print_reduction (out, level + 1, symbols[i]->tag, - reds->rules[j], false); - } - } + bool defaulted = false; + bool count = bitset_test (no_reduce_set, i); + + for (j = 0; j < reds->num; ++j) + if (bitset_test (reds->lookahead_tokens[j], i)) + { + if (! count) + { + if (reds->rules[j] != default_reduction) + print_reduction (out, level + 1, symbols[i]->tag, + reds->rules[j], true); + else + defaulted = true; + count = true; + } + else + { + if (defaulted) + print_reduction (out, level + 1, symbols[i]->tag, + 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, ""); } @@ -383,7 +359,7 @@ print_state (FILE *out, int level, state *s) xml_printf (out, level, "", 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, ""); fputs (s->solved_conflicts_xml, out); @@ -413,90 +389,52 @@ print_grammar (FILE *out, int level) for (i = 0; i < max_user_token_number + 1; i++) 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, - "", - 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, "%d", r); - break; - } - xml_puts (out, level + 2, ""); + char const *tag = symbols[token_translations[i]]->tag; + int precedence = symbols[token_translations[i]]->prec; + assoc associativity = symbols[token_translations[i]]->assoc; + xml_indent (out, level + 2); + fprintf (out, + "\n", out); } xml_puts (out, level + 1, ""); /* Nonterminals */ xml_puts (out, level + 1, ""); - 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, - "", - i, xml_escape (tag)); - - if (left_count > 0) - { - xml_puts (out, level + 3, ""); - for (r = 0; r < nrules; r++) - { - if (rules[r].lhs->number == i) - xml_printf (out, level + 4, "%d", r); - } - xml_puts (out, level + 3, ""); - } - - if (right_count > 0) - { - xml_puts (out, level + 3, ""); - 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, "%d", r); - break; - } - } - xml_puts (out, level + 3, ""); - } - - xml_puts (out, level + 2, ""); + "", + i, xml_escape (tag), + reduce_nonterminal_useless_in_grammar (i) + ? "useless-in-grammar" : "useful"); } xml_puts (out, level + 1, ""); xml_puts (out, level, ""); } 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); } @@ -504,11 +442,9 @@ xml_puts (FILE *out, int level, char const *s) 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); @@ -517,12 +453,6 @@ xml_printf (FILE *out, int level, char const *fmt, ...) fputc ('\n', out); } -struct escape_buf -{ - char *ptr; - size_t size; -}; - static char const * xml_escape_string (struct escape_buf *buf, char const *str) { @@ -554,8 +484,7 @@ 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 * @@ -573,27 +502,21 @@ print_xml (void) FILE *out = xfopen (spec_xml_file, "w"); fputs ("\n\n", out); - xml_printf (out, level, "", - xml_escape (VERSION)); + xml_printf (out, level, + "", + xml_escape_n (0, VERSION), + xml_escape_n (1, PACKAGE_BUGREPORT), + xml_escape_n (2, PACKAGE_URL)); fputc ('\n', out); xml_printf (out, level + 1, "%s", - 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); + xml_escape (grammar_file)); /* 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 */ @@ -604,10 +527,12 @@ print_xml (void) xml_puts (out, level + 1, ""); bitset_free (no_reduce_set); - if (report_flag & report_itemsets) - free_closure (); + free_closure (); xml_puts (out, 0, ""); + free (escape_bufs[0].ptr); + free (escape_bufs[1].ptr); + xfclose (out); }