From: Joel E. Denny Date: Sat, 24 Nov 2007 19:41:25 +0000 (+0000) Subject: In the XML output, list useless and unused symbols and rules with the X-Git-Tag: v2.3b~56 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/d80fb37a26b24d8aa7a4428389a6004c7f2e93be In the XML output, list useless and unused symbols and rules with the useful ones and add a "usefulness" attribute. Discussed starting at . * src/gram.c (grammar_rules_partial_print_xml): Remove. (grammar_rules_print_xml): Print all rules instead of just those useful in the grammar, and add a "usefulness" attribute. * src/gram.h (grammar_rules_partial_print_xml): Remove prototype. * src/print-xml.c (print_rules_useless_in_parser): Remove. (print_grammar): Print all nonterminals instead of just useful ones, and add a "usefulness" attribute to nonterminals and terminals. (print_xml): Don't print a separate "reductions" or "rules-useless-in-parser" element. * src/reduce.c (reduce_output): Use reduce_token_unused_in_grammar. (reduce_xml): Remove. (reduce_token_unused_in_grammar): New. (reduce_nonterminal_useless_in_grammar): New. * src/reduce.h (reduce_xml): Remove prototype. (reduce_token_unused_in_grammar): Add prototype. (reduce_nonterminal_useless_in_grammar): Add prototype. * data/xslt/xml2text.xsl: Update for XML changes. * data/xslt/xml2xhtml.xsl: Update for XML changes. * tests/reduce.at (Useless Terminals): Update output. (Useless Rules): Update output. (Reduced Automaton): Update output. Say "Terminals unused in grammar" instead of "Unused terminals". * NEWS (2.3a+): Update. * doc/bison.texinfo (Understanding): Update example output. * src/reduce.c (reduce_output): Implement. * data/xslt/xml2text.xsl: Implement. * data/xslt/xml2xhtml.xsl: Implement. --- diff --git a/ChangeLog b/ChangeLog index 741f68ed..0b81e8c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,37 @@ +2007-11-24 Joel E. Denny + + In the XML output, list useless and unused symbols and rules with the + useful ones and add a "usefulness" attribute. Discussed starting at + . + * src/gram.c (grammar_rules_partial_print_xml): Remove. + (grammar_rules_print_xml): Print all rules instead of just those + useful in the grammar, and add a "usefulness" attribute. + * src/gram.h (grammar_rules_partial_print_xml): Remove prototype. + * src/print-xml.c (print_rules_useless_in_parser): Remove. + (print_grammar): Print all nonterminals instead of just useful ones, + and add a "usefulness" attribute to nonterminals and terminals. + (print_xml): Don't print a separate "reductions" or + "rules-useless-in-parser" element. + * src/reduce.c (reduce_output): Use reduce_token_unused_in_grammar. + (reduce_xml): Remove. + (reduce_token_unused_in_grammar): New. + (reduce_nonterminal_useless_in_grammar): New. + * src/reduce.h (reduce_xml): Remove prototype. + (reduce_token_unused_in_grammar): Add prototype. + (reduce_nonterminal_useless_in_grammar): Add prototype. + * data/xslt/xml2text.xsl: Update for XML changes. + * data/xslt/xml2xhtml.xsl: Update for XML changes. + * tests/reduce.at (Useless Terminals): Update output. + (Useless Rules): Update output. + (Reduced Automaton): Update output. + + Say "Terminals unused in grammar" instead of "Unused terminals". + * NEWS (2.3a+): Update. + * doc/bison.texinfo (Understanding): Update example output. + * src/reduce.c (reduce_output): Implement. + * data/xslt/xml2text.xsl: Implement. + * data/xslt/xml2xhtml.xsl: Implement. + 2007-11-18 Joel E. Denny Accept --report-file=FILE to override the default `.output' filename. diff --git a/NEWS b/NEWS index afc35421..2a3e57c2 100644 --- a/NEWS +++ b/NEWS @@ -41,9 +41,10 @@ Changes in version 2.3a+ (????-??-??): %defines "parser.h" -* When reporting useless rules and nonterminals, Bison now employs the term - "useless in grammar" instead of "useless" and employs the term "useless in - parser" instead of "never reduced". +* When reporting useless rules, useless nonterminals, and unused terminals, + Bison now employs the terms "useless in grammar" instead of "useless", + "useless in parser" instead of "never reduced", and "unused in grammar" + instead of "unused". * Unreachable State Removal diff --git a/data/xslt/xml2text.xsl b/data/xslt/xml2text.xsl index b2c73eac..c2568a44 100644 --- a/data/xslt/xml2text.xsl +++ b/data/xslt/xml2text.xsl @@ -36,73 +36,95 @@ - - + + - - - Rules useless in parser due to conflicts - - - - - + + + + - - - - + + + Nonterminals useless in grammar + + + + + + + - - - Nonterminals useless in grammar - + + + Terminals unused in grammar + + - + - - + + + Rules useless in grammar - - - + + + - - - Terminals which are not used - - - - - + + + + Rules useless in parser due to conflicts + + + Grammar - - - + + + + + + + + + + + + + + + + + + Terminals, with rules where they appear @@ -111,7 +133,7 @@ Nonterminals, with rules where they appear - + @@ -293,8 +315,7 @@ - + @@ -307,8 +328,7 @@ - + diff --git a/data/xslt/xml2xhtml.xsl b/data/xslt/xml2xhtml.xsl index 62776637..15155e9d 100644 --- a/data/xslt/xml2xhtml.xsl +++ b/data/xslt/xml2xhtml.xsl @@ -109,9 +109,9 @@ Reductions @@ -127,50 +127,34 @@
  • Automaton
  • - - + +
    - - -

    - - Rules useless in parser due to conflicts -

    - -

    - - - -

    - -
    -
    - - +

    Reductions

    - - - + + +
    - +

    Nonterminals useless in grammar

    - +

    - + - + @@ -178,40 +162,94 @@ - +

    - - Rules useless in grammar + + Terminals unused in grammar

    - - + +

    - - - + + + + + +

    - +

    - - Terminals which are not used + + Rules useless in grammar

    - - + + +

    - - - - - + + +

    + + + +

    + + Rules useless in parser due to conflicts +

    + +

    + + + +

    + +
    +
    + + +

    + + Grammar +

    + +

    + + + +

    + + + +
    + + + + + + + + + + + + + + + +

    @@ -260,22 +298,6 @@ - -

    - - Grammar -

    - -

    - - - -

    - - - - -

    @@ -295,7 +317,9 @@

    - +

    @@ -446,8 +470,7 @@ - + @@ -483,8 +506,7 @@ - + diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 9d0da773..210bbad4 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -7197,14 +7197,14 @@ State 11 conflicts: 4 shift/reduce The next section reports useless tokens, nonterminal and rules. Useless nonterminals and rules are removed in order to produce a smaller parser, but useless tokens are preserved, since they might be used by the -scanner (note the difference between ``useless'' and ``not used'' +scanner (note the difference between ``useless'' and ``unused'' below): @example -Useless nonterminals: +Nonterminals useless in grammar: useless -Terminals which are not used: +Terminals unused in grammar: STR Rules useless in grammar: diff --git a/src/gram.c b/src/gram.c index fa3fdbf5..1f2885f0 100644 --- a/src/gram.c +++ b/src/gram.c @@ -197,45 +197,41 @@ grammar_rules_partial_print (FILE *out, const char *title, } void -grammar_rules_partial_print_xml (FILE *out, int level, bool rtag, - rule_filter filter) +grammar_rules_print (FILE *out) +{ + grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p); +} + +void +grammar_rules_print_xml (FILE *out, int level) { rule_number r; bool first = true; for (r = 0; r < nrules + nuseless_productions; r++) { - if (filter && !filter (&rules[r])) - continue; - if (rtag && first) + if (first) xml_puts (out, level + 1, ""); first = false; - - xml_printf (out, level + 2, "", - rules[r].number); + { + char const *usefulness; + if (rule_useless_in_grammar_p (&rules[r])) + usefulness = "useless-in-grammar"; + else if (rule_useless_in_parser_p (&rules[r])) + usefulness = "useless-in-parser"; + else + usefulness = "useful"; + xml_printf (out, level + 2, "", + rules[r].number, usefulness); + } rule_lhs_print_xml (&rules[r], out, level + 3); rule_rhs_print_xml (&rules[r], out, level + 3); xml_puts (out, level + 2, ""); } - if (rtag) - { - if (!first) - xml_puts (out, level + 1, ""); - else - xml_puts (out, level + 1, ""); - } -} - -void -grammar_rules_print (FILE *out) -{ - grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p); -} - -void -grammar_rules_print_xml (FILE *out, int level) -{ - grammar_rules_partial_print_xml (out, level, true, rule_useful_in_grammar_p); + if (!first) + xml_puts (out, level + 1, ""); + else + xml_puts (out, level + 1, ""); } void diff --git a/src/gram.h b/src/gram.h index 57097379..8e997ec5 100644 --- a/src/gram.h +++ b/src/gram.h @@ -254,11 +254,10 @@ size_t ritem_longest_rhs (void); /* Print the grammar's rules that match FILTER on OUT under TITLE. */ void grammar_rules_partial_print (FILE *out, const char *title, rule_filter filter); -void grammar_rules_partial_print_xml (FILE *out, int level, bool rtag, - rule_filter filter); /* Print the grammar's useful rules on OUT. */ void grammar_rules_print (FILE *out); +/* Print all of the grammar's rules with a "usefulness" attribute. */ void grammar_rules_print_xml (FILE *out, int level); /* Dump the grammar. */ diff --git a/src/print-xml.c b/src/print-xml.c index f7cae3f0..ff2bacd9 100644 --- a/src/print-xml.c +++ b/src/print-xml.c @@ -51,35 +51,6 @@ struct escape_buf static struct escape_buf escape_bufs[2]; -/*--------------------------------. -| Print rules useless in parser. | -`--------------------------------*/ - -static void -print_rules_useless_in_parser (FILE *out, int level) -{ - rule_number r; - bool count = false; - - for (r = 0; r < nrules + nuseless_productions; r++) - { - if (rule_useless_in_parser_p (&rules[r])) - { - count = true; - break; - } - } - - if (count) { - xml_puts (out, level, ""); - grammar_rules_partial_print_xml (out, level - 1, - false, rule_useless_in_parser_p); - xml_puts (out, level, ""); - } - else - xml_puts (out, level, ""); -} - /*--------------------------------. | Report information on a state. | `--------------------------------*/ @@ -430,8 +401,10 @@ print_grammar (FILE *out, int level) xml_printf (out, level + 2, "", - token_translations[i], i, xml_escape (tag)); + " name=\"%s\" usefulness=\"%s\">", + token_translations[i], i, xml_escape (tag), + reduce_token_unused_in_grammar (token_translations[i]) + ? "unused-in-grammar" : "useful"); for (r = 0; r < nrules; r++) for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++) @@ -446,7 +419,7 @@ print_grammar (FILE *out, int level) /* 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; @@ -466,8 +439,11 @@ print_grammar (FILE *out, int level) } xml_printf (out, level + 2, - "", - i, xml_escape (tag)); + "", + i, xml_escape (tag), + reduce_nonterminal_useless_in_grammar (i) + ? "useless-in-grammar" : "useful"); if (left_count > 0) { @@ -584,12 +560,6 @@ print_xml (void) xml_printf (out, level + 1, "%s", xml_escape (grammar_file)); - /* print reductions */ - reduce_xml (out, level + 1); - - /* print rules useless in parser */ - print_rules_useless_in_parser (out, level + 1); - /* print grammar */ print_grammar (out, level + 1); diff --git a/src/reduce.c b/src/reduce.c index 2d01255c..1306374a 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -358,10 +358,10 @@ reduce_output (FILE *out) bool b = false; int i; for (i = 0; i < ntokens; i++) - if (!bitset_test (V, i) && !bitset_test (V1, i)) + if (reduce_token_unused_in_grammar (i)) { if (!b) - fprintf (out, "%s\n\n", _("Terminals which are not used")); + fprintf (out, "%s\n\n", _("Terminals unused in grammar")); b = true; fprintf (out, " %s\n", symbols[i]->tag); } @@ -375,64 +375,6 @@ reduce_output (FILE *out) } -/*--------------------------------------------------------------. -| Output the detailed results of the reductions. For FILE.xml. | -`---------------------------------------------------------------*/ - -void -reduce_xml (FILE *out, int level) -{ - fputc ('\n', out); - xml_puts (out, level, ""); - xml_puts (out, level + 1, ""); - - if (nuseless_nonterminals > 0) - { - int i; - xml_puts (out, level + 2, ""); - for (i = 0; i < nuseless_nonterminals; ++i) - xml_printf (out, level + 3, - "%s", - symbols[nsyms + i]->tag); - xml_puts (out, level + 2, ""); - } - else - xml_puts (out, level + 2, ""); - - if (nuseless_productions > 0) - grammar_rules_partial_print_xml (out, level + 1, true, - rule_useless_in_grammar_p); - else - xml_puts (out, level + 2, ""); - - xml_puts (out, level + 1, ""); - xml_puts (out, level + 1, ""); - - { - bool b = false; - int i; - for (i = 0; i < ntokens; i++) - if (!bitset_test (V, i) && !bitset_test (V1, i)) - { - if (!b) - xml_puts (out, level + 2, ""); - b = true; - xml_printf (out, level + 3, - "%s", - xml_escape (symbols[i]->tag)); - } - if (b) - xml_puts (out, level + 2, ""); - else - xml_puts (out, level + 2, ""); - } - - xml_puts (out, level + 1, ""); - xml_puts (out, level, ""); - fputc ('\n', out); -} - - /*-------------------------------. | Report the results to STDERR. | `-------------------------------*/ @@ -506,6 +448,19 @@ reduce_grammar (void) } } +bool +reduce_token_unused_in_grammar (symbol_number i) +{ + aver (i < ntokens); + return !bitset_test (V, i) && !bitset_test (V1, i); +} + +bool +reduce_nonterminal_useless_in_grammar (symbol_number i) +{ + aver (ntokens <= i && i < nsyms + nuseless_nonterminals); + return nsyms <= i; +} /*-----------------------------------------------------------. | Free the global sets used to compute the reduced grammar. | diff --git a/src/reduce.h b/src/reduce.h index aaa2bab1..3d039ef5 100644 --- a/src/reduce.h +++ b/src/reduce.h @@ -22,7 +22,8 @@ void reduce_grammar (void); void reduce_output (FILE *out); -void reduce_xml (FILE *out, int level); +bool reduce_token_unused_in_grammar (symbol_number i); +bool reduce_nonterminal_useless_in_grammar (symbol_number i); void reduce_free (void); extern symbol_number nuseless_nonterminals; diff --git a/tests/reduce.at b/tests/reduce.at index 9f331b0b..9e5246af 100644 --- a/tests/reduce.at +++ b/tests/reduce.at @@ -45,7 +45,7 @@ exp: useful; AT_CHECK([[bison input.y]]) AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, -[[Terminals which are not used +[[Terminals unused in grammar useless1 useless2 useless3 @@ -174,7 +174,7 @@ AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, useless7 useless8 useless9 -Terminals which are not used +Terminals unused in grammar '1' '2' '3' @@ -249,7 +249,7 @@ AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0, [[Nonterminals useless in grammar not_reachable non_productive -Terminals which are not used +Terminals unused in grammar useless_token Rules useless in grammar 2 exp: non_productive