简体   繁体   English

如何将此语法转换为LL(1)?

[英]How to convert this grammar to LL(1)?

S → Y | S→Y | 1X 1倍

X → 1X | X→1X | 0 0

Y → Y 0 | Y→Y 0 | 1X1 | 1X1 | 2X2 2X2

I do not understand how to do factoring or substitution when there are more than one of the same symbol. 当同一符号不止一个时,我不明白如何进行分解或替换。 Thanking you. 感谢您。

X → 1 X | 0

refers to a 0-or-more 1 followed by 0 , so it's equivalent to 指的是0或更多的1后跟0 ,所以它等效于

X → 1* 0

The same approach can be used to remove your left-recursion. 可以使用相同的方法删除您的左递归。 You can rewrite 你可以重写

S → Y | 1 X
X → 1 X | 0
Y → Y 0 | 1 X 1 | 2 X 2

as

S → Y | 1 X
X → 1* 0
Y → ( 1 X 1 | 2 X 2 )* 0

In EBNF: 在EBNF中:

S → Y | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2

In BNF: 在BNF中:

S → Y | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2

1* → 1 1* | Ɛ
A* → A A* | Ɛ

If all you wanted to do was to eliminated left-recursion, you're done. 如果您要做的就是消除左递归,那么您就完成了。


If you want to eliminate common prefixes too, you're not done because both sub-rules of S can start with 1 X . 如果您也想消除公共前缀,则不会做完,因为S两个子规则都可以以1 X开头。 To fix this, inline and distribute to obtain the following: 要解决此问题,请内联并分发以获得以下内容:

S → 0
  | 1 X 1 Y
  | 2 X 2 Y
  | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2

Now, we're finally in a position to factor out the common 1 X . 现在,我们终于可以排除常见的1 X

S → 0
  | 1 X ( 1 Y )?
  | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2

In EBNF: 在EBNF中:

S → 0 | 1 X B? | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
B → 1 Y

In BNF: 在BNF中:

S → 0 | 1 X B? | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
B → 1 Y

B? → B    | Ɛ
1* → 1 1* | Ɛ
A* → A A* | Ɛ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM