我的EBNF逻辑错了吗?

发布于 2024-10-27 13:42:44 字数 1280 浏览 0 评论 0原文

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

I'm using SimpleParse in a Python program in order to parse some rather simple linguistics. It should be able to parse the following sample text (each line separately):

d6
(d4 + d8 + 5) + 6
{5d20}+12
[d10 + 6d6] + 9
(d10 + d12) + 8d8

I've written up the following EBNF for the above, but the parser keeps crashing on me, even on the simple case of "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]*

I'm beginning to wonder if maybe I've gotten the logic in my EBNF wrong somewhere.

EDIT: For the curious, here is what the final EBNF looks like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-11-03 13:42:44

您尚未定义 number,并且我没有在文档中看到它预定义的。

You haven't defined number, and I don't see it predefined in the docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文