Predictive Parsing: Recall The Main Idea of Top-Down Parsing
Predictive Parsing: Recall The Main Idea of Top-Down Parsing
How?
What if we do some "preprocessing" to answer the
question: Given a non-terminal A and lookahead t,
which (if any) production of A is guaranteed to start
with a t?
1
Predictive parsing
If we have two productions: A
, we want a
distinct way of choosing the correct one.
Define:
Predictive parsing
Compute FIRST(X) as follows:
if X is a terminal, then FIRST(X)={X}
if X is a production, then add to FIRST(X)
if X is a non-terminal and XY1Y2...Yn is a production,
add FIRST(Yi) to FIRST(X) if the preceding Yjs contain
in their FIRSTs
Predictive parsing
What if we have a "candidate" production A
where = or *?
We could expand if we knew that there is some
sentential form where the current input symbol
appears after A.
Define:
Predictive parsing
Compute FOLLOW as follows:
FOLLOW(S) contains EOF
For productions AB, everything in FIRST()
except goes into FOLLOW(B)
For productions AB or AB where FIRST()
contains , FOLLOW(B) contains everything that is in
FOLLOW(A)
Predictive parsing
Armed with
FIRST
FOLLOW
we can build a parser where no backtracking is
required!
Building a parser
Original grammar:
EE+E
EE*E
E(E)
Eid
Remove ambiguity:
EE+T
TT*F
F(E)
Fid
Building a parser
The grammar:
ETE'
E'+TE'|
TFT'
T'*FT'|
F(E)
Fid
Parsing table
+
E
E' E'+TE'
T
T'
T'
F
match
+
*
(
)
id
$
T'*FT'
match
(
ETE'
TFT'
F(E)
match
)
E'
T'
match
id
ETE'
TFT'
Fid
match
$
E'
T'
accept
11
Parsing table
Parse the input id*id using the parse table and a stack
Step Stack
1
$E
2
$E'T
3
$E'T'F
4
$E'T'id
5
$E'T'
6
$T'F*
7
$T'F
8
$T'id
9
$T'
10
$
Input
id*id$
id*id$
id*id$
id*id$
*id$
*id$
id$
id$
$
$
Next Action
ETE'
TFT'
Fid
match id
T'*FT'
match *
Fid
match id
T'
accept
12
E() {
if (T())
then return Eprime()
else return false
}
Eprime() {
if (token == '+')
then token=get_next_token()
if (T())
then return Eprime()
else return false
else if (token==')' or token=='$')
then return true
else return false
}
LL(1) parsing
Our parser scans the input Left-to-right,
LL(1) parsing
For example, the following grammar will have
15
LL(1) parsing
Here's an example of a grammar that is NOT
16
slot is encountered.
We would like our parser to be able to recover
from an error and continue parsing.
Phase-level recovery
17
19