Is it safe to invoke rules from within actions? Could this ever upset the flow of the parser?
Created May 4, 2012
a : A | x ; x : {y();} ; y : Y ;
Clearly in this case x matches nothing as far as ANTLR is concerned even though we know it matches y(), or Y, every time.
Now, for your particular example, where you reference a rule and then have an action that determines when to use tail recursion: it may or may not work. If the lookahead computation for "rule" needs to see past the "anotherRule" reference, it will probably not generate a valid parser. Seeing past "anotherRule" means it should look into "rule", but it can't since it's in an action.
Fortunately, there is a way to solve this problem by simply telling ANTLR what you really mean by the action. You mean, optionally go back to "rule" if some condition is true. Try the following
rule : anotherRule ({condition}? rule)? ;
In this case, the lookahead and the semantic predicate condition will determine whether or not to use tail recursion.