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

Kieran Simpson kierans777 at gmail.com
Sun Mar 7 19:25:49 PST 2010


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
>
>
>   


More information about the antlr-interest mailing list