[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

automatic code conversion from Ruby to C ?

Axel Etzold

10/27/2008 7:44:00 PM

Dear all,

I am looking for a (semi-)automatic conversion of a Ruby script to C or C++, so
that readable source code for a human C/C++ programmer is produced.

The program should read in text from a file as a string, split and search it using regular expressions,
convert some numbers into floats and perform some calculations on them
(the latter part is done using some existing C code).

The text file to be read has this structure:

--------------------
P1
P2 23
P4 27
--------------------
P2
P3 1
P5 457
P6 3
P377 56
--------------------

Thus, P1 is a starting point and the distance to point P2 is 23 etc.
There is a varying number of entries in each of the parts separated by
dashes.
Is there some gem that would allow me to write a nice short
Ruby script doing the above-mentioned manipulations and translate it for me into
readable C/C++, such that I'd have to specify as few as possible size and variable
type declarations ?
I have about a hundred thousand points and relative distances, so speed could matter here ...
and I'd have a hard time defending the use of a scripting language with the people
I am collaborating here.


Thank you very much!

Best regards,

Axel


--
"Feel free" - 5 GB Mailbox, 50 FreeSMS/Monat ...
Jetzt GMX ProMail testen: http://www.gmx.net/de/...

13 Answers

Florian Gilcher

10/27/2008 8:45:00 PM

0

Why don't you just use Bison/Flex for that task?

As far as i know, most Ruby Parser Generators compile to Ruby. (for
example DHAKA)
The resulting (compiled) statemachine is not very readable.

And even if you write the parser by hand, I don't have high hopes that
a translation to "readable" C-Code
is easily possible.[1]

So if speed is an issue, don't code it by hand but code it with a tool
that is right for the job. Thats neither C
nor Ruby.
If speed is not that much of an issue, write you parser in one of the
available libraries and measure wether
it fits your needs. Thats the only way to find out.

Regards,
Florian Gilcher

[1]: Whats that anyway? ;)

On Oct 27, 2008, at 8:44 PM, Axel Etzold wrote:

> Dear all,
>
> I am looking for a (semi-)automatic conversion of a Ruby script to C
> or C++, so
> that readable source code for a human C/C++ programmer is produced.
>
> The program should read in text from a file as a string, split and
> search it using regular expressions,
> convert some numbers into floats and perform some calculations on them
> (the latter part is done using some existing C code).
>
> The text file to be read has this structure:
>
> --------------------
> P1
> P2 23
> P4 27
> --------------------
> P2
> P3 1
> P5 457
> P6 3
> P377 56
> --------------------
>
> Thus, P1 is a starting point and the distance to point P2 is 23 etc.
> There is a varying number of entries in each of the parts separated by
> dashes.
> Is there some gem that would allow me to write a nice short
> Ruby script doing the above-mentioned manipulations and translate it
> for me into
> readable C/C++, such that I'd have to specify as few as possible
> size and variable
> type declarations ?
> I have about a hundred thousand points and relative distances, so
> speed could matter here ...
> and I'd have a hard time defending the use of a scripting language
> with the people
> I am collaborating here.
>
>
> Thank you very much!
>
> Best regards,
>
> Axel
>
>
> --
> "Feel free" - 5 GB Mailbox, 50 FreeSMS/Monat ...
> Jetzt GMX ProMail testen: http://www.gmx.net/de/...
>


Brian Candler

10/27/2008 9:13:00 PM

0

Axel Etzold wrote:
> I am looking for a (semi-)automatic conversion of a Ruby script to C or
> C++, so
> that readable source code for a human C/C++ programmer is produced.
>
> The program should read in text from a file as a string, split and
> search it using regular expressions,
> convert some numbers into floats and perform some calculations on them
> (the latter part is done using some existing C code).

It sounds like you think that you can use Ruby as a shorthand way of
writing C. Unfortunately it doesn't work that way.

Bear in mind that the biggest pain with C is to do with memory
allocation. Ruby has a whole run-time environment for creating and
manipulating Ruby objects. So this mechanically-generated C code would
just be calling Ruby libraries to create objects or dispatch methods.

So in that case, why not just write it in Ruby and run it?

The other part you say is that you want to interact with existing C
code. This may be easier than you think. A good starting point is
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext...

Look also at the RubyInline gem, which lets you write C directly inside
your Ruby (yes!)

> Is there some gem that would ... translate it for
> me into
> readable C/C++, such that I'd have to specify as few as possible size
> and variable
> type declarations ?

If it did, it would just be

VALUE foo;
VALUE bar;
VALUE baz;

/* VALUE is a reference to a Ruby object, whether it be an Array, a
String, or an integer */

> and I'd have a hard time defending the use of a scripting language with
> the people
> I am collaborating here.

Prototype it in Ruby. If it works fast enough, then you've saved
yourself a lot of work. Even if it's too slow, you'll have a more
concrete idea what you're trying to do and how to achieve it.

Reading 100,000 lines in Ruby doesn't take very long.

Another option would be to write a Ruby script to "normalise" your input
into a format which is easier for your C program to read. For example,
it could output

point1 point2 distance

tuples. This would probably be sufficient:

src_point = nil
while line = $stdin.gets
line.chomp!
if line =~ /^-/
src_point = nil
elsif src_point.nil?
src_point = line
elsif line =~ /^(\S+)\s*(\d+)$/
puts "#{src_point} #{$1} #{$2}"
else
STDERR.puts "Invalid line: #{line.inspect}"
end
end

Output:

P1 P2 23
P1 P4 27
P2 P3 1
P2 P5 457
P2 P6 3
P2 P377 56

Your C program would still need to build a graph data structure from
this though. There are probably existing C libraries to work on data
sets like this, if you hunt hard enough.
--
Posted via http://www.ruby-....

Robert Klemme

10/27/2008 9:25:00 PM

0

On 27.10.2008 20:44, Axel Etzold wrote:
> I am looking for a (semi-)automatic conversion of a Ruby script to C or C++, so
> that readable source code for a human C/C++ programmer is produced.

Hm, automatic conversion tends to produce not so readable code.

> The program should read in text from a file as a string, split and search it using regular expressions,
> convert some numbers into floats and perform some calculations on them
> (the latter part is done using some existing C code).
>
> The text file to be read has this structure:
>
> --------------------
> P1
> P2 23
> P4 27
> --------------------
> P2
> P3 1
> P5 457
> P6 3
> P377 56
> --------------------
>
> Thus, P1 is a starting point and the distance to point P2 is 23 etc.
> There is a varying number of entries in each of the parts separated by
> dashes.
> Is there some gem that would allow me to write a nice short
> Ruby script doing the above-mentioned manipulations and translate it for me into
> readable C/C++, such that I'd have to specify as few as possible size and variable
> type declarations ?

So you basically want a script like this converted:

graph = []
p = nil

ARGF.each do |line|
line.chomp!
case line
when /^-+$/
p = nil
when /^P(\d+)$/
p = $1.to_i
graph[p] = {}
when /^P(\d+)\s+(\d+)$/
graph[p][$1.to_i] = $2.to_i
else
raise "Cannot parse: #{line.inspect}"
end
end

This should be similar easy with regular expressions in C/C++ or even
parsing by hand.

> I have about a hundred thousand points and relative distances, so speed could matter here ...
> and I'd have a hard time defending the use of a scripting language with the people
> I am collaborating here.

What do you want to do with the data afterwards? Maybe you can just add
the missing code and benchmark (your writing time as well as runtime).

Kind regards

robert

Mohit Sindhwani

10/28/2008 3:21:00 AM

0

Axel Etzold wrote:
> Dear all,
>
> I am looking for a (semi-)automatic conversion of a Ruby script to C or C++, so
> that readable source code for a human C/C++ programmer is produced.
>
> The program should read in text from a file as a string, split and search it using regular expressions,
> convert some numbers into floats and perform some calculations on them
> (the latter part is done using some existing C code).
>
> The text file to be read has this structure:
>
> --------------------
> P1
> P2 23
> P4 27
> --------------------
> P2
> P3 1
> P5 457
> P6 3
> P377 56
> --------------------
>
Hi Axel,

My recommendation would be to 'prototype' in Ruby to establish the
correct logic of what you want to achieve and then recode in C/C++ if
speed is not fast enough - that's what we do for a number of things on
our side.

If you don't use too many Ruby-isms, your Ruby code can look a lot like
the corresponding C code - of course, you'd have to rewrite the string
parsing in C/C++ to suit your needs.

Cheers
Mohit.


Robert Klemme

10/28/2008 8:31:00 AM

0

2008/10/27 Robert Klemme <shortcutter@googlemail.com>:
> On 27.10.2008 20:44, Axel Etzold wrote:
>>
>> I am looking for a (semi-)automatic conversion of a Ruby script to C or
>> C++, so
>> that readable source code for a human C/C++ programmer is produced.

>> I have about a hundred thousand points and relative distances, so speed
>> could matter here ...
>> and I'd have a hard time defending the use of a scripting language with
>> the people
>> I am collaborating here.
>
> What do you want to do with the data afterwards? Maybe you can just add the
> missing code and benchmark (your writing time as well as runtime).

OTOH, maybe Ruby is just not the right tool for the job. IMHO there is
no point in forcing Ruby into this if C or C++ do fit the problem at
hand much better.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Axel Etzold

10/28/2008 8:01:00 PM

0

Dear Florian, Brian, Robert and Mohit,

thank you all for your responses.
Actually, choosing what the code I'll have to deliver is written in has to be C/C++, since the
customers here want something they can feed into gcc and understand when they read it.
I am not absolutely sure, but I don't think they know Ruby very well and probably aren't willing to consider its
use in this problem, because of the speed issue.

I was just wondering whether there'd be some tool that would translate some of my Ruby
code into C/C++ to speed up the development process.
I looked at RubyToC and at cplus2ruby, but it seems that I'd spend almost as much time writing
C from scratch as editing the translated code. (This might be due to the fact that I couldn't find more
documentation than the examples provided, and there doesn't seem to be a way of translating Ruby's
Hashes to C or its IO routines etc... with these softwares).

Please correct me if the above isn't true ....

Thank you very much again!

Best regards,

Axel

--
"Feel free" - 5 GB Mailbox, 50 FreeSMS/Monat ...
Jetzt GMX ProMail testen: http://www.gmx.net/de/...

Robert Klemme

10/28/2008 9:07:00 PM

0

On 28.10.2008 21:00, Axel Etzold wrote:
> I looked at RubyToC and at cplus2ruby, but it seems that I'd spend almost as much time writing
> C from scratch as editing the translated code.

Indeed: the problem is simple enough to directly implement it in C* - no
need to push Ruby into this IMHO.

Kind regards

robert

Mohit Sindhwani

10/29/2008 2:48:00 AM

0

Hi Axel,

Axel Etzold wrote:
> thank you all for your responses.
> Actually, choosing what the code I'll have to deliver is written in has to be C/C++, since the
> customers here want something they can feed into gcc and understand when they read it.
> I am not absolutely sure, but I don't think they know Ruby very well and probably aren't willing to consider its
> use in this problem, because of the speed issue.
>

I find that sometimes customers don't appreciate a new tool/ device -
they like to stay with what they know best... which is fine. My advice
to you would be to prototype in Ruby if there is an element of
uncertainty in the program you're developing. Once the customer sees it
working, they may be convinced. On the other hand, once it's already in
Ruby, translating to C/ C++ isn't that far a step... even if you do it
manually. The only problem is that you need to maintain updates by hand
(or update only one or the other).

> I was just wondering whether there'd be some tool that would translate some of my Ruby
> code into C/C++ to speed up the development process.
>
I haven't used anything that would help.

> I looked at RubyToC and at cplus2ruby, but it seems that I'd spend almost as much time writing
> C from scratch as editing the translated code. (This might be due to the fact that I couldn't find more
> documentation than the examples provided, and there doesn't seem to be a way of translating Ruby's
> Hashes to C or its IO routines etc... with these softwares).
If you're using C++, hashes can be translated into STL Maps if you need
it... though anything C/C++ is going to be more strongly typed than the
duck typing in Ruby.


Cheers,
Mohit.
10/29/2008 | 10:47 AM.




Robert Klemme

10/29/2008 7:52:00 AM

0

2008/10/29 Mohit Sindhwani <mo_mail@onghu.com>:
>> thank you all for your responses.
>> Actually, choosing what the code I'll have to deliver is written in has to
>> be C/C++, since the
>> customers here want something they can feed into gcc and understand when
>> they read it.
>> I am not absolutely sure, but I don't think they know Ruby very well and
>> probably aren't willing to consider its
>> use in this problem, because of the speed issue.
>
> I find that sometimes customers don't appreciate a new tool/ device - they
> like to stay with what they know best... which is fine. My advice to you
> would be to prototype in Ruby if there is an element of uncertainty in the
> program you're developing. Once the customer sees it working, they may be
> convinced. On the other hand, once it's already in Ruby, translating to C/
> C++ isn't that far a step... even if you do it manually. The only problem
> is that you need to maintain updates by hand (or update only one or the
> other).

Given the size of the problem (i.e. small) and the fact that existing
C library code has to be used I would directly do this in C or C++.
There is also the advantage of not having a hybrid solution which
makes certain things easier (building, distribution, code protection
etc.). IMHO implementing the parsing of such a simple language
("P1...") is not worthwhile doing this in Ruby. It's always about
tradeoffs and forcing Ruby (or any other tool for that matter) into a
project where it does not fit will not bring any advantage (rather the
opposite).

> If you're using C++, hashes can be translated into STL Maps if you need
> it... though anything C/C++ is going to be more strongly typed than the duck
> typing in Ruby.

<nitpick>Note though that Ruby is as strongly typed as C++ - the
difference is between "static" and "dynamic" typing.</nitpick>

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Mohit Sindhwani

10/29/2008 8:43:00 AM

0

Robert Klemme wrote:
> <nitpick>Note though that Ruby is as strongly typed as C++ - the
> difference is between "static" and "dynamic" typing.</nitpick>
>

Point taken! :)

Cheers,
Mohit.
10/29/2008 | 4:42 PM.