mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-22 10:43:02 +00:00
java: examples: prefer switch to chains of else-if
* examples/java/calc/Calc.y, examples/java/simple/Calc.y: here. * examples/java/simple/Calc.y: Use the tokenizer's handling of blanks.
This commit is contained in:
@@ -118,23 +118,22 @@ class CalcLexer implements Calc.Lexer {
|
|||||||
start.set (end);
|
start.set (end);
|
||||||
int ttype = st.nextToken ();
|
int ttype = st.nextToken ();
|
||||||
end.set (reader.getPosition ());
|
end.set (reader.getPosition ());
|
||||||
if (ttype == st.TT_EOF)
|
switch (ttype)
|
||||||
return EOF;
|
|
||||||
else if (ttype == st.TT_EOL)
|
|
||||||
{
|
{
|
||||||
|
case StreamTokenizer.TT_EOF:
|
||||||
|
return EOF;
|
||||||
|
case StreamTokenizer.TT_EOL:
|
||||||
end.line += 1;
|
end.line += 1;
|
||||||
end.column = 0;
|
end.column = 0;
|
||||||
return (int) '\n';
|
return (int) '\n';
|
||||||
}
|
case StreamTokenizer.TT_WORD:
|
||||||
else if (ttype == st.TT_WORD)
|
|
||||||
{
|
|
||||||
yylval = new Integer (st.sval);
|
yylval = new Integer (st.sval);
|
||||||
return NUM;
|
return NUM;
|
||||||
|
case ' ': case '\t':
|
||||||
|
return yylex ();
|
||||||
|
default:
|
||||||
|
return ttype;
|
||||||
}
|
}
|
||||||
else if (st.ttype == ' ' || st.ttype == '\t')
|
|
||||||
return yylex ();
|
|
||||||
else
|
|
||||||
return st.ttype;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ class CalcLexer implements Calc.Lexer {
|
|||||||
st = new StreamTokenizer (new InputStreamReader (is));
|
st = new StreamTokenizer (new InputStreamReader (is));
|
||||||
st.resetSyntax ();
|
st.resetSyntax ();
|
||||||
st.eolIsSignificant (true);
|
st.eolIsSignificant (true);
|
||||||
|
st.whitespaceChars ('\t', '\t');
|
||||||
|
st.whitespaceChars (' ', ' ');
|
||||||
st.wordChars ('0', '9');
|
st.wordChars ('0', '9');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,18 +94,17 @@ class CalcLexer implements Calc.Lexer {
|
|||||||
|
|
||||||
public int yylex () throws IOException {
|
public int yylex () throws IOException {
|
||||||
int ttype = st.nextToken ();
|
int ttype = st.nextToken ();
|
||||||
if (ttype == st.TT_EOF)
|
switch (ttype)
|
||||||
return EOF;
|
|
||||||
else if (ttype == st.TT_EOL)
|
|
||||||
return (int) '\n';
|
|
||||||
else if (ttype == st.TT_WORD)
|
|
||||||
{
|
{
|
||||||
|
case StreamTokenizer.TT_EOF:
|
||||||
|
return EOF;
|
||||||
|
case StreamTokenizer.TT_EOL:
|
||||||
|
return (int) '\n';
|
||||||
|
case StreamTokenizer.TT_WORD:
|
||||||
yylval = new Integer (st.sval);
|
yylval = new Integer (st.sval);
|
||||||
return NUM;
|
return NUM;
|
||||||
|
default:
|
||||||
|
return ttype;
|
||||||
}
|
}
|
||||||
else if (st.ttype == ' ' || st.ttype == '\t')
|
|
||||||
return yylex ();
|
|
||||||
else
|
|
||||||
return st.ttype;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user