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.
This commit is contained in:
Akim Demaille
2020-04-25 15:03:01 +02:00
parent c90110efd3
commit 60366b152b
4 changed files with 21 additions and 3 deletions

View File

@@ -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

View File

@@ -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'

View File

@@ -146,6 +146,7 @@ exp:
| "-" exp %prec NEG { $$ = -$2; }
| exp[l] "^" exp[r] { $$ = pow ($l, $r); }
| "(" exp ")" { $$ = $2; }
| "(" error ")" { $$ = 666; }
;
// End of grammar.