[antlr-interest] (newbie problem)reference to rewrite element name without re...

Kay Roepke kroepke at classdump.org
Mon Jul 9 17:16:27 PDT 2007


On Jul 10, 2007, at 1:53 AM, Randall R Schulz wrote:

> On Monday 09 July 2007 16:43, ali azimi wrote:
>>   I apologize for my grammar being cluttered. I will try to use
>> underscore more often.
>
> That's soooo 1980s! Get with the interCap / camelCase program!!

Then watch with horror at the classnames ANTLR generates for rule  
returns :P

>>   Do I have to write different rewrite rules for each sub-rule like
>> the followings?
>>
>>   signal_list_item
>>               :signal_identifier-> ^(signal_identifier)
>>               |signal_list_identifier-> ^(signal_list_identifier)
>>               |timer_identifier-> ^(timer_identifier)
>>               ;

In this case you don't actually need to use rewrite rules. The tree  
operators are
usually more concise, though a bit less explicit, IMHO. On the other  
hand, sometimes
they are really nifty :)

signal_list_item
               : (signal_identifier^
		|signal_list_identifier^
		|timer_identifier^
		)
	      ;

>>   Or this one is correct? (what is the difference?)
>>
>>   Rule2 signal_list_item
>>
>>               :( signal_identifier
>>                |signal_list_identifier|timer_identifier)
>>               -> ^(SLIST[$signal_list_item.start ]  
>> signalidentifier signal_list_identifier
>>                    timer_identifier)
>
> The answer depends on the rewrite you need to perform, but it looks  
> like
> the answer is probably "yes," in this case. Offhand, my guess would be
> that in most cases, you'll be creating a separate rewrite for each  
> distinct
> right-hand side.

Well, the last one doesn't give you a really stable tree, does it?  
I'm not actually sure it would work at all.
Don't know off-hand how the rewrite mechanism copes with references  
being null and stuff.
So to sum up, the first one is probably correct (=in accordance with  
what you meant to do).

In any case, every rewrite always sets the return tree for the entire  
rule. That's why you can refer to the
existing tree (that's been built up by previous rewrites) with  
$rulename in the rewrite.

cheers,
-k

-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list