[antlr-interest] philosophy - part 2
Andy Tripp
antlr at jazillian.com
Thu Oct 12 12:24:44 PDT 2006
Nagesh, Harsha wrote:
>Andy,
>
> In one of the previous mails you mentioned about using Collections.Sort routine of java to replace an equivalent piece of code in C. I am really curious how would you go about doing this ? There can be any number of ways one could write a sorting algorithm and I am wondering if you really try to do some fancy token matching to determine that "Aha this is indeed the xyz sorting algorithm" and then replace it with one function call
>
>Harsha
>
>
I've done just a little of this, but yea, it does seem to be pretty
impractical.
Here's one example where I map a couple of alternatives to a single
function call:
for (v1=0; (v2 = getc(v3)) != '\n';)
v1 = v1 * 10 + (v2 - '0');
---
for (v1=0; (v2 = getc(v3)) != EOF && v2 != '\n';)
v1 = v1 * 10 + (v2 - '0');
-->
v1 = v3.readIntChars();
In this case, I think maybe it's useful - we may actually come across
one of those patterns
of code in the future. On the other hand, here's a ridiculous rule that
is so specific that it
will surely never match anything ever again:
while (true) {
while (true) {
printf(v5);
v4 = 0;
v1 = v2.readLine();
break;
}
if (v3 == v1) {
continue;
}
for (v3 = v1; isspace(*v3); v3++) {
continue;
}
for (;isdigit(*v3); v3++) {
v4 *= 10 + *v3 - '0';
}
if (*v3 == '\n') {
return v4;
}
else {
x2
}
}
-->
while (true) {
System.out.print(v5);
v1 = v2.readLine().trim();
try {
return Integer.parseInt(v1);
}
catch (NumberFormatException e) {
x2
}
}
The two rules above are both "text-based" rules, but I do have lots of
rules that are not so simple
and so are written with Java code. Here, we're a lot more flexible. For
example, I have a
TemporaryVariableRule that look for patterns things like:
String s = a;
s += " ";
s += c;
a = s;
...and removes the temporary variable s:
a += " ";
a += c;
(of course, there can be no other references to s). Since we're writing
code, we have the
flexibility to say "look for a variable that's assigned to some other
value ("s = a;"), and later
an assignment back ("a = s"). Make sure the "a" never changes in
between, and all assignments
to "s" can be combined. Make sure that "s" is never referenced anywhere
else.
So all that is just capturing what a real person would do: notice cases
where a temporary
variable is just making the code more complicated, and simplifying.
Andy
More information about the antlr-interest
mailing list