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

Kirby Bohling kirby.bohling at gmail.com
Tue Nov 2 17:49:37 PDT 2010


On Tue, Nov 2, 2010 at 5:59 PM, Patrick Niemeyer <pat at pat.net> wrote:
>
> I had been building up the experience to post something thoughtful on this topic, but I'll just chime in now :)
>
<snip...>
> I'd be open to being proved wrong on the tree grammar front... if anyone has examples of how it simplified their code, etc.
>
>

I maintain several tree grammars (for various phases of my
processing).  I have one that is absolutely pristine (no code of any
type allowed, it is a mechanical one-to-one translation from the
parser grammar as described in the ANTLR book), that I then import
into all of the others tree grammars I have.  I do have to keep the
one pristine one, and the parser grammar in sync by hand but it is
trivial to tell from the diff if I screwed that up.  For each phase of
my processing I only have to keep it in sync if that phase actually
touches the rules I modified in my parser.  So I was going to be
touching it either way, because they are related to the area of
change.

I try and keep each Tree Grammar well focused, and handling
essentially one pass/phase of my problem.  I don't do anything hugely
sophisticated, so it might not be useful in a full blown
compiler/interpreter setup.

Prior to doing that, I had to touch all the phases every time I
touched the tree grammar.  It'd be really cool if Antlr had a standard
tool go kick out the tree walker for the grammar that I could use as
the basis of my import.  For all I know the tool does exist, and I
just don't know it.

No idea if that helps Pat, but it's the approach I've used in the past
with success.

Thanks,
   Kirby


> Pat Niemeyer
>
>
> On Nov 2, 2010, at 2:48 PM, 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
>>
>
> Pat
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list