Unit 2 - Session 7
Unit 2 - Session 7
COMPILER DESIGN
UNIT 2
SESSION 7
Topics that will be covered in this Session
• Computation of FIRST
• Problems related to FIRST
COMPUTATION OF
FIRST
FIRST and FOLLOW – Introduction
• The functions FIRST and FOLLOW help in the construction of both top-down and bottom-up
parsers, associated with a grammar G
• During top-down parsing, FIRST and FOLLOW allows us to choose which production to apply,
based on the next input symbol
• During panic-mode error recovery, sets of tokens produced by FOLLOW can be used as
synchronizing tokens
Computation of FIRST
Definition
• FIRST() is defined as the set of terminals that begin strings derived from
Apply the following rules until no more terminals or ε can be added to any FIRST set
FIRST(E) = FIRST(T)
FIRST(T) = FIRST(F)
FIRST(E′) = { +,ε }
FIRST(T′) = { *,ε }
Example 2
• Consider the grammar
S→(L)|a
L→L,S|S
FIRST(S) = { (, a )
FIRST(L) = FIRST(S) = { (, a )
Example 3
• Consider the grammar
S→cAd
A → a A′
A′ → b | ε
FIRST(S) = { c )
FIRST(A) = { a )
FIRST(A′) = { b , ε }
Example 4
• Consider the grammar
S→L=R|R
L → * R | id
R→L
FIRST(L) = { * , id )
FIRST(R) = FIRST(L) = { * , id }
Therefore FIRST(S) = { * , id }