[antlr-interest] Rewrite separators to root nodes.

Craig Main craig at palantir.co.za
Mon Aug 4 09:01:20 PDT 2008


Hi Gavin,

I did originally have it like this.
I ended up struggling to get the template output to work.
My templates are not also recursing so it get's tricky.

rules
   : ^('or' a=rules b=rules) -> ORTEMPLATE(a.returnvalue,b.returnvalue)
   | ^('and' a=rules b=rules) -> ANDTEMPLATE(a.returnvalue,b.returnvalue)
   | r=rule -> RULETEMPLATE(r.returnvalue)
   ;

Only the highest level precedence template seems to get called (it's obvious
why) and I cannot find an example that recurses using templates?
That's why I tried a simple list rather than a complete tree. If I can get
the tree to work, then fine but it's tricky.

What does the template have to look like to make it recurse as well.

Regards
Craig.

-----Original Message-----
From: Gavin Lambert [mailto:antlr at mirality.co.nz] 
Sent: 04 August 2008 12:01 PM
To: craig at palantir.co.za; antlr-interest at antlr.org
Subject: Re: [antlr-interest] Rewrite separators to root nodes.

At 21:44 4/08/2008, craig at palantir.co.za wrote:
 >rules:	rule (('and'|'or') rule)*
 >                ;
 >rule
 >:	ROOT xn+=xname (':' xn+=xname)+
 >	-> ^(ROOT xname*);
 >
 >I want to use tree rewrites to produce a tree that matches what 
i
 >have below.
 >
 >rule:    ^(ROOT 'and' xname*)
 >        |^(ROOT 'or' xname*)
 >        |^(ROOT xname*)

It might be possible to do that, but it'd be much easier to 
generate a slightly different structure.  If you change the 
'rules' rule to this:

rules:	rule (('and'|'or')^ rule)*;

It will produce trees that can be matched like this:

rules
   : ^('or' a=rules b=rules)
   | ^('and' a=rules b=rules)
   | rule
   ;
rule
   : ^(ROOT xname*)
   ;

I think that's much easier to deal with :)





More information about the antlr-interest mailing list