使用哪个工具解析Python中的编程语言? [英] Which tool to use to parse programming languages in Python?

查看:98
本文介绍了使用哪个工具解析Python中的编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以推荐使用哪种Python工具来解析编程语言?它应该允许在源代码中以可读的方式表示语言语法,并且应该能够扩展到复杂的语言(诸如Python本身这样复杂的语法).

Which Python tool can you recommend to parse programming languages? It should allow for a readable representation of the language grammar inside the source, and it should be able to scale to complicated languages (something with a grammar as complex as e.g. Python itself).

搜索时,我主要会发现pyparsing,我将对其进行评估,但是我当然对其他选择感兴趣.

When I search, I mostly find pyparsing, which I will be evaluating, but of course I'm interested in other alternatives.

如果它附带良好的错误报告以及语法树元素附带的源代码位置,则可加分.

Bonus points if it comes with good error reporting and source code locations attached to syntax tree elements.

推荐答案

我真的很喜欢

I really like pyPEG. Its error reporting isn't very friendly, but it can add source code locations to the AST.

pyPEG没有单独的词法分析器,这会使解析Python本身变得很困难(我认为CPython可以识别词法分析器中的缩进和缩进),但是我使用pyPEG为C#的子集构建了一个解析器,而工作却出乎意料的少

pyPEG doesn't have a separate lexer, which would make parsing Python itself hard (I think CPython recognises indent and dedent in the lexer), but I've used pyPEG to build a parser for subset of C# with surprisingly little work.

fdik.org/pyPEG/改编而成的示例:像这样的简单语言:

An example adapted from fdik.org/pyPEG/: A simple language like this:

function fak(n) {
    if (n==0) { // 0! is 1 by definition
        return 1;
    } else {
        return n * fak(n - 1);
    };
}

该语言的pyPEG解析器:

A pyPEG parser for that language:

def comment():          return [re.compile(r"//.*"),
                                re.compile("/\*.*?\*/", re.S)]
def literal():          return re.compile(r'\d*\.\d*|\d+|".*?"')
def symbol():           return re.compile(r"\w+")
def operator():         return re.compile(r"\+|\-|\*|\/|\=\=")
def operation():        return symbol, operator, [literal, functioncall]
def expression():       return [literal, operation, functioncall]
def expressionlist():   return expression, -1, (",", expression)
def returnstatement():  return keyword("return"), expression
def ifstatement():      return (keyword("if"), "(", expression, ")", block,
                                keyword("else"), block)
def statement():        return [ifstatement, returnstatement], ";"
def block():            return "{", -2, statement, "}"
def parameterlist():    return "(", symbol, -1, (",", symbol), ")"
def functioncall():     return symbol, "(", expressionlist, ")"
def function():         return keyword("function"), symbol, parameterlist, block
def simpleLanguage():   return function

这篇关于使用哪个工具解析Python中的编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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