cex: display the rule numbers

From

    Example: "if" expr "then" "if" expr "then" stmt • "else" stmt
    Shift derivation
      if_stmt
      ↳ "if" expr "then" stmt
                         ↳ if_stmt
                           ↳ "if" expr "then" stmt • "else" stmt
    Reduce derivation
      if_stmt
      ↳ "if" expr "then" stmt                        "else" stmt
                         ↳ if_stmt
                           ↳ "if" expr "then" stmt •

to

    Example: "if" expr "then" "if" expr "then" stmt • "else" stmt
    Shift derivation
      if_stmt
      ↳ 3: "if" expr "then" stmt
                            ↳ 2: if_stmt
                                 ↳ 4: "if" expr "then" stmt • "else" stmt
    Example: "if" expr "then" "if" expr "then" stmt • "else" stmt
    Reduce derivation
      if_stmt
      ↳ 4: "if" expr "then" stmt                              "else" stmt
                            ↳ 2: if_stmt
                                 ↳ 3: "if" expr "then" stmt •

* src/state-item.h, src/state-item.c (state_item_rule): New.
* src/derivation.h, src/derivation.c (struct derivation): Add a rule
member.
Adjust dependencies.
* src/counterexample.c, src/parse-simulation.c: Pass the rule to
derivation_new.
* src/derivation.c (fprintf_if): New.
(derivation_width, derivation_print_tree_impl): Take the rule number
into account.

* tests/conflicts.at, tests/counterexample.at, tests/diagnostics.at,
* tests/report.at: Adjust.

* doc/bison.texi: Adjust.
This commit is contained in:
Akim Demaille
2020-07-28 19:50:22 +02:00
parent 2becdace96
commit 3c36d871fa
13 changed files with 407 additions and 357 deletions

View File

@@ -25,6 +25,7 @@
#include <c-ctype.h>
#include <gl_linked_list.h>
#include <mbswidth.h>
#include <vasnprintf.h>
#include "system.h"
#include "complain.h"
@@ -34,13 +35,15 @@ struct derivation
symbol_number sym;
derivation_list children;
int reference_count;
// The rule SYM -> CHILDREN.
const rule *rule;
// Color assigned for styling. Guarantees that the derivation is
// always displayed with the same color, independently of the order
// in which the derivations are traversed.
int color;
};
static derivation d_dot = { -1, NULL, -1, -1 };
static derivation d_dot = { -1, NULL, -1, NULL, -1 };
derivation *
derivation_dot (void)
@@ -74,12 +77,14 @@ void derivation_list_free (derivation_list dl)
}
derivation *
derivation_new (symbol_number sym, derivation_list children)
derivation_new (symbol_number sym, derivation_list children,
const rule *r)
{
derivation *res = xmalloc (sizeof *res);
res->sym = sym;
res->children = children;
res->reference_count = 0;
res->rule = r;
res->color = -1;
return res;
}
@@ -205,6 +210,23 @@ fputs_if (bool cond, FILE *out, int *padding, const char *s)
return res;
}
static int
fprintf_if (bool cond, FILE *out, int *padding, const char *fmt, ...)
{
char buf[256];
size_t len = sizeof (buf);
va_list args;
va_start (args, fmt);
char *cp = vasnprintf (buf, &len, fmt, args);
va_end (args);
if (!cp)
xalloc_die ();
int res = fputs_if (cond, out, padding, cp);
if (cp != buf)
free (cp);
return res;
}
// The width taken to report this derivation recursively down to its
// leaves.
static int
@@ -217,6 +239,7 @@ derivation_width (const derivation *deriv)
// Arrow and space.
int children_width = down_arrow_width;
children_width += snprintf (NULL, 0, "%d: ", deriv->rule->number);
if (gl_list_size (deriv->children) == 0)
// Empty rhs.
children_width += empty_width;
@@ -281,6 +304,7 @@ derivation_print_tree_impl (const derivation *deriv, FILE *out,
else
{
res += fputs_if (depth == 1, out, padding, down_arrow);
res += fprintf_if (depth == 1, out, padding, "%d: ", deriv->rule->number);
if (gl_list_size (deriv->children) == 0)
// Empty rhs.
res += fputs_if (depth == 1, out, padding, empty);