[antlr-interest] Manual Tree Walking Vs. Tree Grammars

Mike Matera mike.matera at xilinx.com
Tue Nov 2 16:39:55 PDT 2010


Hi Amr,

I have used ANTLR tree parsers as a runtime for my scripting languages 
and I quite like it.  Is this what you're tying to do?  Here's what I 
mean (this code is simplified for clarity):

<<tree parser definition>>

exec: ^(statement*)

statement
  : ^('+' a=NUM b=NUM) {push(a+b);}
  | ^('*' a=NUM b=NUM) {push(a*b);}
  | ^('if' statement then_block=. else_block=.) {
    if (pop()) {
      exec(then_block);
    }else{
      exec(else_block);
    }
  }
  ;

I disagree with Pat (the last poster) that it's awkward to maintain.  In 
practice when you add a new construct to your language you'll have to 
update your runtime for that construct anyway.  For me having it all in 
one place is handy.

However, you may be running into this bug:

http://www.antlr.org/jira/browse/ANTLR-418

I filed it against ANTLR 3.2 and I don't know if it's fixed.  The 
problem is that your 'then_block' will contain your 'else_block' so 
you'll have the situation where:

then_block = {then, else}
else_block = {else}

You will always see your 'else_block' run.  If you seem to be having 
that problem downgrade to ANTLR 3.1.1.

Cheers
./m




Amr Muhammad wrote:
> Hello,
>
> In this post : http://www.antlr.org/pipermail/antlr
> -interest/2010-October/039862.html
> The following was mentioned:
>
>   
>> Also, remember to only call external Helper methods from your parsers/tree
>> walkers. Do not embedded any code other than the calling code and pass the
>> whole tree or token pointer. This means your calls won't care what gets done
>> by the helper API and the helper API will not care how the parsers decided
>> to call it. Anything else is an unmaintainable mess.
>>
>>
>>     
> So,
> does this imply that it is easier to walk the AST manually rather than
> embedding actions in the tree grammar ?
>
> Based on what i have tried till now, it seems that getting the embedded
> actions to work, as expected, is not easy. So, I'd like to know if there is
> some benefit that I would get out of writing embedded actions in tree
> grammars?
>
> Also, there is this post that seems to advocate manual tree walking:
> http://www.antlr.org/article/1170602723163/treewalkers.html
>
> So, I'm confused as to whether continue trying to make tree grammars do what
> I want, or switch to manual tree walking. Appreciate your guidance...
>
> Thank you for your time :)
> Best Regards,
>
> Amr Muhammad
> Cairo Univ. Computer Eng. Grad.
> twitter:@amrmuhammad <http://twitter.com/amrmuhammad>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>   

This email and any attachments are intended for the sole use of the named recipient(s) and contain(s) confidential information that may be proprietary, privileged or copyrighted under applicable law. If you are not the intended recipient, do not read, copy, or forward this email message or any attachments. Delete this email message and any attachments immediately.




More information about the antlr-interest mailing list