[antlr-interest] Can't create AST Node?
Tiller, Michael (M.M.)
mtiller at ford.com
Thu Jul 8 10:52:18 PDT 2004
> From: Monty Zukowski [mailto:monty at codetransform.com]
> Subject: Re: [antlr-interest] Can't create AST Node?
>
> On Jul 8, 2004, at 7:54 AM, Tiller, Michael ((M.M.)) wrote:
>
> > Actually, I've reached the end of the rope with heterogenous AST
types
> > in ANTLR. It was such a pain to do this in C++ and I see a whole
new
> > horizon of problems on the Java side. I'm just going to bite the
> > bullet
> > and turn "buildAST" off and make the tree myself. It is just too
much
> > trouble (for me) to deal with all the inheritance and casting
issues.
> > The downside is that this tosses tree parsing right out the window
but
> > I
> > haven't really used the tree parser stuff anyway so I'm not losing
much
> > there.
>
> Not sure if you answered this before, but why do you want to use
> heterogenous trees?
Sort of a, "Tell me you need a TV in your golf cart" kind of question?
:-)
I'm hard pressed to provide a more defendable answer than "because that
is the way I think". It's sort of like the object models between
Smalltalk, Java, C++ etc. Some people are comfortable with the
"everything is a subclass of Object" way of doing things while others
say "why do you needlessly abstract things into a common base class".
When I think about the various structures in the language I'm parsing, I
think of them as distinctly different entities (declarations, type
definitions, expressions) and not as just a bunch of homogenous nodes.
That is just the way I think about it. I feel much more comfortable
working with an object knowing what it is and what I can expect from it
without having to do a bunch of run-time type interrogation and casting.
Think about it this way. Imagine trying to program in Java or C++ and
not ever using the "." (as in computer.drives[1].head.position) and
instead having to interrogate every object using only "getFirstChild()"
and "getNextSibling()" and having only type and text information.
Imagine trying to express what I just typed? (i.e.
computer.drives[1].head.position) You get something like:
Object getHeadPosition(Object computer, int drive_number) {
int found = 0;
// Search for all drives associated with computer
for(Object d=computer.getFirstChild();d!=null;d=d.getNextSibling()) {
// Is this a drive (or some other member of computer)?
if (d.type==Drive) {
found++;
if (found==drive_number) {
for(Object h=d.getFirstChild();h!=null;h=h.getNextSibling()) {
if (h.type==Head) {
for(Object p=h.getFirstChild();p!=null;p=p.getNextSibling())
{
if (p.type==Position) {
return p;
}
}
throw NoPosition;
}
return d;
}
throw NoHead;
}
}
}
throw CantFindDrive;
}
Yikes! Isn't "computer.drives[1].position.head" much simpler and
statically checkable?
Granted, tree parsers make this a lot easier, but I'm not using tree
parsers (and perhaps I'm just dense, but I don't think they will help me
in what I'm trying to do).
> The main reason I've seen is for people who want
> to access the node objects directly from C++ instead of through a tree
> grammar. I've only recently tried to use them with C++ on a contract
> and I agree that antlr's tree building scheme is not easy to
> comprehend.
Let's forget about C++. I'm doing this in Java (although many of my
previous comments *were* directed at the C++ side). I switched to Java
in the hopes that things would be a lot easier but it didn't turn out
that way.
> The more diverse uses we collect the better off we are in the future.
> You're the one who wrote up a whole bunch of notes on tree building
> already, right?
Yes, I did write up a bunch of stuff before. I'm not sure how useful it
was but I will try to write up some additional material once I get
something I'm happy with (on the off chance that other people are
interested in an alternative approach).
> It really helps.
>
> Monty
--
Mike
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