generating C source from Perl source

E

Eden Cardim

Greetings,

I'd like to know if there's any tool available that generates C source
based on some Perl program. The standard B::C doesn't do what I need,
since it generates code that deals with Perl's internal representation,
I need something more literal, that would automatically convert the
sub-routine calls, builtin functions, conditional evaluations, etc, and
maybe even build structs out of perl hashes and build calls to malloc
(sic). I'm not even sure its possible to do so, but who knows?
The reason I need this translation is that my computer science
graduation course changed it's schedule and now I'm being forced to
watch some basic programming classes because of bureaucratic issues
(even though I'll by graduating next semester), and my teacher wants
all the homework done in C. Since I already work with Perl for quite
some time and am pretty well familiarized with all the key programming
concepts, I'd like to save some time (to work on my final paper) by
doing my homework in Perl and then convert it to C in order to please
the teacher, plus I'm too lazy to go up the attic and undig all my C
literature ;-).
Thanks in advance.
 
A

A. Sinan Unur

I need something more literal, that would automatically convert the
sub-routine calls, builtin functions, conditional evaluations, etc,
and maybe even build structs out of perl hashes and build calls to
malloc (sic).

And you are actually getting a CS degree?
I'd like to save some time (to work on my final paper) by
doing my homework in Perl and then convert it to C in order to please
the teacher,

You know, machine generated code tends to look very different than human
generated code. I doubt machine generated code would make your teacher
happy (at least, I hope not).

Sinan
 
E

Eden Cardim

A. Sinan Unur escreveu:
And you are actually getting a CS degree?
Yep, if they like my paper about AI applied alongside the Mutual
Information theory for pattern recognition in genetic sequences...
You know, machine generated code tends to look very different than human
generated code. I doubt machine generated code would make your teacher
happy (at least, I hope not).
Of course I'd review the code and make some 'human adjustments'...
 
J

Josef Moellers

Eden said:
A. Sinan Unur escreveu:



Yep, if they like my paper about AI applied alongside the Mutual
Information theory for pattern recognition in genetic sequences...



Of course I'd review the code and make some 'human adjustments'...

I thought you"'d like to save some time".

Have you ever seen computer-generated code?
Computer generated code tends to completely ignore anything humans want
in their code:
- meaningfull identifiers
- indentation
- terseness
in summary
- readability

Especially the first point is crucial: you'd need to find out what the
heck this variable
"_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCPP_3.2"
does (NB this name was generated by gcc compiling a c++ program)?
 
X

xhoster

Eden Cardim said:
Greetings,

I'd like to know if there's any tool available that generates C source
based on some Perl program. The standard B::C doesn't do what I need,
since it generates code that deals with Perl's internal representation,
I need something more literal, that would automatically convert the
sub-routine calls, builtin functions, conditional evaluations, etc, and
maybe even build structs out of perl hashes and build calls to malloc
(sic). I'm not even sure its possible to do so, but who knows?
The reason I need this translation is that my computer science
graduation course changed it's schedule and now I'm being forced to
watch some basic programming classes because of bureaucratic issues
(even though I'll by graduating next semester), and my teacher wants
all the homework done in C.

Why shouldn't this be considered cheating? You may not like the decisions
of the bureaucrats, but it is there job and their authority to make them.

Xho
 
E

Eden Cardim

Why shouldn't this be considered cheating? You may not like the decisions
of the bureaucrats, but it is there job and their authority to make them.

I live and study in Brazil, known worldwide as the land of incompetent
bureaucrats, I shouldn't be paying for their incompetence should I? I
work with research at the college bioinformatics lab, why should I be
wasting my time implementing 10 different sort and graph traversal
algorithms in C (again, cos I already did this when I was a freshman)
in order to please some bureaucrat, when I could be solving a
scientific open problem and working my way on to get a master degree?
 
E

Eden Cardim

Josef said:
Have you ever seen computer-generated code?

I mentioned trying B::C previously so that would be a "yes"...
Computer generated code tends to completely ignore anything humans want
in their code:
- meaningfull identifiers
- indentation
- terseness in summary
- readability

Perl::Tidy takes care of indentation, and to a certain point,
readability and it's computer-altered code. If you read the original
post, you'll notice me mentioning "I'm not sure if it's possible" and
"who knows"...
I'm not going to translate a flight-control software system, I just
want to be able to insert an element in an array with:
@new_array = (@old_array[0..$pos-1], $item,
@old_array[$pos..$#old_array]);
instead of this:
int *new_array = (int*) malloc(sizeof(old_array) + 1);
int i;
/*insert 20 LOC here*/
 
A

A. Sinan Unur

instead of this:
int *new_array = (int*) malloc(sizeof(old_array) + 1);

You might benefit from a C refresher, it looks like.

Casting the return value of malloc is completely unnecessary, and can
mask errors from failing to include stdlib.h.

Also, is old_array also a pointer? In that case, sizeof(old_array) is
unlikely to tell you anything useful.

So, you probably want:

int *new_array = malloc((old_length + 1) * sizeof *new_array);

/* check if malloc succeded */

However, do you really need both old_array and new_array? Why not just
use realloc?

Maybe the bureaucrats know something.

Bye.

Sinan
 
J

Josef Moellers

Eden said:
I mentioned trying B::C previously so that would be a "yes"...
I'm not going to translate a flight-control software system, I just
want to be able to insert an element in an array with:
@new_array = (@old_array[0..$pos-1], $item,
@old_array[$pos..$#old_array]);

If you answered "Yes" to the above, you know that this little statement
(and 3 additional ones to set up @old_array, $pos and $item) generates
some 600+ LOCC!
instead of this:
int *new_array = (int*) malloc(sizeof(old_array) + 1);
int i;
/*insert 20 LOC here*/

I'd opt for the 20 lines I do understand and am proud of having written.

Seing the 600+ lines of code, I'd add to my list that computer generated
code needs to cater for just about everything that might or might not go
wrong. Your self-written code will only have to cater for those things
that you cannot control. You can declare certain limitations to your
code (preconditions) that the computer doesn't know.

I re-state that digging through 600 lines of computer-generated code and
finding out the meaning of fragments and variables, and stripping it
down to the necessary bits may be more work than writing your 20 lines
of C code.
 
J

Josef Moellers

Eden said:
I live and study in Brazil, known worldwide as the land of incompetent
bureaucrats, I shouldn't be paying for their incompetence should I? I
work with research at the college bioinformatics lab, why should I be
wasting my time implementing 10 different sort and graph traversal
algorithms in C (again, cos I already did this when I was a freshman)
in order to please some bureaucrat, when I could be solving a
scientific open problem and working my way on to get a master degree?

Hm, that's a nice justification for cheating. I will remeber it when I
stand trial for a crime I haven't yet comitted B-{)

Yes, why bother learning when I could be on my way to a Nobel Prize?
After all, the end justifies the means!

If you "already did this when you were a freshman", you would be able to
do it. If you can't remember, it will be worth refreshing your knowledge.

Sounds to me there are more then just "incompetent bureaucrats" in Brazil.

BTW I do have a special mail for "brazil" in my Mozilla message filter.
Fits with my previous statement.
 
A

Anno Siegel

Eden Cardim said:
Greetings,

I'd like to know if there's any tool available that generates C source
based on some Perl program. The standard B::C doesn't do what I need,
since it generates code that deals with Perl's internal representation,
I need something more literal, that would automatically convert the
sub-routine calls, builtin functions, conditional evaluations, etc, and
maybe even build structs out of perl hashes and build calls to malloc
(sic). I'm not even sure its possible to do so, but who knows?

It isn't reasonably possible to build such a thing.

To pick just one of your suggestions, an automatic translator can not
build C structs out of hashes because Perl hashes can acquire new keys
at run time, a struct can't. Only those hashes whose keys (not values)
can be fixed at compile time could be treated this way. It would
take a very intelligent translator to figure this out.
The reason I need this translation is that my computer science
graduation course changed it's schedule and now I'm being forced to
watch some basic programming classes because of bureaucratic issues
(even though I'll by graduating next semester), and my teacher wants
all the homework done in C. Since I already work with Perl for quite
some time and am pretty well familiarized with all the key programming
concepts, I'd like to save some time (to work on my final paper) by
doing my homework in Perl and then convert it to C in order to please
the teacher, plus I'm too lazy to go up the attic and undig all my C
literature ;-).

The only way to benefit from Perl this way is to prototype in Perl but
write the final program in C. That way you debug the algorithm and
general program structure conveniently in Perl and only deal with C
when all that is done.

In a prototype, you can use all Perl constructs freely, but that may
make for a difficult translation to C. Unless you can use a good
hash library in your C solution, it would be hard to translate to C
a Perl program that relies heavily on hash tables, just for instance.

For a small project I'd write the Perl prototype with an eye to the
final translation to C. That may mean no hashes, probably lots of
indexing into arrays, and generally poor Perl style. It may still
speed up the development cycle.

And no-one can call it cheating.

Anno
 
D

Dr.Ruud

Anno Siegel:
an automatic translator can not
build C structs out of hashes because Perl hashes can acquire new keys
at run time, a struct can't. Only those hashes whose keys (not
values) can be fixed at compile time could be treated this way. It
would take a very intelligent translator to figure this out.

It doesn't have to make that difference, and could just always translate
it to a dynamic structure.
Example: http://www.cl.cam.ac.uk/users/cwc22/hashtable/
 
E

Eden Cardim

A. Sinan Unur said:
You might benefit from a C refresher.

Maybe not, I don't intend on becoming a C expert. I did say I haven't
been into C for quite a while and I don't intend going back there
anymore.
Casting the return value of malloc is completely unnecessary, and can
mask errors from failing to include stdlib.h.

Also, is old_array also a pointer? In that case, sizeof(old_array) is
unlikely to tell you anything useful.

So, you probably want:

int *new_array = malloc((old_length + 1) * sizeof *new_array);

/* check if malloc succeded */

Thanks for the help, but I think I'd post in a C newsgroup if I wanted
help with C.
However, do you really need both old_array and new_array? Why not just
use realloc?

Wow, I'd need a pretty smart translator to notice that new_array and
old_array are actually related and generate code with realloc, wouldn't
I?
 
A

A. Sinan Unur

Maybe not, I don't intend on becoming a C expert. I did say I haven't
been into C for quite a while and I don't intend going back there
anymore.

You are studying for a degree. One of the requirements of the degree, it
sounds from what you are saying, is that you have a certain level of
proficiency. You have only two honest options: Earn the proficiency or
drop out.

Anyway, bye.

Sinan
 
E

Eden Cardim

Anno said:
To pick just one of your suggestions, an automatic translator can not
build C structs out of hashes because Perl hashes can acquire new keys
at run time, a struct can't. Only those hashes whose keys (not values)
can be fixed at compile time could be treated this way. It would
take a very intelligent translator to figure this out.

Yes it would, but I've seen lots of solutions for apparently impossible
tasks be solved by some insane programmer that decided to tackle a
difficult problem just for the heck of it, I was hoping maybe I could
find someones' crazy solution for this problem.
The only way to benefit from Perl this way is to prototype in Perl but
write the final program in C. That way you debug the algorithm and
general program structure conveniently in Perl and only deal with C
when all that is done.

In a prototype, you can use all Perl constructs freely, but that may
make for a difficult translation to C. Unless you can use a good
hash library in your C solution, it would be hard to translate to C
a Perl program that relies heavily on hash tables, just for instance.

For a small project I'd write the Perl prototype with an eye to the
final translation to C. That may mean no hashes, probably lots of
indexing into arrays, and generally poor Perl style. It may still
speed up the development cycle.

And no-one can call it cheating.

Yes, I do that a lot at my job already and it's very productive.
Finally, someone got my point and provided a reasonable answer. After I
read the first followup I noticed I wouldn't get anything useful so I
already following your suggestion before you posted it, but thanks
anyway.
 
E

Eden Cardim

Josef said:
Hm, that's a nice justification for cheating. I will remeber it when I
stand trial for a crime I haven't yet comitted B-{)

Yes, why bother learning when I could be on my way to a Nobel Prize?
After all, the end justifies the means!

If you "already did this when you were a freshman", you would be able to
do it. If you can't remember, it will be worth refreshing your knowledge.

I'm not sure what the definition of 'algorithm' says wherever you live,
but around here they are (supposed to be) language-independent. One's
knowldge of a computer language isn't a good metric to use when judging
one's ability algorithms.
Sounds to me there are more then just "incompetent bureaucrats" in Brazil.

BTW I do have a special mail for "brazil" in my Mozilla message filter.
Fits with my previous statement.

Were going a bit off-topic here, aren't we?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top