[antlr-interest] How to do complex Tree Pattern Matching

Gerald Rosenberg gerald at certiv.net
Sat Sep 19 18:11:17 PDT 2009


Unless I am missing something, the convenience methods:

inContext(String)
hasAncestor(int)
hasParent(int)

belong in TreeFilter, or at least in the TreeFilter 
heirarchy.  BaseTree#hasAncestor(int) is just plain awkward to code 
in a tree matcher grammar predicate. Also, the inContext method 
really is needed.  Just no other convenient way to discretely 
distinguish a branch in a complex AST.

Attached is the TreeFilter subclass that I am currently using.  The 
inContext method is a rework of your earlier method of the same name 
-- I have not exercised this version greatly.  In a next version, 
would like to get away form the String parameter and use an int list 
instead, maybe with a pre-defined imaginary token representing '.*'.


At 03:21 PM 9/12/2009, Terence Parr wrote:
>yeah, I realized that I had opened up a huge issue without thinking it
>through. I'd like to have something like inContext but the more I
>added to it, the more I realized that the simplest thing to do would
>be to give the programmer basic tools like hasAncestor and let them
>add whatever logic they need.
>
>Ter
>On Sep 12, 2009, at 3:18 PM, Gerald Rosenberg wrote:
>
>>At 02:07 PM 9/12/2009, Terence Parr wrote:
>>
>>>On Sep 12, 2009, at 12:06 AM, Gerald Rosenberg wrote:
>>>>Given an existing AST with the two paths:
>>>
>>>i'd recommend doing ^(m .*) match and a pred in front that says
>>>hasAncestor(a) or something
>>>Ter
>>
>>OK, I completely missed BaseTree#hasAncestor
>>
>>Regards the "or something,"  TreeParser#inContext would seem to be
>>it.  However, that method seems to have gone missing from 3.1.3 (it
>>was in 3.1.2, but is still absent in 3.1.4).  Is there a reason that
>>method got pulled?
>>
>>Thanks.
>>
>>
>>
>>
>>
>>List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>Unsubscribe: 
>>http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list