[antlr-interest] philosophy about translation

Andy Tripp antlr at jazillian.com
Sat Oct 7 10:55:59 PDT 2006


Monty Zukowski 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.  So do you actually work directly with tokens, or do you
> write grammars to parse your token streams and emit yet another token
> stream, or do you have your own token matching language you write your
> rules in?

For simple text matching rules like those shown above, I lex (but not 
parse) the
"from" and "to" patterns (which are separated by "-->"). I then have a 
pattern-matching
engine that, starting at a particular location in a List of Tokens, 
tells whether the "from"
is matched or not. If it is matched, then the matcher also provide the 
"to" pattern
with the appropriate substitutions made.
So given this rule:
ADD v1 TO v2. --> v2 += v1;

the token squence "ADD A TO B." does match the "ADD v1 to v2." pattern, 
and "B += A;"
is the appropriate replacement.

Andy

>
> Monty
>



More information about the antlr-interest mailing list