Rules of First and Follow in Predictive Parsing

Rules for First in Predictive Parsing
- Suppose X is a terminal then First(X) is just X!
- Suppose there is a Production rule of X โ ฮต then add ฮต to first(X)
- Suppose there is a Production rule of X โ A1, A2โฆ..Ak then add first(A1,A2..Ak) to first(X)
- First(A1, A2..Ak) is either
- First(A1) (Suppose First(A1) doesnโt contain ฮต)
- 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)
- 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
- First, put $ (the end of input marker) in Follow(S) (S is the start symbol)
- 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).
- Suppose there is a production rule of A โ aB, then everything in FOLLOW(A) is in FOLLOW(B)
- 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 }