











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Recall the theory:
for each terminal a A
for each rule A !# , # a sequence of
terminals and variables
where S is the grammar's
start symbol
Recall the theory:
At each step, PDA can either
for each terminal a A
for each rule A !# , # a sequence of
terminals and variables
where S is the grammar's
start symbol
Recall the theory:
At each step, PDA can either
the grammar (derive).
for each terminal a A
for each rule A !# , # a sequence of
terminals and variables
where S is the grammar's
start symbol
Recall the Practice
and moreConcat () =
if (couldBeSimple (!lookahead))
then
let val x = closure()
val y = moreConcat()
in case y of
NONE => SOME x
| SOME z => SOME(Concat(x,z))
end
else NONE
and couldBeSimple LeftParen = true
| couldBeSimple Hash = true
| couldBeSimple Zero = true
| couldBeSimple (Single _) = true
| couldBeSimple _ = false
Top-down predictive parsers
Use an explicit stack instead of recursion.
Represent the transitions of the PDA in a
table, rather than as code
Choice of action of the parser (which
production to use to expend the stack
symbol) represented by three functions:
Nullable
First
Follow
Example First and Follow Sets
E → T Eʹ $
Eʹ → + T Eʹ
Eʹ → Λ
T → F Tʹ
Tʹ →
F Tʹ
Tʹ → Λ
F → ( E )
F → id
First E = { "(", "id"} Follow E = {")","$"}
First F = { "(", "id"} Follow F = {"+","
First T = { "(", "id"} Follow T = {{"+",")","$"}
First Eʹ = { "+", Λ } Follow E' = {")","$"}
First T' = { "
", Λ } Follow T' = {"+",")","$"}
defined on a sequence of
symbols.
Nullable
if Λ is in First(symbol) then that symbol is
nullable.
Sometimes, rather than let Λ be a symbol, we
use an additional function Nullable.
‣
‣
‣
Example First Computation
» (^) First(F) = {id , ( }
» (^) First(T') = { Λ, * }
» (^) First(T) = First(F) = {id, ( }
» (^) First(E') = { Λ, + }
» (^) First(E) = First(T) = {id, ( }
E → T E' $
E' → + T E'
E' → Λ
T → F T'
T' → * F T'
T' → Λ
F → ( E )
F → id
Computing FOLLOW
to any follow set:
start symbol.
(This is Hein’s rule 1)
FOLLOW(B)
(This is Hein’s rule 3)
where FIRST( b ) contains Λ (i.e., nullable b ), then put everything
in FOLLOW(A) into FOLLOW(B)
(This is a combination of Hein’s rules 2 & 4)
For each production A → ω do steps 1–3 below:
in Follow A.
First E = { "(", "id"} Follow E = {")","$"}
First F = { "(", "id"} Follow F = {"+","
",”)”,"$"}
First T = { "(", "id"} Follow T = {{"+",")","$"}
First Eʹ = { "+", Λ } Follow E' = {")","$"}
First T' = { "
", Λ } Follow T' = {"+",")","$"}
Table from First and Follow
1 E → T E' $
2 E' → + T E'
3 | Λ
4 T → F T'
5 T' → * F T'
6 | Λ
7 F → ( E )
8 | id
terminals
+ * ) ( id $
n
o
n-
t
e
r
m
s
E
E’
T
T’
F
M[A,t]
1 1
2 3 3
4 4
6 5 6 6
7 8
Predictive Parsing Table
E
E’
T
T’
F
id
( ) (^) $
T E’ T E’
Λ
F T’
F T’
Λ
Λ Λ
id ( E )
Example
Parse
E
E’
T
T’
F
id +^
( ) $
T E’ T E’
Λ
F T’ F T’
Λ
Λ Λ
id ( E )
Stack Input
E x + y $
T E’ x + y $
F T’ E’ x + y $
id T’ E’ x + y $
T’ E’ + y $
E’ + y $
+ T E’ + y $
T E’ y $
F T’ E’ y $
id T’ E’ y $
Top of stack