1. C to Eu translator

Hi all!

No, there is no typo in the subject. blink I actually mean a C -> Eu
translator. Does someone intend to write such a program?

As a layman and autodidact, I believe I learnt 80-90% of what I know
about progranmming, by studying source code written by other people.
Many -- especially professional -- code is written in C. Because I
cannot understand most of that code, I can't learn from it.

I know some very simple things that can be done in an editor:
- replace "//" with "--"
- replace "==" with "="
- replace "++" with "+=1"
- replace "&&" with "and"
- replace "||" with "or"
- replace "~" with "not_bits("
- replace "printf(" with "printf(1, "
- replace word "const" with "constant"
- replace word "int" with "integer"
- (replace word "void" with "procedure")

This is not much, and a translator would be much better of course.
Is an automatic translation actually possible?

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |  Money is the root of all evil.
 \ /  against HTML in       |  Send 20 Dollars for more info.
  X   e-mail and news,      |
 / \  and unneeded MIME     |  http://home.arcor.de/luethje/prog/

new topic     » topic index » view message » categorize

2. Re: C to Eu translator

----- Original Message ----- 
From: "Juergen Luethje" <j.lue at gmx.de>
To: "EUforum" <EUforum at topica.com>
Subject: C to Eu translator


> 
> 
> Hi all!
> 
> No, there is no typo in the subject. blink I actually mean a C -> Eu
> translator. Does someone intend to write such a program?
> 
> As a layman and autodidact, I believe I learnt 80-90% of what I know
> about progranmming, by studying source code written by other people.
> Many -- especially professional -- code is written in C. Because I
> cannot understand most of that code, I can't learn from it.
> 
> I know some very simple things that can be done in an editor:
> - replace "//" with "--"
> - replace "==" with "="
> - replace "++" with "+=1"
> - replace "&&" with "and"
> - replace "||" with "or"
> - replace "~" with "not_bits("
> - replace "printf(" with "printf(1, "
> - replace word "const" with "constant"
> - replace word "int" with "integer"
> - (replace word "void" with "procedure")
> 
> This is not much, and a translator would be much better of course.
> Is an automatic translation actually possible?
> 

A number of C constructs are not available in Euphoria so they would have to be
emulated (at best) and this would make the source code even more obscure.
Constructs such as goto, setjump, pointers, pass-by-reference, structures, are
very common in C code and none of them are in Euphoria.

So to answer your question, Yes I suppose it could be done, but would it be
worth it. It might be easier to learn C instead. blink

-- 
Derek

new topic     » goto parent     » topic index » view message » categorize

3. Re: C to Eu translator

Hi,

thank you Al and Andy and Derek for your replies.

Al wrote:

> Juergen Luethje wrote:

<snip>

>> Is an automatic translation actually possible?
>
>
> Hello Juergen,
>
> It's certainly possible, and already done to a point smile
>
> A while back i wrote a COM to Euphoria translator that takes
> a file with the code for a COM C interface and translates it
> into Euphoria code in a form suitable for my WinClass library.
> It takes a few seconds to convert an interface to Euphoria
> although i usually add a few custom items as well.
> Without it it would take a couple hours probably.

That sounds great!

> Some things are really easy to do, like place atoms
> where C variables once were, but other things are a
> little harder like making it aware that a POINT in C
> is actually going to be two atoms in the function call.
>
> I find it helps if you have another library in mind that
> you intend to use the resulting code with.  You can then
> gear the output to use parts of the library.  This helps
> with things like LOWORD and HIWORD and stuff like that.
>
> Try it yourself; it's not that hard really.

I think I had to write at least a scanner and a parser for the C
language. Thanks for considerably overestimating my programming
skills. smile
Maybe you can remove the "OM" from your "COM to Euphoria
translator"? How much micro $$ do you want? smile

I also understand what you Andy and Derek wrote.
An automatic translator, that produces correct but hard to read
Euphoria code is not very useful ...

How about only translating things that are safe and clear to do,
e.g. replacing all symbols such as "//", "==", "++", "&&", "||", "~",
and other stuff, thereby letting the C constructs that are not available
in Euphoria such as goto, setjump, pointers, pass-by-reference,
structures, etc. alone?

Then only the rest of the C program must be converted manually. Do you
think that there are many C programs, where this would save a
considerable amount of time, compared to converting the whole program
manually? Or is it not worth the effort, to write such a
"semi-translator"?

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |  Math problems?
 \ /  against HTML in       |  Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].
  X   e-mail and news,      |
 / \  and unneeded MIME     |  http://home.arcor.de/luethje/prog/

new topic     » goto parent     » topic index » view message » categorize

4. Re: C to Eu translator

On Sun, 04 Jan 2004 15:46:31 +0100, Juergen Luethje <j.lue at gmx.de>
wrote:

Whilst agreeing with other comments in this thread, and thinking it
would be better to leave it in C, but (learn how to) make it a dll,
there was one point you made I thought deserved special notice:
>How about only translating things that are safe and clear to do,
>e.g. replacing all symbols such as "//", "==", "++", "&&", "||", "~",
I don't believe even these would be safe and clear to translate:
== might want to be '=' in some cases, 'equal()' in others, and not
possible in others (eg a pointer value). You can ++a or a++ in C, so
it would have to be a separate statement in Euphoria, before, after,
or even sometimes in the middle of an expression... && and || may also
sometimes short-circuit differently in C to Euphoria(?).

When I translate C to Euphoria (which is not particularly often), I
start by commenting out the entire source, then using this as a guide
code one function (or line!) at a time, testing thoroughly as I go.

The only example (not specially relevant, either) to hand is a little
mandlebrot demo, first in C:

main(){

int x, y, k;
char *b = " .:,;!/>)|&IH%*#";
float r, i, z, Z, t, c, C;
for (y=30; puts(""), C = y*0.1 - 1.5, y--;){
     for (x=0; c = x*0.04 - 2, z=0, Z=0, x++ < 75;){
        for (r=c, i=C, k=0; t = z*z - Z*Z + r, Z = 2*z*Z + i, z=t,
k<112; k++)
           if (z*z + Z*Z > 10) break;
        printf ("%c", b[k%16]);
        }
     }
 }

...and below in Euphoria:

constant b=" .:,;!/>)|&IH%*#"
atom r, i, c, C, z, Z, t
integer k
    for y=30 to 1 by -1 do
        C=y*0.1-1.5
        puts(1,'\n')
        for x=0 to 74 do
            c=x*0.04-2
            z=0
            Z=0
            r=c
            i=C
            k=0
            while k<112 do
                t=z*z-Z*Z+r
                Z=2*z*Z+i
                z=t
                if z*z+Z*Z>10 then exit end if
                k+=1
            end while
            puts(1,b[remainder(k,16)+1])
        end for
    end for

After some careful study you will see it requires a fairly deep
semantic understanding of the C code in order to rip it apart into
single Euphoria statements, in the right order, were you to try this
automagically. Notice that the inner loop is a while, because k is
used outside it, x and y are no longer declared, the fact that zero
indexing is the norm in C adds to the fun, etc...

HTH,
Pete

new topic     » goto parent     » topic index » view message » categorize

5. Re: C to Eu translator

Andy wrote:

>> How about only translating things that are safe and clear to do,
>> e.g. replacing all symbols such as "//", "==", "++", "&&", "||", "~",
>> and other stuff, thereby letting the C constructs that are not available
>> in Euphoria such as goto, setjump, pointers, pass-by-reference,
>> structures, etc. alone?
>>
>> Then only the rest of the C program must be converted manually. Do you
>> think that there are many C programs, where this would save a
>> considerable amount of time, compared to converting the whole program
>> manually? Or is it not worth the effort, to write such a
>> "semi-translator"?
>>
>
> Well, what is the ultimate purpose?  If you're doing something like the
> COM library or something where it is actually useful to mimic the
> C-style, then ok.
>
> But you were talking about trying to learn from the code, but not
> understanding what it is doing.

Yes, trying to learn the logic of the C code, without understanding what
it is technically doing, was my original intention. But you and Derek
already had convinced me, that this wouldn't be possible.

> If you have to do the important (hard)
> parts of the translation manually, you'll have to understand exactly
> what the C code is doing.  And once you understand that, it is much
> easier to just rewrite a Euphoria version from scratch (not a
> translation, but a re-expression of the same ideas in Euphoria) now that
> you know the things that need to be accomplished.
>
> You can't translate C without understanding C, bottom line.  And if
> someone else made an auto-translator for you, the resulting Euphoria
> code would be no easier to understand than the original code...

After Al's reply, I focused on a program, that might safe time, as Al
wrote. And I didn't talk about "auto-translation" any more.

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |  This message has been ROT-13 encrypted
 \ /  against HTML in       |  twice for higher security.
  X   e-mail and news,      |
 / \  and unneeded MIME     |  http://home.arcor.de/luethje/prog/

new topic     » goto parent     » topic index » view message » categorize

6. Re: C to Eu translator

Pete wrote:

> On Sun, 04 Jan 2004 15:46:31 +0100, Juergen Luethje wrote:
>
> Whilst agreeing with other comments in this thread, and thinking it
> would be better to leave it in C, but (learn how to) make it a dll,
> there was one point you made I thought deserved special notice:
>> How about only translating things that are safe and clear to do,
>> e.g. replacing all symbols such as "//", "==", "++", "&&", "||", "~",
> I don't believe even these would be safe and clear to translate:
> == might want to be '=' in some cases, 'equal()' in others, and not
> possible in others (eg a pointer value). You can ++a or a++ in C, so
> it would have to be a separate statement in Euphoria, before, after,
> or even sometimes in the middle of an expression... && and || may also
> sometimes short-circuit differently in C to Euphoria(?).

Urgs. sad

> When I translate C to Euphoria (which is not particularly often), I
> start by commenting out the entire source, then using this as a guide
> code one function (or line!) at a time, testing thoroughly as I go.
>
> The only example (not specially relevant, either) to hand is a little
> mandlebrot demo, first in C:
>
> main(){
>
> int x, y, k;
> char *b = " .:,;!/>)|&IH%*#";
> float r, i, z, Z, t, c, C;
> for (y=30; puts(""), C = y*0.1 - 1.5, y--;){
>    for (x=0; c = x*0.04 - 2, z=0, Z=0, x++ < 75;){
>       for (r=c, i=C, k=0; t = z*z - Z*Z + r, Z = 2*z*Z + i, z=t, k<112; k++)
>          if (z*z + Z*Z > 10) break;
>       printf ("%c", b[k%16]);
>       }
>    }
> }
>
> ...and below in Euphoria:
>
> constant b=" .:,;!/>)|&IH%*#"
> atom r, i, c, C, z, Z, t
> integer k
>     for y=30 to 1 by -1 do
>         C=y*0.1-1.5
>         puts(1,'\n')
>         for x=0 to 74 do
>             c=x*0.04-2
>             z=0
>             Z=0
>             r=c
>             i=C
>             k=0
>             while k<112 do
>                 t=z*z-Z*Z+r
>                 Z=2*z*Z+i
>                 z=t
>                 if z*z+Z*Z>10 then exit end if
>                 k+=1
>             end while
>             puts(1,b[remainder(k,16)+1])
>         end for
>     end for
>
> After some careful study you will see it requires a fairly deep
> semantic understanding of the C code in order to rip it apart into
> single Euphoria statements, in the right order, were you to try this
> automagically. Notice that the inner loop is a while, because k is
> used outside it, x and y are no longer declared, the fact that zero
> indexing is the norm in C adds to the fun, etc...
>
> HTH,
> Pete

Thanks for these instructive explanations!

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |    |\      _,,,---,,_
 \ /  against HTML in       |    /,`.-'`'    -.  ;-;;,_
  X   e-mail and news,      |   |,4-  ) )-,_..;\ (  `'-'
 / \  and unneeded MIME     |  '---''(_/--'  `-'\_)

new topic     » goto parent     » topic index » view message » categorize

7. Re: C to Eu translator

Jerry wrote:

> If the purpose of translating C to Euphoria is to
> understand the C source code, then maybe that purpose
> would be better accomplished by translating from C to
> English.
> http://www-2.cs.cmu.edu/~dst/DeCSS/Baccash/


The download file contains code for Unix (or Linux) and Windows.
It looks, as if the author never had tested the Windows version,
but in the end, I managed to run the program on Windows.

Very interesting, thanks!
   Juergen

-- 
 /"\  ASCII ribbon campain  |    |\      _,,,---,,_
 \ /  against HTML in       |    /,`.-'`'    -.  ;-;;,_
  X   e-mail and news,      |   |,4-  ) )-,_..;\ (  `'-'
 / \  and unneeded MIME     |  '---''(_/--'  `-'\_)

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu