Writing a language converter in Ruby ?

Z

Zouplaz

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 ?
 
D

Dylan Evans

[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.
 
J

Justin Collins

Zouplaz said:
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 ?

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

-Justin
 
B

Brian Candler

Dylan said:
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.
 
I

Ian Trudel

Zouplaz said:
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
 
B

Brian Candler

Albert said:
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.
 
A

Albert Schlef

Brian said:
Albert said:
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/).
 
A

Alex Gutteridge

Brian said:
Albert said:
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/).


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

Alex Gutteridge
Systems Biology Centre
University of Cambridge
 
J

James Coglan

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

2009/3/26 Alex Gutteridge said:
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/).

-- 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.
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top