tatsu将ebnf变体中的语法作为输入,并在python中输出一个记忆peg/packrat解析器。

TatSu的Python项目详细描述


licensepyversionsfurycirclecitravisdocs

At least for the people who send me mail about a new language that they’re designing, the general advice is: do it to learn about how to write a compiler. Don’t have any expectations that anyone will use it, unless you hook up with some sort of organization in a position to push it hard. It’s a lottery, and some can buy a lot of the tickets. There are plenty of beautiful languages (more beautiful than C) that didn’t catch on. But someone does win the lottery, and doing a language at least teaches you something.

Dennis Ritchie (1941-2011) Creator of the C programming language and of Unix

竜{str 1}$tatsu

defWARNING():return'v4.4.0 is the last version of |TatSu| supporting Python 2.7'

tatsu(继承Grako)是一个在 变量EBNF作为输入,输出memoizingPackratPEG解析器在Python

tatsu可以将存储在字符串中的语法编译为 tatsu.grammars.Grammar对象,可用于分析任何给定的 输入,就像re模块处理正则表达式一样,或者它可以生成实现解析器的Python模块。

竜{str 1}$tatsu支持PEG语法中的left-recursive规则,使用algorithmbylaurentmens。生成的AST具有预期的左关联性。

安装

$ pip install TatSu

使用工具

tatsu可以用作库,非常类似于Pythonre,方法是将语法作为字符串嵌入并生成语法模型,而不是生成Python代码。

  • tatsu.compile(grammar, name=None, **kwargs)

    编译语法并生成一个model模型,该模型随后可用于解析输入。

  • tatsu.parse(grammar, input, **kwargs)

    编译语法并解析给定的输入,结果生成AST。结果相当于调用:

    model = compile(grammar)
    ast = model.parse(input)
    

    为了提高效率,已编译的语法被缓存。

  • tatsu.to_python_sourcecode(grammar, name=None, filename=None, **kwargs)

    将语法编译为实现解析器的Python源代码。

这是一个如何使用竜tatsu作为库的示例:

GRAMMAR='''
    @@grammar::CALC


    start = expression $ ;


    expression
        =
        | expression '+' term
        | expression '-' term
        | term
        ;


    term
        =
        | term '*' factor
        | term '/' factor
        | factor
        ;


    factor
        =
        | '(' expression ')'
        | number
        ;


    number = /\d+/ ;
'''if__name__=='__main__':importpprintimportjsonfromtatsuimportparsefromtatsu.utilimportasjsonast=parse(GRAMMAR,'3 + 5 * ( 10 - 20 )')print('# PPRINT')pprint.pprint(ast,indent=2,width=20)print()print('# JSON')print(json.dumps(asjson(ast),indent=2))print()

竜{str 1}$tatsu将使用语法中定义的第一个规则作为start规则。

这是输出:

# PPRINT
[ '3',
  '+',
  [ '5',
    '*',
    [ '10',
      '-',
      '20']]]

# JSON
[
  "3",
  "+",
  [
    "5",
    "*",
    [
      "10",
      "-",
      "20"
    ]
  ]
]

文件

有关竜tatsu功能的详细说明,请参见 documentation

有问题吗?

请使用StackOverflow上的[tatsu]标记进行常规问答,并将github问题限制为bug、增强建议和功能请求。

变化

有关详细信息,请参见CHANGELOG

许可证

根据BSD式许可的条款,您可以使用tatsu。 在所附的LICENSE.txt文件中描述。如果您的项目 需要不同的许可证email

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用“Wed,01 Jul 2015 17:32:41 EDT”解析字符串   java Storm apache升级(1.0.0到2.0.0)   java类驻留在不同的目录中,而不是包指定的目录。为什么?   将Java中的图像缩放到非常小的维度   java如何通过子文档从自定义方面访问ElasticSearch parentdoc字段   java如何在RationalSoftwareArchitect中使用findbugs?   Java中的事件提升处理   java值被添加到arrayList的所有索引中,而不是在“”时添加到最后一个索引中。正在使用arraylist的add()方法   JFrame中的java JPanel派生类   java如何用循环和异步方法模拟类   java Android阻止可绘制背景超出视图范围   为客户排序Java阵列   java Apache poi如何将工作表设置为枚举位置值属性?   java Rhino在使用自定义类参数调用javascript函数时出错   java格式化日期从年月日到年月日   spring如何修复java。lang.illegalargumentexception在此特定场景中是否尝试创建具有null实体的合并事件?   java如何创建更好的对象