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

Kieran Simpson kierans777 at gmail.com
Mon Mar 8 00:04:27 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.
>
You are correct Gokulakannan.  I tried the semantic predicate approach
as well (just to see if it was a better approach) and in some of the
"synpred fragment" functions generated by the C target, I got compiler
errors as the functions was trying to use the rule parameter (in this
example, isRuleA) when the rule argument wasn't passed to the fragment
function.  The fragment function didn't even declare a parameter in the
signature to match the rule parameters.  I eventually used a combination
of parameter passing and target language if conditions.  It didn't add
to much to the grammar.
> 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
> <mailto: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