[antlr-interest] Beginner grammar question
Gavin Lambert
antlr at mirality.co.nz
Fri Sep 12 05:04:40 PDT 2008
At 19:39 12/09/2008, Fabian Baboschi wrote:
>element : BAR NAME
> ;
>
>BAR : 'bar'
> ;
>
>NAME
> : ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
> ;
[...]
>I tried to use the following input: bar bar
>and I get errors because the parser doesn't recognize that the
>second bar should be treated as a name.
The other common solution to this (the first being what Matt just
suggested) is to leave your BAR rule in place but change element
like so:
element : BAR name ;
name : NAME | BAR ;
The key point to realise in all of this is that lexing happens
first, without any parser context. So given a choice between an
exact-match keyword and a vague-match identifier rule, the keyword
will win.
More information about the antlr-interest
mailing list