[antlr-interest] Rewrite Rules (ANTLR v3)

Liehann Loots liehannl at gmail.com
Wed Jul 2 15:03:25 PDT 2008


Hi,

I'm relatively new to ANTLR and I was hoping I could get some help
with rewrite rules and tree construction.

I'm essentially building a simple scripting language grammar. I have a
rule for an expression, that can be dereferenced, and invoked.

expr	:	num_expr (((PERIOD)! id+=IDENT)+ ((LT_BRKT)! prm=param_list
(RT_BRKT)!)?)?;

I want the tree to be rooted at INVOKE if param_list is present, DEREF
if IDENT is present, and at num_expr if neither of param_list or IDENT
is present. I've tried sub rules with the ^ operator but then the
children nodes end up in the wrong order.

With conditional rewrite rules it would look something like this, but
I'm getting errors doing this:
	-> {$id == null && $prm == null} ^(num_expr)
	-> {$id == null && $prm != null} ^(INVOKE num_expr param_list?)
	-> {$id != null && $prm == null} ^(DEREF num_expr IDENT*)
	-> {$id != null && $prm != null} ^(INVOKE ^(DEREF num_expr IDENT*)
param_list?);

I have gone through what material I could find regarding tree
construction so either I didn't click something or I just didn't find
the right information.

Any help would be greatly appreciated.

Regards,
Liehann


More information about the antlr-interest mailing list