如何实现左递归消除器?
我该如何为此实施消除器?
A := AB |
AC |
D |
E ;
How can i implement an eliminator for this?
A := AB |
AC |
D |
E ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是所谓的立即左递归的示例,并按如下方式删除:
基本思想是首先注意,在解析
A
时,您必须以 <代码>D 或E
。在D
或E
之后,您将结束(尾部是 ε)或继续(如果我们处于AB
或AC 结构)。
实际的算法是这样工作的:
对于任何像这样的左递归产生式:
A ->一个 a1 | ... |一个AK | b1 | b2 | ... | bm
将产生式替换为A ->; b1 A' | b2 A' | ... | bm A'
并添加产生式A' -> ε | a1 A'| ... |又名 A'
。有关消除算法(包括消除间接左递归)的更多信息,请参阅维基百科:左递归 。
This is an example of so called immediate left recursion, and is removed like this:
The basic idea is to first note that when parsing an
A
you will necessarily start with aD
or anE
. After theD
or anE
you will either end (tail is ε) or continue (if we're in aAB
orAC
construction).The actual algorithm works like this:
For any left-recursive production like this:
A -> A a1 | ... | A ak | b1 | b2 | ... | bm
replace the production withA -> b1 A' | b2 A' | ... | bm A'
and add the productionA' -> ε | a1 A' | ... | ak A'
.See Wikipedia: Left Recursion for more information on the elimination algorithm (including elimination of indirect left recursion).
另一种可用的形式是:
执行此操作的机制大致相同,但某些解析器可能会更好地处理该形式。还要考虑如何将动作规则与语法本身一起修改;另一种形式需要分解工具为
A'
规则生成新类型以返回此形式不需要的位置。Another form available is:
The mechanics of doing it are about the same but some parsers might handle that form better. Also consider what it will take to munge the action rules along with the grammar its self; the other form requires the factoring tool to generate a new type for the
A'
rule to return where as this form doesn't.