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

Gerald Rosenberg gerald at certiv.net
Sat Sep 12 00:06:57 PDT 2009


Given an existing AST with the two paths:

^( a b? ^(m n) c? d )

and

^( a b? ^(x y? ^(m n)) c? d )

(that is, both paths have the same root node 'a')

Given that 'm' is the interesting node, is there a way to write tree 
pattern matching rules that distinguish the 'm' based on the separate paths?


topdown : ^(m .*)  ; // works nicely, but matches on both paths

topdown : ^(a .* ^(m *.) .*) ; // should this work?  I would expect 
it to still match both paths


// should this work?
topdown : ^(a .* ( ^(x .* ^(m .*) )    { call_1($m); } // on match second path
                  | ^(m .*)             { call_2($m); } // on match first path
                  )
                  .*) ;


As a concrete example, the ANTLR grammar defines identical ^(OPTIONS 
.*) blocks, one that is a subtree of GRAMMAR and another that is a 
subtree of RULE, which in turn is a subtree of GRAMMAR.  Would be 
best to directly distinguish them in the pattern match, rather than 
poking around in the AST to examine parents.

Thanks.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090912/ff07d68d/attachment.html 


More information about the antlr-interest mailing list