LR1 解析器和 Epsilon

发布于 2024-07-13 00:41:52 字数 453 浏览 15 评论 0原文

我试图了解 LR1 解析器是如何工作的,但我遇到了一个奇怪的问题:如果语法包含 Epsilons 怎么办? 例如:如果我有语法:

S -> A
A -> a A | B
B -> a

很清楚如何开始:

S -> .A
A -> .a A 
A -> .B

...等等,

但我不知道如何执行这样的语法:

S -> A
A -> a A a | \epsilon

这样做是否正确:

S -> .A
A -> .a A a
( A -> .\epsilon )

然后在 DFA 中设置此状态接受?

任何帮助将不胜感激!

I'm trying to understand how LR1 Parsers work but I came up with a strange problem: What if the grammar contains Epsilons? For instance: if I have the grammar:

S -> A
A -> a A | B
B -> a

It's clear how to start:

S -> .A
A -> .a A 
A -> .B

... and so on

but I don't know how to do it for such a grammar:

S -> A
A -> a A a | \epsilon

Is it correct to do:

S -> .A
A -> .a A a
( A -> .\epsilon )

And then make this State in the DFA accepting?

Any help would really be appreciated!

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

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

发布评论

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

评论(2

岛徒 2024-07-20 00:41:52

是的,确实如此(将 epsilon 视为空白空间,其两侧没有两个点可以放置)。

在 LR(0) 自动机中,您可以使状态接受并归约到 A。但是,由于 A->a A a 产生式,会出现移位/归约冲突。

在 LR(1) 自动机中,您可以使用前瞻来确定是移位还是归约(a -> 移位,FOLLOW(A) 中的任何内容 -> 归约)

请参阅维基百科文章

Yes, exactly (think of the epsilon as empty space, where there aren't two places for the dot at the sides).

In an LR(0) automaton, you would make the state accepting and reduce to A. However, due to the A->a A a production, there'd be a shift/reduce conflict.

In a LR(1) automaton, you would determine whether to shift or reduce using lookahead (a -> shift, anything in FOLLOW(A) -> reduce)

See the Wikipedia article

墨落成白 2024-07-20 00:41:52

您可以使用此站点来计算:
https://cyberzhg.github.io/toolbox/lr1

查看结果:

在此处输入图像描述

You can use this site to compute this:
https://cyberzhg.github.io/toolbox/lr1

See the results:

enter image description here

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