Rules of First and Follow in Predictive Parsing

By: Prof. Dr. Fazal Rehman | Last updated: March 3, 2022

Rules of First and Follow in Predictive Parsing

Predictive Parsing
Predictive Parsing

Rules for First in Predictive Parsing

  1. Suppose X is a terminal then First(X) is just X!
  2. Suppose there is a Production rule of X โ†’ ฮต then add ฮต to first(X)
  3. Suppose there is a Production rule of X โ†’ A1, A2โ€ฆ..Ak then add first(A1,A2..Ak) to first(X)
  4. First(A1, A2..Ak) is either
    1. First(A1) (Suppose First(A1) doesnโ€™t contain ฮต)
    2. OR (Suppose First(A1) does contain ฮต) then First (A1, A2..Ak) is everything in First(A1) <except for ฮต > as well as everything in First(A2..Ak)
    3. Suppose First(A1) First(A2)โ€ฆ..First(Ak) all contain ฮต then add ฮต to First(A1, A2..Ak) as well.

Rule 1

Production Rule / CFG First
A โ†’ lmn / opq / rst First(A) = { l , o , r }

Rule 2

Production Rule / CFG First
X โ†’ โˆˆ First(X) = { โˆˆ }

Rule3

Production Rule / CFG First
X โ†’ a First(X) = { a }

Rule 4

  • Rule 4.1: If โˆˆ does not belongs to First(E), then First(EF) = First(E)
  • Rule 4.2: If โˆˆ belongs to First(E), then First(EF) = { First(E) โ€“ โˆˆ } โˆช First(F)

Example of Rule 4.1:

If โˆˆ does not belongs to First(E), then First(EF) = First(E)

Production Rule / CFG First
S -> E F
E -> g | o
F -> f | โˆˆ
First(S) = { g, o }

First(E) = { g,  o }

First(F) = { f, โˆˆ }

Example of Rule 4.2:

If โˆˆ belongs to First(E), then First(EF) = { First(E) โ€“ โˆˆ } โˆช First(F)

Production Rule / CFG First
S -> E F
E -> g | โˆˆ
F -> f | o
First(S) = { g, โˆˆ }

First(E) = { g,  โˆˆ}

First(F) = { f, o }

Rule 5

For a production rule S โ†’ EFG,

  • Rule 5.1: If โˆˆ does not belongs to First(E), then First(S) = First(E)
  • Rule 5.2: If โˆˆ belongs to First(E), then First(S) = { First(E) โ€“ โˆˆ } โˆช First(FG)

Example of Rule 5.1:

If โˆˆ does not belongs to First(E), then First(S) = First(E)

Explanation: โˆˆ does not belongs to First(E) because First(E) are only { g,  0 }.

Production Rule / CFG First
S -> E F G
E -> g | o
F -> f | โˆˆ
G -> k | l
First(S) = { g, o }

First(E) = { g,  0 }

First(F) = { f, โˆˆ }

First(G) = { k, l }

Example of Rule 5.2:

If โˆˆ belongs to First(E), then First(S) = { First(E) โ€“ โˆˆ } โˆช First(FG)

Explanation: โˆˆ belongs to First(E) because First(E) are only { g,  โˆˆ }.

Production Rule / CFG First
S -> E F G
E -> g | โˆˆ
F -> f | โˆˆ
G -> k | l
First(S) = { g, โˆˆ }

First(E) = { g,  โˆˆ }

First(F) = { f, โˆˆ }

First(G) = { k, l }

 

Rules for Follow in Predictive Parsing

  1. First, put $ (the end of input marker) in Follow(S) (S is the start symbol)
  2. Suppose there is a production rule of A โ†’ aBb, (where a can be a whole string) then everything in FIRST(b) except for ฮต is placed in FOLLOW(B).
  3. Suppose there is a production rule of A โ†’ aB, then everything in FOLLOW(A) is in FOLLOW(B)
  4. Suppose there is a production rule of A โ†’ aBb, where FIRST(b) contains ฮต, then everything in FOLLOW(A) is in FOLLOW(B)

Rule 1:

For the start symbol S, place $ in Follow(S).

Production Rule / CFG Follow
S -> a Follow(S) = {$}

Rule 2:

For any production rule S -> a X,

Follow(X) = Follow(S)

Production Rule / CFG  Follow
S -> a X  Follow(S)= {$}

Rule 3

For any production rule S โ†’ ฮฑXฮฒ,

  • If โˆˆ โˆ‰ First(ฮฒ), then Follow(X) = First(ฮฒ)
  • If โˆˆ โˆˆ First(ฮฒ), then Follow(X) = { First(ฮฒ) โ€“ โˆˆ } โˆช Follow(S)

 


CFG

S -> E F

E -> g | โˆˆ

F -> f | โˆˆ

Symbol First Follow
E g โˆˆ f โˆˆ
F f โˆˆ $
S g โˆˆ $

CFG

S -> E F G
E -> g | o
F -> f | โˆˆ
G -> k | l

Symbol First Follow
E g o f โˆˆ
F f โˆˆ k l
G k l $
S g o $

Example of first and follow function

How to find the first and follow functions for the given CFG?

S โ†’ aXZh

X โ†’ cY

Y โ†’ bY / โˆˆ

Z โ†’ EF

E โ†’ g / โˆˆ

F โ†’ f / โˆˆ

The first and follow functions are given below;

First Functions

  • First(S) = { a }
  • First(X) = { c }
  • First(Y) = { b , โˆˆ }
  • First(Z) = { First(E) โ€“ โˆˆ } โˆช First(F) = { g , f , โˆˆ }
  • First(E) = { g , โˆˆ }
  • First(F) = { f , โˆˆ }

Follow Functions

  • Follow(S) = { $ }
  • Follow(X) = { First(Z) โ€“ โˆˆ } โˆช First(h) = { g , f , h }
  • Follow(Y) = Follow(X) = { g , f , h }
  • Follow(Z) = First(h) = { h }
  • Follow(E) = { First(F) โ€“ โˆˆ } โˆช Follow(Z) = { f , h }
  • Follow(F) = Follow(Z) = { h }

 

Example of first and follow function with Left Recursion

How to find the first and follow functions for the given CFG with Left Recursive production rules.?

S โ†’ X

X โ†’ aY / Xd

Y โ†’ b

C โ†’ t

The given grammar is left recursive. So, we first remove the left recursion from the given grammar.

After the elimination of left recursion, we get the following grammar.

S โ†’ X

Xโ†’ aYXโ€™

Xโ€™ โ†’ dXโ€™ / โˆˆ

Y โ†’ b

C โ†’ t

The first and follow functions are described below;

First Functions

  • First(S) = First(X) = { a }
  • First(X) = { a }
  • First(Xโ€™) = { d , โˆˆ }
  • First(Y) = { b }
  • First(C) = { t }

Follow Functions-

  • Follow(S) = { $ }
  • Follow(X) = Follow(S) = { $ }
  • Follow(Xโ€™) = Follow(X) = { $ }
  • Follow(Y) = { First(Aโ€™) โ€“ โˆˆ } โˆช Follow(A) = { d , $ }
  • Follow(C) = NA

 

Example of first and follow

How to find the first and follow functions for the given grammar?

S โ†’ (F) / g

F โ†’ S Fโ€™

Fโ€™ โ†’ ,S Fโ€™ / โˆˆ

The first and follow functions are described below;

First Functions

  • First(S) = { ( , g }
  • First(F) = First(S) = { ( , g }
  • First(Fโ€™) = { , , โˆˆ }

Follow Functions-

  • Follow(S) = { $ } โˆช { First(Fโ€™) โ€“ โˆˆ } โˆช Follow(F) โˆช Follow(Fโ€™) = { $ , , , ) }
  • Follow(F) = { ) }
  • Follow(Fโ€™) = Follow(F) = { ) }

Example of first and follow

How to find the first and follow for the given grammar?

 

S โ†’ XaAb / YbYa

X โ†’ โˆˆ

Y โ†’ โˆˆ

The first and follow functions are described below;

First Functions

  • First(S) = { First(X) โ€“ โˆˆ } โˆช First(a) โˆช { First(Y) โ€“ โˆˆ } โˆช First(b) = { a , b }
  • First(X) = { โˆˆ }
  • First(Y) = { โˆˆ }

Follow Functions

  • Follow(S) = { $ }
  • Follow(X) = First(a) โˆช First(b) = { a , b }
  • Follow(Y) = First(b) โˆช First(a) = { a , b }

Example of first and follow function with Left Recursion

How to find the first and follow functions for the given CFG with Left Recursive production rules.?

Xโ†’ X + Y / Y

Y โ†’ Y d F / F

F โ†’ (X) / id

Solution-

The given grammar is left recursive. So, we first remove the left recursion from the given grammar.

After the elimination of left recursion, we get the following grammar.

X โ†’ YXโ€™

Xโ€™ โ†’ + YXโ€™ / โˆˆ

Y โ†’ FYโ€™

Yโ€™ โ†’ d FYโ€™ / โˆˆ

F โ†’ (X) / id

The first and follow functions are described below;

First Functions

  • First(X) = First(Y) = First(F) = { ( , id }
  • First(Xโ€™) = { + , โˆˆ }
  • First(Y) = First(F) = { ( , id }
  • First(Yโ€™) = { d , โˆˆ }
  • First(F) = { ( , id }

Follow Functions

  • Follow(X) = { $ , ) }
  • Follow(Xโ€™) = Follow(E) = { $ , ) }
  • Follow(Y) = { First(Xโ€™) โ€“ โˆˆ } โˆช Follow(X) โˆช Follow(Xโ€™) = { + , $ , ) }
  • Follow(Yโ€™) = Follow(Y) = { + , $ , ) }
  • Follow(F) = { First(Yโ€™) โ€“ โˆˆ } โˆช Follow(Y) โˆช Follow(Yโ€™) = { d , + , $ , ) }

Example of first and follow

How to find the first and follow functions for the given grammar?

S โ†’ XCY / CbY / Ya

X โ†’ da / YC

Y โ†’ g / โˆˆ

C โ†’ h / โˆˆ

The first and follow functions are described below;

First Functions-

  • First(S) = { First(X) โ€“ โˆˆ } โˆช { First(C) โ€“ โˆˆ } โˆช First(Y) โˆช First(b) โˆช { First(Y) โ€“ โˆˆ } โˆช First(a) = { d , g , h , โˆˆ , b , a }
  • First(X) = First(d) โˆช { First(Y) โ€“ โˆˆ } โˆช First(C) = { d , g , h , โˆˆ }
  • First(Y) = { g , โˆˆ }
  • First(C) = { h , โˆˆ }

Follow Functions-

  • Follow(S) = { $ }
  • Follow(X) = { First(C) โ€“ โˆˆ } โˆช { First(Y) โ€“ โˆˆ } โˆช Follow(S) = { h , g , $ }
  • Follow(Y) = Follow(S) โˆช First(a) โˆช { First(C) โ€“ โˆˆ } โˆช Follow(X) = { $ , a , h , g }
  • Follow(C) = { First(Y) โ€“ โˆˆ } โˆช Follow(S) โˆช First(b) โˆช Follow(X) = { g , $ , b , h }

Leave a Comment

All Copyrights Reserved 2025 Reserved by T4Tutorials