# Class 14: Predictive Parsing, Continued

Back to Predictive Parsing, Continued. On to Shift-Reduce Parsing.

Held Wednesday, February 21, 2001

Summary

Today we consider how to build and use predictive parsing tables.

Notes

• Attendance at lab tomorrow is required. No ifs, ands, or buts. (However, I may leave the early lab early to do something important with my son.)
• Don't forget the summer research talk tomorrow.

Overview

• Building the tables and functions
• The `First` table and `first` function
• The `Nullable` table and `nullable` function
• The `Follow` table
• Using the tables

## Building the First table and first function

• How do we define the first function?
• We know that if a sequence begins with a token, then it can only derive strings that begin with that token.
• But what if the sequence begins with a nonterminal?
• Then the sequence can derive strings that begin with any token that begins strings that the nonterminal can derive.
• What what if the nonterminal can derive the empty string?
• Then we need to look further in the original sequence.
• Putting it all together
```TokenSet first(s1 ... sn) {
if (isToken(s1)) {
return new TokenSet(s1);
}
else {
tmp = First(s1);
if (Nullable(s1)) {
first(s2 ... sn))
} // if s1 is nullable
} // if s1 is a nonterminal
} // first
```
• Note that this relies on the First table and the Nullable table.
• How do we build the First table? Using the first function
```  for each nonterminal N, First(N) = emptySet();
repeat
for each production N ::= RHS
First(N) = union(First(N), first(RHS))
end for
```

## Building the Nullable table and the nullable function

• The nullable function is relatively easy to define
```boolean nullable(s1 ... sn) {
// The empty string can derive the empty string
if (n == 0) {
return true;
}
// If we begin with a token, we're not nullable
if (isToken(s1)) {
return false;
}
// Otherwise, we begin with a nonterminal.  That nonterminal must
// be nullable, as must the rest of the sequence.
else {
return Nullable(s1) &&
nullable(s2..sn);
}
} // nullable
```
• And, once again, we can define the Nullable table in terms of the nullable function.
```  foreach nonterminal N, Nullable(N) = false
repeat
for each production N := RHS
if nullable(RHS) {
Nullable(N) = true;
}
end for
```

• The construction of the `Follow` table is a little bit more subtle.
• In general, every production can be written as N ::= alpha M beta, where alpha and beta are sequences of tokens and nonterminals.
• Note that there are a variety of ways to write a production in that form. Consider N ::= a B b C D
• We might have M is B, alpha is a, and beta is b C D
• We might have M is C, alpha is a B b, and beta is C D
• We might have M is D, alpha is a B b C, and beta is epsilon
• How do we use this information to build the `Follow` table? We know that M can be followed by anything that can begin a string derivable from beta.
• What if beta can derive epsilon (that is, beta is nullable)? Then anything that can follow N can follow M.
• S =>* aardvark N baboon -- Derivation of a string with N
• => aardvark alpha M beta baboon -- N ::= alpha M beta
• => aardvark alpha M baboon -- beta derives epsilon
• Putting it all together
```for each nonterminal, N
Follow(N) = {};
for each production P
for each form of P, N ::= alpha M beta
Follow(M) = Follow(M) union first(beta)
end for
end for
repeat
for each production P
for each form of P, N :: = alpha M beta
if nullable(beta)
Follow(M) = Follow(M) union Follow(N)
end if
end for
end for
end for
```

## History

Monday, 22 January 2001

• Created as a blank outline.

Wednesday, 21 February 2001

Back to Predictive Parsing, Continued. On to Shift-Reduce Parsing.

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Mon Apr 30 10:51:54 2001.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2001S/outline.14.html`.