[antlr-interest] [BUG] Java generator produces useless conditionals
Marco Hunsicker
devel at hunsicker.de
Fri Feb 21 07:14:31 PST 2003
Hello Dr. Parr,
I found a, well, maybe not really a bug, more of a cosmetic issue, but
anyway.
The following lexer rule (taken from the Java grammar)
<pre>
WS : ( ' '
| '\t'
| '\f'
// handle newlines
| ( options {generateAmbigWarnings=false;}
: "\r\n" // Evil DOS
| '\n' // Unix (the right way)
| '\r' // Macintosh
)
{ newline(); }
)+
;
</pre>
produces (ANTLR 2.7.2) Java code such as (only the relevant part
shown)
<pre>
case '\n':
case '\r': {
if (
(LA(1) == '\r') && (LA(2) == '\n')
&& (true) && (true)) { // <== USELESS
match("\r\n");
} else if ((LA(1) == '\n')) {
match('\n');
} else if (
(LA(1) == '\r') && (true) && (true) // <== USELESS
&& (true)) { // <== USELESS
match('\r');
} else {
throw new NoViableAltForCharException(
(char) LA(1), getFilename(),
getLine(), getColumn());
}
newline();
break;
</pre>
which contains several useless conditionals. It's a really minor
issue, but as it's easy to fix, I thought I post the code here.
The problem is the method getLookaheadTestExpression(Lookahead[], int)
in the Java CodeGenerator. I changed it to
<pre>
protected String getLookaheadTestExpression(Lookahead[] look, int k) {
StringBuffer e = new StringBuffer(100);
boolean first = true;
e.append("(");
for (int i = 1; i <= k; i++) {
BitSet p = look[i].fset;
// Syn preds can yield <end-of-syn-pred> (epsilon) lookahead.
// There is no way to predict what that token would be. Just
// allow anything instead.
if (look[i].containsEpsilon()) {
if (first) {
e.append(true);
}
}
else {
if (!first) {
e.append(") && (");
}
e.append(getLookaheadTestTerm(i, p));
}
first = false;
}
e.append(")");
return e.toString();
}
</pre>
to get rid of the obsolete tests.
And BTW, wow ;)
--
Best regards,
Marco
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list