[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Writing a language converter in Ruby ?

user@domain.invalid

3/20/2009 11:01:00 AM

Hi, I wonder where to start if I wanted to write a converter from XX
language source code to Ruby source code or from Ruby to XX ?

Eg. Converting Basic source code to Ruby ?

Do I absolutely need to use a tool like CocoR
(http://www.zenspider.com/ZSS/Produ...) ? Or is there a
standalone approach that only uses Ruby capacities ?


10 Answers

Dylan Evans

3/20/2009 12:35:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

It's a good idea to use a parser generator, it will simplify your code and
make it easier to work with. Of course you can just start reading the basic
files and parse them with a bunch of logic. Of course the code quality will
always be much higher if you just rewrite it, unless you want to compile it
but then you would want to target C.


On Fri, Mar 20, 2009 at 9:02 PM, Zouplaz <user@domain.invalid> wrote:

> Hi, I wonder where to start if I wanted to write a converter from XX
> language source code to Ruby source code or from Ruby to XX ?
>
> Eg. Converting Basic source code to Ruby ?
>
> Do I absolutely need to use a tool like CocoR
> (http://www.zenspider.com/ZSS/Produ...) ? Or is there a standalone
> approach that only uses Ruby capacities ?
>
>
>
>


--
The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

Justin Collins

3/21/2009 3:10:00 AM

0

Zouplaz wrote:
> Hi, I wonder where to start if I wanted to write a converter from XX
> language source code to Ruby source code or from Ruby to XX ?
>
> Eg. Converting Basic source code to Ruby ?
>
> Do I absolutely need to use a tool like CocoR
> (http://www.zenspider.com/ZSS/Produ...) ? Or is there a
> standalone approach that only uses Ruby capacities ?
>
>
>

Why not use existing tools? I like treetop, myself:
http://treetop.ruby...

-Justin

Brian Candler

3/23/2009 3:49:00 PM

0

Dylan Evans wrote:
> It's a good idea to use a parser generator, it will simplify your code
> and
> make it easier to work with. Of course you can just start reading the
> basic
> files and parse them with a bunch of logic.

Parsing is only half the answer. Once you have parsed, you will normally
have built some internal structure representing the input. You then
usually need a code generator to output code in the target language.

It's possible to generate naive code as you parse, but it will usually
be of poor quality. It may be possible to perform transformations on
this output to improve it, e.g. as a "peephole" optimiser does.

Recommended venerable book: "Compilers: Principles, Techniques and
Tools" by Aho, Sethi, Ullman - aka "The Dragon Book". I believe it has
been recently revised.
--
Posted via http://www.ruby-....

Ian Trudel

3/23/2009 5:36:00 PM

0

Zouplaz wrote:
> Hi, I wonder where to start if I wanted to write a converter from XX
> language source code to Ruby source code or from Ruby to XX ?
>
> Eg. Converting Basic source code to Ruby ?
>
> Do I absolutely need to use a tool like CocoR
> (http://www.zenspider.com/ZSS/Produ...) ? Or is there a
> standalone approach that only uses Ruby capacities ?

Perhaps, you would just be better off a completely different tool. A
source transformation programming language, such as TXL.
http://w...

Regards,
Ian
--
Posted via http://www.ruby-....

Albert Schlef

3/24/2009 8:50:00 AM

0

Justin Collins wrote:
> Why not use existing tools? I like treetop, myself:
> http://treetop.ruby...
>
> -Justin

That's interesting. I see that their DSL looks like:

rule additive
multitive '+' additive / multitive
end

Do you notice? There's "end" there but no "do"! How is it possible?
--
Posted via http://www.ruby-....

Brian Candler

3/24/2009 10:21:00 AM

0

Albert Schlef wrote:
> That's interesting. I see that their DSL looks like:
>
> rule additive
> multitive '+' additive / multitive
> end
>
> Do you notice? There's "end" there but no "do"! How is it possible?

Because it's not Ruby.

In Ruby, blocks are optional after a method call. Both these are legal:

(1)
foo

(2)
foo do
... more stuff
end

Presumably, TXL's language *requires* a rule body followed by end.
--
Posted via http://www.ruby-....

Brian Candler

3/24/2009 11:57:00 AM

0

Brian Candler wrote:
> Presumably, TXL's language *requires* a rule body followed by end.

BTW: TXL is distributed only as a binaries (closed source).

http://txl.ca/forum/viewtopic.php?t=15&sid=7d84759579f71726edffea...
--
Posted via http://www.ruby-....

Albert Schlef

3/26/2009 4:51:00 PM

0

Brian Candler wrote:
> Albert Schlef wrote:
>> That's interesting. I see that their DSL looks like:
>>
>> rule additive
>> multitive '+' additive / multitive
>> end
>>
>> Do you notice? There's "end" there but no "do"! How is it possible?
>
> Because it's not Ruby.
> [snip]
> Presumably, TXL's language *requires* a rule body followed by end.

But I was not talking about TXL. I was talking about Treetop
(http://treetop.ruby...).

--
Posted via http://www.ruby-....

Alex Gutteridge

3/26/2009 5:00:00 PM

0

On 26 Mar 2009, at 16:51, Albert Schlef wrote:

> Brian Candler wrote:
>> Albert Schlef wrote:
>>> That's interesting. I see that their DSL looks like:
>>>
>>> rule additive
>>> multitive '+' additive / multitive
>>> end
>>>
>>> Do you notice? There's "end" there but no "do"! How is it possible?
>>
>> Because it's not Ruby.
>> [snip]
>> Presumably, TXL's language *requires* a rule body followed by end.
>
> But I was not talking about TXL. I was talking about Treetop
> (http://treetop.ruby...).
>
> --
> Posted via http://www.ruby-....


Treetop grammers aren't written in Ruby (or TXL) they're their own
custom language.

Alex Gutteridge
Systems Biology Centre
University of Cambridge


James Coglan

3/26/2009 5:07:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

2009/3/26 Alex Gutteridge <alexg@ruggedtextile.com>

> On 26 Mar 2009, at 16:51, Albert Schlef wrote:
>
> Brian Candler wrote:
>>
>>> Albert Schlef wrote:
>>>
>>>> That's interesting. I see that their DSL looks like:
>>>>
>>>> rule additive
>>>> multitive '+' additive / multitive
>>>> end
>>>>
>>>> Do you notice? There's "end" there but no "do"! How is it possible?
>>>>
>>>
>>> Because it's not Ruby.
>>> [snip]
>>> Presumably, TXL's language *requires* a rule body followed by end.
>>>
>>
>> But I was not talking about TXL. I was talking about Treetop
>> (http://treetop.ruby...).
>>
>> -- Posted via http://www.ruby-....
>>
>
>
> Treetop grammers aren't written in Ruby (or TXL) they're their own custom
> language.



Though, worth noting that the language bears a very close relationship to
Ruby modules, since Treetop grammars can be mixed into each other so
languages can be composed from smaller languages. When you compile a Treetop
grammar, you get a Ruby module that can be mixed into any other class to
make that class a parser for the grammar.

--
James Coglan
http://github.c...