[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [Grammatica-users] GetParent question
From: |
Luc Morin |
Subject: |
RE: [Grammatica-users] GetParent question |
Date: |
Sun, 14 Dec 2003 15:24:29 -0500 |
Thanks a lot for the clarification.
Adding a Start Production did the trick, though at first I had put it after the
property Production in the grammar file, so it wasn't "caught" at all by the
Analyzer. Putting it before the Property Production solved it.
I didn't think the order of appearance in the grammar was of any importance,
but that is most certainly caused by my total lack of knowledge in this field
:-)
Anyway, my Eplan 21 Format String analyzer now works like I intended at first,
thanks to Grammatica !
Luc Morin
Electrical Designer
Wulftec / M.J. Maillis Group
Ph.: (819) 838-4232 ext. 232
Fx.: (819) 838-5539
www.wulftec.com
www.maillis.gr
<< Avis de confidentialité >>
Ce courrier électronique ainsi que tous les documents qui y sont attachés sont
de nature privilégiée et confidentielle et sont destinés à l'utilisation
exclusive du ou des destinataire(s) ci-haut mentionné(s) . Toute autre personne
ayant reçu ce message par erreur est priée de bien vouloir s'abstenir de
divulguer, distribuer ou reproduire ce message et/ou son contenu. Si le
destinataire ne peut être rejoint ou vous est inconnu, nous vous prions
d'informer immédiatement l'expéditeur de ce présent message par retour de
courriel en plus d'effacer ce message et détruire toutes les copies. Merci.
<< Confidentiality Notice >>
This e-mail message and any files transmitted with it are confidential, may be
privileged and are intended for the exclusive use of the addressee(s). Any
other person is strictly prohibited from disclosing, distributing or
reproducing it. If the addressee cannot be reached or is unknown to you, please
inform the sender by return e-mail immediately, delete this e-mail message and
destroy all copies. Thank you.
-----Original Message-----
From: Per Cederberg [mailto:address@hidden
Sent: Saturday, December 13, 2003 8:00 PM
To: address@hidden
Subject: Re: [Grammatica-users] GetParent question
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
_______________________________________________
Grammatica-users mailing list
address@hidden http://mail.nongnu.org/mailman/listinfo/grammatica-users