[antlr-interest] Global backtrack breaks rule that works otherwise; why?

Seref Arikan serefarikan at kurumsalteknoloji.com
Tue Jan 17 03:10:40 PST 2012


Greetings,
The first alternative in the following grammar does not work when global
backtrack is on. I know that I can remove the predicate in the second
alternative, since it is the complement of the first one, but I don't
understand why backtrack leads to parser code that can never recognize the
first alternative.
It works if the rules are not complements of each other such as this
example, but I'm trying to understand the behaviour of Antlr better.

Best regards
Seref


grammar TestBTrack;
options{backtrack=true;}

alt_container
    :
{input.LT(1).getText().toUpperCase().startsWith("A")}?=>a_identifier
    |
{!(input.LT(1).getText().toUpperCase().startsWith("A"))}?=>non_a_identifier
    ;

a_identifier
    :    ID;
non_a_identifier
    :    ID;

ID  :    ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;


More information about the antlr-interest mailing list