[antlr-interest] How to do complex Tree Pattern Matching
Terence Parr
parrt at cs.usfca.edu
Sat Sep 12 14:07:45 PDT 2009
On Sep 12, 2009, at 12:06 AM, Gerald Rosenberg wrote:
> 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
Hiya. i think structure is a bit off. you need ^(m .*) down a level.
> // should this work?
> topdown : ^(a .* ( ^(x .* ^(m .*) ) { call_1($m); } // on match
> second path
> | ^(m .*) { call_2($m); } // on match
> first path
> )
> .*) ;
something like that *should* work but might not due to bug.
> 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.
>
i'd recommend doing ^(m .*) match and a pred in front that says
hasAncestor(a) or something
Ter
More information about the antlr-interest
mailing list