[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Grammatica-users] GetParent question
From: |
Per Cederberg |
Subject: |
Re: [Grammatica-users] GetParent question |
Date: |
Sun, 14 Dec 2003 01:59:37 +0100 |
On Sat, 2003-12-13 at 19:26, Luc Morin wrote:
> I need to know if a property(2001) has an
> IndirectProperty(2003) parent, as the treatment is
> different. The problem is that within ExitProperty(),
> the Production cannot access its parent node. A call to
> node.GetParent() always returns null.
>
> Is this a normal behavior?
Yes, this is the expected and poorly documented
behavior. See the class comment for the Analyzer base
class for a minimal explanation. To make that short
story a bit longer, I'll elaborate that a bit more
here:
First thing that is important to note is that the
Analyzer class is called *during* the parsing. This
makes the parser more memory efficient, as you can
prune the parse tree (i.e. skip nodes) while it is
being created. This is a powerful way to manipulate
the parse tree, but it also causes some limitations
as the Analyzer is actually *building* the parse
tree while at the same time processing it.
Second thing that is important to note is that the
parse tree is analyzed in a somewhat bottom-up
fashion (the class comment is misleading there). That
is, child nodes will be added to a parent node once
they have been completely analyzed. The result from
the exitX() method is what will be added.
This means that children cannot access parent nodes,
while parents may access completely analyzed children
in their childX() or exitX() methods.
I'll add a mental note to improve the documentation
on these topics.
> How can I access the parent node of a Production ?
The simple answer to your question is... you can't.
Now, as I guess that answer wasn't so satisfying, some
alternative solutions might be in order...
1. Change the exitProperty() method to add node values
containing whatever information you need from the
property. Then add a Start production in the grammar
like this:
Start = Property ;
Now you can process indirect properties in
exitIndirectProperty() and the top ones in exitStart().
2. Create an instance variable "int propertyNesting = 0"
that you increase by one in enterIndirectProperty()
and decrease by one in exitIndirectProperty(). When it
equals zero you are at the top level. (NOTE: This
solution assumes a certain tree traversal order that
is NOT guaranteed, just happens to work.)
3. ...Hmm... Can't think of a third way right now, but
there surely must be one...
I'd recommend #1 if applicable in your case. The general
idea when implementing an Analyzer is to have the exitX()
methods return a node with semantical values attached
(see Node.getValue() and Node.addValue()). These values
should contain data that can be used by parents for
further analysis and processing. You might want to check
out the ArithmeticCalculator.java in the
test/src/java/net/percederberg/grammatica/test directory
for some examples of this.
Ok, this was a long answer to a short question. I hope
at least some part of it was understandable enough to
help you along. Please write back if you have further
comments/questions/doubts/whatever!
Cheers,
/Per