[antlr-interest] Newbie question to lookahead function

Wenliang Cai wenliangcai at yahoo.ca
Wed May 21 03:11:11 PDT 2008


Hi list,

 

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. 

 

Then I tried to wrap IDENT in the similar way (see below),
"MyIdent!SubIdent!= 0" but still can not get parsed properly. If possible,
could anyone show me the correct grammar with the logic - "if the
exclamation sign ('!') followed by equal sign, the '!' sign is not a part of
the identifier."? Your help is greatly appreciated!

 

 

IDENT

options {testLiterals=true;}

    : (IDENT2 "!=") => IDENT2

    | IDENT2

    ;

 

IDENT2

options {testLiterals=true;}

    : (IDENTATOM "!=") => IDENTATOM

    | IDENTATOM ('!' IDENTATOM)*

    ;

    

IDENTATOM

options {testLiterals=true;}

    : ('_'|'a'..'z')('#'|'_'|'a'..'z'|'0'..'9')*

    ;

 

 

Thanks,

William

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080521/3525b653/attachment.html 


More information about the antlr-interest mailing list