[PATCH 7/8] cex: also include in the report on --report=counterexamples

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
And let --report=all include the counterexamples.

* src/getargs.h, src/getargs.c (report_cex): New.
* src/main.c: Compute counterexamples when -rcex is specified.
* src/print.c: Include the counterexamples when -rcex is specified.

* tests/conflicts.at, tests/existing.at, tests/local.at: Adjust.
---
 TODO               |  3 +++
 src/getargs.c      | 27 +++++++++++++++------------
 src/getargs.h      |  1 +
 src/main.c         |  3 ++-
 src/print.c        |  4 +++-
 tests/conflicts.at | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tests/existing.at  |  2 +-
 tests/local.at     |  2 +-
 8 files changed, 71 insertions(+), 16 deletions(-)

diff --git a/TODO b/TODO
index 5c9ea504..875baeab 100644
--- a/TODO
+++ b/TODO
@@ -32,6 +32,9 @@ could try to use the same color for the same rule.
 Show the counterexamples.  This is going to be really hard and/or painful.
 Unless we play it dumb (little structure).
 
+*** Doc
+-Wcounterexamples, --report=counterexamples
+
 ** Bistromathic
 - Hitting tab on a line with a syntax error is ugly
 
diff --git a/src/getargs.c b/src/getargs.c
index afbc2bb4..f6c4d182 100644
--- a/src/getargs.c
+++ b/src/getargs.c
@@ -206,23 +206,26 @@ ARGMATCH_DEFINE_GROUP (report, enum report)
 
 static const argmatch_report_doc argmatch_report_docs[] =
 {
-  { "states",     N_("describe the states") },
-  { "itemsets",   N_("complete the core item sets with their closure") },
-  { "lookaheads", N_("explicitly associate lookahead tokens to items") },
-  { "solved",     N_("describe shift/reduce conflicts solving") },
-  { "all",        N_("include all the above information") },
-  { "none",       N_("disable the report") },
+  { "states",          N_("describe the states") },
+  { "itemsets",        N_("complete the core item sets with their closure") },
+  { "lookaheads",      N_("explicitly associate lookahead tokens to items") },
+  { "solved",          N_("describe shift/reduce conflicts solving") },
+  { "counterexamples", N_("generate conflict counterexamples") },
+  { "all",             N_("include all the above information") },
+  { "none",            N_("disable the report") },
   { NULL, NULL },
 };
 
 static const argmatch_report_arg argmatch_report_args[] =
 {
-  { "none",        report_none },
-  { "states",      report_states },
-  { "itemsets",    report_states | report_itemsets },
-  { "lookaheads",  report_states | report_lookahead_tokens },
-  { "solved",      report_states | report_solved_conflicts },
-  { "all",         report_all },
+  { "none",            report_none },
+  { "states",          report_states },
+  { "itemsets",        report_states | report_itemsets },
+  { "lookaheads",      report_states | report_lookahead_tokens },
+  { "solved",          report_states | report_solved_conflicts },
+  { "counterexamples", report_cex },
+  { "cex",             report_cex },
+  { "all",             report_all },
   { NULL, report_none },
 };
 
diff --git a/src/getargs.h b/src/getargs.h
index 70be70a4..8c47fa58 100644
--- a/src/getargs.h
+++ b/src/getargs.h
@@ -79,6 +79,7 @@ enum report
     report_itemsets         = 1 << 1,
     report_lookahead_tokens = 1 << 2,
     report_solved_conflicts = 1 << 3,
+    report_cex              = 1 << 4,
     report_all              = ~0
   };
 /** What appears in the *.output file.  */
diff --git a/src/main.c b/src/main.c
index 18b6cc19..c0176c29 100644
--- a/src/main.c
+++ b/src/main.c
@@ -146,7 +146,8 @@ main (int argc, char *argv[])
       conflicts_update_state_numbers (old_to_new, nstates_old);
       free (old_to_new);
     }
-  if (warning_is_enabled (Wcounterexamples))
+  if (report_flag & report_cex
+      || warning_is_enabled (Wcounterexamples))
     counterexample_init ();
   conflicts_print ();
   timevar_pop (tv_conflicts);
diff --git a/src/print.c b/src/print.c
index e0940f3b..556bbc11 100644
--- a/src/print.c
+++ b/src/print.c
@@ -356,7 +356,9 @@ print_state (FILE *out, const state *s)
       fputc ('\n', out);
       fputs (s->solved_conflicts, out);
     }
-  if (warning_is_enabled (Wcounterexamples) && has_conflicts (s))
+  if (has_conflicts (s)
+      && (report_flag & report_cex
+          || warning_is_enabled (Wcounterexamples)))
     {
       fputc ('\n', out);
       counterexample_report_state (s, out, "    ");
diff --git a/tests/conflicts.at b/tests/conflicts.at
index 807a07c2..3e061ad9 100644
--- a/tests/conflicts.at
+++ b/tests/conflicts.at
@@ -860,6 +860,15 @@ State 5
 
     OP        [reduce using rule 1 (exp)]
     $default  reduce using rule 1 (exp)
+
+    Shift/reduce conflict on token OP:
+        1 exp: exp OP exp .
+        1 exp: exp . OP exp
+      Example                  exp OP exp • OP exp
+      First derivation         exp ::=[ exp ::=[ exp OP exp • ] OP exp ]
+      Example                  exp OP exp • OP exp
+      Second derivation        exp ::=[ exp OP exp ::=[ exp • OP exp ] ]
+
 ]])
 
 AT_CLEANUP
@@ -1195,6 +1204,15 @@ State 1
     $end      [reduce using rule 4 (id)]
     $default  reduce using rule 3 (num)
 
+    Reduce/reduce conflict on token $end:
+        3 num: '0' .
+        4 id: '0' .
+      Example                  '0' •
+      First derivation         exp ::=[ num ::=[ '0' • ] ]
+      Example                  '0' •
+      Second derivation        exp ::=[ id ::=[ '0' • ] ]
+
+
 
 State 2
 
@@ -1734,6 +1752,15 @@ State 4
 
     reported_conflicts  go to state 6
 
+    Shift/reduce conflict on token 'a':
+       10 reported_conflicts: . %empty
+        8 reported_conflicts: . 'a'
+      First example            resolved_conflict • 'a'
+      First derivation         start ::=[ resolved_conflict reported_conflicts ::=[ • ] 'a' ]
+      Second example           resolved_conflict • 'a' 'a'
+      Second derivation        start ::=[ resolved_conflict reported_conflicts ::=[ • 'a' ] 'a' ]
+
+
 
 State 5
 
@@ -1744,6 +1771,15 @@ State 5
     'a'       [reduce using rule 9 (reported_conflicts)]
     $default  reduce using rule 8 (reported_conflicts)
 
+    Reduce/reduce conflict on token 'a':
+        8 reported_conflicts: 'a' .
+        9 reported_conflicts: 'a' .
+      Example                  'a' •
+      First derivation         reported_conflicts ::=[ 'a' • ]
+      Example                  'a' •
+      Second derivation        reported_conflicts ::=[ 'a' • ]
+
+
 
 State 6
 
@@ -1920,6 +1956,15 @@ AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
     Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
     Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
 
+    Reduce/reduce conflict on token 'c':
+       12 empty_c2: . %empty
+       13 empty_c3: . %empty
+      Example                  • 'c'
+      First derivation         start ::=[ empty_c2 ::=[ • ] 'c' ]
+      Example                  • 'c'
+      Second derivation        start ::=[ empty_c3 ::=[ • ] 'c' ]
+
+
 
 State 1
 ]])
diff --git a/tests/existing.at b/tests/existing.at
index d0f6cb95..d1011fe5 100644
--- a/tests/existing.at
+++ b/tests/existing.at
@@ -36,7 +36,7 @@ dnl support is rapidly evolving, let's keep that information to be careful.
 dnl However, we don't do diffs for canonical LR(1) because the diff is huge.
 m4_pushdef([AT_LALR1_DIFF_CHECK],
 [AT_CHECK([[sed 's/^%define lr.type .*$//' input.y > input-lalr.y]])
-AT_BISON_CHECK([[--report=all input-lalr.y]], [[0]], [ignore], [ignore])
+AT_BISON_CHECK([[--report=all,no-cex input-lalr.y]], [[0]], [ignore], [ignore])
 AT_DIFF_U_CHECK([[input-lalr.output input.output]], [$1])])
 
 AT_TEST_TABLES_AND_PARSE([$2[: LALR(1)]], [[LALR]], [[last-state]],
diff --git a/tests/local.at b/tests/local.at
index ad40a877..863f75e9 100644
--- a/tests/local.at
+++ b/tests/local.at
@@ -1594,7 +1594,7 @@ yylex (void)
 
 m4_if(m4_index(m4_quote($3), [no-xml]), -1,
       [AT_BISON_CHECK],
-      [AT_BISON_CHECK_NO_XML])([[-Wall --report=all --defines -o input.c input.y]],
+      [AT_BISON_CHECK_NO_XML])([[-Wall --report=all,no-cex --defines -o input.c input.y]],
                                [0], [], m4_dquote($7))
 
 m4_if(m4_index(m4_quote($3), [last-state]), -1,
-- 
2.27.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.