SlideShare a Scribd company logo
1 of 65
Download to read offline
1
Ms. Bushra Abdullah Al-Anesi
Compiler Construction
Alfred V. Aho, , "Compilers", Addison Wesley Publishing Company; 2 edition (2007).
Syntax Analysis
Chapter 4
2
FIRST Function
32
▪ FIRST function indicates the set of terminals that begin strings derived
from a given string of grammar symbols
Rules for Finding FIRST Sets:
1. If X is a terminal, then FIRST(X) = {X}.
2. If X is a nonterminal and X→ Y1Y2 . . Yk is a production for some
k>=1, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and ℇ is
in all of FIRST(Y1), . . . , FIRST(Yi-1); that is, Y1 . . . Yi-1 ⇒ ℇ. If ℇ is in
FIRST(Yj) for all j = 1, 2, . . . , k, then add ℇ to FIRST(X). For example,
everything in FIRST(Y1) is surely in FIRST(X). If Yi does not derive ℇ
then we add nothing more to FIRST(X), but if Y1 ⇒ ℇ, then we add
FIRST(Y2), and so on.
3. If X → ℇ is a production, then add ℇ to FIRST(X).
4. Continue until no more terminals or ℇ can be added to any FIRST set.
*
*
FOLLOW Function
34
▪ FOLLOW function indicates the set of terminals that can appear
immediately to the right of a given nonterminal in some sentential form
▪ Rules:
1. Place $ in FOLLOW(S), where S is the start symbol, and $ is the input
right end marker.
2. If there is a production A → 𝞪B𝞫, then everything in FIRST(𝞫) except ℇ
is in FOLLOW(B).
3. If there is a production A → 𝞪B, or a production A → 𝞪B𝞫, where
FIRST(𝞫) contains ℇ, then everything in FOLLOW(A) is in FOLLOW(B).
4. Continue until nothing can be added to any FOLLOW set.
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
Nonterminal S has 2 productions:
1. the first will be FIRST(B)
2. the first will be FIRST(C)
So need to solve these first
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
Nonterminal B has 2
productions, the first
begins with a and
second is ε
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
Nonterminal B has 2
productions, the first
begins with a and
second is ε
{ a, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, ε }
Nonterminal C has 2
productions, the first
begins with a and
second is ε
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, ε }
Nonterminal C has 2
productions, the first
begins with a and
second is ε
{ c, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, ε }
{ c, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, ε }
{ c, ε }
Nonterminal S has 2 productions:
1. the first will be FIRST(B) which is {a, ε}
Here we won’t write the ε, instead we
will use what is after nonterminal B
2. the second will be FIRST(C) which is {c,
ε} Here we won’t write the ε, instead we
will use what is after nonterminal C
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
Nonterminal S has 2 productions:
1. the first will be FIRST(B) which is {a, ε}
Here we won’t write the ε, instead we
will use what is after nonterminal B
2. the second will be FIRST(C) which is {c,
ε} Here we won’t write the ε, instead we
will use what is after nonterminal C
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
Since S is start symbol,
FOLLOW is $
{ a, ε }
{ c, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
Since S is start symbol,
FOLLOW is $
{ a, ε }
{ c, ε }
{ $ }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
Find occurrence(s) of B in rhs:
1. What comes after it in first occurrence? b
2. What comes after it in second occurrence? Since
it is at right most in production, so
FOLLOW(nonterminal on left) = FOLLOW(B)
which again will be b
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
Find occurrence(s) of B in rhs:
1. What comes after it in first occurrence? b
2. What comes after it in second occurrence? Since
it is at right most in production, so
FOLLOW(nonterminal on left) = FOLLOW(B)
which again will be b
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
Find occurrence(s) of B in rhs:
1. What comes after it in first occurrence? b
2. What comes after it in second occurrence? Since
it is at right most in production, so
FOLLOW(nonterminal on left) = FOLLOW(B)
which again will be b
{ b }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
{ b }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
{ b }
Find occurrence(s) of C in rhs:
1. What comes after it in first occurrence? d
2. What comes after it in second occurrence? Since
it is at right most in production, so
FOLLOW(nonterminal on left) = FOLLOW(C)
which again will be d
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
{ b }
Find occurrence(s) of C in rhs:
1. What comes after it in first occurrence? d
2. What comes after it in second occurrence? Since
it is at right most in production, so
FOLLOW(nonterminal on left) = FOLLOW(C)
which again will be d
FIRST and FOLLOW Functions, Contd.
36
▪ Example 1:
Nontermina
l
First Follow
S
B
C
S → B b | C d
B → a B | ε
C → c C | ε
{ a, b, c, d }
{ a, ε }
{ c, ε }
{ $ }
{ b }
Find occurrence(s) of C in rhs:
1. What comes after it in first occurrence? d
2. What comes after it in second occurrence? Since
it is at right most in production, so
FOLLOW(nonterminal on left) = FOLLOW(C)
which again will be d
{ d }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
Nonterminal E has 1
production that starts with T
and T starts with F, so do
FIRST(F)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
Nonterminal F has 2
productions that starts with
( or id
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
Nonterminal F has 2
productions that starts with
( or id
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
Nonterminal T’ has 2
productions that start with *
or ε
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
Nonterminal T’ has 2
productions that start with *
or ε
{ *, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
Nonterminal T has
FIRST(F)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
Nonterminal T has
FIRST(F)
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
Nonterminal E’ has 2
productions
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
Nonterminal E’ has 2
productions
{ +, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
Nonterminal E has
FIRST(T) Remember,
FIRST(T) is FIRST (F)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
Nonterminal E has
FIRST(T) Remember,
FIRST(T) is FIRST (F)
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
1. Since start symbol
FOLLOW is the $
2. Check for E in rhs, the )
is after it
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
1. Since start symbol
FOLLOW is the $
2. Check for E in rhs, the )
is after it
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
1. Since start symbol
FOLLOW is the $
2. Check for E in rhs, the )
is after it
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
Check for E’ in rhs:
1. In first occurrence it is in E
production at right most part, so it
will have FOLLOW(E) = {$, )}
2. In 2nd occurrence it is in E’
production at right most part so
will have FOLLOW(E’)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
Check for E’ in rhs:
1. In first occurrence it is in E
production at right most part, so it
will have FOLLOW(E) = {$, )}
2. In 2nd occurrence it is in E’
production at right most part so
will have FOLLOW(E’)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
Check for E’ in rhs:
1. In first occurrence it is in E
production at right most part, so it
will have FOLLOW(E) = {$, )}
2. In 2nd occurrence it is in E’
production at right most part so
will have FOLLOW(E’)
{ $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
Check for T in rhs:
1. In first occurrence it is in E production
followed by E’, so it will have FIRST(E’) = {+,
ε}
Can’t have ε in FOLLOW, so replace E’ in the
production with empty so production
becomes E → T and since T is at right most,
we will get FOLLOW(E) = {$, )}
2. In second occurrence it is followed by E’ so
FIRST(E’) = {+, ε)} … (same as 1.)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
Check for T in rhs:
1. In first occurrence it is in E production
followed by E’, so it will have FIRST(E’) = {+,
ε}
Can’t have ε in FOLLOW, so replace E’ in the
production with empty so production
becomes E → T and since T is at right most,
we will get FOLLOW(E) = {$, )}
2. In second occurrence it is followed by E’ so
FIRST(E’) = {+, ε)} … (same as 1.)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
{ +, $, ) }
Check for T in rhs:
1. In first occurrence it is in E production
followed by E’, so it will have FIRST(E’) = {+,
ε}
Can’t have ε in FOLLOW, so replace E’ in the
production with empty so production
becomes E → T and since T is at right most,
we will get FOLLOW(E) = {$, )}
2. In second occurrence it is followed by E’ so
FIRST(E’) = {+, ε)} … (same as 1.)
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
{ +, $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
Check for T’ in rhs:
1. In first occurrence it is in T production at right
most side so will be FOLLOW(T) = {+, $, )}
2. In 2nd occurrence it is in T’ production at right
most side so will be FOLLOW(T’) = {+, $, )}
{ +, $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
Check for T’ in rhs:
1. In first occurrence it is in T production at right
most side so will be FOLLOW(T) = {+, $, )}
2. In 2nd occurrence it is in T’ production at right
most side so will be FOLLOW(T’) = {+, $, )}
{ +, $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
Check for T’ in rhs:
1. In first occurrence it is in T production at right
most side so will be FOLLOW(T) = {+, $, )}
2. In 2nd occurrence it is in T’ production at right
most side so will be FOLLOW(T’) = {+, $, )}
{ +, $, ) }
{ +, $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
{ +, $, ) }
{ +, $, ) }
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
{ +, $, ) }
{ +, $, ) }
Check for F in rhs:
1. In first occurrence it is in T production
followed by T’ so will be FIRST(T’) = { *, ε }
Can’t have ε, so replace T’ with empty, then F
will be at right most of T production, so will
have FOLLOW(T) = {+, $, ) }
2. In 2nd occurrence it is in T’ production – same
as 1
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
{ +, $, ) }
{ +, $, ) }
Check for F in rhs:
1. In first occurrence it is in T production
followed by T’ so will be FIRST(T’) = { *, ε }
Can’t have ε, so replace T’ with empty, then F
will be at right most of T production, so will
have FOLLOW(T) = {+, $, ) }
2. In 2nd occurrence it is in T’ production – same
as 1
FIRST and FOLLOW Functions, Contd.
36
▪ Example 2:
Nontermin
al
First Follow
E
E’
T
T’
F
E → T E’
E’ → + T E’ | ε
T → F T’
T’ →* F T’ | ε
F → ( E ) | id
{ (, id }
{ *, ε }
{ (, id }
{ +, ε }
{ (, id } { $, ) }
{ $, ) }
{ +, $, ) }
{ +, $, ) }
Check for F in rhs:
1. In first occurrence it is in T production
followed by T’ so will be FIRST(T’) = { *, ε }
Can’t have ε, so replace T’ with empty, then F
will be at right most of T production, so will
have FOLLOW(T) = {+, $, ) }
2. In 2nd occurrence it is in T’ production – same
as 1
{ *, +, $, ) }
9
Resource
Alfred V. Aho, , "Compilers", Addison Wesley Publishing
Company; 2 edition (2007).

More Related Content

Similar to Ch04 syntax analysis first_follow_2019 (7)

Compiler first set_followset_brief
Compiler first set_followset_briefCompiler first set_followset_brief
Compiler first set_followset_brief
 
Compiler_FirstSet_FollowSet-examples.pptx
Compiler_FirstSet_FollowSet-examples.pptxCompiler_FirstSet_FollowSet-examples.pptx
Compiler_FirstSet_FollowSet-examples.pptx
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0
 
Module 5 circular functions
Module 5   circular functionsModule 5   circular functions
Module 5 circular functions
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 

Ch04 syntax analysis first_follow_2019

  • 1. 1 Ms. Bushra Abdullah Al-Anesi Compiler Construction Alfred V. Aho, , "Compilers", Addison Wesley Publishing Company; 2 edition (2007).
  • 3. FIRST Function 32 ▪ FIRST function indicates the set of terminals that begin strings derived from a given string of grammar symbols Rules for Finding FIRST Sets: 1. If X is a terminal, then FIRST(X) = {X}. 2. If X is a nonterminal and X→ Y1Y2 . . Yk is a production for some k>=1, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and ℇ is in all of FIRST(Y1), . . . , FIRST(Yi-1); that is, Y1 . . . Yi-1 ⇒ ℇ. If ℇ is in FIRST(Yj) for all j = 1, 2, . . . , k, then add ℇ to FIRST(X). For example, everything in FIRST(Y1) is surely in FIRST(X). If Yi does not derive ℇ then we add nothing more to FIRST(X), but if Y1 ⇒ ℇ, then we add FIRST(Y2), and so on. 3. If X → ℇ is a production, then add ℇ to FIRST(X). 4. Continue until no more terminals or ℇ can be added to any FIRST set. * *
  • 4. FOLLOW Function 34 ▪ FOLLOW function indicates the set of terminals that can appear immediately to the right of a given nonterminal in some sentential form ▪ Rules: 1. Place $ in FOLLOW(S), where S is the start symbol, and $ is the input right end marker. 2. If there is a production A → 𝞪B𝞫, then everything in FIRST(𝞫) except ℇ is in FOLLOW(B). 3. If there is a production A → 𝞪B, or a production A → 𝞪B𝞫, where FIRST(𝞫) contains ℇ, then everything in FOLLOW(A) is in FOLLOW(B). 4. Continue until nothing can be added to any FOLLOW set.
  • 5. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε
  • 6. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε Nonterminal S has 2 productions: 1. the first will be FIRST(B) 2. the first will be FIRST(C) So need to solve these first
  • 7. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε
  • 8. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε Nonterminal B has 2 productions, the first begins with a and second is ε
  • 9. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε Nonterminal B has 2 productions, the first begins with a and second is ε { a, ε }
  • 10. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, ε }
  • 11. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, ε } Nonterminal C has 2 productions, the first begins with a and second is ε
  • 12. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, ε } Nonterminal C has 2 productions, the first begins with a and second is ε { c, ε }
  • 13. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, ε } { c, ε }
  • 14. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, ε } { c, ε } Nonterminal S has 2 productions: 1. the first will be FIRST(B) which is {a, ε} Here we won’t write the ε, instead we will use what is after nonterminal B 2. the second will be FIRST(C) which is {c, ε} Here we won’t write the ε, instead we will use what is after nonterminal C
  • 15. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } Nonterminal S has 2 productions: 1. the first will be FIRST(B) which is {a, ε} Here we won’t write the ε, instead we will use what is after nonterminal B 2. the second will be FIRST(C) which is {c, ε} Here we won’t write the ε, instead we will use what is after nonterminal C
  • 16. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε }
  • 17. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } Since S is start symbol, FOLLOW is $ { a, ε } { c, ε }
  • 18. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } Since S is start symbol, FOLLOW is $ { a, ε } { c, ε } { $ }
  • 19. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ }
  • 20. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } Find occurrence(s) of B in rhs: 1. What comes after it in first occurrence? b 2. What comes after it in second occurrence? Since it is at right most in production, so FOLLOW(nonterminal on left) = FOLLOW(B) which again will be b
  • 21. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } Find occurrence(s) of B in rhs: 1. What comes after it in first occurrence? b 2. What comes after it in second occurrence? Since it is at right most in production, so FOLLOW(nonterminal on left) = FOLLOW(B) which again will be b
  • 22. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } Find occurrence(s) of B in rhs: 1. What comes after it in first occurrence? b 2. What comes after it in second occurrence? Since it is at right most in production, so FOLLOW(nonterminal on left) = FOLLOW(B) which again will be b { b }
  • 23. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } { b }
  • 24. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } { b } Find occurrence(s) of C in rhs: 1. What comes after it in first occurrence? d 2. What comes after it in second occurrence? Since it is at right most in production, so FOLLOW(nonterminal on left) = FOLLOW(C) which again will be d
  • 25. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } { b } Find occurrence(s) of C in rhs: 1. What comes after it in first occurrence? d 2. What comes after it in second occurrence? Since it is at right most in production, so FOLLOW(nonterminal on left) = FOLLOW(C) which again will be d
  • 26. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 1: Nontermina l First Follow S B C S → B b | C d B → a B | ε C → c C | ε { a, b, c, d } { a, ε } { c, ε } { $ } { b } Find occurrence(s) of C in rhs: 1. What comes after it in first occurrence? d 2. What comes after it in second occurrence? Since it is at right most in production, so FOLLOW(nonterminal on left) = FOLLOW(C) which again will be d { d }
  • 27. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id
  • 28. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id Nonterminal E has 1 production that starts with T and T starts with F, so do FIRST(F)
  • 29. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id
  • 30. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id Nonterminal F has 2 productions that starts with ( or id
  • 31. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id Nonterminal F has 2 productions that starts with ( or id { (, id }
  • 32. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id }
  • 33. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } Nonterminal T’ has 2 productions that start with * or ε
  • 34. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } Nonterminal T’ has 2 productions that start with * or ε { *, ε }
  • 35. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε }
  • 36. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } Nonterminal T has FIRST(F)
  • 37. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } Nonterminal T has FIRST(F) { (, id }
  • 38. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id }
  • 39. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } Nonterminal E’ has 2 productions
  • 40. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } Nonterminal E’ has 2 productions { +, ε }
  • 41. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε }
  • 42. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } Nonterminal E has FIRST(T) Remember, FIRST(T) is FIRST (F)
  • 43. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } Nonterminal E has FIRST(T) Remember, FIRST(T) is FIRST (F) { (, id }
  • 44. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id }
  • 45. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id }
  • 46. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id 1. Since start symbol FOLLOW is the $ 2. Check for E in rhs, the ) is after it { (, id } { *, ε } { (, id } { +, ε } { (, id }
  • 47. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id 1. Since start symbol FOLLOW is the $ 2. Check for E in rhs, the ) is after it { (, id } { *, ε } { (, id } { +, ε } { (, id }
  • 48. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id 1. Since start symbol FOLLOW is the $ 2. Check for E in rhs, the ) is after it { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) }
  • 49. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) }
  • 50. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } Check for E’ in rhs: 1. In first occurrence it is in E production at right most part, so it will have FOLLOW(E) = {$, )} 2. In 2nd occurrence it is in E’ production at right most part so will have FOLLOW(E’)
  • 51. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } Check for E’ in rhs: 1. In first occurrence it is in E production at right most part, so it will have FOLLOW(E) = {$, )} 2. In 2nd occurrence it is in E’ production at right most part so will have FOLLOW(E’)
  • 52. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } Check for E’ in rhs: 1. In first occurrence it is in E production at right most part, so it will have FOLLOW(E) = {$, )} 2. In 2nd occurrence it is in E’ production at right most part so will have FOLLOW(E’) { $, ) }
  • 53. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) }
  • 54. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } Check for T in rhs: 1. In first occurrence it is in E production followed by E’, so it will have FIRST(E’) = {+, ε} Can’t have ε in FOLLOW, so replace E’ in the production with empty so production becomes E → T and since T is at right most, we will get FOLLOW(E) = {$, )} 2. In second occurrence it is followed by E’ so FIRST(E’) = {+, ε)} … (same as 1.)
  • 55. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } Check for T in rhs: 1. In first occurrence it is in E production followed by E’, so it will have FIRST(E’) = {+, ε} Can’t have ε in FOLLOW, so replace E’ in the production with empty so production becomes E → T and since T is at right most, we will get FOLLOW(E) = {$, )} 2. In second occurrence it is followed by E’ so FIRST(E’) = {+, ε)} … (same as 1.)
  • 56. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } { +, $, ) } Check for T in rhs: 1. In first occurrence it is in E production followed by E’, so it will have FIRST(E’) = {+, ε} Can’t have ε in FOLLOW, so replace E’ in the production with empty so production becomes E → T and since T is at right most, we will get FOLLOW(E) = {$, )} 2. In second occurrence it is followed by E’ so FIRST(E’) = {+, ε)} … (same as 1.)
  • 57. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } { +, $, ) }
  • 58. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } Check for T’ in rhs: 1. In first occurrence it is in T production at right most side so will be FOLLOW(T) = {+, $, )} 2. In 2nd occurrence it is in T’ production at right most side so will be FOLLOW(T’) = {+, $, )} { +, $, ) }
  • 59. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } Check for T’ in rhs: 1. In first occurrence it is in T production at right most side so will be FOLLOW(T) = {+, $, )} 2. In 2nd occurrence it is in T’ production at right most side so will be FOLLOW(T’) = {+, $, )} { +, $, ) }
  • 60. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } Check for T’ in rhs: 1. In first occurrence it is in T production at right most side so will be FOLLOW(T) = {+, $, )} 2. In 2nd occurrence it is in T’ production at right most side so will be FOLLOW(T’) = {+, $, )} { +, $, ) } { +, $, ) }
  • 61. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } { +, $, ) } { +, $, ) }
  • 62. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } { +, $, ) } { +, $, ) } Check for F in rhs: 1. In first occurrence it is in T production followed by T’ so will be FIRST(T’) = { *, ε } Can’t have ε, so replace T’ with empty, then F will be at right most of T production, so will have FOLLOW(T) = {+, $, ) } 2. In 2nd occurrence it is in T’ production – same as 1
  • 63. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } { +, $, ) } { +, $, ) } Check for F in rhs: 1. In first occurrence it is in T production followed by T’ so will be FIRST(T’) = { *, ε } Can’t have ε, so replace T’ with empty, then F will be at right most of T production, so will have FOLLOW(T) = {+, $, ) } 2. In 2nd occurrence it is in T’ production – same as 1
  • 64. FIRST and FOLLOW Functions, Contd. 36 ▪ Example 2: Nontermin al First Follow E E’ T T’ F E → T E’ E’ → + T E’ | ε T → F T’ T’ →* F T’ | ε F → ( E ) | id { (, id } { *, ε } { (, id } { +, ε } { (, id } { $, ) } { $, ) } { +, $, ) } { +, $, ) } Check for F in rhs: 1. In first occurrence it is in T production followed by T’ so will be FIRST(T’) = { *, ε } Can’t have ε, so replace T’ with empty, then F will be at right most of T production, so will have FOLLOW(T) = {+, $, ) } 2. In 2nd occurrence it is in T’ production – same as 1 { *, +, $, ) }
  • 65. 9 Resource Alfred V. Aho, , "Compilers", Addison Wesley Publishing Company; 2 edition (2007).