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).

Ch04 syntax analysis first_follow_2019

  • 1.
    1 Ms. Bushra AbdullahAl-Anesi Compiler Construction Alfred V. Aho, , "Compilers", Addison Wesley Publishing Company; 2 edition (2007).
  • 2.
  • 3.
    FIRST Function 32 ▪ FIRSTfunction 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 ▪ FOLLOWfunction 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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 FOLLOWFunctions, 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).