python – 如何使用pyparsing来解析具有多个开启/关闭类型的嵌套表达式?

我想使用pyparsing来解析形式的表达式:expr ='(gimme [some {nested [lists]}])’,并得到一个以下形式的python列表:[[[gimme’,[ ‘some’,[‘nested’,[‘lists’]]]]]].现在我的语法如下所示:

nestedParens = nestedExpr(‘(‘,’)’)
nestedBrackets = nestedExpr(‘[‘,’]’)
nestedCurlies = nestedExpr(‘{‘,’}’)
closed = nestedParens | nestedBrackets | nestedCurlies

目前,surround.searchString(expr)返回一个表单的列表:[[[‘gimme’,[‘some’,'{nested’,'[lists]}’]]]].这不是我想要的,因为它不能识别正方形或大括号,但我不知道为什么.

最佳答案
这是一个使用自修改语法动态匹配正确的关闭括号字符的解决方案.

from pyparsing import *

data = '(gimme [some {nested, nested [lists]}])'

opening = oneOf("( { [")
nonBracePrintables = ''.join(c for c in printables if c not in '(){}[]')
closingFor = dict(zip("({[",")}]"))
closing = Forward()
# initialize closing with an expression
closing << NoMatch()
closingStack = []
def pushClosing(t):
    closingStack.append(closing.expr)
    closing << Literal( closingFor[t[0]] )
def popClosing():
    closing << closingStack.pop()
opening.setParseAction(pushClosing)
closing.setParseAction(popClosing)

matchedNesting = nestedExpr( opening, closing, Word(alphas) | Word(nonBracePrintables) )

print matchedNesting.parseString(data).asList()

打印:

[['gimme', ['some', ['nested', ',', 'nested', ['lists']]]]]

更新:我发布了上述解决方案,因为我一年前曾经写过一个实验.我只是仔细看看你的原始帖子,这让我想到了由operatorPrecedence方法创建的递归类型定义,所以我使用你的原始方法来改变这个解决方案 – 要简单得多! (可能具有正确输入数据的左递归问题,但未经过彻底测试):

from pyparsing import *

enclosed = Forward()
nestedParens = nestedExpr('(', ')', content=enclosed) 
nestedBrackets = nestedExpr('[', ']', content=enclosed) 
nestedCurlies = nestedExpr('{', '}', content=enclosed) 
enclosed << (Word(alphas) | ',' | nestedParens | nestedBrackets | nestedCurlies)


data = '(gimme [some {nested, nested [lists]}])' 

print enclosed.parseString(data).asList()

得到:

[['gimme', ['some', ['nested', ',', 'nested', ['lists']]]]]

转载注明原文:python – 如何使用pyparsing来解析具有多个开启/关闭类型的嵌套表达式? - 代码日志