Forum: Ruby Writing a language converter in Ruby ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
998dcd37297540b08b20a6558c40ec64?d=identicon&s=25 Zouplaz (Guest)
on 2009-03-20 12:06
(Received via mailing list)
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/Products/CocoR/) ? Or is there a
standalone approach that only uses Ruby capacities ?
42773b24e0c3fb506a8a875c058a9dd7?d=identicon&s=25 Dylan Evans (Guest)
on 2009-03-20 13:38
(Received via mailing list)
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.
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-03-21 04:13
(Received via mailing list)
Zouplaz wrote:
>
Why not use existing tools? I like treetop, myself:
http://treetop.rubyforge.org/

-Justin
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-23 16:51
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.
0814ff69e309b033508b3c7f363236fd?d=identicon&s=25 Ian Trudel (backorder)
on 2009-03-23 18:39
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/Products/CocoR/) ? 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://www.txl.ca/

Regards,
Ian
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-03-24 09:52
Justin Collins wrote:
> Why not use existing tools? I like treetop, myself:
> http://treetop.rubyforge.org/
>
> -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?
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-24 11:24
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-03-24 12:59
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=7d84759...
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-03-26 17:54
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.rubyforge.org/).
Ab7126737dc26c07393d69d7fb6571e9?d=identicon&s=25 Alex Gutteridge (Guest)
on 2009-03-26 18:04
(Received via mailing list)
On 26 Mar 2009, at 16:51, Albert Schlef wrote:

>> 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.rubyforge.org/).
>
> --
> Posted via http://www.ruby-forum.com/.


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

Alex Gutteridge
Systems Biology Centre
University of Cambridge
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-03-26 18:10
(Received via mailing list)
2009/3/26 Alex Gutteridge <alexg@ruggedtextile.com>

>>>>  end
>> (http://treetop.rubyforge.org/).
>>
>> -- Posted via http://www.ruby-forum.com/.
>>
>
>
> 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.
This topic is locked and can not be replied to.