[antlr-interest] philosophy about translation

Andy Tripp antlr at jazillian.com
Wed Oct 11 11:29:46 PDT 2006


Sohail Somani wrote:

>On Tue, 2006-10-10 at 11:12 -0400, Andy Tripp wrote:
>  
>
>>Sohail Somani wrote:
>>    
>>
>>>>Its weird that you're walking token streams, but what it seems it that
>>>>you're implementing the recursive descent parser by hand...
>>>>   
>>>>
>>>>        
>>>>
>>But a parser converts a token stream to an AST, I'm not using an AST, 
>>but dealing with
>>the stream directly because that seems easier to me. I find it easier to 
>>search for
>>pattern "f(...) {" to look for a function, rather than search a tree for 
>>a node of type
>>FUNCTION_DECLARATION.
>>    
>>
>
>A parser doesn't *have* to create an AST. For example, consider a syntax
>directed translation. That is something that simply recognizes token
>structure and performs some actions.
>  
>
I realize that. I'm just saying that I wouldn't consider my library of 
functions on token-streams to
be "implementing a recursive descent parser by hand" as you said. Not 
even close.

>  
>
>>>I believe there is an unspoken rule that all bets are off with COBOL?
>>> 
>>>
>>>      
>>>
>>Yes, I suppose. But even with C and C++, what seems simple may not be. 
>>For example, you'd think that
>>struct person p[100];
>>might correspond to a single line of Java, but it doesn't (you need to 
>>initialize the
>>array). So suddenly, out of nowhere, you may have to add a static block 
>>of code. That's
>>a typical one-to-many type of thing.
>>    
>>
>
>I'm not sure how an AST a la antlr prevents you from doing this.
>
I'm not saying using an AST prevents you from doing this, just that it's 
not helping. I've found that
for over 99% of what I'm doing, an AST is not helping. Therefore, I 
don't have an AST-centric
architecture.

> 
>
>  
>
>>because it may certainly have changed since you last looked at it..in 
>>other words, the symbol
>>table can be a mess to maintain. In my case, the return type of b() may 
>>have change, the name of it
>>may have changed, its argument types may have changed.
>>    
>>
>
>Changed from what to what? And how the heck can they change in a single
>run of the compiler? I'm confused.
>  
>
It's not a compiler, it's a translator! "if (1)" works in C, but not 
Java. So when we have
"if (a)" and "a" is type "int", we need to change its type to boolean. 
(And no, I'm not just going
to change it to "if (a != 0)"  ).

>  
>
>>Also, my tool does not have to be fast - if it takes an hour to 
>>translate some code, when it could
>>have taken 2 seconds if I had designed it "right", that's ok.
>>    
>>
>
>I don't think anyone is telling you that you didn't design it right! I
>myself would like to have the power of:
>
>v1 = v1 + v2 => v1 += v2
>
>transformations. Myself, I'm only trying to figure out what the big deal
>is that prevents you from using antlr. Personally, I know I wouldn't
>want to write token matching code.
>  
>
Nothing preventing me from using ANTLR. Just saying that I've found that 
walking an AST is
not the best architecture for what I'm doing. I use ANTLR for lexing, 
and even for AST generation
for expressions. It's just that my translator is not in any way "a 
treewalker" approach, either using
ANTLR treewalkers or just a Visitor pattern.

>Please don't think I am attacking your translator. We all know how
>difficult the problem is you're trying to solve. All I'm trying to
>understand is how you solved it.
>  
>
No problem. I have thick skin :)

I feel like I'm always "talking past" people on this list, and I think 
it's the "compiler mindset".
I really do think that C-to-Java and COBOL-to-Java, where you produce 
"natural" Java code,
is more like English-to-Spanish than it is like Java-to-bytecode. Sure, 
the input and output
are structured and treelike, but to emulate what a person does, simply 
"walking the tree"
isn't going to work for English-to-Spanish or COBOL-to-Java. Go ahead 
and and put
"Woods Eyes Masters" into a tree and then convert to Spanish. You'll 
come back later
and say "...but my program would have to know the context to even see 
that it's talking
about Tiger!" and I'll grin and say "that's right."



More information about the antlr-interest mailing list