[antlr-interest] philosophy about translation

Anthony W. Youngman antlr at thewolery.demon.co.uk
Wed Oct 25 04:11:47 PDT 2006


In message <45267B17.6060107 at jazillian.com>, Andy Tripp 
<antlr at jazillian.com> writes
>Jim O'Connor wrote:
>
>>
>>COBOL -> generic language -> JAVA. In the general case, any specific
>>language -> generic language.  Generic language -> any specific
>>language.
>>
>>
>I've thought a lot about this idea of having a generic intermediate 
>representation. There are at least
>a couple of products that say they do this. I can't see how to make it 
>work. Say we have
>a generic "add A to B" idea ("ADD A TO B" in COBOL, "B += A" in Java). 
>If we're going to
>produce "natural" Java code, we'll produce "A++" when B is 1. Or should 
>it be "++A"???
>No way to know, unless we also store that information, so we add some 
>flag to our
>generic representation. The guy working on the generic-to-Java part 
>demands that the
>guy working on the COBOL-to-generic part stores that information, even 
>though it's
>meaningless to the COBOL guy.
>
>Then, the next day, the generic-to-Java guy realizes that A can be any 
>arbitrary expression,
>and that the expression must be evaluated first, before the assignment 
>is made. He starts
>going to make the generic representation more generic, and the COBOL 
>sees him and says
>"WTF are you doing? You can only add two numbers! I'm not going to deal 
>with your
>complicated data structure, I just want to store the fact that you're 
>adding one number to another.
>
>And then the next day, the problem goes the other way. The COBOL guy 
>realizes that
>the Java guy is going to need to know that B is 10 digits before the 
>decimal, 5 after the decimal,
>and a particular precision. The Java guy eventually realizes
>that he can't even use "B += A", he's got to use BigDecimal. That 
>doesn't bother him so much
>as the term "digits".  "You're going to tell me how many 'digits' it 
>is???" He screams.
>"Don't you mean bytes?" No, he really does mean "digits". And you 
>haven't seen nothin' yet,
>Java guy. Wait till he tells you how to store those digits on disk in 
>packed decimal format.
>
>So in the end, the "generic representation" can't really be "generic". 
>The Java guy just wants to know
>whether a variable is a "int" or a "long", while the COBOL guy wants to 
>say how many digits it is
>before and after the decimal. OK, I suppose you could make it generic, 
>you can store both,
>or have the "generic representation" automatically
>convert between the two. But things are going very badly and you just 
>got started :)
>
I've been wanting to say this, ever since Monty said "AREV and VB are 
very different" ...

Look at the following snippet of AREV code (bear in mind that : is the 
string concatenation operator, and all variables in AREV are strings...)

X = X : 0

What do you think the programmer meant?



















In VB, this *probably* translates as

int X
X = X*10

Of course, if the AREV guy accidentally put a real number in X, then it 
would have no effect at all, and if he'd put text in X he'd have 
appended a 0 to it (note that in the original code, the 0 wasn't quoted, 
so it was a number not a string ...).

Your point about the programmer's original intent was well made, Andy 
:-) (and no I'm not suggesting programmers should write code like that, 
but as someone who programs in that language, I make use of that sort of 
trick)

Cheers,
Wol
-- 
Anthony W. Youngman - anthony at thewolery.demon.co.uk



More information about the antlr-interest mailing list