5- Lecture05 - Top-Down Parsing
5- Lecture05 - Top-Down Parsing
Top-Down Parsing
Lecture 5
1
LL(1) Predictive Parsers
• Left-factored grammar
ETX X+E|
T ( E ) | int Y Y*T|
• The LL(1) parsing table: next input token
int * + ( ) $
E TX TX
X +E
T int Y (E)
Y *T
rhs of production to use
leftmost non-terminal
Prof. Aiken 3
LL(1) Parsing Table Example (Cont.)
Prof. Aiken 4
LL(1) Parsing Tables. Errors
Prof. Aiken 5
LL(1) Parsing Algorithm
a + b $ Input (String + terminator $)
Stack
• If * t
– can derive a t in the first position
– We say that t First()
• If A and * and S * A t
– Useful if stack has A, input is t, and A cannot derive t
– In this case only option is to get rid of A (by deriving )
• Can work only if t can follow A in at least one derivation
– We say t Follow(A)
Prof. Aiken 8
Constructing LL(1) Parsing Tables
Prof. Aiken 9
Example 1
E TX X +E|
T (E) | int Y Y * T|
int * + ( ) $
E TX TX
X +E
T int Y (E)
Y *T
Prof. Aiken 10
Example 2
S S a | b
First( S ) = { b }
Follow( S ) = { $, a }
a b $
S b, Sa
Prof. Aiken 11
Notes on LL(1) Parsing Tables
Prof. Aiken 12
Notes on LL(1) Grammars
a + b $ Input
Stack
X Predictive Parsing Output
Program
Y
Z
Parsing Table
$ M[A,a]
15
Panic-Mode Recovery (Cont.)
Modify the empty cells of the Parsing Table.
1. if M[A, a] = {empty} and a belongs to Follow(A) then
we set M[A, a] = “synch”
Error-recovery Strategy :
If A=top-of-the-stack and a=current-token,
1. If A is NT and M[A, a] = {empty} then skip a from the
input.
2. If A is NT and M[A, a] = {synch} then pop A.
3. If A is a terminal and A!=a then pop A (This is
essentially inserting A before a).
16
Parse Table / Example
id + * ( ) $
E T E’ T E’ synch synch
E’ + T E’
T F T’ synch F T’ synch synch
T’ * F T’
F id synch synch (E) synch synch
E T E'
E' + T E' |
T F T'
T' * F T' |
F ( E ) | id
17
Parsing Example
id + * ( ) $
E T E’ T E’ synch synch
E’ + T E’
T F T’ synch F T’ synch synch
T’ * F T’
F id synch synch (E) synch synch
id + * ( ) $
E T E’ T E’ synch synch
E’ + T E’
T F T’ synch F T’ synch synch
T’ * F T’
F id synch synch (E) synch synch
Transition Diagrams
20
Transition Diagrams
E TE’ T FT’ F ( E ) | id
E’ + TE’ | T’ * FT’ |
22
Transition Diagrams can be Simplified (2)
+ T E’
E’: 3 4 5 6
+ T
E’: 3 4 5
6
23
Transition Diagrams can be Simplified (3)
+ T E’
E’: 3 4 5 6
T
+ T +
E’: 3 4 5 E’: 3 4
6 6
24
Transition Diagrams can be Simplified (4)
+ T E’
E’: 3 4 5 6
T
+ T +
E’: 3 4 5 E’: 3 4
6 6
T E’
E: 0 1 2
25
Transition Diagrams can be Simplified (5)
+ T E’
E’: 3 4 5 6
T
+ T +
E’: 3 4 5 E’: 3 4
6 6
T E’
E: 0 1 2
+
T
T
E: 0 3
T +
E: 0 3 4
6 26
6
Similar steps for T and T’
* F T’
T’: 10 11 12 13
*
T’: 10 11
13
F T’
T: 7 8 9
F 27
T: 7 8 13
Simplified Transition diagrams
+
T
E: 0 3
6
F
T: 7 8 13
( E )
F: 14 15 16 17
id 28
Implementing Panic-Mode Recovery
29
Implementing Panic-Mode Recovery (Cont.)
Suppose the parser is in diagram A, the current token is a,
and a syntax error is detected:
1. if a Follow (A),
Report the error by ‘illegal a found on line N’,
where N is the line number of token a,
then get the next token from the scanner, and then call
diagram A .
2. if a Follow (A),
Report the error by: ‘missing A1 on line N’, where N is the
line number of token a; then resume parsing by exiting
from A.
1Note that in a real compiler, in the error message, A should
be replaced by a simple token that can be derived from A.
30
Implementing Panic-Mode Recovery (Cont.)
31
Question?
Choose the next parse state given the grammar, parse table, and
current state below. The initial string is:
if true then { true } else { if false then { false } } $
if then else { } true false $
E if Bthen { E } E’ B B
E’ else { E}
B true false
Stack Input E if B then { E } E’
Current E’$ else { if false then { false } } $ | B|
$ $ E’ else { E } |
B true | false
else {E} $ else { if false then { false } } $
Prof. Aiken 32
Question?
S i E t S S’ | a First(S) = Follow(S) =
S’ e S | First(S’) = Follow(S’) =
E b First(E) = Follow(E) =
a b e i t $
S
S’
E
33
Question?
id + * ( ) $
E
E’
T
T’
F 34
Question?