[antlr-interest] New article on StringTemplates and Treewalkers
Andy Tripp
atripp at comcast.net
Tue Jan 10 19:39:57 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?
>
My pattern matcher is specialized to understand C syntax. The only
special ability it has is
that you have "v" placeholders that will match a variable or constant,
and "x" placeholders that will
match anything. So "v1 = strcpy(x)" will match what you'd expect it to
match, where the matcher
will look for the appropriate right paren.
>
>> 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?
>
Only relevent in the sense that one or the other is going to be easier
to write and read and have
enough flexibility. I find it far easier to read "if (v1=0; v1<v2;
v1++)" than the equivalent
AST tree structure in ANTLR syntax, especially when you consider that
we're doing more
than matching AST structure here: when I repeat "v1" there, all the
"v1"s must match the same
variable. As always, I'm sure that can be done with ANTLR, but I bet it
muddles things up a bit.
>
>> 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?
>
I can't...I have to write the code. But now that I've written that code
which checks for all these
patterns, at what point in the grammar to I invoke it? There is no one
place that it fits. The
treewalker approach is just getting in the way here if I were to use it.
>
>> 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?
>
Sorry, I didn't mean to imply that I had an special solution, just a
bunch of Java code. I was just
answering the other poster's question about "why not just check for
those 5 lines".
>
>Ter
>
More information about the antlr-interest
mailing list