[antlr-interest] New article on StringTemplates and Treewalkers

Terence Parr parrt at cs.usfca.edu
Tue Jan 10 15:16:19 PST 2006


On Jan 10, 2006, at 2:50 PM, Andy Tripp wrote:
> The interesting thing about your example is that the "natural" Java  
> translation (what a person would write) is
> if (x == 5)
> whereas any traditional AST-walking translator would probably produce
> if ((x = 5) != 0)
>
> While the second one is technically correct, and is probably what  
> my product would produce today,
> my product is designed so that I can easily add a rule to check for  
> a "if (v=n)" bug, and go ahead and
> fix the bug during translation. And I can and do add those sorts of  
> rules! Do not try this at home
> with your treewalking translator, kids :)

ooops...too late ;)

ifstat
	: ^(IF ^(ASSIGN ID expr) stat ) -> ifassign(...)
	| ^(IF expr stat) -> ifstat(...)
	;

where the ->foo(...) builds a template called foo and I've omitted  
the args for clarity.  This is ANTLR v3.  Not hard...this is  
precisely what grammars are useful for.

Pattern engines have trouble with semantic-based context.  For  
example, I can add a sem pred that tells me that an int ref is in a  
while or for of if...can your pattern matcher say "apply a rule only  
in this semantic or syntactic context"?  ;)

The thing is Andy that you are specifying text based rules and I am  
specifying tree based rules.  Further, I can only assume that you  
have a rule application engine that does this magic whereas I am  
using a weaker but more deterministic linear tree walker.

Note that most academics will agree with you about translation, lest  
you think you're the only one that thinks this way. See TXL,  
Stratego, ...  Also recall from a previous post of mine that your  
pattern matching engine may run into invalid paths or may even not  
terminate depending on your application strategy.  This is a very  
well known problem with this academic approach.

I will also point out that the number of people using declarative  
systems such as the academic ones are not used much by people  
building translators in the wild.  That said, the authors of those  
tools seem to be able to get them to do amazing things!

Ter


More information about the antlr-interest mailing list