[antlr-interest] Parsing optional component in Tree Walker
Bryan Ewbank
ewbank at gmail.com
Tue Apr 12 07:12:24 PDT 2005
Yes, it's quite flexible -- almost /too/ flexible for me.
I've found that I tend to avoid the ()* and ()? containers in ANTLR in
favor of explicit null alternatives simply because it forces me to
think about the alternatives:
(a)? => (a | /*null*/ )
(a)* => ( (a)+ | /*null*/ )
At the same time, the other advice (to make the tree regular or use a
different root node) is better in some senses because it keeps the
code simpler. There's mention of this in the ANTLR manual regarding
unary minus versus binary minus - better to have UMINUS as a distinct
root to avoid having to do some hairy stuff each time you walk the
tree:
/* easy and obvious */
#(UMINUS e1) { ... unary_op(e1); }
#(MINUS e1 e2) { ... binary_op(e1,e2); }
/* huh? */
#(MINUS e1
( /*null*/ { ... unary_op(e1); }
| e2 { ... binary_op(e1,e2); }
)?
)
On Apr 12, 2005 8:39 AM, Peter Kronenberg <PKronenberg at technicacorp.com> wrote:
> Thanks. That's perfect. I didn't realize the syntax was so flexible
>
> Peter Kronenberg
> Software Engineer
> (703) 885-1222
> pkronenberg at technicacorp.com
>
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Bryan Ewbank
> Sent: Monday, April 11, 2005 3:25 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Parsing optional component in Tree Walker
>
> What's wrong with doing what you said - an optional component:
>
> #(ROOT A B
> ( C
> { ... handleAB (...) }
> | /* empty */
> { ... handleAB (... true); }
> )
> )
>
> On Apr 11, 2005 8:43 AM, Peter Kronenberg <PKronenberg at technicacorp.com>
> wrote:
> >
> >
> > I have a tree walker that needs to deal with an optional component C.
> >
> > If tree is #(ROOT A B) then call handleAB(a.getText(), b.getText()).
> > If tree is #(ROOT A B C) then call handleAB(a.getText(), b.getText(),
> true);
> >
> > The handler for C has already set some stuff, but handleAB needs to
> know
> > about it. Since there is likely to be recursion while walking the
> tree,
> > setting a variable isn't feasable.
> >
> > What I've come up with is
> >
> > (#(ROOT A B C)) => #(ROOT a:A b:B C) {
> > handleAB(#a.getText(), #b.getText(), true);
> > }
> > | #(ROOT A B) {
> > handleAB(#a.getText(), #b.getText());
> > }
> >
> > It seems a bit kludgy and redundant. Is there a better way?
> > I can adjust the parameters of handleAB, but I need some indication if
> the
> > optional C is there
> >
> >
> > Peter Kronenberg
> > Software Engineer
> > (703) 885-1222
> > pkronenberg at technicacorp.com
> >
> >
> >
> > The information contained in this transmission may contain privileged
> and
> > confidential information. It is intended only for the use of the
> person(s)
> > named above. If you are not the intended recipient, you are hereby
> notified
> > that any review, dissemination, distribution or duplication of this
> > communication is strictly prohibited. If you are not the intended
> recipient,
> > please contact the sender by reply e-mail and destroy all copies of
> the
> > original message. Technica Corporation does not represent this e-mail
> to be
> > free from any virus, fault or defect and it is therefore the
> responsibility
> > of the recipient to first scan it for viruses, faults and defects. To
> reply
> > to our e-mail administrator directly, please send an e-mail to
> > postmaster at technicacorp.com. Thank you.
> >
> >
>
More information about the antlr-interest
mailing list