[antlr-interest] [BUG] Java generator produces useless conditionals

Terence Parr parrt at jguru.com
Fri Feb 21 10:22:06 PST 2003


Hi Marco,

THanks for the fix.  I didn't bother optimizing as I knew that the 
compiler would remove these "if true" tests :)  I'm also frightened by 
consequences of changing code generator ;)

Ter

On Friday, February 21, 2003, at 07:14 AM, Marco Hunsicker wrote:

> 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/
>
>
--
Co-founder, http://www.jguru.com
Creator, ANTLR Parser Generator: http://www.antlr.org
Lecturer in Comp. Sci., University of San Francisco


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list