[antlr-interest] philosophy about translation

Johnicholas Hines johnicholas.hines at gmail.com
Fri Oct 27 09:14:01 PDT 2006


You know,

v + x --> v.add(x)

could be parsed into trees, recognizing v and x as metasyntactic
variables, with enough tool support.

If we have a formal notion of "tree transducer" (I'm sure there are
several definitions, not sure which one Antlr uses), then several of
these rules could be composed to create one tree transducer. Running a
single tree transducer over a tree is likely to be faster than the
"lots and lots of linear searches" that Andy Tripp seems to be
recommending.

Johnicholas

> I'd rather have a set of rules that look like this:
>
> v + x --> v.add(x)
>
> ...than the correct-but-nontrivial tree version:
>
> PLUS
>   v
>   x
> -->
> METHOD_CALL
>    DOT
>       v
>       add
>    ELIST
>       x
>
> I'm sorry, but I just didn't know, off the top of my head, what the AST
> for "v.add(x)" looks like.
> I'm going to be writing at least 10 of these sorts of "pattern
> replacements" just for this BigDecimalRule
> alone, and I refuse to get bogged down in mental images of ASTs.
>
> So what I'm doing is write a tree-matching class that allows you to say
> "v + x --> v.add(x)" to mean "look through an AST for a "+"
> node with a first child that's an IDENT that has a particular text, and
> any arbitrary
> second child. If found, replace that AST node with a different AST node
> that has a METHOD_CALL node as its root, a DOT node as first child,
> etc."
>
> Humans think in terms of token streams, translators can go ahead and
> implement
> the humans' wishes in terms of nicely structured trees.
>


More information about the antlr-interest mailing list