Python的Parsec实现? [英] Python implementation of Parsec?

查看:141
本文介绍了Python的Parsec实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用Ply在Python中编写了一个解析器(这是yacc的python重新实现).当我几乎完成解析器的工作后,我发现我需要解析的语法要求我在解析过程中进行一些查找以告知词法分析器.如果不进行查询以通知词法分析器,就无法正确解析该语言中的字符串.

I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the strings in the language.

鉴于我可以从语法规则中控制词法分析器的状态,我想我将使用解析器模块中的查找表来解决用例,但是维护/测试可能变得太困难了.因此,我想了解其他一些选项.

Given than I can control the state of the lexer from the grammar rules I think I'll be solving my use case using a look up table in the parser module, but it may become too difficult to maintain/test. So I want to know about some of the other options.

在Haskell中,我将使用Parsec(解析函数库)(称为组合器).有Parsec的Python实现吗?还是其他一些具有解析功能的生产质量库,以便我可以在Python中构建上下文相关的解析器?

In Haskell I would use Parsec, a library of parsing functions (known as combinators). Is there a Python implementation of Parsec? Or perhaps some other production quality library full of parsing functionality so I can build a context sensitive parser in Python?

我所有的上下文自由解析尝试都失败了.因此,我不希望ANTLR在这里有用.

All my attempts at context free parsing have failed. For this reason, I don't expect ANTLR to be useful here.

推荐答案

如果可以使用LL解析器,则可以考虑使用

An option you may consider, if an LL parser is ok to you, is to give ANTLR a try, it can generate python too (actually it is LL(*) as they name it, * stands for the quantity of lookahead it can cope with).

这篇关于Python的Parsec实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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