views:

583

answers:

9

My teacher told me that if I wanted to get the best grade in our programming class, I should code a Simple Source Code Converter.

Python to Ruby (the simplest he said)

Now my question to you: how hard is it to code a simple source code converter for python to ruby. (It should convert file controlling, Control Statements, etc.)

Do you have any tips for me?

Which language should I use to code the converter (C#, Python or Ruby)?

A: 

Sounds like a compiler to me. Though Ruby and Python are very similar, instead of a very complex set of instructions (write a parser, code generator, etc) look at the documentation of both Ruby and Python, and write the "converter" in a language you are most comfortable. For something like this I would suggest a language with good regular expressions, like Ruby/Python/Perl/Scheme(PLT).

Coltin
+1 for perl in this for simple regex
jimi hendrix
A: 

As simple as coming up with enough clever regexps that convert the syntax correctly.
Ruby and python's syntax is close enough for this to be not very hard.
You might need to do abit of extra work to rewrite stuff that you have in python that doesn't exist in ruby like listing comprehension for instance.

shoosh
A: 

First simple may mean that it does not take care of all the valid semantics of Python, but only a subset of this.

The first thing I would get would be a copy of the dragon book, which you can find in any university library. The second thing I would do would be to get a copy of the syntax and semantics of Python.

tomjen
Not to taunt Happy Fun Dragon Book, but it isn't the most friendly thing for beginners, and requires a good bit of work to become actually useful.
David Thornley
A: 

the language should not matter. pick the one you are most comfortable with strings in.

tips wise i would use a dictionary/look-up array for the keywords. The hardest part will be dealing with the white space in python

jimi hendrix
+8  A: 

I think your teacher is fibbing - this is pretty hard. It is equivalent to writing a compiler/interpreter. I don't know how much time you have available for this project, but you are typically looking at several man-years of work.

anon
And you can pretty much bet that isn't the scale of problem the professor has in mind.
Charlie Martin
ask your teacher what features he wants then implement. simple implies that it wont be every single thing in python being converted to ruby...
jimi hendrix
Yeah, somebody is having their leg pulled.
Chuck
+1  A: 

There is a name for a program which converts one type of code to another. It's called a compiler (even if the target language is not in fact machine or byte code).

Compilers are not the easiest part of computer science, and this is project that, if it were to be anything more than a toy implementation of a converter, would be a massive project. Certainly larger than what one would normally do for a class project in most university courses. (Even many/most compilers courses have fairly modest project assignments.

As to what language to use? Well, whichever one you know best is probably the answer. Though if you want to learn something new, Haskell would be a good choice, with its pattern matching features. (Disclaimer: I'm new to haskell.) (Yacc could also be used, if you're really serious about getting into compilers.)

You'll also want to consult: The Dragon Compiler Book, which is worth studying even if you don't plan to write compilers.

Rob Lachlan
A: 

It sounds like your teacher is a bit of a practical joker!

dysfunctor
A: 

The hardest part would be preserving semantics.

Like how do you deal with metaclass assignments, or function decorators, or yield-based generators when going to Ruby? I have no Ruby experience so I don't know what is directly supported.

A: 

It would be fairly simple to write a converter between Brainf*** and C. I'm sure that's well below the scope of what you need to do (I'm guessing something that's teaching about parsing context-free grammars), but would be really easy to do.

FryGuy