0% found this document useful (0 votes)
4K views

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

The document discusses first and follow sets which are used by parsers to apply production rules correctly. It provides definitions and rules for calculating first and follow functions. Several examples of grammars are given along with the steps to eliminate left recursion and calculate first and follow sets. Calculating first and follow sets is important for understanding parser operations in compiler design.

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)
4K views

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

The document discusses first and follow sets which are used by parsers to apply production rules correctly. It provides definitions and rules for calculating first and follow functions. Several examples of grammars are given along with the steps to eliminate left recursion and calculate first and follow sets. Calculating first and follow sets is important for understanding parser operations in compiler design.

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/ 20

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,

What is Parser and Types of Parsers in Compiler


Design
Parser is that phase of compiler which takes token string as input and with the help of existing
grammar, converts it into the corresponding parse tree. Parser is also known as Syntax Analyzer.
Parsing –

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.

Two approaches in general.

Expanding from the start symbol to the whole program (top down)

Reduction from the whole program to start symbol (bottom up).

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.

(ii). Non-recursive descent parser:


It is also known as LL(1) parser or predictive parser or without backtracking parser or dynamic parser. It
uses parsing table to generate the parse tree instead of backtracking.

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.

Construction of LL(1) Parsing Table:


To construct the Parsing table, we have two functions:
1: First(): If there is a variable, and from that variable if we try to drive all the strings then the
beginning Terminal Symbol is called the first.
2: Follow(): What is the Terminal Symbol which follow a variable in the process of derivation.
Now, after computing the First and Follow set for each Non-Terminal symbol we have to construct
the Parsing table. In the table Rows will contain the Non-Terminals and the column will contain
the Terminal Symbols.
All the Null Productions of the Grammars will go under the Follow elements and the remaining
productions will lie under the elements of First set.
Now, let’s understand with an example.
Example-1:
Consider the Grammar:
E --> TE'
E' --> +TE' | e
T --> FT'
T' --> *FT' | e
F --> id | (E)

**e denotes epsilon


Grammar FIRST FOLLOW

E –> TE’ { id, ( } { $, ) }

E’ –> +TE’/e { +, e } { $, ) }

T –> FT’ { id, ( } { +, $, ) }

T’ –> *FT’/e { *, e } { +, $, ) }

F –> id/(E) { id, ( } { *, +, $, ) }

Find their first and follow sets:

Now, the LL(1) Parsing Table is:


ID + * ( ) $
E E –> TE’ E –> TE’
E’ E’ –> +TE’ E’ –> e E’ –> e
T T –> FT’ T –> FT’
T’ T’ –> e T’ –> *FT T’ –> e T’ –> e
F F –> id F –> (E)
As you can see that all the null productions are put under the follow set of that symbol and all the
remaining productions are lie under the first of that symbol.
Note: Every grammar is not feasible for LL(1) Parsing table. It may be possible that one cell may
contain more than one production.
Let’s see with an example.

Example-2:
Consider the Grammar
S --> A | a
A --> a
Find their first and follow sets:
Grammar FIRST FOLLOW
S –> A/a {a} {$}

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.

In the LR parsing, "L" stands for left-to-right scanning of the input.

"R" stands for constructing a right most derivation in reverse.

"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:

 Add Augment production in the given grammar.


 Create Canonical collection of LR (0) items.
 Draw a data flow diagram (DFA).
 Construct a LR (1) parsing table.
 Grammar is not LR 1 if
Two reduced productions in one state – RR conflict or One reduced and one shifted production
in one state – SR conflict.
 Grammar is LR1
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.

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  

The Augment grammar G` is represented by

S`→ S  

S → AA  

A → aA | b  

Canonical Collection of LR(0) items


An LR (0) item is a production G with dot at some position on the right side of the
production.

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.

In the LR (0), we place the reduce node in the entire row.

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 = Closure (S` → •S)


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

I1= Go to (I0, S) = closure (S` → S•) = S` → S•

Here, the Production is reduced so close the State.

I1= S` → S•

I2= Go to (I0, A) = closure (S → A•A)

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

Go to (I2,a) = Closure (A → a•A) = (same as I3)

Go to (I2, b) = Closure (A → b•) = (same as I4)

I3= Go to (I0,a) = Closure (A → a•A)

Add productions starting with A in I3.

A → a•A

A → •aA

A → •b

Go to (I3, a) = Closure (A → a•A) = (same as I3)

Go to (I3, b) = Closure (A → b•) = (same as I4)

I4= Go to (I0, b) = closure (A → b•) = A → b•

I5= Go to (I2, A) = Closure (S → AA•) = SA → A


I6= Go to (I3, A) = Closure (A → aA•) = A → aA

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 is going to some other state on a variable, then it correspond to go to 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:

 Find first set and fellow set of the given grammar


 Add Augment production in the given grammar.
 Create Canonical collection of LR (0) items.
 Draw a data flow diagram (DFA).
 Construct a LR (1) parsing table.
 Grammar is not LR 1 if
Two reduced productions in one state – RR conflict or One reduced and one shifted production
in one state – SR conflict.
 Grammar is LR1
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.

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,$}

The Augment grammar G` is represented by

S`→ S  

S → AA  

A → aA | b  

Canonical Collection of SLR(1) items


An LR (0) item is a production G with dot at some position on the right side of the
production.

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.

In the LR (0), we place the reduce node in the entire row.

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 above grammar no conflict so it is SLR(1) grammar.

CLR (1) Parsing


CLR refers to canonical lookahead. CLR parsing use the canonical collection of LR (1) items
to build the CLR (1) parsing table. CLR (1) parsing table produces the more number of
states as compare to the SLR (1) parsing.

In the CLR (1), we place the reduce node only in the lookahead symbols.

Various steps involved in the CLR (1) Parsing:

o For the given input string write a context free grammar


o Check the ambiguity of the grammar
o Add Augment production in the given grammar
o Create Canonical collection of LR (0) items
o Draw a data flow diagram (DFA)
o Construct a CLR (1) parsing table

LR (1) item

LR (1) item is a collection of LR (0) items and a look ahead symbol.

LR (1) item = LR (0) item + look ahead

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 Augment production to the I0 State and Compute the Closure

I0 = Closure (S` → •S)

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

I1= Go to (I0, S) = closure (S` → S•, $) = S` → S•, $


I2= Go to (I0, A) = closure ( S → A•A, $ )

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, $

I3= Go to (I0, a) = Closure ( A → a•A, a/b )

Add all productions starting with A in I3 State because "." is followed by the non-terminal.
So, the I3 State becomes

I3= A → a•A, a/b


       A → •aA, a/b
       A → •b, a/b

Go to (I3, a) = Closure (A → a•A, a/b) = (same as I3)


Go to (I3, b) = Closure (A → b•, a/b) = (same as I4)

I4= Go to (I0, b) = closure ( A → b•, a/b) = A → b•, a/b


I5= Go to (I2, A) = Closure (S → AA•, $) =S → AA•, $
I6= Go to (I2, a) = Closure (A → a•A, $)

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, $

Go to (I6, a) = Closure (A → a•A, $) = (same as I6)


Go to (I6, b) = Closure (A → b•, $) = (same as I7)

I7= Go to (I2, b) = Closure (A → b•, $) = A → b•, $


I8= Go to (I3, A) = Closure (A → aA•, a/b) = A → aA•, a/b
I9= Go to (I6, A) = Closure (A → aA•, $) = A → aA•, $

Drawing DFA:

CLR (1) Parsing table:


Productions are numbered as follows:

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.

You might also like