[antlr-interest] Tree walking Q

Bryan Ewbank ewbank at gmail.com
Sat Jun 4 16:27:51 PDT 2005


Using predicates has worked well for me; it greatly simplifies
maintenance of intermediate tree parser that want to do just a few
things in specific contexts.  For example, to replace id++ with
id=id+1...

   walker :
      ( (POSTINCR) => process_postincr
      | #( . ( walker )* )
      );

Another option, perhaps, that may not work as well in general, is:

   walker :
      #( op:.
         { preorder(#op); }
         (walker)*
         { postorder(#op); }
      );

At the same time, I'm a bit surprised that you say you can't define
explicit rules for the tree-parser.  The thing that has saved my bacon
more than once is an "AuditAST.g" I've written that defines the
correct shape of the tree.

If you can't define it, can you be sure it's correct?

> On Jun 4, 2005, at 3:37 PM, Gerald B. Rosenberg wrote: 
> > The basic question: Is there a way to set a default token rule for
> > use during tree parsing?  The objective is to skip tokens that are
> > not within a select subset that are to be processed by a particular
> > tree-parser run.
> >
> > My AST is rather large and complex, to the point where I cannot, as
> > a practical matter, define tree-parser rules for all potential
> > tokens.  The ANTLR generated tree-walker quits if it encounters an
> > unknown token.


More information about the antlr-interest mailing list