[antlr-interest] TreeDL news: tutorial, new releases, mailing lists
Alexey Demakov
demakov at ispras.ru
Fri Oct 29 08:26:52 PDT 2004
From: "Tiller, Michael (M.M.)" <mtiller at ford.com>
> > From: Alexey Demakov [mailto:demakov at ispras.ru]
> > Subject: Re: [antlr-interest] TreeDL news: tutorial, new releases,
> mailing
> > lists
> >
> > > Well, regarding the visitor pattern there are really two issues
> here.
> > > First is the interface of the visitor. The interface defines what
> > > operations are involved. This is where I think the "enter" and
> "leave"
> > > methods would be preferable to a "visit" method for anything that
> has
> > > children. This has nothing to do with the tree walking itself (i.e.
> the
> > > 'accept' method invocations), it just provides a comprehensive list
> of
> > > methods associated with each node type. Looking at the
> documentation
> > > you mentioned I see that TreeDL appears to do this part (although it
> is
> > > restricted to 'visit' methods).
> >
> > If you don't mind to write enter/leave in different classes, this
> pattern
> > can be
> > implemented using two visitors. Generated tree walker will be like
> this:
> >
> > class EnterLeaveTreeWalker implements TreeVisitor
> > {
> > TreeVisitor enterVisitor;
> > TreeVisitor leaveVisitor;
> >
> > // for each node
> > void visitNode( Node node )
> > {
> > node.accept( enterVisitor );
> > // walk children
> > node.accept( leaveVisitor );
> > }
> > }
>
> This isn't really what I had in mind. I don't want two visitors, I want
> the traversal (using only one visitor) to include actions for both
> entering and leaving the same node.
Then you can use alternative visitor interface, give me day or two before
you'll need it and I add appropriate plugin. :)
Note, number of methods in both cases is the same. Moreover, if you like to write
method in one class only, it can be adapted to my scheme as:
// manually developed class with actions
class EnterLeaveVisitor implements EnterLeaveVisitorInterface
{
void enterNode( Node node ) {}
void leaveNode( Node node ) {}
}
// generated
class EnterVisitor implements TreeVisitor
{
EnterLeaveVisitorInterface visitor;
visitNode( Node node ) { visitor.enterNode( node ); }
}
// generated
class LeaveVisitor implements TreeVisitor
{
EnterLeaveVisitorInterface visitor;
visitNode( Node node ) { visitor.leaveNode( node ); }
}
// generated
class EnterLeaveTreeWalker implements TreeVisitor
> For information on how SableCC
> handles the visitor pattern, you might take a look at:
>
> http://sablecc.org/thesis/thesis.html#PAGE52
TreeDL handles visitors exaclty the same way as described at pages 56-58.
It is used for tree inheritance. The only difference - Switchable is not
separate interface, accept() is directly in Node interface.
SableCC - TreeDL
Switch - Visitor
Switchable - Node
apply() - accept()
case...() - visit...()
> They use the terms "in" and "out" instead of "enter" and "leave". You
> can see some discussion of this specifically at:
>
> http://sablecc.org/thesis/thesis.html#PAGE60
Yes, it is very similar to my approach but uses inheritance instead of delegation.
I like to separate actions from tree walking completely, it is more flexible.
> > > The second part is the tree walking part. Here it is possible to
> write
> > > tree walkers automatically but you have to choose a pattern (e.g.
> > > depth-first).
>
> Discussion of the AST walkers in SableCC can be found here:
>
> http://sablecc.org/thesis/thesis.html#PAGE58
Ok, I'll add tree walkers generation. And will change generation of empty visitor
to use default action as described at this page, it is useful.
Regards,
Alexey
-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list