[antlr-interest] Quaestion about adding logic checks into the parsers grammer..

John Allen Green greenj at ix.netcom.com
Wed Sep 11 03:52:13 PDT 2002


Hi Kevin,

I'll try answering, but hopefully the experts will check my work. :-)

I think you are trying to do things in an LR kind of way that won't work
with Antlr's LL. You will want to move your semantic predicate up to name:

name
    :    {LT(1).get()->getText()=="a"}? name_a
    |    name_b
    ;


Since Terrence is going back to teaching, maybe he can ease back into it by
grading my answer. ;-)


Regards,
John
www.joanju.com




--On 10/09/2002 11:23 AM +0200 Kevin "MiNdCRY" Erath wrote:

> Hi,
> 
> after using flex/bison for months (if not years), i thought it's time to
> get used to a more modern lexer/parser :-)
> actually i am playing around with antlr to see what is possible and what
> not..
> 
> actually i try to figure out how i can add my own checks to the grammer, a
> pretty silly example (to help you understand what i want to try):
> 
> the lexer returns me a ID which is defined like ('a'..'z'|'A'..'Z')
> 
> now in the parser i want to have something simular to:
> 
> name
>     :    name_a // got a ID beginning with a
>     |    name_b // got a ID beginning with b
>     ;
> 
> name_b
>     :    /*only match if ID begins with b*/ ID
>     ;
> 
> name_a
>     :    /*only match if ID begins with a*/ ID
>     ;
> 
> for sure in this case it could be done in the lexer, and it's just a dang
> silly example...
> 
> i tryed to do:
> name_a
>     :    {LA(1)==ID && LT(1).get()->getText()=="a"}? ID
>     ;
> 
> or:
> 
> name_a
>     :    ({LA(1)==ID && LT(1).get()->getText()=="a"}?)=> ID
>     |    IGNORE_ME_TOKEN
>     ;
> 
> and:
> 
> name_a
>     :    i:ID {i->getText()=="a"}?
>     ;
> 
> but it seems that they all only help for semantical checks, how can i do
> such checks for the Syntax ?
> i want to use this with a Symbol Table, if a ID has been already used i
> want to use different rules etc...
> 
> thanks for your help,
> Kevin
> 
> "¨¨°º©o.,MiNdCRY,.o©º°¨¨"
> ------------------------------------------
>   www.game-designer.net
> ------------------------------------------
> 
> 
> 
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 
> 
>  



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list