[antlr-interest] action execution at the rule prediction stage

Jiri Palecek jpalecek at web.de
Sat Apr 25 17:23:42 PDT 2009


Xie, Linlin wrote:
> Dear All,
>
> I have a question about the time of execution of the actions put inside
> or at the end of the rules. I can see from the generated lexer or parser
> code that the actions (at the end of the rule) are inserted after the
> match of the whole rule. But I would like to know if any actions (in the
> middle or at the end) would be executed before the match of the rule?
> i.e. at the Look Ahead (Prediction) stage, would the match of a part of
> the rule trigger any actions of the subrule matched?
>
> For example:
>
> Rule : Rule1| Rule2;
>
> Rule1 : Rule3* Rule4;
>
> Rule2 : ( Rule3 Rule3 )* Rule5;
>
> Rule3 : AnyRule { AnyAction; };
>
> Rule4 : xxx;
>
> Rule5 : yyy;
>
> When trying to find out if the input matches Rule1 or Rule2, will the
> AnyAction be executed? Or if the action is in the middle of the rule,
> will it be executed before the match of the full rule?

No. No actions are executed during prediction stage, either the prediction is 
done by DFAs (which ignore actions) or you use backtracking (which is probably 
your case), in which case the actions are explicitly inhibited by code like

if(state.backtracking==1)

etc. Cf your generated code.

Regards
     Jiri Palecek


More information about the antlr-interest mailing list