[antlr-interest] philosophy about translation

Terence Parr parrt at cs.usfca.edu
Fri Oct 6 11:33:14 PDT 2006


On Oct 6, 2006, at 6:59 AM, Andy Tripp wrote:
> Here's my alternative:
> I have hundreds of "transformation rules" where each rule can  
> either be a simple mapping like this:
> ADD v1 TO v2. --> v2 += v1;
> ADD v1 v2 TO v3 v4. --> v3 += v1 + v2; v4 += v1 + v2
> ADD v1 TO v2 GIVING v3.  --> v3 = v1 + v2;
>
> ...or actual Java code that operates on sequences of tokens, not  
> trees.
> I find it easier to think of the source and target languages as  
> sequences of tokens
> ("ADD" followed by a variable followed by "TO"...) rather than  
> trees (an "ADD" node
> with the first child being a variable and the second child being  
> "TO"...)

  interesting.  What happens when you need to use structure not just  
token sequence?

>> Well, that problem becomes a "static analysis" problem and a  
>> "constant
>> expression substitution that is aware of printf args" problem as  
>> well.
>
> Right. So I guess my view is just that language translation, at  
> least when producing
> realistic output, is 99% "static analysis" and 1% "tree  
> transformation".

Interesting... I really must have a look at all of your rules sometime.

> Well, I preferred sequences of tokens over ASTs even for C to Java  
> and C++ to Java,
> so my views are pretty extreme. On the other hand, of all the  
> language translators out there,
> I don't see any that (to my eye) produce realistic, "natural" code,  
> other than my Jazillian.
> It's one thing to produce working Java (source or byte code) code  
> from C. It's quite another
> to produce code that looks hand-written. The logic to combine those  
> 3 "printf" statements
> into a single, realistic Java one is far harder than just changing  
> to Java syntax. Somewhere
> in writing that code I came to realize "Hey, the AST is just  
> getting in the way here".

  how did you decide that you could replace those variables with  
strings?  Did you do flow analysis?  If so how did you do it on a  
linked list of tokens?

> I guess what I'm doing is more akin to natural language  
> translation. I'm sure an English to
> Spanish translator has relatively little logic that does tree  
> transformation (e.g. putting the
> adjectives *after* the nouns).

  an interesting analogy.  You are saying that language translation  
is mostly word for word change and some grammatical changes such as  
you would see from English to German where the verb's at the end.

Hmm....  Do you have multiple phases also, just declarative rule  
replacement style?

Ter


More information about the antlr-interest mailing list