mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 20:33:03 +00:00
From
public interface Lexer {
/* Token kinds. */
/** Token number, to be returned by the scanner. */
static final int YYEOF = 0;
/** Token number, to be returned by the scanner. */
static final int YYERRCODE = 256;
/** Token number, to be returned by the scanner. */
static final int YYUNDEF = 257;
/** Token number, to be returned by the scanner. */
static final int BANG = 258;
...
/** Deprecated, use b4_symbol(0, id) instead. */
public static final int EOF = YYEOF;
to
public interface Lexer {
/* Token kinds. */
/** Token "end of file", to be returned by the scanner. */
static final int YYEOF = 0;
/** Token error, to be returned by the scanner. */
static final int YYerror = 256;
/** Token "invalid token", to be returned by the scanner. */
static final int YYUNDEF = 257;
/** Token "!", to be returned by the scanner. */
static final int BANG = 258;
...
/** Deprecated, use YYEOF instead. */
public static final int EOF = YYEOF;
* data/skeletons/java.m4 (b4_token_enum): Display the symbol's tag in
comment.
* data/skeletons/lalr1.java: Address overquotation issue.
* examples/java/calc/Calc.y, examples/java/simple/Calc.y: Use YYEOF,
not EOF.
107 lines
2.2 KiB
Plaintext
107 lines
2.2 KiB
Plaintext
%language "Java"
|
|
|
|
%define api.parser.class {Calc}
|
|
%define api.parser.public
|
|
|
|
%define parse.error verbose
|
|
|
|
%code imports {
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.Reader;
|
|
import java.io.StreamTokenizer;
|
|
}
|
|
|
|
%code {
|
|
public static void main(String[] args) throws IOException {
|
|
CalcLexer l = new CalcLexer(System.in);
|
|
Calc p = new Calc(l);
|
|
if (!p.parse())
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
/* Bison Declarations */
|
|
%token <Integer> NUM "number"
|
|
%type <Integer> exp
|
|
|
|
%nonassoc '=' /* comparison */
|
|
%left '-' '+'
|
|
%left '*' '/'
|
|
%precedence NEG /* negation--unary minus */
|
|
%right '^' /* exponentiation */
|
|
|
|
/* Grammar follows */
|
|
%%
|
|
input:
|
|
line
|
|
| input line
|
|
;
|
|
|
|
line:
|
|
'\n'
|
|
| exp '\n' { System.out.println($exp); }
|
|
| error '\n'
|
|
;
|
|
|
|
exp:
|
|
NUM { $$ = $1; }
|
|
| 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; }
|
|
;
|
|
|
|
|
|
%%
|
|
class CalcLexer implements Calc.Lexer {
|
|
|
|
StreamTokenizer st;
|
|
|
|
public CalcLexer(InputStream is) {
|
|
st = new StreamTokenizer(new InputStreamReader(is));
|
|
st.resetSyntax();
|
|
st.eolIsSignificant(true);
|
|
st.whitespaceChars('\t', '\t');
|
|
st.whitespaceChars(' ', ' ');
|
|
st.wordChars('0', '9');
|
|
}
|
|
|
|
public void yyerror(String s) {
|
|
System.err.println(s);
|
|
}
|
|
|
|
Integer yylval;
|
|
|
|
public Object getLVal() {
|
|
return yylval;
|
|
}
|
|
|
|
public int yylex() throws IOException {
|
|
int ttype = st.nextToken();
|
|
switch (ttype) {
|
|
case StreamTokenizer.TT_EOF:
|
|
return YYEOF;
|
|
case StreamTokenizer.TT_EOL:
|
|
return (int) '\n';
|
|
case StreamTokenizer.TT_WORD:
|
|
yylval = new Integer(st.sval);
|
|
return NUM;
|
|
default:
|
|
return ttype;
|
|
}
|
|
}
|
|
}
|