gpt4 book ai didi

pyparsing 不解析整个字符串

转载 作者:行者123 更新时间:2023-12-04 23:15:57 28 4
gpt4 key购买 nike

我有以下语法和测试用例:

from pyparsing import Word, nums, Forward, Suppress, OneOrMore, Group

#A grammar for a simple class of regular expressions
number = Word(nums)('number')
lparen = Suppress('(')
rparen = Suppress(')')

expression = Forward()('expression')

concatenation = Group(expression + expression)
concatenation.setResultsName('concatenation')

disjunction = Group(lparen + OneOrMore(expression + Suppress('|')) + expression + rparen)
disjunction.setResultsName('disjunction')

kleene = Group(lparen + expression + rparen + '*')
kleene.setResultsName('kleene')

expression << (number | disjunction | kleene | concatenation)

#Test a simple input
tests = """
(8)*((3|2)|2)
""".splitlines()[1:]

for t in tests:
print t
print expression.parseString(t)
print

结果应该是
[['8', '*'],[['3', '2'], '2']]

但相反,我只得到
[['8', '*']]

如何让 pyparsing 解析整个字符串?

最佳答案

parseString有一个参数parseAll .如果您调用parseStringparseAll=True如果您的语法没有解析整个字符串,您将收到错误消息。从那里去!

关于pyparsing 不解析整个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42168000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com