0% found this document useful (0 votes)
11 views30 pages

Lecture 08 - FIRST and FOLLOW

Uploaded by

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

Lecture 08 - FIRST and FOLLOW

Uploaded by

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

FIRST and FOLLOW

Lecture 8
Mon, Feb 7, 2005
Left Factoring
 A problem occurs when two productions for
the same nonterminal begin with the same
token.
 We cannot decide which production to use.
 This is not necessarily a problem since we
could process the part they have in common,
then make a decision based on what follows.
Left Factoring
 Consider the grammar
A   | .
 We use left factorization to transform it into
the form
A  A'
A'   | .
 Now we can apply the productions
immediately and unambiguously.
Example: Left Factoring
 In the earlier example, we had the
productions
C  id == num | id != num | id < num
 To perform left factoring, introduce a
nonterminal C':
C  id C'
C'  == num | != num | < num
Example: Left Factoring
 Consider the grammar of if statements.
S  if C then S else S
| if C then S
 We rewrite it as
S  if C then S S'
S'  else S | .
LL Parsing Methods
 LL parsing methods read the tokens from Left
to right and parse them top-down according
to a Leftmost derivation.
Table-Driven LL Parsing
 To build the parsing table, we need the notion
of nullability and the two functions
 FIRST
 FOLLOW
Nullability
 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 | .
Nullability
 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.
Nullability
 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.
Summary

Nonterminal Nullable
E No
E' Yes
T No
T' Yes
F No
FIRST and FOLLOW
 Given a 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.
FIRST
 For a grammar symbol X, FIRST(X) is
defined as follows.
 For every terminal X, FIRST(X) = {X}.
 For every nonterminal X, if X  Y1Y2…Yn is a
production, then
 FIRST(Y1)  FIRST(X).
 Furthermore, if Y1, Y2, …, Yk are nullable, then
FIRST(Yk + 1)  FIRST(X).
FIRST
 We are concerned with FIRST(X) only for the
nonterminals of the grammar.
 FIRST(X) for terminals is trivial.
 According to the definition, to determine
FIRST(A), we must inspect all productions
that have A on the left.
Example: FIRST
 Let the grammar be
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
Example: FIRST
 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).
Example: FIRST
 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).
Example: FIRST
 Find FIRST(F).
 FIRST(F) = {(, id, num}.
 Therefore,
 FIRST(E) = {(, id, num}.
 FIRST(T) = {(, id, num}.
Example: FIRST
 Find FIRST(E').
 FIRST(E') = {+}.
 Find FIRST(T').
 FIRST(T') = {*}.
Summary

Nonterminal Nullable FIRST


E No {(, id, num}
E' Yes {+}
T No {(, id, num}
T' Yes {*}
F No {(, id, num}
FOLLOW
 For a 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).
FOLLOW
 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.
Example: FOLLOW
 Let the grammar be
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
Example: FOLLOW
 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) = {$, )}.
Example: FOLLOW
 Find FOLLOW(E').
 E' occurs on the right in two productions.
E  T E'
E'  + T E'.
 Therefore, FOLLOW(E') = FOLLOW(E) = {$, )}.
Example: FOLLOW
 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) = {+, $, )}.
Example: FOLLOW
 Find FOLLOW(T').
 T' occurs on the right in two productions.
T  F T'
T'  * F T'.
 Therefore, FOLLOW(T') = FOLLOW(T) = {$, ), +}.
Example: FOLLOW
 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) = {*, $, ), +}.
Summary

Nonterminal Nullable FIRST FOLLOW


E No {(, id, num} {$, )}
E' Yes {+} {$, )}
T No {(, id, num} {$, ), +}
T' Yes {*} {$, ), +}
F No {(, id, num} {*, $, ), +}
Exercise
 The grammar
R  R  R | RR | R* | (R) | a | b
generates all regular expressions on the
alphabet {a, b}.
 Using the result of the exercise from the
previous lecture, find FIRST(X) and
FOLLOW(X) for each nonterminal X in the
grammar.

You might also like