类似于Python的语言的Python解析器 [英] Python parser for Python-like language

查看:160
本文介绍了类似于Python的语言的Python解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为源文件编写Python导入过滤器或预处理器,这些源文件本质上是具有额外语言元素的Python.目的是读取源文件,将其解析为抽象语法树,应用一些转换以实现该语言的新部分,并编写有效的Python源,然后CPython可以使用该源.我想用Python编写此东西,并正在寻找适合该任务的最佳解析器.

I'm looking to write a Python import filter or preprocessor for source files that are essentially Python with extra language elements. The goal is to read the source file, parse it to an abstract syntax tree, apply some transforms in order to implement the new parts of the language, and write valid Python source which can then be consumed by CPython. I want to write this thing in Python and am looking for the best parser for the task.

Python内置的解析器是不合适的,因为它要求源文件是实际的Python,而实际上不是.有很多可以与Python一起使用的解析器(或解析器生成器),但是如果不进行大量研究,很难说哪一个最适合我的需求.

The parser built in to Python is not appropriate because it requires the source files be actual Python, which these will not be. There are tons of parsers (or parser generators) that will work with Python, but it's hard to tell which is the best for my needs without a whole bunch of research.

总而言之,我的要求是:

In summary, my requirements are:

  1. 解析器是用Python编写的或具有Python绑定.
  2. 带有我可以调整的Python语法,或者可以轻松使用其他地方可用的可调整的Python语法(例如
  1. Parser is written in Python or has Python bindings.
  2. Comes with a Python grammar that I can tweak, or can easily consume a tweakable Python grammar available elsewhere (such as http://docs.python.org/reference/grammar.html).
  3. Can re-serialize the AST after transforming it.
  4. Should not be too horrific to work with API-wise.

有什么建议吗?

推荐答案

首先想到的是 lib2to3 .它是Python解析器的完整的纯Python实现.它读取一个Python语法文件,并根据该语法分析Python源文件.它提供了一个强大的基础结构,可以执行AST操作并写出格式正确的Python代码-毕竟它的目的是在两种语法稍有不同的类Python语言之间进行转换.

The first thing that comes to mind is lib2to3. It is a complete pure-Python implementation of a Python parser. It reads a Python grammar file and parses Python source files according to this grammar. It offers a great infrastructure for performing AST manipulations and writing back nicely formatted Python code -- after all it's purpose is to transform between two Python-like languages with slightly different grammars.

不幸的是,它缺少文档并且不能保证稳定的界面.不过,有些项目是建立在lib2to3之上的,并且源代码可读性强.如果API稳定性是一个问题,则可以将其分叉.

Unfortunately it's lacking documentation and doesn't guarantee a stable interface. There are projects that build on top of lib2to3 nevertheless, and the source code is quite readable. If API stability is an issue, you can just fork it.

这篇关于类似于Python的语言的Python解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆