summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-04-25 15:03:01 +0200
committerAkim Demaille <akim.demaille@gmail.com>2020-04-26 16:08:47 +0200
commit60366b152be3f46abdf1756b712205a4bc2fc7d4 (patch)
tree3ee6014e3f192c44f669708f7fb3aa0f9ec9e6f9 /examples
parentc90110efd389bdfe62bd644d8626a602ef2915f5 (diff)
downloadbison-60366b152be3f46abdf1756b712205a4bc2fc7d4.tar.gz
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/c/README.md1
-rw-r--r--examples/c/bistromathic/README.md1
-rwxr-xr-xexamples/c/bistromathic/bistromathic.test21
-rw-r--r--examples/c/bistromathic/parse.y1
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 <<EOF
-100%
+((1 ++ 2) ** 3)
+(1 ++ 2) + (3 ** 4)
EOF
-run 0 '> 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 <<EOF
+100% + 10
+EOF
+run 0 '> 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.