[antlr-interest] NullPointerException in parser bug

Mike Gadsdon m.gadsdon at rheagroup.com
Wed Mar 23 03:34:37 PST 2005


Hi All

I've come across what appears to be a bug in ANTLR (i'm using version
2.7.4).

One of my parser rules generates Java code like this :-

else if ((LA(1)==MINUS||LA(1)==PLUS||LA(1)==NUMBER) &&
         (_tokenSet_8.member(LA(2))) &&
         (_tokenSet_9.member(LA(3))) &&
         (_tokenSet_10.member(LA(4))) &&
         (_tokenSet_11.member(LA(5))) &&
         (_tokenSet_7.member(LA(6))) &&
         (_tokenSet_7.member(LA(7)))) {
   decimalConstant();

I've split the if condition onto separate lines, but otherwise this is
automatically generated by ANTLR, and presumably goes out to LA(7) due to my
lookahead being set to 7.

The problem arises when this code is executed as a result of tokens near the
end of the input file. In the case I've debugged, LA(5) is the end of file,
so LA(6) does not exist. The previous conditions (LA(1) -> LA(5)) all pass,
so the condition on LA(6) is executed.

The LA code in TokenBuffer ends up running :-

    public final int LA(int i) throws TokenStreamException {
        fill(i);
        return queue.elementAt(markerOffset + i - 1).type;
    }

but the queue contains null at the accessed index position, so the type
method call fails with a NullPointerException.

I'm not entirely sure what should be done about this. Maybe LA should
return -1 if we are past the end of the input and the 'member' method always
return true if the token type is -1. Whatever the solution, it would appear
to need changes in ANTLR itself.

Any suggestions for a workaround would be gratefully received.

Mike

==============================================================
Mike Gadsdon                   mailto:m.gadsdon at rheagroup.com
Rhea System S.A                Tel +44 (0)20 8891 0702
                               Fax +44 (0)20 8891 6702
==============================================================



More information about the antlr-interest mailing list