[antlr-interest] philosophy about translation
Andy Tripp
antlr at jazillian.com
Sat Oct 7 10:51:05 PDT 2006
Monty Zukowski wrote:
> Andy Tripp wrote:
>
>> I think just this simple example that I brought up before actually
>> brings the problem to the surface:
>>
>> String hello = "hello";
>> String world = "world";
>> printf("%s %s\n", hello, world);
>>
>> ...becomes...
>>
>> System.out.println("Hello World");
>>
>> I can't see how that can be done by treewalking. By the time the code is
>> actually written to implement "printf to System.out.println",
>> there will be almost no "tree-transformation" or even "tree walking"
>> logic.
>>
>
> Well, I would have one pass that replaced all variable references of
> variables that are defined and used before being modified with the
> value from the declaration. That would leave a printf("%s %s\n",
> "hello", "world");
By the time you can handle...
printf("d=%3.2lf n=%d\n s=%s c=%c\n\n", d, n, s, c);
....you've got a lot of code and very little treewalking.
I've got 150 lines of code, including this one:
// the valid characters that could follow "%" in a printf() format
string:
// See section 7.19.6.1, item 6, of the C98 standard.
private static String CONVERSION_CHARACTERS =
"-+#0123456789.*diouxXcCseEfFgGlaAhljztLpn";
>
> In a later pass I would check for and eliminate any variables that are
> never referenced.
Yup, that's what I do.
>
> Also later I would have a printf rewrite rule that does string
> substitution for any constant arguments.
Yup, me too. Also, an earlier rule that lets you handle printf, sprintf,
and fprintf
all with the same logic.
>
>
> Treewalking is just a handy way for me to do pattern matching.
And I just do "token sequence walking" to do the same. Here's my actual
pattern matching code:
public boolean match(Source source) {
return source.currentToken.getText().equals("fprintf");
}
Andy
>
> Monty
>
More information about the antlr-interest
mailing list