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

tcorbat at hsr.ch tcorbat at hsr.ch
Sat Apr 17 00:39:01 PDT 2010


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


More information about the antlr-interest mailing list