我在 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]*