[antlr-interest] Parsing with inverse matches
Gavin Lambert
antlr at mirality.co.nz
Sun Nov 22 23:15:43 PST 2009
At 11:20 23/11/2009, Vipul Delwadia wrote:
>x : STRING+;
>
>fragment BACKSLASH
> : '\\';
>
>NOTA: BACKSLASH A;
>
>A : 'a';
>
>STRING
> : (~(A)|NOTA)+;
[...]
> This works for the most part except when I try and match
> just "\a", at which point I get a MismatchTokenException
> (or sometimes a NoViableAltException).
The input "\a" by itself will produce a NOTA token, since that's
the best-fit non-fragment rule.
Your parser is however not expecting that. Almost certainly, both
A and NOTA should be fragment rules.
(In general, you be suspicious any time a non-fragment lexer rule
refers to any other non-fragment lexer rule. It's not illegal,
and sometimes it's essential, but unless you're really careful
it's easy to break things by doing so.)
Also, as Kirby said, inside the STRING rule itself you have one
alt which is a subset of the other. Whenever you do this you
should normally list the most general alt last, so NOTA should
come before ~A.
More information about the antlr-interest
mailing list