cex: color the counterexamples

Use colors to show the counterexamples and the derivations in color,
to highlight their structure.  Align the outputs, and add i18n
support. Reduce width by using a one-space separator instead of
two-space.

From

    Example  A  •  B  C
    First  derivation  s ::=[ a ::=[ A  • ]  x ::=[ B  C ] ]
    Second derivation  s ::=[ y ::=[ A  •  B ]  c ::=[ C ] ]

to

    Example              A • B C
    First derivation     s ::=[ a ::=[ A • ] x ::=[ B C ] ]
    Example              A • B C
    Second derivation    s ::=[ y ::=[ A • B ] c ::=[ C ] ]

with colors.

* data/bison-default.css (cex-dot, cex-0, cex-1, cex-2, cex-3, cex-4)
(cex-5, cex-6, cex-7, cex-step, cex-leaf): New.
* src/derivation.c (derivation_print_styled_impl): New.
(derivation_print, derivation_print_leaves): Use it.
* src/counterexample.c: Reformat the output.
* tests/counterexample.at: Adjust.
This commit is contained in:
Akim Demaille
2020-05-23 19:13:01 +02:00
parent 296e2f90ab
commit ae5edcc23b
5 changed files with 197 additions and 147 deletions

View File

@@ -103,21 +103,18 @@ static void
print_counterexample (counterexample *cex)
{
FILE *out = stderr;
if (cex->unifying)
fprintf (out, "Example ");
else
fprintf (out, "First Example ");
fprintf (out, "%-20s ", cex->unifying ? _("Example") : _("First example"));
derivation_print_leaves (cex->d1, out);
fprintf (out, "\nFirst derivation ");
fprintf (out, "%-20s ", _("First derivation"));
derivation_print (cex->d1, out);
if (!cex->unifying)
{
fprintf (out, "\nSecond Example ");
derivation_print_leaves (cex->d2, out);
}
fprintf (out, "\nSecond derivation ");
fprintf (out, "%-20s ", cex->unifying ? _("Example") : _("Second example"));
derivation_print_leaves (cex->d2, out);
fprintf (out, "%-20s ", _("Second derivation"));
derivation_print (cex->d2, out);
fputs ("\n\n", out);
fputc ('\n', out);
}
/*

View File

@@ -18,11 +18,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "derivation.h"
#include <gl_linked_list.h>
#include "system.h"
#include "derivation.h"
#include "complain.h"
struct derivation
{
@@ -124,57 +126,72 @@ derivation_size (const derivation *deriv)
return size;
}
// these are used rarely enough that I don't think they should be interned.
/* Print DERIV, colored according to COUNTER. */
static void
derivation_print_impl (const derivation *deriv, FILE *f,
bool leaves_only,
int *counter)
{
if (deriv->children)
{
const symbol *sym = symbols[deriv->sym];
char style[20];
snprintf (style, 20, "cex-%d", *counter);
++*counter;
begin_use_class (style, f);
if (!leaves_only)
{
begin_use_class ("cex-step", f);
fprintf (f, "%s ::=[ ", sym->tag);
end_use_class ("cex-step", f);
}
const char *sep = "";
derivation *child;
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
derivation_list_next (&it, &child);
)
{
fputs (sep, f);
sep = " ";
derivation_print_impl (child, f, leaves_only, counter);
}
if (!leaves_only)
{
begin_use_class ("cex-step", f);
fputs (" ]", f);
end_use_class ("cex-step", f);
}
end_use_class (style, f);
}
else if (deriv == &d_dot)
{
begin_use_class ("cex-dot", f);
fputs ("", f);
end_use_class ("cex-dot", f);
}
else // leaf.
{
const symbol *sym = symbols[deriv->sym];
begin_use_class ("cex-leaf", f);
fprintf (f, "%s", sym->tag);
end_use_class ("cex-leaf", f);
}
}
void
derivation_print (const derivation *deriv, FILE *f)
{
if (deriv == &d_dot)
{
fputs ("", f);
return;
}
symbol *sym = symbols[deriv->sym];
if (!deriv->children)
{
fprintf (f, " %s", sym->tag);
return;
}
fprintf (f, " %s ::=[", sym->tag);
derivation *child;
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
derivation_list_next (&it, &child);
)
{
derivation_print (child, f);
fputs (" ", f);
}
fputs ("]", f);
int counter = 0;
derivation_print_impl (deriv, f, false, &counter);
fputc ('\n', f);
}
void
derivation_print_leaves (const derivation *deriv, FILE *f)
{
if (deriv == &d_dot)
{
fputs ("", f);
return;
}
if (!deriv->children)
{
symbol *sym = symbols[deriv->sym];
fprintf (f, "%s", sym->tag);
return;
}
const char *sep = "";
derivation *child;
for (gl_list_iterator_t it = gl_list_iterator (deriv->children);
derivation_list_next (&it, &child);
)
{
fputs (sep, f);
sep = " ";
derivation_print_leaves (child, f);
}
int counter = 0;
derivation_print_impl (deriv, f, true, &counter);
fputc ('\n', f);
}

View File

@@ -20,6 +20,7 @@
#ifndef DERIVATION_H
# define DERIVATION_H
# include <gl_linked_list.h>
# include <gl_xlist.h>
# include "gram.h"