First and Follow-First Function - Rules For Calculating First Function
First and Follow-First Function - Rules For Calculating First Function
-
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 α.
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.
A → Aα / β
(Left Recursive Grammar)
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’ / ∈
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,
1. The process to determine whether the start symbol can derive the program
2. If successful, the program is a valid program.
3. If failed, the program is invalid.
Expanding from the start symbol to the whole program (top down)
Types of Parser:
Parser is mainly classified into 2 categories: Top-down Parser, and Bottom-up Parser. These are
explained as following below.
1. Top-down Parser:
Top-down parser is the parser which generates parse for the given input string with the help of
grammar productions by expanding the non-terminals i.e. it starts from the start symbol and ends
on the terminals. It uses left most derivation.
Further Top-down parser is classified into 2 types: Recursive descent parser, and Non-recursive
descent parser.
(i). Recursive descent parser:
It is also known as Brute force parser or the with backtracking parser. It basically generates the parse tree
by using brute force and backtracking.
Back-tracking –
It means, if one derivation of a production fails, syntax analyzer restarts process using different
rules of same production. This technique may process input string more than once to determine
right production. Top- down parser start from root node (start symbol) and match input string
against production rules to replace them (if matched).
To understand this, take following example of CFG:
S -> aAb | aBb
A -> cx | dx
B -> xe
For an input string – read, a top-down parser, will behave like this.
It will start with S from production rules and will match its yield to left-most letter of input, i.e. ‘a’.
The very production of S (S -> aAb) matches with it. So top-down parser advances to next input
letter (i.e., ‘d’). The parser tries to expand non-terminal ‘A’ and checks its production from left (A
-> cx). It does not match with next input symbol. So top-down parser backtracks to obtain next
production rule of A, (A -> dx).
Now parser matches all input letters in an ordered manner. The string is accepted.
LL(1) Parsing:
Here the 1st L represents that the scanning of the Input will be done from Left to Right manner
and second L shows that in this Parsing technique we are going to use Left most Derivation Tree.
and finally the 1 represents the number of look ahead, means how many symbols are you going
to see when you want to make a decision.
E’ –> +TE’/e { +, e } { $, ) }
T’ –> *FT’/e { *, e } { +, $, ) }
Example-2:
Consider the Grammar
S --> A | a
A --> a
Find their first and follow sets:
Grammar FIRST FOLLOW
S –> A/a {a} {$}
Parsing Table:
A $
S S –> A, S –> a
A A –> a
Here, we can see that there are two productions into the same cell. Hence, this grammar is not
feasible for LL(1) Parser.
LR Parser
LR parsing is one type of bottom-up parsing. It is used to parse the large class of
grammars.
"K" is the number of input symbols of the look ahead used to make number of parsing
decision.
LR parsing is divided into four parts: LR (0) parsing, SLR parsing, CLR parsing and LALR
parsing.
LR (0) Parsing
Various steps involved in the LR (0) Parsing:
Augment Grammar
Augmented grammar G` will be generated if we add one more production in the given
grammar G. It helps the parser to identify when to stop the parsing and announce the
acceptance of the input.
Example
Given grammar
S → AA
A → aA | b
S`→ S
S → AA
A → aA | b
LR(0) items is useful to indicate that how much of the input has been scanned up to a given
point in the process of parsing.
Example
Given grammar:
S → AA
A → aA | b
Add Augment Production and insert '•' symbol at the first position for every production in G
S` → •S
S → •AA
A → •aA
A → •b
I0 State:
Add Augment production to the I0 State and Compute the Closure
I0 = S` → •S
S → •AA
Add all productions starting with "A" in modified I0 State because "•" is followed by the non-
terminal. So, the I0 State becomes.
I0= S` → •S
S → •AA
A → •aA
A → •b
I1= S` → S•
Add all productions starting with A in to I2 State because "•" is followed by the non-
terminal. So, the I2 State becomes
I2 =S→A•A
A → •aA
A → •b
A → a•A
A → •aA
A → •b
Drawing DFA:
The DFA contains the 7 states I0 to I6.
LR(0) Table
If a state is going to some other state on a terminal, then it correspond to a shift move.
If a state contains the final item in the particular row then write the reduce node
completely.
Explanation:
I0 on S is going to I1 so write it as 1.
I0 on A is going to I2 so write it as 2.
I2 on A is going to I5 so write it as 5.
I3 on A is going to I6 so write it as 6.
I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
I4, I5 and I6 all states contains the final item because they contain • in the right most end.
So rate the production as production number.
NOTE:
1. Two reduced productions in one state – RR conflict.
2. One reduced and one shifted production in one state – SR conflict
If no SR or RR conflict present in the parsing table then the grammar is LR(0) grammar.
In above grammar no conflict so it is LR(0) grammar.
SLR Parser
The SLR parser is similar to LR(0) parser except that the reduced entry. The reduced productions
are written only in the FOLLOW of the variable whose production is reduced.
Various steps involved in the SLR (1) Parsing:
Augment Grammar
Augmented grammar G` will be generated if we add one more production in the given
grammar G. It helps the parser to identify when to stop the parsing and announce the
acceptance of the input.
Example
Given grammar
S → AA
A → aA | b
S First(S)First(A) Follow(S){$)
A First(A){a,b} Follow(A){First(A)U follow(A)} {a,b,$}
S`→ S
S → AA
A → aA | b
LR(0) items is useful to indicate that how much of the input has been scanned up to a given
point in the process of parsing.
Example
Given grammar:
S → AA
A → aA | b
Add Augment Production and insert '•' symbol at the first position for every production in G
S` → •S
S → •AA
A → •aA
A → •b
Drawing DFA:
The DFA contains the 7 states I0 to I6.
S First(S)First(A) Follow(S){$)
A First(A){a,b} Follow(A){First(A)U follow(A)} {a,b,$}
SLR(1) Table
In the CLR (1), we place the reduce node only in the lookahead symbols.
LR (1) item
The look ahead is used to determine that where we place the final item.
The look ahead always add $ symbol for the argument production.
Example
CLR ( 1 ) Grammar
1. S → AA
2. A → aA
3. A → b
Add Augment Production, insert '•' symbol at the first position for every production in G and
also add the lookahead.
1. S` → •S, $
2. S → •AA, $
3. A → •aA, a/b
4. A → •b, a/b
I0 State:
Add all productions starting with S in to I0 State because "." is followed by the non-
terminal. So, the I0 State becomes
I0 = S` → •S, $
S → •AA, $
Add all productions starting with A in modified I0 State because "." is followed by the non-
terminal. So, the I0 State becomes.
I0= S` → •S, $
S → •AA, $
A → •aA, a/b
A → •b, a/b
Add all productions starting with A in I2 State because "." is followed by the non-terminal.
So, the I2 State becomes
I2= S → A•A, $
A → •aA, $
A → •b, $
Add all productions starting with A in I3 State because "." is followed by the non-terminal.
So, the I3 State becomes
Add all productions starting with A in I6 State because "." is followed by the non-terminal.
So, the I6 State becomes
I6 = A → a•A, $
A → •aA, $
A → •b, $
Drawing DFA:
1. S → AA ... (1)
2. A → aA ....(2)
3. A → b ... (3)
The placement of shift node in CLR (1) parsing table is same as the SLR (1) parsing table.
Only difference in the placement of reduce node.
I4 contains the final item which drives ( A → b•, a/b), so action {I4, a} = R3, action {I4, b}
= R3.
I5 contains the final item which drives ( S → AA•, $), so action {I5, $} = R1.
I7 contains the final item which drives ( A → b•,$), so action {I7, $} = R3.
I8 contains the final item which drives ( A → aA•, a/b), so action {I8, a} = R2, action {I8,
b} = R2.
I9 contains the final item which drives ( A → aA•, $), so action {I9, $} = R2.