[antlr-interest] Parsing optional component in Tree Walker

Bryan Ewbank ewbank at gmail.com
Mon Apr 11 12:24:41 PDT 2005


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