Top Down Parsing Summary
Top Down Parsing Summary
1 Basic Concepts
1.1 Calculating the First set of a symbol with lambda
productions
The First set for a nonterminal is the set of terminals which could
potentially be the first token on the left of the nonterminals
structure.
Previously we have seen how to calculate this, but now we extend
the approach to allow for lambda productions.
Where a is a terminal, First(a) = { a }
The First set of a nonterminal N can be found as follows:
1. Set First(N) = { }
2. For each production N :
symbols):
= X1 X2 Xn
For terminal a: First(a) = { a }
First() can be calculated as follows:
1. Set i to 1
2. First() = { }
3. Add First(Xi) to First()
4. If I < n AND FIRST(Xi) includes :
a. Delete from First()
b. Increment i
c. Goto 3
Basically, First() is the First() set of the first symbol of , and
also of subsequent symbols where the first symbols can be
realised as lambda.
Example: Given the following grammar,
SA | B
AaAb | Bc
By |
First(B) is { y, }
First(A) is {a, y, c}
First(S) is {a, y, c, }
1
Algorithm:
For each production A:= of the grammar:
For each terminal symbol a First(),
Add the production A:= in the cell T[A,a]
2
Example:
For the following grammar:
E := TE
E := +TE
E :=
T := FT
T := *FT
T :=
F := (E)
F := id
Firsts and Follows:
First(F) = { (, id }
First(T) = { *, }
First(T) = First(F) = { (, id }
First(E) = { +, }
First(E) = First(T) = { (, id }
Follow(E) = { $, ) }
Follow(T) = Follow(T)
=
+Follow(E)
= {+, $, ) }
First(E)-
Result:
+
E := +TE
T := *FT
E :=
T := FT
T :=
F := (E)
E := TE
T := FT
T :=
id
E :=
T
T
E := TE
E
E
T :=
F := id
Stack
Input
Action
Input
Action
Action
Example:
Step Stack
Input
Action
$E
id+id$
Apply E::=TE
$ET
id+id$
Apply T::=FT
$ETF
id+id$
Apply F::=id
$ETid
id+id$
Advance
$ET
+id$
Apply T::=
$E
+id$
Apply E::=+TE
$ET+
+id$
Advance
$ET
id$
Apply T::=FT
$ETF
id$
Apply F::=id
10
$ETid
id$
Advance
11
$ET
Apply T::=
12
$E
Apply E::=
13
Input Accepted