Parsing a context-free grammar in Python

人走茶凉 提交于 2019-11-30 03:34:59

问题


What tools are available in Python to assist in parsing a context-free grammar?

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.


回答1:


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.

I used it to implement my parser for ANSI C and the interaction with PLY was almost effortless.



来源:https://stackoverflow.com/questions/2979703/parsing-a-context-free-grammar-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!