lecture 8 - first and follow
TRANSCRIPT
![Page 1: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/1.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
FIRST and FOLLOWLecture 8
Section 4.4
Robb T. Koether
Hampden-Sydney College
Mon, Feb 9, 2009
![Page 2: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/2.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Outline
1 Left Factoring
2 Table-Driven LL ParsingNullabilityThe FIRST FunctionThe FOLLOW Function
3 Assignment
![Page 3: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/3.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Left Factoring
A problem occurs when two productions for the samenonterminal begin with the same token.We cannot decide which production to use.This is not necessarily a problem since we couldprocess the part they have in common, then make adecision based on what follows.
![Page 4: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/4.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
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 andunambiguously.
![Page 5: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/5.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
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
![Page 6: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/6.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
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 SS′
S′ → else S | ε
![Page 7: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/7.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Table-Driven LL Parsing
To build the parsing table, we need three concepts:NullabilityThe FIRST functionThe FOLLOW function
![Page 8: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/8.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Nullability
Definition (Nullable)A nonterminal A is nullable if
A⇒∗ ε.
![Page 9: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/9.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Nullability
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 | ε
![Page 10: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/10.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
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.
![Page 11: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/11.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (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.
![Page 12: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/12.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (Nullability)
Nonterminal NullableE NoE′ YesT NoT ′ YesF No
![Page 13: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/13.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
FIRST and FOLLOW
Definition (FIRST)FIRST(α) is the set of all terminals that may appear as thefirst symbol in a replacement string of α.
Definition (FOLLOW)FOLLOW(α) is the set of all terminals that may follow α in aderivation.
Given a grammar G, we may define the functionsFIRST and FOLLOW on the strings of symbols of G.
![Page 14: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/14.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
FIRST
For a grammar symbol X, FIRST(X) is computed asfollows.
For every terminal X, FIRST(X) = {X}.For every nonterminal X, if
X → Y1Y2 . . . Yn
is a production, thenFIRST(Y1) ⊆ FIRST(X).Furthermore, if Y1, Y2, . . . , Yk are nullable, then
FIRST(Yk+1) ⊆ FIRST(X).
![Page 15: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/15.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
FIRST
We are concerned with FIRST(X) only for thenonterminals of the grammar.FIRST(X) for terminals is trivial.According to the definition, to determine FIRST(A), wemust inspect all productions that have A on the left.
![Page 16: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/16.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FIRST)Let the grammar be
E → T E′
E′ → + T E′ | εT → F T ′
T ′ → * F T ′ | εF → (E) | id | num
![Page 17: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/17.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
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).
![Page 18: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/18.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
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).
![Page 19: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/19.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FIRST)Find FIRST(F).
FIRST(F) = {(, id,num}.Therefore,
FIRST(E) = {(, id,num}.FIRST(T) = {(, id,num}.
![Page 20: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/20.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FIRST)
Find FIRST(E′).FIRST(E′) = {+}.
Find FIRST(T ′).FIRST(T ′) = {*}.
![Page 21: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/21.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FIRST)
Nonterminal Nullable FIRSTE No {(, id,num}E′ Yes {+}T No {(, id,num}T ′ Yes {*}F No {(, id,num}
![Page 22: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/22.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
FOLLOW
For a grammar symbol X, FOLLOW(X) is defined asfollows.
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 productionand β is nullable, then
FOLLOW(A) ⊆ FOLLOW(B).
![Page 23: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/23.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
FOLLOW
We are concerned about FOLLOW(X) only for thenonterminals of the grammar.According to the definition, to determine FOLLOW(A),we must inspect all productions that have A on the right.
![Page 24: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/24.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)Let the grammar be
E → T E′
E′ → + T E′ | εT → F T ′
T ′ → * F T ′ | εF → (E) | id | num
![Page 25: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/25.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)Find FOLLOW(E).
E is the start symbol, therefore
$ ∈ FOLLOW(E).
E occurs on the right in only one production.
F → (E)
ThereforeFOLLOW(E) = {$,)}.
![Page 26: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/26.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)
Find FOLLOW(E′).E′ occurs on the right in two productions.
E → T E′
E′ → + T E′
Therefore,
FOLLOW(E′) = FOLLOW(E) = {$,)}.
![Page 27: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/27.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)Find FOLLOW(T).
T occurs on the right in two productions.
E → T E′
E′ → + T E′
Therefore, FOLLOW(T) ⊇ FIRST(E′) = {+}.However, E′ is nullable, therefore it also containsFOLLOW(E) = {$,)} and FOLLOW(E′) = {$,)}.Therefore,
FOLLOW(T) = {+, $,)}.
![Page 28: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/28.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)
Find FOLLOW(T ′).T ′ occurs on the right in two productions.
T → F T ′
T ′ → * F T ′
Therefore,
FOLLOW(T ′) = FOLLOW(T) = {$,),+}.
![Page 29: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/29.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)Find FOLLOW(F).
F occurs on the right in two productions.
T → F T ′
T ′ → * F T ′.
Therefore,
FOLLOW(F) ⊇ FIRST(T ′) = {*}.
However, T ′ is nullable, therefore it also containsFOLLOW(T) = {+, $,)} and FOLLOW(T ′) = {$,),+}.Therefore,
FOLLOW(F) = {*, $,),+}.
![Page 30: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/30.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Example
Example (FOLLOW)
Nonterminal Nullable FIRST FOLLOWE No {(, id,num} {$,)}E′ Yes {+} {$,)}T No {(, id,num} {$,),+}T ′ Yes {*} {$,),+}F No {(, id,num} {*, $,),+}
![Page 31: Lecture 8 - First and Follow](https://reader031.vdocuments.site/reader031/viewer/2022020723/546a5d49b4af9f8c7d8b45a8/html5/thumbnails/31.jpg)
FIRST andFOLLOW
Robb T.Koether
Left Factoring
Table-DrivenLL ParsingNullability
The FIRST Function
The FOLLOWFunction
Assignment
Assignment
HomeworkThe 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 previouslecture, find FIRST(X) and FOLLOW(X) for eachnonterminal X in the grammar.