[antlr-interest] Newbie question to lookahead function
Gavin Lambert
antlr at mirality.co.nz
Wed May 21 06:07:17 PDT 2008
At 22:11 21/05/2008, Wenliang Cai wrote:
>I get a problem when having Antlr 2.7.2 parsing
>a normal operation expression. The scenario is like below
>
>Grammar for identifier
>IDENT
>options {testLiterals=true;}
> : (IDENTATOM "!=") => IDENTATOM
> | IDENTATOM ('!' IDENTATOM)*
> ;
>
>IDENTATOM
>options {testLiterals=true;}
> : ('_'|'a'..'z')('#'|'_'|'a'..'z'|'0'..'9')*
> ;
>In this case, we can get below result:
>a. Input string: MyIdent!=0, result: parser
>code can parse the string correctly.
>b. Input string: MyIdent != 0, result: parser
>code can parse the string correctly
>c. Input string MyIdent!SubIdent != 0, result:
>parser code can parse the string correctly
>d. Input string MyIdent!SubIdent!= 0, result:
>parser code throws out an exception, and
>complains the equal sign is unacceptable.
I think this should work:
IDENT
options {testLiterals=true;}
: IDENTATOM (('!' ~'=') => '!' IDENTATOM)*
;
You *might* need an extra set of parentheses (ie.
"~('=')"); I think I remember reading that ANTLR
v2 was a bit more strict about this than v3 is.
More information about the antlr-interest
mailing list