Find FIRST & FOLLOW for the following Grammar
E → TE′
E → +TE′|ε
T → FT′
T′ →* FT′|ε
F → (E)|id


Solution

Computation of FIRST

  • E →TE′

Applying Rule (4b) of FIRST

Since FIRST (T) does not contain ε, or T does not derive ε.

∴ FIRST (E) = FIRST(TE′) = FIRST(T)

∴ FIRST (E) = {FIRST(T)} (1)

  • E → +TE′|ε

Applying Rule (3) of FIRST

Comparing E′ → +TE′with X → aα

∴ FIRST(E′) = {+}

Apply Rule (2) on E′ → ε

FIRST (E′) = {ε}

∴ FIRST(E′) = {+, ε} (2)

  • T→FT′

Apply rule (4b) of FIRST

Since, FIRST(F) does not derive ε

∴ FIRST(T) = FIRST(FT′) = FIRST(F)

∴ FIRST(T) = {FIRST(F)} (3)

  • T′→*FT′|ε

Comparing with rule (2) & (3) of FIRST, we get

∴ FIRST(T′) = {ε,∗} (4)

  • F→(E)|id

Comparing with rule (3) of FIRST

∴ FIRST(F) = {(, id} (5)

Combining statement (1), (2), (3),(4),(5)

FIRST (E) = {FIRST(T)}

FIRST(E′) = {+, ε}

FIRST(T) = {FIRST(F)}

FIRST(T′) = {ε,*}

FIRST(F) = {(, id}

∴ FIRST (E) = FIRST(T) = FIRST(F) = {(, id}

FIRST(E′) = {+, ε}

FIRST(T′) = {ε,*}

Computation of FOLLOW

E → TE′

E → +TE′|ε

T → FT′

T′ →∗ FT′|ε

F → (E)|id

Applying Rule (1) FOLLOW

∴ FOLLOW (E) = {$}                                                                          (1)

  • E → TE′

Applying Rule (2) FOLLOW

E →εTE′
A →αBβ

∴ A = E, α = ε, B = T, β = E′

Since FIRST (β) = FIRST (E′) contains ε.

∴ Rule (2b) of FOLLOW

FOLLOW (T) = FIRST (E′) − {ε} ∪ FOLLOW (E)

FOLLOW (T) = {+, ε} − {ε} ∪ FOLLOW (E)

∴ FOLLOW (T) = {+} ∪ FOLLOW (E)                                                  (2)

Applying Rule (3) of FOLLOW

E →TE′
A →αβ

A = E, α = T, B = E′

∴ FOLLOW (E′) = {FOLLOW (E)}                                                         (3)

  • E′ → +TE′

Applying Rule (2)

E →TE′
A →Bβ

A = E, α = +, B = T, β = E′

Since FIRST(β) = FIRST(E′)contains ε.

Rule (2b) of FOLLOW

∴ FOLLOW (T) = FIRST(E′) − {ε} ∪ FOLLOW(E′)

∴ FOLLOW (T) = {+, ε} − {ε} ∪ FOLLOW(E′)

∴ FOLLOW (T) = {+} ∪ FOLLOW(E′)                                                      (4)

  • Applying Rule (3)
E →+TE′
A →αB

∴ FOLLOW(E′) = {FOLLOW(E′)}                                                              (5)

  • T′ →FT′

Applying Rule (2)

T →εFT′
A →αBβ

Since FIRST(β)derives ε.

Rule (2b) of FOLLOW

∴ FOLLOW (F) = FIRST(T′) − {ε} ∪ FOLLOW(T)

∴ FOLLOW (F) = {∗, ε} − {ε} ∪ FOLLOW(T)

∴ FOLLOW (F) = {∗} ∪ FOLLOW(T)                                                            (6)

Applying Rule (3)

T →FT′
A →αB

∴ FOLLOW(T′) = {FOLLOW(T)}                                                                   (7)

  • T →*FT′|ε

Applying Rule (2) FOLLOW

T′ →*FT′
A →αBβ

FIRST(β) = FIRST(T′) contains ε or T′ derives ε.

Rule (2b)

∴ FOLLOW (F) = FIRST(T′) − {ε} ∪ FOLLOW(T′)

∴ FOLLOW (F) = {*, ε} − {ε} ∪ FOLLOW(T′)

∴ FOLLOW (F) = {*} ∪ FOLLOW(T′)                                                             (8)

Applying Rule (3) of FOLLOW

T′ →*FT′
A →αB

∴ FOLLOW(T′) = {FOLLOW(T′)}                                                                   (9)

  • F→(E)

Applying Rule (2)

F →(E)
A →αBβ

FIRST(β) or FIRST( )) = {)} do not contain ε.

Rule (2a)

∴ FOLLOW (E) = FIRST( ))

∴ FOLLOW (E) = {)}                                                                                        (10)

Rule (3) does not apply to this production.

As A → α B has B nonterminal at the right corner of R. H. S of production. But F → (E)has ) the terminal on its right corner.

Rule (2) and Rule (3) does not apply to the remaining productions, as they don’t match with rules.

Combining Production (1) to (10)

FOLLOW (E) = {$}                                                     (1)

FOLLOW (T) = {+} ∪ FOLLOW (E)                           (2)

FOLLOW (E′) = {FOLLOW (E)}                                (3)

FOLLOW (T) = {+} ∪ FOLLOW(E′)                           (4)

FOLLOW(E′) = {FOLLOW(E′)}                                  (5)

FOLLOW (F) = {*} ∪ FOLLOW(T)                             (6)

FOLLOW(T′) = {FOLLOW(T)}                                   (7)

FOLLOW (F) = {*} ∪ FOLLOW(T′)                             (8)

FOLLOW(T′) = {FOLLOW(T′)}                                   (9)

FOLLOW (E) = {)}                                                     (10)

From (1), (3) and (10)

FOLLOW (E) = FOLLOW(E′) = {$, )}                        (11)

∴ From rule 4, 7, and 11

∴ FOLLOW (T) = {+} ∪ FOLLOW(E′)

∴ FOLLOW (T) = {+} ∪ {$, )}

∴ FOLLOW (T) = {+, ), $}

FOLLOW (T′) = { FOLLOW (T)} = {+, ), $}               (12)

∴ From statement 6, 8 and 12

FOLLOW (F) = {*} ∪ FOLLOW(T)

FOLLOW (F) = {*} ∪ {+, ), $, }

FOLLOW (F) = (*, +, ), $} (13)

∴ Statements 11, 12 and 13 give the required answer

FOLLOW (E) = FOLLOW(E′) = { ), $}

FOLLOW (T) = FOLLOW(T′) = {+, ), $}

FOLLOW (F) = {+,*, ), $}

Updated on: 01-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements