[antlr-interest] New article on StringTemplates and Treewalkers

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


On Jan 10, 2006, at 2:40 PM, Andy Tripp wrote:
>> I was struck by the example of 5 lines of C code being folded into  
>> one
>> line of Java code. Why couldn't you have a tree parser do this? It  
>> would
>> be inefficient, but I don't know if that is the point of the article.
>>
>
> You can't have a tree parser look for those 5 lines because
> there might be hundreds of different combinations of 5 lines that  
> would all fold into
> that one line of Java code.

Did you build a general prolog like pattern translator?  If so, what  
strategy of rule application do you use?  You're aware I'm sure that  
depending on the strategy, the same set of patterns may not terminate?

> In order to write a "look for a chunk of code that is really just  
> doing a strcpy()" function,
> you're going to have to do some real work.

That is very true.  Whether you specify a pattern in your style or in  
a set of tree grammar patterns is irrelevant, though, right?

> How many ways are there to write your own "strcpy()", each using a  
> few lines?
> You could use a "for" or "while" loop. You can reference the array  
> using pointer syntax or array syntax,
> you can use a pointer to loop through or an index. You can  
> increment the pointer/index with
> i++, ++i, or i=i+1. You could just use strcpy() or strncpy(). You  
> could wrap strcpy() with your
> own macro or function. You could allocate new memory for
> the destination string or assume it's already allocated. You could  
> loop until you hit '\0' or use
> strlen().

So how do you do that with your patterns?

> In order to avoid a combinatorial explosion of various patterns to  
> look for, you'll need to write
> some code that tries to look chunks of code that seem to be doing  
> strcpy(), and yet is
> flexible enough to handles these kinds variations.

We've only heard problems so far without a solution in your  
style...could you bring some light to how you avoid looking for  
various patterns of code to find strcpy chunks?

Ter



More information about the antlr-interest mailing list