cex: display derivations as trees

Sometimes, understanding the derivations is difficult, because they
are serialized to fit in one line.  For instance, the example taken
from the NEWS file:

    %token ID
    %%
    s: a ID
    a: expr
    expr: expr ID ',' | "expr"

gave

    First example        expr • ID ',' ID $end
    Shift derivation     $accept → [ s → [ a → [ expr → [ expr • ID ',' ] ] ID ] $end ]
    Second example       expr • ID $end
    Reduce derivation    $accept → [ s → [ a → [ expr • ] ID ] $end ]

Printing as trees, it gives:

    First example        expr • ID ',' ID $end
    Shift derivation
      $accept
      ↳ s                      $end
        ↳ a                 ID
          ↳ expr
            ↳ expr • ID ','
    Second example       expr • ID $end
    Reduce derivation
      $accept
      ↳ s             $end
        ↳ a        ID
          ↳ expr •

* src/glyphs.h, src/glyphs.c (down_arrow, empty, derivation_separator):
New.
* src/derivation.c (derivation_print, derivation_print_impl): Rename
as...
(derivation_print_flat, derivation_print_flat_impl): These.
(fputs_if, derivation_depth, derivation_width, derivation_print_tree)
(derivation_print_tree_impl, derivation_print): New.
* src/counterexample.c (print_counterexample): Adjust.
* tests/conflicts.at, tests/counterexample.at, tests/diagnostics.at,
* tests/report.at: Adjust.
This commit is contained in:
Akim Demaille
2020-07-14 08:16:16 +02:00
parent 5544615a59
commit fff17fe8fe
9 changed files with 986 additions and 72 deletions

27
NEWS
View File

@@ -46,9 +46,17 @@ Changes in the display of counterexamples.
strings in the grammar which can be parsed in two ways due to the
conflict. For example:
Example exp '+' exp • '/' exp
Shift derivation exp → [ exp '+' exp → [ exp • '/' exp ] ]
Reduce derivation exp → [ exp → [ exp '+' exp • ] '/' exp ]
Shift/reduce conflict on token "/":
Example exp "+" exp • "/" exp
Shift derivation
exp
↳ exp "+" exp
↳ exp • "/" exp
Example exp "+" exp • "/" exp
Reduce derivation
exp
↳ exp "/" exp
↳ exp "+" exp •
When Bison is installed with text styling enabled, the example is actually
shown twice, with colors highlighting the ambiguity.
@@ -59,9 +67,18 @@ Changes in the display of counterexamples.
generates two examples that are the same up until the dot:
First example expr • ID ',' ID $end
Shift derivation $accept → [ s → [ a → [ expr → [ expr • ID ',' ] ] ID ] $end ]
Shift derivation
$accept
↳ s $end
↳ a ID
↳ expr
↳ expr • ID ','
Second example expr • ID $end
Reduce derivation $accept → [ s → [ a → [ expr • ] ID ] $end ]
Reduce derivation
$accept
↳ s $end
↳ a ID
↳ expr •
In these cases, the parser usually doesn't have enough lookahead to
differentiate the two given examples.