6

I have a grammar and would like to prove that it is not in 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. Then how is one to prove that the given grammar is not in LL(1)?

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
rajkumar
  • 365
  • 5
  • 10
  • If the grammar is ambiguous (at least one sentence has more than one parse tree), then the grammar is not in LL(1). Now How should I represent the parsing table here ? – Prakhar Asthana Dec 30 '14 at 10:45
  • I know left recursive grammar , ambiguous grammar do not give ll(1) language .but i need to show this using parsing table...How? – rajkumar Dec 30 '14 at 10:48
  • Follow of (A)={ first of S'} ={a,replacing epsilon with S' i have to write follow of S and S'} which is {a,$} please suggest me where i am wrong . – rajkumar Dec 30 '14 at 13:24
  • Your `FIRST()` and `FOLLOW()` computation is correct as per corrected CFG. I'll have to examine the grammar as well as such rules for LL(1) parsing table. – Am_I_Helpful Dec 30 '14 at 13:50

2 Answers2

3

First of all you are finding FIRST and FOLLOW over the grammar in which you have removed left recursion. 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.

Grammar[ S->SA|A A->a ] is not LL(1) as left recursion exists. To prove it by constructing LL(1) parsing table you need to find FIRST and FOLLOW on this grammar only without modifying it.

Start from bottom A->a , gives FIRST(A)={a}

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

S->SA , gives FIRST(S)=FIRST(S) , I think problem arises here. In such recursive calls rules says calculate FIRST(S) till it changes i.e. until elements are added in FIRST(S) continue to calculate. 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. Parsing Table:

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

So there are two entries for (S,a). Thus it is not LL(1)

obmjo
  • 135
  • 10
0

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.

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). 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

(E->E+n/n ).

So, we can say E->E+n/n is not LL(1).

ochs.tobi
  • 3,214
  • 7
  • 31
  • 52