tags:

views:

20

answers:

1

I'm working on an ANTLR grammar that looks like...

A : B+;

...and I'd like to be able to perform an action before and after each instance of B. For example, I'd like something like...

A : A {out("Before");} B {out("After");}
  | {out("Before");} B {out("After");};

So that on the input stream A B B I would see the output...

Before
After
Before
After

Of course the second example isn't valid ANTLR syntax because of the left recursive rule. Is there a way to accomplish what I want with proper ANTLR syntax?

I should also mention that there are other ways of reaching the B rule so simply surrounding the B rule with before and after won't work.

+1  A: 

Doesn't something like

A : ({out("Before");} B {out("After");})+;

work?

jpalecek
Worked perfectly. Thanks.
Pace