Compiler Design: Examples of First & Follow Prof. Jigar Dalvadi
Compiler Design: Examples of First & Follow Prof. Jigar Dalvadi
First(S) = { a } • Follow(S) = { $ }
First(B) = { c } • Follow(B) = { g , f , h }
First(C) = { b , ∈ } • Follow(C) = { g , f , h }
First(D) = { g , f , ∈ } • Follow(D) = { h }
First(E) = { g , ∈ } • Follow(E) = { f , h }
First(F) = { f , ∈ } • Follow(F) = { h }
First(S) = { a } • Follow(S) = { $ }
First(B) = { c } • Follow(B) = { g , f , h }
First(C) = { b , ∈ } • Follow(C) = { g , f , h }
Solution: First(D) = { g , f , ∈ } • Follow(D) = { h }
First(E) = { g , ∈ } • Follow(E) = { f , h }
Parsing Table First(F) = { f , ∈ } • Follow(F) = { h }
a b c g e f h $
S S → aBDh
B B → cC
D D → EF D → EF D→∈
F F→f F→∈
Find first and follow for grammar:
S → AaAb
S → BbBa
A→ε
B→ε
Solution: Parsing Table
• First(S) = { a , b } a b $
• First(A) = { ∈ }
• First(B) = { ∈ } S S → AaAb S → BbBa
A A→∈ A→∈
• Follow(S) = { $ }
• Follow(A) = {a, b} B B→∈ B→∈
• Follow(B) = {a, b }
S → UVW
U→ (S) | aSb | d Check whether grammar is LL(1) or not ?
V→ aV | ε
W→ cW | ε
Parsing Table
Solution:
a b c d ( ) $
• First(S) = { ( , a , d }
• First(U) = { ( , a , d } S→ S→ S→
S UVW UVW UVW
• First(V) = { a , ∈ }
• First(W) = {c , ∈ } U U→ aSb U→ d U→ (S)
Follow(S) = { ) , b , $}
Follow(U) = {a, c , ) , b , $} V V→ aV V→∈ V→∈ V→∈ V→∈
Follow(V) = {c , ) , b , $}
W W→∈ W→ cW W→∈ W→∈
Follow(W)={) , b , $}
S → AB |eDa
A → ab |c
B → dC
C → eC | ε
D → fD | ε LL(1) or not ?
a b c d e f $
First(S)={a,c,e} Follow(S)={$}
S→ S→ S→
First(A)={a,c} Follow(A)={d} S
AB AB eDa
First(B)={d} Follow(B)={$} A→ A→
A
First(C)={e, ε} Follow(C)={$} ab c
B→
First(D)={f, ε} Follow(D)={a} B
dC
C→ C→
C
eC ε
D→ D→
D
ε fD
Self Practice:
S->Aa/bAc/Bc/bBa
A->d
B->d