diff options
author | Akim Demaille <akim.demaille@gmail.com> | 2020-01-31 06:34:50 +0100 |
---|---|---|
committer | Akim Demaille <akim.demaille@gmail.com> | 2020-02-02 11:32:55 +0100 |
commit | d5f929d407012ea6cb3cdc19b0a0406d40cf921f (patch) | |
tree | 5318895b8e115a7d9ecb2bc8be2b61a953430916 /examples | |
parent | c5b215b5e6a8272e71ee79225ed0ea953f773652 (diff) | |
download | bison-d5f929d407012ea6cb3cdc19b0a0406d40cf921f.tar.gz |
java: example: rely on autoboxing
AFAICT, autoboxing/unboxing was added in Java 5 (September 30, 2004).
I think we can afford to use it. It should help us merge some Java
tests with the main ones.
However, beware that != does not unbox: it compares the object
addresses.
* examples/java/Calc.y, tests/java.at: Simplify.
* examples/java/Calc.test, tests/java.at: Improve tests.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/java/Calc.test | 7 | ||||
-rw-r--r-- | examples/java/Calc.y | 24 |
2 files changed, 18 insertions, 13 deletions
diff --git a/examples/java/Calc.test b/examples/java/Calc.test index d5e483f4..996b20e0 100644 --- a/examples/java/Calc.test +++ b/examples/java/Calc.test @@ -21,6 +21,13 @@ EOF run 0 7 cat >input <<EOF +1 + 2 * 3 = 7 +(1 + 2) * 3 = 9 +EOF +run 0 '7 +9' + +cat >input <<EOF 1 + 2 * * 3 EOF run 0 "err: 1.5: syntax error, unexpected '*', expecting number or '-' or '(' or '!'" diff --git a/examples/java/Calc.y b/examples/java/Calc.y index b69241b5..28195ddd 100644 --- a/examples/java/Calc.y +++ b/examples/java/Calc.y @@ -47,24 +47,22 @@ line: ; exp: - NUM { $$ = $1; } + NUM { $$ = $1; } | exp '=' exp { if ($1.intValue () != $3.intValue ()) yyerror (@$, "calc: error: " + $1 + " != " + $3); } -| exp '+' exp { $$ = new Integer ($1.intValue () + $3.intValue ()); } -| exp '-' exp { $$ = new Integer ($1.intValue () - $3.intValue ()); } -| exp '*' exp { $$ = new Integer ($1.intValue () * $3.intValue ()); } -| exp '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); } -| '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); } -| exp '^' exp { $$ = new Integer ((int) - Math.pow ($1.intValue (), - $3.intValue ())); } -| '(' exp ')' { $$ = $2; } -| '(' error ')' { $$ = new Integer (1111); } -| '!' { $$ = new Integer (0); return YYERROR; } -| '-' error { $$ = new Integer (0); return YYERROR; } +| exp '+' exp { $$ = $1 + $3; } +| exp '-' exp { $$ = $1 - $3; } +| exp '*' exp { $$ = $1 * $3; } +| exp '/' exp { $$ = $1 / $3; } +| '-' exp %prec NEG { $$ = -$2; } +| exp '^' exp { $$ = (int) Math.pow ($1, $3); } +| '(' exp ')' { $$ = $2; } +| '(' error ')' { $$ = 1111; } +| '!' { $$ = 0; return YYERROR; } +| '-' error { $$ = 0; return YYERROR; } ; |