[antlr-interest] Fwd: Rewrite Rules (ANTLR v3)

Liehann Loots liehannl at gmail.com
Wed Jul 2 15:24:40 PDT 2008


Sorry, should have kept this on the mailing list.

My second error was I needed to drop the '$'s in the conditional
statements. My guess is when you insert those conditions you're
actually inserting bits of Java code (anyone feel free to correct me
if I'm wrong). I'm particularly interested if these conditions are
portable over different platforms (Java / C++ / Ruby etc)

Also, if anyone else has a better solution on how to build this tree,
I'd love to hear it.

For posterity my final rule is as follows:

expr	:	
	num_expr (((PERIOD)! id+=IDENT)+ ((LT_BRKT)! prm=param_list (RT_BRKT)!)?)?
	-> {id == null && prm == null}? ^(num_expr)
	-> {id == null && prm != null}? ^(INVOKE num_expr $prm?)
	-> {id != null && prm == null}? ^(DEREF num_expr $id*)
	-> ^(INVOKE ^(DEREF num_expr $id*) $prm?);

Regards,
Liehann
---------- Forwarded message ----------
From: Liehann Loots <liehannl at gmail.com>
Date: Thu, Jul 3, 2008 at 12:13 AM
Subject: Re: [antlr-interest] Rewrite Rules (ANTLR v3)
To: Johannes Luber <jaluber at gmx.de>


Thanks,

I actually picked that up after posting the message. It also seems the
last rule may not have any conditionals, which makes sense as there
has to be a "default" rule.

I'm now getting a "missing attribute access on rule scope : prm" error.

On Thu, Jul 3, 2008 at 12:09 AM, Johannes Luber <jaluber at gmx.de> wrote:
> 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