LL(1) 不能有歧义
如何证明 LL(1) 文法不能是二义性的?
我知道什么是二义性语法,但无法证明上述定理/引理。
How can it be shown that no LL(1) grammar can be ambiguous?
I know what is ambiguous grammar but could not prove the above theorem/lemma.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的校样初稿。它可能需要一些微调,但我认为它涵盖了所有情况。我认为许多解决方案都是可能的。这是一个直接的证明。
(旁注:遗憾的是 SO 不支持数学,例如在 LaTeX 中。)
证明
令 T 和 N 为终结符号和非终结符号的集合。
令以下成立 请
注意,如果以下对每对产生式 A -> A -> A 都成立,则语法为 LL(1)。 B和A-> C:
考虑一种语言,它是 LL(1),
A ->; B
和A -> C。
也就是说,存在一些终端 TZ 字符串,它允许通过不同的解析树进行多个推导。
假设左导达到
S ->* TAY ->* TZ
。下一步可以是TAY -> TBY
,或TAY -> TCY
。因此,如果同时有
BY ->* Z
和CY ->* Z
,则该语言是不明确的。(请注意,由于 A 是任意非终结符,因此如果不存在这种情况,则该语言是无歧义的。)
情况 1:Z = 空
根据 LL(1) 语法的规则 1,最多可以选择 B 和 C 之一导出空(非歧义情况)。
情况 2:Z 非空,且 B 和 C 都不为空
根据 LL(1) 语法的规则 2,最多 B 和 C 之一可以允许进一步推导,因为 Z 的前导终结符不能同时出现在
First 中(B)
和First(C)
(明确的情况)。情况 3:Z 非空,并且
MaybeEmpty(B)
或MaybeEmpty(C)
请注意,根据 LL(1) 语法的规则 1,B 和 C 不能同时存在推导为空。因此假设
MaybeEmpty(C)
为真。这给出了两个子情况。
情况 3a:
CY ->是的;情况 3b:
CY ->* DY
,其中 D 不为空。在 3a 中,我们必须在
BY ->* Z
和CY ->* 之间进行选择。 Y ->* Z
,但请注意 Follow(A) 的First(Y) 子集
。由于Follow(A)
与First(B)
不相交,因此只能进行一次推导(无二义性)。在 3b 中,我们必须在
BY ->* Z
和CY ->* DY ->* Z
之间进行选择,但请注意First(D) 子集-第一个(C)
。由于First(C)
与First(B)
不相交,因此只能进行一次推导(无二义性)。因此,在每种情况下,推导只能通过可用的产生式之一来扩展。因此语法并无二义性。
Here's my first draft at a proof. It might need some fine tuning, but I think it covers all the cases. I think many solutions are possible. This is a direct proof.
(Side note: it is a pity SO doesn't support math, such as in LaTeX.)
Proof
Let T and N be the sets of terminal and non-terminal symbols.
Let the following hold
Note that a grammar is LL(1) if the following holds for every pair of productions A -> B and A -> C:
Consider a language with is LL(1), with
A -> B
andA -> C
.That is to say there is some string of terminals TZ which admits multiple derivations by distinct parse trees.
Suppose that the left derivation reaches
S ->* TAY ->* TZ
. The next step may be eitherTAY -> TBY
, orTAY -> TCY
.Thus the language is ambiguous if both
BY ->* Z
andCY ->* Z
.(Note that since A is an arbitrary non-terminal, if no such case exists, the language is non-ambiguous.)
Case 1: Z = empty
By rule 1 of LL(1) grammars, at most one of B and C can derive empty (non-ambiguous case).
Case 2: Z non-empty, and neither B nor C derive empty
By rule 2 of LL(1) grammars, at most one of B and C can permit further derivation because the leading terminal of Z cannot be in both
First(B)
andFirst(C)
(non-ambiguous case).Case 3: Z non-empty, and either
MaybeEmpty(B)
orMaybeEmpty(C)
Note the by rule 1 of LL(1) grammars, B and C cannot both derive empty. Suppose therefore that
MaybeEmpty(C)
is true.This gives two sub-cases.
Case 3a:
CY -> Y
; and Case 3b:CY ->* DY
, where D is not empty.In 3a we must choose between
BY ->* Z
andCY -> Y ->* Z
, but notice thatFirst(Y) subset-of Follow(A)
. SinceFollow(A)
does not intersectFirst(B)
, only one derivation can proceed (non-ambiguous).In 3b we must choose between
BY ->* Z
andCY ->* DY ->* Z
, but notice thatFirst(D) subset-of First(C)
. SinceFirst(C)
does not intersectFirst(B)
, only one derivation can proceed (non-ambiguous).Thus in every case the derivation can only be expanded by one of the available productions. Therefore the grammar is not ambiguous.
我认为这几乎是 LL(1) 定义的直接结果。尝试用反证法证明;假设您有一个不明确的 LL(1) 语法,并寻找可以证明是真的和不真的的东西。作为起点“当你处理输入时你总是知道什么?”
由于这看起来像是一个家庭作业问题,而且实际上我还没有完成上面列出的问题,所以我就到此为止。
I think it's nearly a direct result of the definition of LL(1). Try proof by contradiction; assume that you have an LL(1) grammar that is ambiguous and look for something you can show to be true and not true. As a starting point "what do you always know as you process input?"
As this seems like a homework problem and I actually haven't finished the problem any more than I sketched out above, I'll stop there.
证明没有二义文法可以是 LL(1) 文法。有关提示,请参阅 http://www.cse.ohio- state.edu/~rountev/756/pdf/SyntaxAnalysis.pdf,幻灯片 18-20。另请参阅 http://seclab.cs.sunysb.edu/sekar/cse304/Parse .pdf,第 15 页。 11 及之前。
Prove that no ambiguous grammar can be an LL(1) grammar. For hints, see http://www.cse.ohio-state.edu/~rountev/756/pdf/SyntaxAnalysis.pdf, slides 18-20. Also see http://seclab.cs.sunysb.edu/sekar/cse304/Parse.pdf, pg. 11 and preceding.