[antlr-interest] Code Generation Instruction Ordering / Order of Execution of Rules Actions

Kevin J. Cummings cummings at kjchome.homeip.net
Tue Nov 2 11:37:10 PDT 2010


On 11/02/2010 12:16 PM, Amr Muhammad wrote:
> Hello,
> 
> I was wondering if it is possible to execute actions for rules, that calls
> other rules, before executing the actions for the rules, that were called.

If I understand you correctly, yes.  You can put an action block after
any matching token or sub-rule.

> Let's say I have the following grammar:
> ---------------------------------------------------------------
> block_rule: BLOCK block_name
	{ // generate your block name stuff here }
> '{'
>     property1_rule
> 
>     NEWLINE
> 
>     property2_rule
> 
> '}'
	{ // Then any remaining actions can go here (after propert1_rule and
property2_rule have been run }

> property1_rule: property1= value1 { // generate block of statements say s1 }
> ;
> property2_rule: property2= value2 { // generate block of statements say s2 }
> ;
> 
> block_name: ID;
> property1: ID;
> value1: ID;
> property2: ID;
> value2: ID;
> ---------------------------------------------------------------
> 
> I would want the actions for rule block_rule to get executed before the
> actions of property1_rule or
> property2_rule
> 
> I am trying to do sth like:
> -----------------------------------------
> block_object  block_name;
> block_name.property1= value1;
> block_name.property2= value2;
> -----------------------------------------
> but what i get is:
> -----------------------------------------
> block_name.property1= value1;
> block_name.property2= value2;
> block_object  block_name;
> -----------------------------------------

My suggestion should get you what you want.

> Just in case it matters, what i am actually doing is using the parser
> grammar to generate an AST, and then using a tree grammar to walk the tree.

> Thank you for your time :)
> Best Regards,
> 
> Amr Muhammad
> Cairo Univ. Computer Eng. Grad.
> twitter:@amrmuhammad <http://twitter.com/amrmuhammad>

Good luck, happy parsing!

-- 
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list