[antlr-interest] guessing w/ predicates

Bryan Ewbank ewbank at gmail.com
Thu Feb 3 09:15:21 PST 2005


To summarize, Loring and Ric told my I misunderstood.  I saw the
discussion of predicate hoisting, but missed the fact that it applied
in this way as well.

The bug I thought I saw was in my brain :-)

So, how do I write a rule that traverses a whole tree and operates on
every node of a specific type?  I've written, for example:

// START
// THIS IS WRONG - DON'T DO IT.
root
   : (known)=>known
   | #(. (root)* )
   ;

known
   : (A) => #(A ... ) { process "A" node; }
   | (B) => #(B ... ) { process "B" node; }
   ;
// THIS IS WRONG - DON'T DO IT.
// END

If the predicates on known are not sufficient for the predicate on
root to match, how do I detect and reject a malformed "A" node that
does not match the #(A ...) shape?

Do I refactor the predicates thus:

root
   : (known_nodetype)=>known
   | #(. (root)* )
   ;

known_nodetype
   : (A|B)
   ;

known
   : #(A ... ) { process "A" node; }
   | #(B ... ) { process "B" node; }
   ;

It seems I have the same information in two places now - the nodetypes
of interest.

Off to rewrite my tree parsers :-(


More information about the antlr-interest mailing list