如何使用 pyparsing 来解析具有多个开启/关闭类型的嵌套表达式?
我想使用 pyparsing 来解析以下形式的表达式: expr = '(gimme [some {nested [lists]}])'
,并返回以下形式的 python 列表: <代码>[[['gimme', ['some', ['nested', ['lists']]]]]]。现在我的语法如下所示:
nestedParens =nestedExpr('(', ')')
nestedBrackets =nestedExpr('[', ']')
nestedCurlies =nestedExpr('{', '}')
封闭 = 嵌套Parens |嵌套括号 | nestedCurlies
目前,enheld.searchString(expr)
返回以下形式的列表:[[['gimme', ['some', '{nested', '[lists]}'] ]]]
。这不是我想要的,因为它无法识别方括号或大括号,但我不知道为什么。
I'd like to use pyparsing to parse an expression of the form: expr = '(gimme [some {nested [lists]}])'
, and get back a python list of the form: [[['gimme', ['some', ['nested', ['lists']]]]]]
. Right now my grammar looks like this:
nestedParens = nestedExpr('(', ')')
nestedBrackets = nestedExpr('[', ']')
nestedCurlies = nestedExpr('{', '}')
enclosed = nestedParens | nestedBrackets | nestedCurlies
Presently, enclosed.searchString(expr)
returns a list of the form: [[['gimme', ['some', '{nested', '[lists]}']]]]
. This is not what I want because it's not recognizing the square or curly brackets, but I don't know why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 pyparsing 解决方案,它使用自修改语法来动态匹配正确的右大括号字符。
prints:
更新: 我发布了上述解决方案,因为我实际上是在一年前作为实验编写的。我只是仔细查看了您的原始帖子,它让我想到了由
operatorPrecedence
方法创建的递归类型定义,因此我使用您原来的方法重新编写了此解决方案 - 更容易遵循! (虽然右输入数据可能存在左递归问题,但未经过彻底测试):给出:
编辑:
这是更新后的解析器的图表,使用 pyparsing 3.0 中提供的铁路图表支持。
Here's a pyparsing solution that uses a self-modifying grammar to dynamically match the correct closing brace character.
prints:
Updated: I posted the above solution because I had actually written it over a year ago as an experiment. I just took a closer look at your original post, and it made me think of the recursive type definition created by the
operatorPrecedence
method, and so I redid this solution, using your original approach - much simpler to follow! (might have a left-recursion issue with the right input data though, not thoroughly tested):Gives:
EDITED:
Here is a diagram of the updated parser, using the railroad diagramming support coming in pyparsing 3.0.
这应该对你有用。我在你的例子上测试了它:
如果你需要更多,请评论
This should do the trick for you. I tested it on your example:
Comment if you need more