[antlr-interest] greedy subrule option idiom
Junkman
j at junkwallah.org
Thu May 27 15:45:52 PDT 2010
Hello,
The following grammar generates error:
---------------------
grammar Test;
fragment
CHAR : . ;
STRING : '"' ( options {greedy=false;} : CHAR )* '"' ;
stmt :
( . )+
;
---------------------
The error message generated by "Check Grammar" option of Antlrwork (1.4) is:
[15:34:52] error(201): Test.g:6:47: The following alternatives can never
be matched: 2
I think it means it cannot exit the non-greedy subrule (of the lexer
rule STRING).
If I substitute "." directly for "CHAR", no error.
Is this the expected behavior? Is there a problem with the grammar
given above?
Thanks for any insight/assistance.
J
Junkman wrote:
> Hello,
>
> Following is a lexer rule to match quoted string that allows backslash
> escape sequence.
>
>
> STRING
> : '"' ( options {greedy=false;} : ( ~ '\\' | '\\' . ) )* '"'
> ;
>
>
> It seems to work. But if you put the '*' operator inside the subrule
> like this:
>
>
> STRING
> : '"' ( options {greedy=false;} : ( ~ '\\' | '\\' . )* ) '"'
> ;
>
>
> It eats up everything to EOF.
>
> It's as if the greedy option applies to the ((subrule)*) instead of the
> subrule itself, and only if the subrule is suffixed with '*' operator
> (or with '+') externally (as in (subrule)*).
>
> To my eyes, the second version seems the "correct" one.
>
> Thoughts?
>
> J
>
More information about the antlr-interest
mailing list