[antlr-interest] @init actions executed during lookahead, @after actions not

Sam Harwell sharwell at pixelminegames.com
Sat Apr 17 06:20:45 PDT 2010


Now that I think about it, it seems a lot more likely that the code from
the @after block would have instead been an action if the intent was to
only execute as an action (when not backtracking). However, IIRC some
items (maybe it's the token start/stop indices) are only set after the
inline actions run, so if you want to work with them you have to use the
@after{} block instead. I just checked my grammars and found that I
currently have *both* possibilities at different locations - some assume
that the code always runs, and others assume it's only during
backtracking.

Maybe we should allow a @finally{} block immediately following where the
@init{} block goes?

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of tcorbat at hsr.ch
Sent: Saturday, April 17, 2010 2:39 AM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] @init actions executed during
lookahead, at after actions not

Hi Ron,

I think this behavior is as intended. The @init block is intended to
declare local variables. Therefore, it will always be executed. 
You could move the action, manipulating your stack, out of the @init
block into an action inside the rule. Then, it would not get executed
during lookahead.
I don't know if it is favorable, but if this manipulation of the stack
is required in the subrules to correctly decide on alternatives, you
could leave the action in @init and put the cleanup action (form @after)
to the "finally" block, which will always get executed, regardless of
the backtracking state.

I hope this helps.
Thomas


________________________________________
Von: antlr-interest-bounces at antlr.org [antlr-interest-bounces at antlr.org]
im Auftrag von Ron Hunter-Duvar [ron.hunter-duvar at oracle.com]
Gesendet: Samstag, 17. April 2010 00:59
An: antlr-interest at antlr.org
Betreff: [antlr-interest] @init actions executed during lookahead,
@after actions not

Hi,

I just ran into something a little odd. I'm using @init actions in some
parser rules to stack some information and @after to pop it again. In
the generated Java code, the @after action gets wrapped in an "if (
state.backtracking==0 ) {...}", so that it only gets executed when other
actions are being executed, not during lookahead. This is what I
expected. But I noticed that the @init actions are executed
unconditionally, including during lookahead. I didn't expect this. The
result was a lot of junk on the stack when it went into a dfa. The fix
was easy enough, just checking state.backtracking myself. But I was
wondering if this is an Antlr bug or if it's supposed to work this way.

Ron

--
Ron Hunter-Duvar | Software Developer V | 403-272-6580
Oracle Service Engineering
Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5

All opinions expressed here are mine, and do not necessarily represent
those of my employer.


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

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