[antlr-interest] Rewrite Rules (ANTLR v3)
Johannes Luber
jaluber at gmx.de
Wed Jul 2 15:09:24 PDT 2008
Liehann Loots schrieb:
> 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
>
It looks like you forgot to add '?' after the action blocks:
-> {$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*)
Johannes
More information about the antlr-interest
mailing list