[antlr-interest] ANTLR3.4: help on @init and @after

Sam Harwell sam at tunnelvisionlabs.com
Tue Aug 21 07:45:45 PDT 2012


For guaranteed cleanup, you should be able to use a finally block which is
placed after the ';' as follows.

rule : ... ;
finally {
...
}

--
Sam Harwell
Owner, Lead Developer
http://tunnelvisionlabs.com

-----Original Message-----
From: Francis ANDRE [mailto:francis.andre.kampbell at orange.fr] 
Sent: Tuesday, August 21, 2012 8:56 AM
To: Sam Harwell
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] ANTLR3.4: help on @init and @after

Hi Sam

I understand the point on the @init section which is fine for me, but my
question was more related to the @after section which I would consider as
the @init, i.e., not beeing conditioned by the state.backtracking test.
IMHO, it would be more coherent....

I tried to use @after {{  }} but that' produces a syntax error.....

Le 21/08/2012 14:46, Sam Harwell a écrit :
> The @init section is frequently used to declare and initialize 
> variables which are used in predicates and/or actions later in the 
> rule. If these were placed inside a generated (state.backtracking==0) 
> block, the scoping rules of the language would leave you with no way 
> to declare variables and would not initialize variables if they are 
> used in semantic predicates and/or forced actions.
>
> --
> Sam Harwell
> Owner, Lead Developer
> http://tunnelvisionlabs.com
>
>
> -----Original Message-----
> From: Francis ANDRE [mailto:francis.andre.kampbell at orange.fr]
> Sent: Tuesday, August 21, 2012 2:33 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] ANTLR3.4: help on @init and @after
>
> Hi
>
> This parser rule
>
> foo
>       returns[Token = null]
> @init
> { allowFOO(true); }
> @after
> { allowFOO(false); }
> :    FOO { l = $FOO; }
> ;
>
> is translated into this snippet
>
>            allowFOO(true);
>           try {
>               {
>               FOO1=(Lexem)match(input,FOO,FOLLOW_FOO_in_FOO1247); if
> (state.failed) return l;
>               if ( state.backtracking==0 ) { l = FOO1; }
>               }
>               if ( state.backtracking==0 ) { allowFOO(false); }
>
> Why the @after action is conditioned by a backtracking equal to 0, 
> while the @init one is not??
>
> FA
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>



More information about the antlr-interest mailing list