[antlr-interest] Tree Pattern Matching (filter=true) and the Wildcard...
Kevin Wooten
kdubb at me.com
Mon Jan 10 20:13:59 PST 2011
So I couldn't really get the rules below to work because I have VDECL nodes that have IADECL and ISDECL nodes mixed inside them. With apparently no way to match each subtree individually...
Example:
Given Input - (VDECL (TYPE ty) (IADECL a 9) (ISDECL b) (ISDECL c) (IADECL d 5))
I really wanting to match each "sub-tree" individually such as...
(VDECL (TYPE ty) (IADECL a 9)),
(VDECL (TYPE ty) (ISDECL b)),
(VDECL (TYPE ty) (ISDECL c)),
(VDECL (TYPE ty) (IADECL d 5))
After (it didn't work) a little examination I realized I wasn't trying to match "sub-trees" at all but something more complicated. So I tried a different approach, with a new single rule, that almost seemed to work...
Rule-
expand_declarations
: ^(VDECL ty=. ( ^( IADECL aids+=. bnds+=. ) | ^( ISDECL sids+=. ) )* ) -> ^(VDECLS ^(VDECL ^(ARRAY $ty $bnds) $aids)* ^(VDECL $ty $sids)*)
;
Here I am doing all the matching in one rule and ANTLR matched all of my rules properly.
Unfortunately ANTLR faulted on the rewrite part. After looking at the code I realized that it has no protection from the sizes of $bnds and $aids being different from $sids. Although according to ANTLR documentation it looks as if the cardinality of each should have take care of this, but it doesn't.
After adding 2 if statements to the generated rewrite code I got this rule to work (seemingly) perfectly. Is there a better way? Is this a bug or am I using it incorrectly?
On Jan 10, 2011, at 6:26 PM, Kevin Wooten wrote:
> How do I get a wildcard to match a complete subtree? For example... (DECL name (ARRAY (TYPE float) 9))
>
> Currently I can only get the wild card to match any flat node like: (DECL name float 9).
>
> Here are my tree pattern match rules...
>
> expand_array_declaration
> : ^(VDECL ty=. ^(IADECL id=. bnds=.)) -> ^(VDECL ^(ARRAY $ty $bnds) $id)
> ;
>
> expand_array_declarations
> : ^(VDECL ty=. (^(IADECL id+=. bnds+=.))+) -> ^(VDECLS ^(VDECL ^(ARRAY $ty $bnds) $id)+)
> ;
>
> expand_scalar_declaration
> : ^(VDECL ty=. ^(ISDECL id=.)) -> ^(VDECL $ty $id)
> ;
>
> expand_scalar_declarations
> : ^(VDECL ty=. (^(ISDECL id+=.))+) -> ^(VDECLS ^(VDECL $ty $id)+)
> ;
>
>
> Also, as you can see, I have to versions that seem to account for a limitation (bug?) in the rewriting that only allows you to replace a node with a single node. What these rules are doing is expanding type declarations so they are unified (e.g. float a, b[4], c, d[5]; -> float a; float[4] b; float c; float[5] d;). So I need to replace one child with many. Simplified version would be: (VDECL type id+) -> (VDECL type id) (VDECL type id) (VDECL type id). Currently I have to do it in two separate rules and then I am guessing I will need a second pass to gather all the VDECLS + VDECL and flatten them out. Am I missing something obvious? Can (or should I be able to) replace on node with many?
>
> Any help on these two issues is much appreciated.
>
> Thanks,
> Kevin
>
>
> 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