[antlr-interest] Parsing optional component in Tree Walker
Monty Zukowski
monty at codetransform.com
Mon Apr 11 06:01:47 PDT 2005
Peter Kronenberg 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
>
Consider building the tree differently. For instance, when building the
tree, if you see C then change the root node type to ROOTC. Then when
walking you can have the option at the root level.
Monty
More information about the antlr-interest
mailing list