views:

259

answers:

4

How to parse *.py file with python?

If my question is duplicated, please let me know another thread, because I can't find it.

Why I like to do is, I like to try Basic Source Code Converter from Python to Go Language.

  • Which module should I use?
  • And your Idea on, should I go ahead or better not?
  • If I should go ahead, please let me know that, if I were you, how will you do that?

Any Suggestions will be appreciated. Thanks in advance.

+12  A: 

Have a look at the language services packages, particularly the ast.

My guess is that if you don't already have a solid grasp of both parsing as well as code generation techniques, this is going to be a difficult project to undertake.

good luck!

Suppressingfire
+3  A: 

The Boo Solution

Are you trying to make a python-like language, that can compiles into Go? This seems most sensible, as you will want to do Go-specific things (to take advantage of Go features).

Look at pyparsing. It includes an example of a complete python parser, but you probably don't want to do that.

You want to incrementally build your converter / translator, so you want to incrementally build the parser, otherwise you might choke on the AST. OK, you could parse everything and just ignore the stuff you don't understand, but that's not great behavior from a compiler.

You could start with parsing basic arithmetic.

The Pyrex Solution

This is similar to the Boo solution, but much harder. Get the Boo solution working first. Then learn to generate wrapper code, so your Go and python parts can work together.

The PyPy Solution

A complete Python-Go compiler? Good luck. You'll need it.

wisty
Thanks for clear explanations, I wish I could vote twice. I like the idea of shedskin, which doing source level converting from Python to C++, but as you know Go has lots of built-in data types which close to python, so I think It could be done.
S.Mark
+2  A: 

There's a good list of parsers rounded-up by Ned Batchelder which might help.

jkp
+3  A: 

As for the 'should I go ahead or better not' question: why do you want to do this in the first place?

If it's a purely learning exercise, then you don't don't need to ask us whether it's worthwhile. You want to learn, so go right ahead.

If it's meant to be a practical tool, then my suggestion is to not do it. An industrial-strength tool to perform such conversions might be useful but I would guess that you're not going to go that far. With that in mind it's probably more fruitful to rewrite the Python code in Go manually.

That assumes there is any real benefit to compiling to Go; current testing suggests that you get better performance and similar code structure from using Stackless Python.

Kylotan
I am not intend to do that for exercise for sure. :DYeah, maintaining code and going upto Industrial standard tool is very important factor need to consider, Thanks for info about stackless python also
S.Mark