[antlr-interest] Can't see the wood (@init and @after)

Paul Keir paul at paulkeir.com
Wed Jul 18 03:12:17 PDT 2007

I know you're dying to hear the conclusion of this one :) Well here it 
is, and I'm delighted.

I'd always assumed that in a rule, the Actions inside an '@init' would 
be called as often as Actions after the start colon (in a rule without 
ORs). In the following example, from ANTLR's C example grammar, I had 
assumed that "a" would be printed as often as " b":

@init { System.out.println("a"); }
    : { System.out.println(" b"); } assignment_expression (',' 

This isn't true. "a" is printed many more times than " b". I had been 
happily updating many (global) member variables inside @init and @after, 
to recently devastatingly confusive results.

I find putting the Actions within the rules quite messy, so that was why 
I'd 'standardised' on using mainly @init and @after. I will now begin to 
change everything. I'm surprised it worked for so long.


More information about the antlr-interest mailing list