[antlr-interest] Tree Pattern Matching (filter=true) and the Wildcard...

Kevin Wooten kdubb at me.com
Mon Jan 10 17:26:04 PST 2011


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



More information about the antlr-interest mailing list