在Python中解析上下文无关的语法 [英] Parsing a context-free grammar in Python
问题描述
Python提供了哪些工具来帮助解析上下文无关的语法?
What tools are available in Python to assist in parsing a context-free grammar?
当然可以自己滚动,但是我正在寻找一个可以为给定CFG生成解析器的通用工具。
Of course it is possible to roll my own, but I am looking for a generic tool that can generate a parser for a given CFG.
推荐答案
我热烈推荐 PLY -它是Python中的Lex / Yacc克隆,它以复杂的方式使用语言的自省功能,可以很自然地指定语法。回想一下,Yacc是CFG在可理解的DSL中的体现,它定义了解析方式。
I warmly recommend PLY - it's a Lex/Yacc clone in Python that uses the language's introspection facilities in a sophisticated manner to allow for a very natural specification of the grammar. Yacc, if you recall, is the very embodiment of CFGs in an understandable DSL that defines how one parses them.
我用它来实现用于ANSI C的解析器,并且与PLY的交互几乎毫不费力。
I used it to implement my parser for ANSI C and the interaction with PLY was almost effortless.
这篇关于在Python中解析上下文无关的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!