What is Algorithm of Predictive Parsing and compute FIRST and FOLLOW for the following Grammar
S → L = R
S → R
L →* R
L → id
R → L


Solution

Computation of FIRST

  • S → L = R

∵ L does not derive ε. By rule (4b)of FIRST

∴ FIRST(S) = {FIRST(L)}                                                  (1)

  • S → R

∵ R does not derive ε. By rule (4b)of FIRST

∴ FIRST(S) = {FIRST(R)}                                                 (2)

  • L →* R

Applying Rule (3) of FIRST

FIRST(L) = {*}                                                                  (3)

  • L → id

Applying Rule (3) of FIRST

FIRST(L) = {id}                                                                (4)

  • R → L

Applying Rule (4b) of FIRST

FIRST(R) = {FIRST(L)}                                                   (5)

Combining (1) to (5) statements

FIRST(L) = {*, id}

FIRST(S) = {FIRST(L)}

FIRST(R) = {FIRST(L)}

FIRST(S) = {FIRST(R)}

∴ FIRST(L) = FIRST(S) = FIRST(R) = {*, id}

Computation of FOLLOW

S → L = R

S → R

L →∗ R

L → id

R → L

Applying Rule (1) of FOLLOW

FOLLOW (S) ={$}                                                               (1)

S → L = R

Applying Rule (2) of FOLLOW

S →εL= R
A →αBβ

∵ FIRST(β) = FIRST(= R) = {=}does not contain ε.

Rule (2a) of FOLLOW

∴ FOLLOW(L) = {=}                                                              (2)

Applying Rule (3) of FOLLOW

S →L =R
A →αB

∴ FOLLOW(R) = {FOLLOW(S)}                                           (3)

  • S → R

We cannot apply Rule(2) on this production as A → 𝛼 B β does not match with S → R.

Applying Rule (3)

S →εR
A →αB

∴ FOLLOW(R) = {FOLLOW(S)} (4)

  • L →* R 

Rule (2) does not apply to this production

Applying Rule (3)

L →*R
A →αB

∴ FOLLOW(R) = {FOLLOW(L)} (5)

  • R → L

Rule (2) cannot be applied.

∴ Applying Rule (3)

R →εl
A →αB

∴ FOLLOW(L) = {FOLLOW(R)} (6)

Combining (1) to (6) statements

FOLLOW(S) = {$}                                                 (1)

FOLLOW(L) = {=}                                                  (2)

FOLLOW(R) = {FOLLOW(S)}                               (3)

FOLLOW(R) = {FOLLOW(S)}                                (4)

FOLLOW(R) = {FOLLOW(L)}                                (5)

FOLLOW(L) = {FOLLOW(R)}                                (6)

∴ FOLLOW(S) = {$}

FOLLOW(L) = FOLLOW(R) = {$, =}

Algorithm for Predictive Parsing

Input − A string to be parsed & Parsing table 'M' for Grammar.

Output − Stack will become empty, or it will give an error.

Method

While Stack is not empty do
{
   Let X be top symbol on Stack
   Let a be next input symbol
   If X is Terminal or $ then
      If X = a then
      Pop X form stack & remove a from input
   else error ( );
   else
      If M [X, a] = A → Y1Y2 … … . . Yn
      Pop X and Push Yn Yn−1 … . . Y1 onto stack
   else error ( );
}

Updated on: 02-Nov-2021

719 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements