[antlr-interest] Newbie question to lookahead function

William Cai wenliangcai at yahoo.ca
Wed May 21 06:21:30 PDT 2008


Yes, it works. Thank you so much! 


----- Original Message ----
From: Gavin Lambert <antlr at mirality.co.nz>
To: Wenliang Cai <wenliangcai at yahoo.ca>; antlr-interest at antlr.org
Sent: Wednesday, May 21, 2008 9:07:17 PM
Subject: Re: [antlr-interest] Newbie question to lookahead function

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.


      __________________________________________________________________
Get a sneak peak at messages with a handy reading pane with All new Yahoo! Mail: http://ca.promos.yahoo.com/newmail/overview2/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080521/d5973992/attachment-0001.html 


More information about the antlr-interest mailing list