简体   繁体   English

如何证明左递归语法不在LL(1)中使用解析表

[英]How to prove left-recursive grammar is not in LL(1) using parsing table

I have a grammar and would like to prove that it is not in LL(1): 我有一个语法,并想证明它不在LL(1)中:

S->SA|A
A->a

As it is a left-recursive grammarm, to find the first and follow sets I eliminated the left recursion and got: 由于它是一个左递归语法,为了找到第一个和后面的集合,我消除了左递归并获得:

S->AS'
S'->AS'|Empty
A->a

first of A={a}      follow of S={$}
first of s'={a,ε}   follow of S'={$}
first of S={a}       follow of A={a,$}

But when I filled in the parsing table, I did not get any cell with 2 entries. 但是当我填写解析表时,我没有得到任何包含2个条目的单元格。 Then how is one to prove that the given grammar is not in LL(1)? 那么如何证明给定的语法不在LL(1)中呢?

First of all you are finding FIRST and FOLLOW over the grammar in which you have removed left recursion. 首先,您要找到FIRST并关注已删除左递归的语法。 Therefore surely if you try to create LL(1) parsing table there won't be any 2 entries as left recursion is removed and grammar is unambiguous. 因此,当然如果您尝试创建LL(1)解析表,则不会有任何2个条目,因为删除了左递归并且语法是明确的。

Grammar[ S->SA|A A->a ] is not LL(1) as left recursion exists. 语法[S-> SA | A A-> a]不是LL(1),因为存在左递归。 To prove it by constructing LL(1) parsing table you need to find FIRST and FOLLOW on this grammar only without modifying it. 要通过构造LL(1)解析表来证明它,您需要在不修改它的情况下找到此语法的FIRST和FOLLOW。

Start from bottom A->a , gives FIRST(A)={a} 从底部开始A-> a,给出FIRST(A)= {a}

S->A , gives FIRST(S)=FIRST(A)={a} S-> A,给出FIRST(S)= FIRST(A)= {a}

S->SA , gives FIRST(S)=FIRST(S) , I think problem arises here. S-> SA,给出FIRST(S)= FIRST(S),我认为问题出现在这里。 In such recursive calls rules says calculate FIRST(S) till it changes ie until elements are added in FIRST(S) continue to calculate. 在这样的递归调用中,规则表示计算FIRST(S)直到它改变,即直到元素被添加到FIRST(S)继续计算。 Once it stops changing that is you answer 一旦它停止改变,你就回答了

Therefore FIRST(S)=FIRST(S)={a} , you call FIRST(S) as many times possible it won't change. 因此,FIRST(S)= FIRST(S)= {a},您可以多次调用FIRST(S),它不会改变。 Parsing Table: 解析表:

      a
------------ 
S   S->SA
    S->A
-------------
A   A->a 

So there are two entries for (S,a). 因此(S,a)有两个条目。 Thus it is not LL(1) 因此它不是LL(1)

For this Left recursive grammar: 对于这个左递归语法:

S->SA|A
A->a

We can eliminate left recursion because it will give the same result as Previous Left recursive grammar does. 我们可以消除左递归,因为它将提供与Previous Left递归语法相同的结果。

S->AS'
S'->AS'|Empty
A->a

first of A={a}      follow of S={$}
first of s'={a,ε}   follow of S'={$}
first of S={a}       follow of A={a,$}

So, for above case actually, we are checking LL(1) for modified Left recursive grammar (as it is same). 因此,对于上述情况,我们正在检查LL(1)是否有修改的左递归语法(因为它是相同的)。 But for following Left-Recursive Grammar:- 但是对于跟随左递归语法: -

E -> E+n/n

We can not modify that grammar, it will Change associativity of + operator. 我们不能修改那个语法,它会改变+运算符的关联性。

So, the only thing we will have to do is checking LL(1) without modifying 因此,我们唯一要做的就是检查LL(1)而不进行修改

(E->E+n/n ).

So, we can say E->E+n/n is not LL(1) . 所以,我们可以说E->E+n/n不是LL(1)

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

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