【发布时间】:2011-03-30 02:46:35
【问题描述】:
我在 Python 程序中使用SimpleParse 来解析一些相当简单的语言。它应该能够解析以下示例文本(每行单独):
d6
(d4 + d8 + 5) + 6
{5d20}+12
[d10 + 6d6] + 9
(d10 + d12) + 8d8
我已经为上面的内容编写了以下 EBNF,但解析器总是在我身上崩溃,即使是在“d6”的简单情况下:
# 'number' is already predefined in SimpleParse to parse exactly what you think it will parse
root := roll
roll := space,operations,space
operations := function+
function := ((dice,op,function)/(grouping,op,function)/(function,op,grouping))/(dice/grouping/constant) #just to clarify, the '/' is a FirstOf operator
constant := number
grouping := ([[(],operations,[])])/'{',dice,'}'
dice := number?,[dD],number
op := space,[-+],space
space := [ \t]*
我开始怀疑我的 EBNF 逻辑是否在某个地方弄错了。
编辑:对于好奇的人,这是最终的 EBNF 的样子:
roll := space,operations,space
operations := function
function := (dice,op,operations)/(grouping,op,operations)/dice/constant/grouping
constant := number
grouping := ('(',operations,')')/('{',dice,'}')/('[',operations,']')
dice := number?,[dD],number
op := space,[-+],space
space := [ \t]*
【问题讨论】: