unit7
unit7
Predictive Parsing
1
Back to the backtrack parser
2
Predictive Parsers
3
LL(1) parser
4
A stringent condition
5
Left Recursion
A grammar is left recursive if it has a non-terminal A such that there
is a derivation.
6
Remind: Immediate Left-Recursion
In general,
7
Left recursion - problem
8
Eliminate left recursion - Algorithm
E E+T | T
T T*F | F
F id | (E)
E T E’
E’ +T E’ |
T F T’
T’ *F T’ |
F id | (E)
10
Predictive Parsing and Left Factoring
• In the grammar
ET+E|T
T int | int * T | ( E )
11
Left Factoring
12
Left Factoring (con’d)
In general,
13
Left factoring algorithm
convert it into
A A’ | 1 | ... | m
A’ 1 | ... | n
14
Left Factoring example
In KPL
16
Parsing table M
• in that case we pop X from the stack and we push all the rhs
symbols of the production M[X, token] .
17
Predictive Parser
The input contains the string to be parsed, followed by $ (EOF)
The stack contains a sequence of grammar symbols, preceded by #, the bottom-of-
stack marker.
Initially the stack contains the start symbol of the grammar preceded by $.
The parsing table is a two dimensional array M[A,a], where A is a nonterminal, and a is
a terminal or the symbol $.
- The parser is controlled by a program that behaves as follows:
- The program determines X, the symbol on top of the stack, and a, the current
input symbol.
- These two symbols determine the action of the parser.
There are three possibilities:
1. If X = a = $, the parser halts and announces successful completion of parsing.
2. If X = a ≠ $, the parser pops X off the stack and advances the input pointer to the
next input symbol.
3. If X is a nonterminal, the program consults entry M[X,a] of the parsing table M.
This entry will be either an X-production of the grammar or an error entry.
If M[X,a] = {X → UVW}, the parser replaces X on top of the stack by WVU (with U on
top).
If M[X,a] = error, the parser calls an error recovery routine.
18
Parsing table for grammar S aSb | c
a b c $
S S aSb Error Sc Error
a Push Error Error Error
b Error Push Error Error
c Error Error Push Error
# Error Error Error Accept
19
LL(1) Parsing Tables. Errors
20
Using Parsing Tables
21
LL(1) Parsing Algorithm
22
LL(1) Parsing Example for aacbb
23
Constructing Parsing Tables
24
Constructing Parsing Tables (Cont.)
25
Computing First Sets
Algorithm sketch :
1. for all terminals t do First(X) { t } //if X is terminal t
2. for each production X do First(X) { }
3. if X A1 … An and First(Ai), 1 i n do
• add First() to First(X)
4. for each X A1 … An s.t. First(Ai), 1 i n do
• add to First(X)
5. repeat steps 4 & 5 until no First set can be grown
26
First Sets. Example
27
Computing Follow Sets
• Definition:
Follow(X) = { t | S * X t }
• Intuition
• If S is the start symbol then $ Follow(S)
28
Computing Follow Sets (Cont.)
Algorithm sketch:
1. Follow(S) contains $.
2. For each production A X
• add First() - {} to Follow(X)
3. For each A X where First()
• add Follow(A) to Follow(X)
• repeat step(s) 2, 3 until no Follow set grows
29
Follow Sets. Example
30
Constructing LL(1) Parsing Tables
31
Example
ETE'
E'+TE'|
TFT'
T'*FT'|
F(E) |int
32
Parsing table
+ * ( ) int $
E ETE' ETE'
E' E'+TE' E' E'
T TFT' TFT'
T' T' T'*FT’ T' T'
F F(E) Fint
+ Push
* Push
( Push
) Push
int Push
# Accept
33
Notes on LL(1) parsing tables
• If G is left recursive
• If G is not left-factored
34