From 60366b152be3f46abdf1756b712205a4bc2fc7d4 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 25 Apr 2020 15:03:01 +0200 Subject: [PATCH] examples: bistromathic: demonstrate error recovery * examples/c/bistromathic/parse.y: here. * examples/c/bistromathic/bistromathic.test: Check it. Included a stupid case where the error is actually ignored. --- examples/c/README.md | 1 + examples/c/bistromathic/README.md | 1 + examples/c/bistromathic/bistromathic.test | 21 ++++++++++++++++++--- examples/c/bistromathic/parse.y | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/examples/c/README.md b/examples/c/README.md index 47963f13..0d8031c3 100644 --- a/examples/c/README.md +++ b/examples/c/README.md @@ -53,6 +53,7 @@ push-parser model. This example demonstrates best practices when using Bison. - Its hand-written scanner tracks locations. - Its interface is pure. +- It uses the `error` token to get error recovery. - Its interface is "incremental", well suited for interaction: it uses the push-parser API to feed the parser with the incoming tokens. - It features an interactive command line with completion based on the diff --git a/examples/c/bistromathic/README.md b/examples/c/bistromathic/README.md index 6b6521de..05f1b7ed 100644 --- a/examples/c/bistromathic/README.md +++ b/examples/c/bistromathic/README.md @@ -2,6 +2,7 @@ This example demonstrates best practices when using Bison. - Its hand-written scanner tracks locations. - Its interface is pure. +- It uses the `error` token to get error recovery. - Its interface is "incremental", well suited for interaction: it uses the push-parser API to feed the parser with the incoming tokens. - It features an interactive command line with completion based on the diff --git a/examples/c/bistromathic/bistromathic.test b/examples/c/bistromathic/bistromathic.test index c32c1f45..629c32c5 100755 --- a/examples/c/bistromathic/bistromathic.test +++ b/examples/c/bistromathic/bistromathic.test @@ -82,11 +82,26 @@ run 0 '> 1 / 0 > '' err: 1.1-5: error: division by zero' +# Error recovery. cat >input < 100% -100 +run 0 '> ((1 ++ 2) ** 3) +666 +> (1 ++ 2) + (3 ** 4) +1332 +> '' +err: 1.6: syntax error: expected - or ( or number or function or variable before + +err: 2.5: syntax error: expected - or ( or number or function or variable before + +err: 2.16: syntax error: expected - or ( or number or function or variable before *' + +# This is really stupid: we just discarded % and did not enter error recovery. +cat >input < 100% + 10 +110 > '' err: 1.4: error: invalid character' diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y index 607a6ef4..5d422651 100644 --- a/examples/c/bistromathic/parse.y +++ b/examples/c/bistromathic/parse.y @@ -146,6 +146,7 @@ exp: | "-" exp %prec NEG { $$ = -$2; } | exp[l] "^" exp[r] { $$ = pow ($l, $r); } | "(" exp ")" { $$ = $2; } +| "(" error ")" { $$ = 666; } ; // End of grammar.