我在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]*
答案 0 :(得分:2)
您尚未定义number
,我在文档中未预先定义它。