tatsu将ebnf变体中的语法作为输入,并在python中输出一个记忆peg/packrat解析器。
TatSu的Python项目详细描述
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作为输入,输出memoizing(Packrat) PEG解析器在Python。
竜tatsu可以将存储在字符串中的语法编译为 tatsu.grammars.Grammar对象,可用于分析任何给定的 输入,就像re模块处理正则表达式一样,或者它可以生成实现解析器的Python模块。
竜{str 1}$tatsu支持PEG语法中的left-recursive规则,使用algorithmbylaurent和mens。生成的AST具有预期的左关联性。
安装
$ pip install TatSu
使用工具
竜tatsu可以用作库,非常类似于Python的re,方法是将语法作为字符串嵌入并生成语法模型,而不是生成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。