Professional Documents
Culture Documents
Note:
⚫ Є cannot come inside a Follow set. The Follow set shall consists
of terminals only.
⚫ Follow set can be generated for non-terminals on RHS only.
2
FOLLOW Sets (Cont’d…)
⚫ In Follow sets computation, we will need First sets.
⚫ Follow sets computation is a three-step process.
3
FIRST Sets (Cont’d…)
A BCDEFGHI
Step-1:
• Put $ in Follow (B). Then take First( C) excluding є and put in Follow(B).
• If First( C) contains є ,then put First (D) excluding є in Follow(B).
• If First(D) contains є , then put First(E) excluding є in Follow(B) and so on…
• If First sets do not contain є, then stop putting in Follow(non-terminals) sets.
• Repeat the same procedure for all Follow sets till you reach on the last symbol i.e. I in
the above grammar on RHS.
Step-2:
• For I, put Follow(A) in Follow(I).
If First(I) contains є, then also put Follow(A) in Follow(H) and so on till є is not in
First sets.
4
FIRST Sets (Cont’d…)
Lets explain the procedure by simple example
S-> XYZ
Y-> a | b
Solution:
Solution:
Follow(S) = {$, b, a}
Example 1: Given grammar below calculate Follow sets.
i. A
BCD
ii.
iii. A
B CD
CD
iv. B eDg
v. C hiA
vi. C є
vii. D ki
viii. D є
7
Example 1: Given grammar below calculate Follow sets.
8
⚫ Lets apply procedure for calculating Follow sets step by step.
A BCD The pre-calculated
⚫ For production (i), calculating Follow sets for non-terminals First Sets are:
on RHS. A CD First (A) = { є, e, k,
B h}
A BCD First (B) = {є, e, k,
CD
h}
Put $ in Follow (B). Take First(C)-{є} and put in Follow B eDg
First (C) = { є, h}
(B). C hiA
First (D) = {є, k}
Follow(B) = {$, h} Cє
D єki
[if Check if then
it was No First(C ) contains
we would є ? (yes)
stop putting in FOLLOW(B) and will calculate FOLLOW(C )]
Take First(D)-{є} and put in Follow(B).
Follow(B) = {$, h , k}
9
Put $ in Follow(D). Take Follow(A) and put in Follow(D).
A BCD The pre-calculated
Follow(D) = {$, Follow(A)} [since Follow(A) is yet to calculate] First Sets are:
Check if First(D) contains є ? (yes) A CD First (A) = { є, e, k,
B h}
Put Follow(A) in Follow(C ). First (B) = {є, e, k,
CD
h}
Follow(C ) = {$, k, Follow(A)} B eDg
First (C) = { є, h}
Check if First(C ) contains є ? (yes) C hiA
First (D) = {є, k}
Cє
Put Follow(A) in Follow(B).
D ki
Follow(B) = {$, h, k, Follow(A)} Dє
1
1
Follow(A) = {$,k, h}
A BCD The pre-calculated
First Sets are:
Now rest of the productions (vi, vii, viii) have only A CD First (A) = { є, e, k,
B h}
terminals on RHS. So, calculation is complete. First (B) = {є, e, k,
CD
Now replace set Follow(A) members in all the above calculated h}
B eDg
First (C) = { є, h}
Follow sets by eliminating duplicates. We have finally, C hiA
First (D) = {є, k}
Cє
D ki
Follow(A) = {$, k, h} Dє
Follow(B) = {$, h, k}
Follow(C ) = {$, k, h}
Follow(D) = {$, h, k, g}
1
2
Example 2: Consider the grammar
Follow(A) = {b, c}
Follow(B) = {c}
10
Home
Task
1. Calculate Follow sets for the grammar:
E Prefix(E)
E v Tail
Prefix f
Prefix є
Tail + E
Tail є
Derive
through step
by step
procedure.
11
Left Factoring
grammar becomes
Factor id Args
Args [ ExprList ] | ( ExprList) | є
13
Left Factoring (Cont’d…)
S C + E | C* E | C / E
S C S’
S’ +E | *E | / E
13
Left Factoring (Cont’d…)
S iEtS | iEtSeS | a
S iEtS S’ | a
S’ Є |eS
Learn by Practice
S->aS’
S’->Sb|bS|b //still non deterministic
S’->Sb|bS”
S’’->S| Є
Learn by Practice
• S->ab|abc|abcd|b
• S->aAb|aABc|aABcd|aA|a
A->a
B->a