[antlr-interest] Using previously matched parser rule in decision making

Gokulakannan Somasundaram gokul007 at gmail.com
Sun Mar 7 22:55:53 PST 2010


What Jim is suggesting is something like this

ruleA: ruleB[true];

ruleD: ruleB[false];

ruleB[boolean isRuleA]:
         {isRuleA}?  .....
         |      .....
;

Usage of semantic predicates. But i think there is an issue with that. From
ruleA / ruleD, if you decide to do a look ahead like LA(n), and if that
lookahead goes to B, then this won't carry the boolean parameter and you
might face some issues and the error thrown will not be intuitive.

Ideal way according to me is

ruleA: ruleB_A;

ruleD: ruleB_D;

ruleB_A :....;

ruleB_D : ....;

If there are lot of things that are common, factorise them as a seperate
rule / seperate actions. Hope my suggestion was helpful.

Thanks,
Gokul.


On Mon, Mar 8, 2010 at 8:55 AM, Kieran Simpson <kierans777 at gmail.com> wrote:

> Thanks for the suggestions.
>
> I had considered the parameter approach, I was curious to know if there
> was a smarter way.
>
> John B. Brodie wrote:
> > Greetings!
> >
> > On Mon, 2010-03-08 at 13:50 +1100, Kieran Simpson wrote:
> >
> >> I have
> >>
> >> ruleA: ruleB;
> >>
> >> ruleC: ruleB;
> >>
> >> ruleB: ruleD;
> >>
> >> In ruleB I want to different target language actions to execute based on
> >> whether it was ruleA or ruleC that was previously matched.  If my
> >> understanding of syntatic/semantic predicates is correct, they only look
> >> forwards, not backwards.
> >>
> >> Is there a way (without refactoring the grammar) to in rule B know which
> >> rule it was invoked from (A or C) and make decisions accordingly?
> >>
> >
> > Off the top of my head, pass a parameter.....
> >
> > ruleA : ruleB[true];
> > ruleC : ruleB[false];
> > ruleB [boolean fromA] : ruleD
> >   { if( fromA )then
> >        ....do this stuff....
> >     else
> >        ....do that stuff....
> >    };
> >
> > (the above probably is not precisely the correct meta-syntax, but
> > hopefully you get the idea...)
> >
> >    -jbb
> >
> >
> >
>
> 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