处理 BNF 语法中的歧义

发布于 2024-11-30 00:54:33 字数 890 浏览 1 评论 0原文

我正在尝试为我正在 .NET 中使用 Irony 构建的 DSL 构建一个解析器,但发现了一个我找不到解决方法的问题。由于它处理 BNF,我认为任何 BNF 解决方案都会有所帮助。

我有以下输入:

$10 yesterday at drug store 

使用以下语法:

<expr> :== unit | expr + unit
<unit> :==  money | date | location

<date> : == yesterday|today|tomorrow
<location> :== .* | <preposition> .*
<preposition> :== at
<money> :== ((\$)?\d*((\.*)\d*)*\,?\d{1,2})

使用此输入就像魅力一样。我得到的正是我想要的结果:

Money Amount: 10
Date: Yesterday
Location: Drug Store

但是,如果我由于减少步骤而按如下方式更改输入的顺序,

$10 at drug store yesterday

则它无法给我相同的输出。输出变为:

Money amount: 10
Location: Drug Store Yesterday

我想知道是否有办法确保仅在捕获所有其他标记并且不剩下任何其他标记时才评估 Location (这是一个真正广泛的正则表达式匹配)。

任何帮助表示赞赏。

谢谢!

编辑:根据建议更新标题

I'm trying to build a parser for a DSL I'm building using Irony in .NET and found a problem I can't find a way around. Since it handles BNF I think that any BNF solution will be of help.

I have the following input:

$10 yesterday at drug store 

With the following grammar:

<expr> :== unit | expr + unit
<unit> :==  money | date | location

<date> : == yesterday|today|tomorrow
<location> :== .* | <preposition> .*
<preposition> :== at
<money> :== ((\$)?\d*((\.*)\d*)*\,?\d{1,2})

It works like a charm with this input. I get exactly the result I wanted which is:

Money Amount: 10
Date: Yesterday
Location: Drug Store

However, if I change the order of the input as following

$10 at drug store yesterday

because of reduce steps it fails to give me the same output. The output becomes:

Money amount: 10
Location: Drug Store Yesterday

I was wondering if there is way to make sure that Location (which is a really broad regex match) is only evaluated when all the other tokens are captured and nothing else is left.

Any help is appreciated.

Thanks!

Edit: Updated title according to suggestion

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

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

发布评论

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

评论(1

江心雾 2024-12-07 00:54:33

除了这不是 BNF 歧义的一般答案之外,我还可以通过创建一个新的终端来解决我的 Irony 问题。

因此,如果其他人遇到此问题,可以在此链接中找到新终端的代码(虽然未添加到主 Irony 项目中):http://irony.codeplex.com/discussions/269483

谢谢

Besides the fact that this is not a general answer to BNF ambiguity I was able to solve my problem with Irony by creating a new Terminal.

So if anyone else encounters this problem, the code for the new Terminal (while not added to main Irony project) can be found in this link: http://irony.codeplex.com/discussions/269483

Thanks

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