mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 20:33:03 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -146,6 +146,7 @@ exp:
|
||||
| "-" exp %prec NEG { $$ = -$2; }
|
||||
| exp[l] "^" exp[r] { $$ = pow ($l, $r); }
|
||||
| "(" exp ")" { $$ = $2; }
|
||||
| "(" error ")" { $$ = 666; }
|
||||
;
|
||||
|
||||
// End of grammar.
|
||||
|
||||
Reference in New Issue
Block a user