0% found this document useful (0 votes)
131 views24 pages

First, Follow Sets

The document discusses the concepts of FIRST and FOLLOW sets in parser construction. It provides examples of computing the FIRST and FOLLOW sets for nonterminals in a sample grammar. The FIRST set contains all possible first terminals for a nonterminal. The FOLLOW set contains all possible terminals that could follow a nonterminal in a derivation. The document explains how to systematically determine these sets based on the grammar productions.

Uploaded by

ahmed mahamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views24 pages

First, Follow Sets

The document discusses the concepts of FIRST and FOLLOW sets in parser construction. It provides examples of computing the FIRST and FOLLOW sets for nonterminals in a sample grammar. The FIRST set contains all possible first terminals for a nonterminal. The FOLLOW set contains all possible terminals that could follow a nonterminal in a derivation. The document explains how to systematically determine these sets based on the grammar productions.

Uploaded by

ahmed mahamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

&

Nullability
 FIRST() is the set of all terminals that may appear as

the first symbol in a replacement string of .

 FOLLOW() is the set of all terminals that may follow

 in a derivation.
E  TE’
E’  +TE’ | 
T  FT’
T’  *FT’ | 
F  (E) | id

FIRST(F) = {(,id}
FIRST(T’) = {*, }
FIRST(T) = {(,id}
FIRST(E’) = {+, }
FIRST(E) = {(,id}

CS416 Compiler Design Fall 2003 3


E  TE’
E’  +TE’ | 
T  FT’
T’  *FT’ | 
F  (E) | id

FOLLOW(E) = { $, ) }
FOLLOW(E’) = { $, ) }
FOLLOW(T) = { +, ), $ }
FOLLOW(T’) = { +, ), $ }
FOLLOW(F) = {+, *, ), $ }

CS416 Compiler Design Fall 2003 4


 Givena grammar G, we may define the
functions FIRST and FOLLOW on the strings of
symbols of G.
 FIRST() is the set of all terminals that may
appear as the first symbol in a replacement
string of .
 FOLLOW() is the set of all terminals that may
follow  in a derivation.
 Let the grammar be
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
 Find FIRST(E).
 E occurs on the left in only one production
E  T E'.
 Therefore, FIRST(T)  FIRST(E).
 Furthermore, T is not nullable.
 Therefore, FIRST(E) = FIRST(T).
 We have yet to determine FIRST(T).
 Find FIRST(T).
 T occurs on the left in only one production
T  F T'.
 Therefore, FIRST(F)  FIRST(T).
 Furthermore, F is not nullable.
 Therefore, FIRST(T) = FIRST(F).
 We have yet to determine FIRST(F).
 Find FIRST(F).
 FIRST(F) = {(, id, num}.
 Therefore,
 FIRST(E) = {(, id, num}.
 FIRST(T) = {(, id, num}.
 Find FIRST(E').
 FIRST(E') = {+}.
 Find FIRST(T').
 FIRST(T') = {*}.
Nonterminal Nullable FIRST
E No {(, id, num}
E' Yes {+}
T No {(, id, num}
T' Yes {*}
F No {(, id, num}
 Fora grammar symbol X, FOLLOW(X) is
defined as follows.
 If S is the start symbol, then $  FOLLOW(S).
 If A  B is a production, then FIRST() 
FOLLOW(B).
 If A  B is a production, or A  B is a
production and  is nullable, then FOLLOW(A) 
FOLLOW(B).
 We are concerned about FOLLOW(X) only for
the nonterminals of the grammar.
 According to the definition, to determine
FOLLOW(A), we must inspect all productions
that have A on the right.
 Let the grammar be
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
 Find FOLLOW(E).
 E is the start symbol, therefore $  FOLLOW(E).
 E occurs on the right in only one production.
F  (E).
 Therefore FOLLOW(E) = {$, )}.
 Find FOLLOW(E').
 E' occurs on the right in two productions.
E  T E'
E'  + T E'.
 Therefore, FOLLOW(E') = FOLLOW(E) = {$, )}.
 Find FOLLOW(T).
 T occurs on the right in two productions.
E  T E'
E'  + T E'.
 Therefore, FOLLOW(T) contains FIRST(E') = {+}.
 However, E' is nullable, therefore it also contains
FOLLOW(E) = {$, )} and FOLLOW(E') = {$, )}.
 Therefore, FOLLOW(T) = {+, $, )}.
 Find FOLLOW(T').
 T' occurs on the right in two productions.
T  F T'
T'  * F T'.
 Therefore, FOLLOW(T') = FOLLOW(T) = {$, ), +}.
 Find FOLLOW(F).
 F occurs on the right in two productions.
T  F T'
T'  * F T'.
 Therefore, FOLLOW(F) contains FIRST(T') = {*}.
 However, T' is nullable, therefore it also contains
FOLLOW(T) = {+, $, )} and FOLLOW(T') = {$, ), +}.
 Therefore, FOLLOW(F) = {*, $, ), +}.
Nonterminal Nullable FIRST FOLLOW
E No {(, id, num} {$, )}
E' Yes {+} {$, )}
T No {(, id, num} {$, ), +}
T' Yes {*} {$, ), +}
F No {(, id, num} {*, $, ), +}
A nonterminal A is nullable if
A * .
 Clearly, A is nullable if it has a production
A  .
 But A is also nullable if there are, for
example, productions
A  BC.
B  A | aC | .
C  aB | Cb | .
 In other words, A is nullable if there is a
production
A  ,
or there is a production
A  B1B2…Bn,
where B1, B2, ..., Bn are nullable.
 In the grammar
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
E' and T' are nullable.
 E, T, and F are not nullable.
Nonterminal Nullable
E No
E' Yes
T No
T' Yes
F No

You might also like