如何使用Python解析*.py文件?

huangapple go评论106阅读模式
英文:

How to parse *.py file with python?

问题

我想解析Python源代码,以尝试制作一个从Python到Go的基本源代码转换器。

  • 我应该使用哪个模块?
  • 我应该继续还是不继续?
  • 如果我应该继续,应该如何进行?
英文:

I'd like to parse Python source in order to try making a basic source code converter from Python to Go.

  • What module should I use?
  • Should I proceed or not?
  • If I should proceed, how?

答案1

得分: 14

请查看<a href="http://docs.python.org/library/language.html">语言服务</a>包,特别是<a href="http://docs.python.org/library/ast.html">ast</a>。

我猜想,如果你对解析和代码生成技术都没有很好的掌握,这将是一个难以完成的项目。

祝你好运!

英文:

Have a look at the <a href="http://docs.python.org/library/language.html">language services</a> packages, particularly the <a href="http://docs.python.org/library/ast.html">ast</a>.

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!

答案2

得分: 3

Boo解决方案

你是否想要创建一种类似Python的语言,可以编译成Go?这似乎是最明智的选择,因为你想要做一些Go特定的事情(以利用Go的特性)。

看看pyparsing。它包含了一个完整的Python解析器的示例,但你可能不想这样做。

你希望逐步构建你的转换器/翻译器,所以你想要逐步构建解析器,否则你可能会在AST上出现问题。好吧,你可以解析所有内容,然后只忽略你不理解的部分,但这对于一个编译器来说并不是很好的行为。

你可以从解析基本算术开始。

Pyrex解决方案

这与Boo解决方案类似,但更难。先让Boo解决方案正常工作。然后学习生成包装代码,这样你的Go和Python部分可以一起工作。

PyPy解决方案

一个完整的Python-Go编译器?祝你好运。你会需要的。

英文:

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.

1: http://pyparsing.wikispaces.com "pyparsing"
2: http://pyparsing.wikispaces.com/file/view/pythonGrammarParser.py "pyparsing-python"
3: http://pyparsing.wikispaces.com/file/view/fourFn.py "pyparsing-calculator"

答案3

得分: 3

关于“我应该继续还是最好不要”的问题:你为什么要首先这样做呢?

如果这只是一个纯粹的学习练习,那么你不需要问我们是否值得。你想学习,所以就继续吧。

如果这是一个实用工具,那么我的建议是不要这样做。一个强大的工具来执行这样的转换可能会有用,但我猜你不会走那么远。考虑到这一点,手动将Python代码重写为Go可能更有成果。

这假设编译为Go有任何真正的好处;目前的测试表明使用Stackless Python可以获得更好的性能和类似的代码结构。

英文:

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.

答案4

得分: 2

有一个由Ned Batchelder整理的很好的解析器列表,可能会有所帮助。

英文:

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

huangapple
  • 本文由 发表于 2009年11月24日 18:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/1789301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定