我认为您已经准备好了大部分内容,您只是在递归部分苦苦挣扎,其中评论本身可以包含子结构,包括更多评论。
你有这个作为你的 BNF:
structure ::= [cars] [colors] [comments] [buyers]
cars ::= '#' integer, ... '#'
buyers ::= '<' integer, ... '>'
根据您给出的示例,我用这些猜测填空:
color ::= word composed of alphas
colors ::= (color | '(' color ')' )...
comments ::= '(' structure ';' ... ')'
我采用了您对汽车和买家的定义,并添加了颜色和递归定义以供评论。然后进行了从 BNF 到 pyparsing 表达式的相当死记硬背的转换:
integer = pp.pyparsing_common.integer
car_ref = "#" + pp.Group(pp.delimitedList(integer))("cars") + "#"
buyer_ref = "<" + pp.Group(pp.delimitedList(integer))("buyers") + ">"
# not sure if this will be sufficient for color, but it works for the given examples
color = pp.Word(pp.alphas)
colors = pp.originalTextFor(pp.OneOrMore(color | '(' + color + ')'))("colors")
# define comment placeholder so it can be used in definition of structure
comment = pp.Forward()
structure = pp.Group(pp.Optional(car_ref)
+ pp.Optional(colors)
+ pp.Optional(comment)("comments")
+ pp.Optional(buyer_ref))
# now insert the definition of a comment as a delimited list of structures; this takes care of
# any nesting of comments within comments
LPAREN, RPAREN = map(pp.Suppress, "()")
comment <<= pp.Group(LPAREN + pp.Optional(pp.delimitedList(structure, delim=';')) + RPAREN)
棘手的部分是将 s 的内容定义comment
为 s 的分隔列表structure
,并使用<<=
运算符将该定义插入到先前定义的 Forward() 占位符中。
将您的示例传递给structure.runTests()
给(默认行为是将类似 Python 的注释视为注释,因此在使用您的特定示例调用 runTests 时我们必须禁用此功能,因为前导“#”是汽车的有效介绍):
structure.runTests(examples, comment=None)
red green
[['red green']]
[0]:
['red green']
- colors: 'red green'
#1# red green
[['#', [1], '#', 'red green']]
[0]:
['#', [1], '#', 'red green']
- cars: [1]
- colors: 'red green'
#1# red green <2>
[['#', [1], '#', 'red green', '<', [2], '>']]
[0]:
['#', [1], '#', 'red green', '<', [2], '>']
- buyers: [2]
- cars: [1]
- colors: 'red green'
#1,2# red green <2,3>
[['#', [1, 2], '#', 'red green', '<', [2, 3], '>']]
[0]:
['#', [1, 2], '#', 'red green', '<', [2, 3], '>']
- buyers: [2, 3]
- cars: [1, 2]
- colors: 'red green'
red green ()
[['red green', [[]]]]
[0]:
['red green', [[]]]
- colors: 'red green'
- comments: [[]]
[0]:
[]
#1# red green (blue)
[['#', [1], '#', 'red green (blue)']]
[0]:
['#', [1], '#', 'red green (blue)']
- cars: [1]
- colors: 'red green (blue)'
#1# red green (#5# blue) <2>
[['#', [1], '#', 'red green', [['#', [5], '#', 'blue']], '<', [2], '>']]
[0]:
['#', [1], '#', 'red green', [['#', [5], '#', 'blue']], '<', [2], '>']
- buyers: [2]
- cars: [1]
- colors: 'red green'
- comments: [['#', [5], '#', 'blue']]
[0]:
['#', [5], '#', 'blue']
- cars: [5]
- colors: 'blue'
#1# red green (#5# blue <6>) <2>
[['#', [1], '#', 'red green', [['#', [5], '#', 'blue', '<', [6], '>']], '<', [2], '>']]
[0]:
['#', [1], '#', 'red green', [['#', [5], '#', 'blue', '<', [6], '>']], '<', [2], '>']
- buyers: [2]
- cars: [1]
- colors: 'red green'
- comments: [['#', [5], '#', 'blue', '<', [6], '>']]
[0]:
['#', [5], '#', 'blue', '<', [6], '>']
- buyers: [6]
- cars: [5]
- colors: 'blue'
#1,2# red green (#5# blue (purple) <6>;#7# yellow <10>) <2,3>
[['#', [1, 2], '#', 'red green', [['#', [5], '#', 'blue (purple)', '<', [6], '>'], ['#', [7], '#', 'yellow', '<', [10], '>']], '<', [2, 3], '>']]
[0]:
['#', [1, 2], '#', 'red green', [['#', [5], '#', 'blue (purple)', '<', [6], '>'], ['#', [7], '#', 'yellow', '<', [10], '>']], '<', [2, 3], '>']
- buyers: [2, 3]
- cars: [1, 2]
- colors: 'red green'
- comments: [['#', [5], '#', 'blue (purple)', '<', [6], '>'], ['#', [7], '#', 'yellow', '<', [10], '>']]
[0]:
['#', [5], '#', 'blue (purple)', '<', [6], '>']
- buyers: [6]
- cars: [5]
- colors: 'blue (purple)'
[1]:
['#', [7], '#', 'yellow', '<', [10], '>']
- buyers: [10]
- cars: [7]
- colors: 'yellow'
#1,2# red (maroon) green (#5# blue (purple) <6>;#7# yellow <10>) <2,3>
[['#', [1, 2], '#', 'red (maroon) green', [['#', [5], '#', 'blue (purple)', '<', [6], '>'], ['#', [7], '#', 'yellow', '<', [10], '>']], '<', [2, 3], '>']]
[0]:
['#', [1, 2], '#', 'red (maroon) green', [['#', [5], '#', 'blue (purple)', '<', [6], '>'], ['#', [7], '#', 'yellow', '<', [10], '>']], '<', [2, 3], '>']
- buyers: [2, 3]
- cars: [1, 2]
- colors: 'red (maroon) green'
- comments: [['#', [5], '#', 'blue (purple)', '<', [6], '>'], ['#', [7], '#', 'yellow', '<', [10], '>']]
[0]:
['#', [5], '#', 'blue (purple)', '<', [6], '>']
- buyers: [6]
- cars: [5]
- colors: 'blue (purple)'
[1]:
['#', [7], '#', 'yellow', '<', [10], '>']
- buyers: [10]
- cars: [7]
- colors: 'yellow'
如果您使用您将所有解析结果转换为常规 Python dicts asDict()
:
structure.runTests(examples, comment=None,
postParse=lambda test, results: results[0].asDict()
)
red green
{'colors': 'red green'}
#1# red green
{'cars': [1], 'colors': 'red green'}
#1# red green <2>
{'colors': 'red green', 'cars': [1], 'buyers': [2]}
#1,2# red green <2,3>
{'colors': 'red green', 'cars': [1, 2], 'buyers': [2, 3]}
red green ()
{'comments': [[]], 'colors': 'red green'}
#1# red green (blue)
{'cars': [1], 'colors': 'red green (blue)'}
#1# red green (#5# blue) <2>
{'colors': 'red green', 'cars': [1], 'comments': [{'cars': [5], 'colors': 'blue'}], 'buyers': [2]}
#1# red green (#5# blue <6>) <2>
{'colors': 'red green', 'cars': [1], 'comments': [{'colors': 'blue', 'cars': [5], 'buyers': [6]}], 'buyers': [2]}
#1,2# red green (#5# blue (purple) <6>;#7# yellow <10>) <2,3>
{'colors': 'red green', 'cars': [1, 2], 'comments': [{'colors': 'blue (purple)', 'cars': [5], 'buyers': [6]}, {'colors': 'yellow', 'cars': [7], 'buyers': [10]}], 'buyers': [2, 3]}
#1,2# red (maroon) green (#5# blue (purple) <6>;#7# yellow <10>) <2,3>
{'colors': 'red (maroon) green', 'cars': [1, 2], 'comments': [{'colors': 'blue (purple)', 'cars': [5], 'buyers': [6]}, {'colors': 'yellow', 'cars': [7], 'buyers': [10]}], 'buyers': [2, 3]}