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

最佳答案

您尚未定义number,我在文档中也没有看到预定义的内容。

关于python - 我的EBNF逻辑在这里错误吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5481315/

10-12 16:59