[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
examples: java: use explicit token identifiers
From: |
Akim Demaille |
Subject: |
examples: java: use explicit token identifiers |
Date: |
Sat, 4 Apr 2020 19:20:10 +0200 |
commit 7fa23136cad1d3009e7d894b1cc0e445dbbd4d3e
Author: Akim Demaille <address@hidden>
Date: Sat Apr 4 08:43:28 2020 +0200
examples: java: use explicit token identifiers
* examples/java/calc/Calc.y: Declare all the tokens, so that we are
compatibile with api.token.raw.
* examples/java/calc/Calc.test: Adjust.
diff --git a/examples/java/calc/Calc.test b/examples/java/calc/Calc.test
index 7d96015a..c4143b29 100644
--- a/examples/java/calc/Calc.test
+++ b/examples/java/calc/Calc.test
@@ -30,9 +30,9 @@ run 0 '7
cat >input <<EOF
1 + 2 * * 3
EOF
-run 0 "err: 1.9-1.10: syntax error: expected number or '-' or '(' or '!'
before '*'"
+run 0 "err: 1.9-1.10: syntax error: expected ! or - or ( or number before *"
cat >input <<EOF
12 222
EOF
-run 0 "err: 1.6-1.9: syntax error: expected end of line or '=' or '-' or '+'
or '*' or '/' or '^' before number"
+run 0 "err: 1.6-1.9: syntax error: expected + or - or * or / or ^ or = or end
of line before number"
diff --git a/examples/java/calc/Calc.y b/examples/java/calc/Calc.y
index dc502671..f760089e 100644
--- a/examples/java/calc/Calc.y
+++ b/examples/java/calc/Calc.y
@@ -38,16 +38,25 @@
/* Bison Declarations */
%token
- '\n' _("end of line")
+ BANG "!"
+ PLUS "+"
+ MINUS "-"
+ STAR "*"
+ SLASH "/"
+ CARET "^"
+ LPAREN "("
+ RPAREN ")"
+ EQUAL "="
+ EOL _("end of line")
<Integer>
NUM _("number")
%type <Integer> exp
-%nonassoc '=' /* comparison */
-%left '-' '+'
-%left '*' '/'
+%nonassoc "=" /* comparison */
+%left "-" "+"
+%left "*" "/"
%precedence NEG /* negation--unary minus */
-%right '^' /* exponentiation */
+%right "^" /* exponentiation */
/* Grammar follows */
%%
@@ -57,28 +66,28 @@ input:
;
line:
- '\n'
-| exp '\n' { System.out.println ($exp); }
-| error '\n'
+ EOL
+| exp EOL { System.out.println ($exp); }
+| error EOL
;
exp:
NUM { $$ = $1; }
-| exp '=' exp
+| exp "=" exp
{
if ($1.intValue () != $3.intValue ())
yyerror (@$, "calc: error: " + $1 + " != " + $3);
}
-| 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; }
+| 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; }
;
%%
@@ -151,15 +160,33 @@ class CalcLexer implements Calc.Lexer {
case StreamTokenizer.TT_EOL:
end.line += 1;
end.column = 0;
- return (int) '\n';
+ return EOL;
case StreamTokenizer.TT_WORD:
yylval = new Integer (st.sval);
end.set (reader.getPreviousPosition ());
return NUM;
case ' ': case '\t':
return yylex ();
+ case '!':
+ return BANG;
+ case '+':
+ return PLUS;
+ case '-':
+ return MINUS;
+ case '*':
+ return STAR;
+ case '/':
+ return SLASH;
+ case '^':
+ return CARET;
+ case '(':
+ return LPAREN;
+ case ')':
+ return RPAREN;
+ case '=':
+ return EQUAL;
default:
- return ttype;
+ throw new AssertionError ("invalid character: " + ttype);
}
}
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- examples: java: use explicit token identifiers,
Akim Demaille <=