0% found this document useful (0 votes)
60 views

First and Follow-First Function - Rules For Calculating First Function

The document provides information on calculating first and follow functions for context-free grammars. It defines first and follow functions and gives rules for calculating them. It also discusses eliminating left recursion from grammars. Several example grammars are given along with calculating their first and follow functions. The key points are: - First(X) is the set of terminal symbols that can begin derivations from X. - Follow(X) is the set of terminal symbols that immediately follow X. - Rules are given for calculating first and follow based on grammar production rules. - Left recursion must be eliminated before calculating first and follow. - Multiple example grammars demonstrate calculating first and follow functions step-by-step

Uploaded by

najma rasool
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

First and Follow-First Function - Rules For Calculating First Function

The document provides information on calculating first and follow functions for context-free grammars. It defines first and follow functions and gives rules for calculating them. It also discusses eliminating left recursion from grammars. Several example grammars are given along with calculating their first and follow functions. The key points are: - First(X) is the set of terminal symbols that can begin derivations from X. - Follow(X) is the set of terminal symbols that immediately follow X. - Rules are given for calculating first and follow based on grammar production rules. - Left recursion must be eliminated before calculating first and follow. - Multiple example grammars demonstrate calculating first and follow functions step-by-step

Uploaded by

najma rasool
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

First and Follow | Solved Examples

-
First and Follow-
First and Follow sets are needed so that the parser can properly apply the needed production rule
at the correct position. 
First Function-
First(α) is a set of terminal symbols that begin in strings derived from α.  
Rules For Calculating First Function-
 Rule-01:
For a production rule X → ∈,
First(X) = { ∈ }

Rule-02:
 For any terminal symbol ‘a’,
First(a) = { a }

 Rule-03:
 
For a production rule X → Y1Y2Y3,
 Calculating First(X)
If ∈ ∉ First(Y1), then First(X) = First(Y1)
If ∈ ∈ First(Y1), then First(X) = { First(Y1) – ∈ } ∪ First(Y2Y3)
Calculating First(Y2Y3)
If ∈ ∉ First(Y2), then First(Y2Y3) = First(Y2)
If ∈ ∈ First(Y2), then First(Y2Y3) = { First(Y2) – ∈ } ∪ First(Y3)
Similarly, we can make expansion for any production rule X → Y1Y2Y3…..Yn.

Follow Function-
Follow(α) is a set of terminal symbols that appear immediately to the right of α. 

Rules For Calculating Follow Function-


Rule-01:
 For the start symbol S, place $ in Follow(S).

Rule-02: 
For any production rule A → αB,
Follow(B) = Follow(A)

Rule-03:
 For any production rule A → αBβ,
If ∈ ∉ First(β), then Follow(B) = First(β)
If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ } ∪ Follow(A)
Note-02:
Before calculating the first and follow functions, eliminate Left Recursion from
the grammar, if present.
Elimination of Left Recursion
 Left recursion is eliminated by converting the grammar into a right recursive grammar.

 If we have the left-recursive pair of productions-

A → Aα / β
(Left Recursive Grammar)

where β does not begin with an A. 

Then, we can eliminate left recursion by replacing the pair of productions with-

A → βA’
A’ → αA’ / ∈

Problem-01:
Consider the following grammar and eliminate left recursion-
A → ABd / Aa / a
B → Be / b

Solution-
 The grammar after eliminating left recursion is-
A → aA’
A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈ 

Problem-02:
Consider the following grammar and eliminate left recursion-
E→E+E/ExE/a

Solution-
 The grammar after eliminating left recursion is-
E → aA
A → +EA / xEA / ∈

Problem-03:
 Consider the following grammar and eliminate left recursion-
E→E+T/T
T→TxF/F
F → id

Solution- 
The grammar after eliminating left recursion is-
E → TE’
E’ → +TE’ / ∈
T → FT’
T’ → xFT’ / ∈
F → id

Problem-04:
Consider the following grammar and eliminate left recursion-
S → (L) / a
L→L,S/S

Solution-
The grammar after eliminating left recursion is-
S → (L) / a
L → SL’
L’ → ,SL’ / ∈

Problem-05:
Consider the following grammar and eliminate left recursion-
S → S0S1S / 01 

Solution-
The grammar after eliminating left recursion is-
S → 01A
A → 0S1SA / ∈

Problem-06:
Consider the following grammar and eliminate left recursion-
S→A
A → Ad / Ae / aB / ac
B → bBc / f

Solution-
 The grammar after eliminating left recursion is-
S→A
A → aBA’ / acA’
A’ → dA’ / eA’ / ∈
B → bBc / f

Problem-07:
Consider the following grammar and eliminate left recursion-
A → AAα / β

Solution-
The grammar after eliminating left recursion is-
A → βA’
A’ → AαA’ / ∈  

PRACTICE PROBLEMS BASED ON CALCULATING


FIRST AND FOLLOW-
Problem-01:
Calculate the first and follow functions for the given grammar-
S → aBDh
B → cC
C → bC / ∈
D → EF
E → g / ∈
F → f / ∈

Solution- 
The first and follow functions are as follows-

First Functions-
First(S) = { a }
First(B) = { c }
First(C) = { b , ∈ }
First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈ }
First(E) = { g , ∈ }
First(F) = { f , ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(B) = { First(D) – ∈ } ∪ First(h) = { g , f , h }
Follow(C) = Follow(B) = { g , f , h }
Follow(D) = First(h) = { h }
Follow(E) = { First(F) – ∈ } ∪ Follow(D) = { f , h }
Follow(F) = Follow(D) = { h }
Problem-02:
Calculate the first and follow functions for the given grammar-
S → A
A → aB / Ad
B → b
C → g

Solution-
We have-
The given grammar is left recursive.
So, we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
S → A
A → aBA’
A’ → dA’ / ∈
B → b
C → g
Now, the first and follow functions are as follows-

First Functions-
First(S) = First(A) = { a }
First(A) = { a }
First(A’) = { d , ∈ }
First(B) = { b }
First(C) = { g }
Follow Functions-
Follow(S) = { $ }
Follow(A) = Follow(S) = { $ }
Follow(A’) = Follow(A) = { $ }
Follow(B) = { First(A’) – ∈ } ∪ Follow(A) = { d , $ }
Follow(C) = NA
Problem-03:
Calculate the first and follow functions for the given grammar-
S → (L) / a
L → SL’
L’ → ,SL’ / ∈
 

Solution-
The first and follow functions are as follows-

First Functions-
First(S) = { ( , a }
First(L) = First(S) = { ( , a }
First(L’) = { , , ∈ }
Follow Functions-
Follow(S) = { $ } ∪ { First(L’) – ∈ } ∪ Follow(L) ∪ Follow(L’) = { $ , , , ) }
Follow(L) = { ) }
Follow(L’) = Follow(L) = { ) }
Problem-04:
Calculate the first and follow functions for the given grammar-
S → AaAb / BbBa
A→∈
B→∈

Solution-
The first and follow functions are as follows-

First Functions-
First(S) = { First(A) – ∈ } ∪ First(a) ∪ { First(B) – ∈ } ∪ First(b) = { a , b }
First(A) = { ∈ }
First(B) = { ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(A) = First(a) ∪ First(b) = { a , b }
Follow(B) = First(b) ∪ First(a) = { a , b }
Problem-05:
Calculate the first and follow functions for the given grammar-
E→E+T/T
T→TxF/F
F → (E) / id

Solution-
We have-
The given grammar is left recursive.
So, we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
E → TE’
E’ → + TE’ / ∈
T → FT’
T’ → x FT’ / ∈
F → (E) / id
Now, the first and follow functions are as follows-

First Functions-
First(E) = First(T) = First(F) = { ( , id }
First(E’) = { + , ∈ }
First(T) = First(F) = { ( , id }
First(T’) = { x , ∈ }
First(F) = { ( , id }
Follow Functions-
Follow(E) = { $ , ) }
Follow(E’) = Follow(E) = { $ , ) }
Follow(T) = { First(E’) – ∈ } ∪ Follow(E) ∪ Follow(E’) = { + , $ , ) }
Follow(T’) = Follow(T) = { + , $ , ) }
Follow(F) = { First(T’) – ∈ } ∪ Follow(T) ∪ Follow(T’) = { x , + , $ , ) }
Problem-06:
Calculate the first and follow functions for the given grammar-
S → ACB / CbB / Ba
A → da / BC
B → g / ∈
C → h / ∈

Solution-
The first and follow functions are as follows-

First Functions 
First(S) = { First(A) – ∈ }  ∪ { First(C) – ∈ } ∪ First(B) ∪ First(b) ∪ { First(B) – ∈ } ∪
First(a) = { d , g , h , ∈ , b , a }
First(A) = First(d) ∪ { First(B) – ∈ } ∪ First(C) = { d , g , h , ∈ }
First(B) = { g , ∈ }
First(C) = { h , ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(A) = { First(C) – ∈ } ∪ { First(B) – ∈ } ∪ Follow(S) = { h , g , $ }
Follow(B) = Follow(S) ∪ First(a) ∪ { First(C) – ∈ } ∪ Follow(A) = { $ , a , h , g }
Follow(C) = { First(B) – ∈ } ∪ Follow(S) ∪ First(b) ∪ Follow(A) = { g , $ , b , h }
To gain better understanding about calculating first and follow functions,

You might also like