Treetop/PEG 中的非贪婪匹配?

发布于 2024-07-25 10:17:59 字数 169 浏览 1 评论 0原文

我将如何在 Treetop 中做这样的事情?

/.+?;/

似乎唯一的方法是:

[^;]+ ';'

这有点丑……还有其他方法吗? .+? 似乎不起作用..

How would I do something like this in Treetop?

/.+?;/

It seems like the only way is to do:

[^;]+ ';'

Which is kind of ugly.. any other way? .+? doesn't seem to work..

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

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

发布评论

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

评论(3

闻呓 2024-08-01 10:17:59

默认情况下,PEG 是贪婪和盲目的,这意味着它们会吃掉尽可能多的输入,并且不考虑之后会发生什么:

S <- P1* P2(贪婪、盲目)

这可能相当大通过使用有序选择(并且不使用前瞻)可以轻松修复:

S <- P1 S / P2(贪婪,非盲)

S <- P2 / P1 S (懒惰,非盲目)

PEGs are greedy and blind by default, that means they eat as much input as they can and they do not consider what comes afterwards:

S <- P1* P2 (greedy, blind)

That can be considerably easy fixed though by making use of the ordered choice (and without using lookaheads):

S <- P1 S / P2 (greedy, non-blind)

S <- P2 / P1 S (lazy, non-blind)

束缚m 2024-08-01 10:17:59

嗯,我知道 PEG 是贪婪的,而且没有办法绕过它。 不过,可以使用前瞻来模仿这种行为,例如 !(';' .)

Well, I learnt PEGs are greedy, and there's no way around it. Lookaheads can be used to mimic this behavior though, like !(';' .)

昵称有卵用 2024-08-01 10:17:59

我不知道 Treetop,但是 /[^;]+;/ 可以工作吗?

通过快速搜索,我看到建议 Treetop 不执行贪婪或惰性(非贪婪)量词,并且 + 实际上是一个所有格量词(由 ++ 其他正则表达式风格)。

如果是这种情况,我不确定除了否定类之外,您还有其他基于正则表达式的选项。

I don't know Treetop, but would /[^;]+;/ work?

From a quick search, I saw suggestion that Treetop doesn't do greedy nor lazy (non-greedy) quantifiers, and that the + is actually a possessive quantifier (represented by ++ in other regex flavours).

If this is the case, I'm not sure you've got any other regex-based options than the negated class.

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