From 9757c359efc607b6d9e4675447a7b1afd873bc6e Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 15 Jun 2002 18:23:33 +0000 Subject: [PATCH] * src/gram.h, src/gram.c (grammar_rules_partial_print): New, eved out of... (grammar_rules_print): here. * src/reduce.c (reduce_output): Use it. * tests/reduce.at (Useless Rules, Reduced Automaton) (Underivable Rules): Adjust. --- ChangeLog | 10 ++++++++++ src/gram.c | 26 ++++++++++++++++++++------ src/gram.h | 5 +++++ src/reduce.c | 17 +++-------------- tests/reduce.at | 42 ++++++++++++++++++++++++------------------ 5 files changed, 62 insertions(+), 38 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4995682a..ab0b47da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2002-06-15 Akim Demaille + + * src/gram.h, src/gram.c (grammar_rules_partial_print): New, eved + out of... + (grammar_rules_print): here. + * src/reduce.c (reduce_output): Use it. + * tests/reduce.at (Useless Rules, Reduced Automaton) + (Underivable Rules): Adjust. + + 2002-06-15 Akim Demaille Copy BYacc's nice way to report the grammar. diff --git a/src/gram.c b/src/gram.c index a1a06308..c1279282 100644 --- a/src/gram.c +++ b/src/gram.c @@ -133,9 +133,10 @@ ritem_longest_rhs (void) } -/*-----------------------------------. -| Print the grammar's rules on OUT. | -`-----------------------------------*/ +/*----------------------------------------------------------------. +| Print the grammar's rules numbers from BEGIN (inclusive) to END | +| (exclusive) on OUT under TITLE. | +`----------------------------------------------------------------*/ static inline void blanks_print (unsigned n, FILE *out) @@ -145,14 +146,15 @@ blanks_print (unsigned n, FILE *out) } void -grammar_rules_print (FILE *out) +grammar_rules_partial_print (FILE *out, const char *title, + int begin, int end) { int r; symbol_t *last_lhs = NULL; /* rule # : LHS -> RHS */ - fprintf (out, "%s\n\n", _("Grammar")); - for (r = 1; r < nrules + 1; r++) + fprintf (out, "%s\n\n", title); + for (r = begin; r < end; r++) { if (last_lhs && last_lhs != rules[r].lhs) fputc ('\n', out); @@ -173,6 +175,18 @@ grammar_rules_print (FILE *out) fputs ("\n\n", out); } + +/*------------------------------------------. +| Print the grammar's useful rules on OUT. | +`------------------------------------------*/ + +void +grammar_rules_print (FILE *out) +{ + grammar_rules_partial_print (out, _("Grammar"), 1, nrules + 1); +} + + /*-------------------. | Dump the grammar. | `-------------------*/ diff --git a/src/gram.h b/src/gram.h index 962b9adb..b0f9daef 100644 --- a/src/gram.h +++ b/src/gram.h @@ -183,6 +183,11 @@ void ritem_print PARAMS ((FILE *out)); /* Return the size of the longest rule RHS. */ size_t ritem_longest_rhs PARAMS ((void)); +/* Print the grammar's rules numbers from BEGIN (inclusive) to END + (exclusive) on OUT under TITLE. */ +void grammar_rules_partial_print PARAMS ((FILE *out, const char *title, + int begin, int end)); + /* Print the grammar's rules on OUT. */ void grammar_rules_print PARAMS ((FILE *out)); diff --git a/src/reduce.c b/src/reduce.c index 2319b60b..35aab2e7 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -371,20 +371,9 @@ reduce_output (FILE *out) } if (nuseless_productions > 0) - { - int i; - fprintf (out, "%s\n\n", _("Useless rules:")); - for (i = nrules + 1; i < nuseless_productions + nrules + 1; i++) - { - item_number_t *r; - fprintf (out, "#%-4d ", rules[i].user_number - 1); - fprintf (out, "%s:", symbol_tag_get (rules[i].lhs)); - for (r = rules[i].rhs; *r >= 0; r++) - fprintf (out, " %s", symbol_tag_get (symbols[*r])); - fputs (";\n", out); - } - fputs ("\n\n", out); - } + grammar_rules_partial_print (out, _("Useless rules"), + nrules + 1, + nuseless_productions + nrules + 1); } diff --git a/tests/reduce.at b/tests/reduce.at index ba9d27ed..7dc939ed 100644 --- a/tests/reduce.at +++ b/tests/reduce.at @@ -124,6 +124,8 @@ AT_CLEANUP AT_SETUP([Useless Rules]) +AT_KEYWORDS([report]) + AT_DATA([[input.y]], [[%verbose %output="input.c" @@ -184,16 +186,16 @@ Terminals which are not used: '7' '8' '9' -Useless rules: -#2 useless1: '1'; -#3 useless2: '2'; -#4 useless3: '3'; -#5 useless4: '4'; -#6 useless5: '5'; -#7 useless6: '6'; -#8 useless7: '7'; -#9 useless8: '8'; -#10 useless9: '9'; +Useless rules + 2 useless1: '1' + 3 useless2: '2' + 4 useless3: '3' + 5 useless4: '4' + 6 useless5: '5' + 7 useless6: '6' + 8 useless7: '7' + 9 useless8: '8' + 10 useless9: '9' ]]) AT_CLEANUP @@ -209,6 +211,8 @@ AT_CLEANUP AT_SETUP([Reduced Automaton]) +AT_KEYWORDS([report]) + # The non reduced grammar. # ------------------------ AT_DATA([[not-reduced.y]], @@ -249,10 +253,10 @@ AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0, non_productive Terminals which are not used: useless_token -Useless rules: -#2 exp: non_productive; -#3 not_reachable: useful; -#4 non_productive: non_productive useless_token; +Useless rules + 2 exp: non_productive + 3 not_reachable: useful + 4 non_productive: non_productive useless_token ]]) # The reduced grammar. @@ -296,6 +300,8 @@ AT_CLEANUP AT_SETUP([Underivable Rules]) +AT_KEYWORDS([report]) + AT_DATA([[input.y]], [[%verbose %output="input.c" @@ -319,10 +325,10 @@ AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0, [[Useless nonterminals: underivable indirection -Useless rules: -#2 exp: underivable; -#3 underivable: indirection; -#4 indirection: underivable; +Useless rules + 2 exp: underivable + 3 underivable: indirection + 4 indirection: underivable ]]) AT_CLEANUP -- 2.45.2