Post on 11-Jan-2016
410/510 1 of 21
Week 2 – Lecture 1
Bottom Up (Shift reduce, LR parsing)SLR, LR(0) parsingSLR parsing table
Compiler Construction
410/510 2 of 21
Bottom-up Parsing
• Shift reduce parsing – Parse from the input string to the start symbol
– Begins at the bottom of the parse tree working up towards the root
• At each step a substring (of terminals and nonterminals comprising the right hand side of a production reduced to a single nonterminal (the left side of a production)
• A rightmost derivation in reverse
410/510 3 of 21
example
E -> E + T | T
T -> T * F | F
F -> (E) | id
What is the rightmost derivation for id + id * id
A handle is a substring which matches the right hand side of a production
Handles are pruned
410/510 4 of 21
Reductions made by shift-reduce parser
Right-sentential form Handle Reducing Production
id1 + id2 * id3 id1 F -> id
F + id2 * id3 F T->F
T + id2 * id3 T E -> T
E + id2 * id3 id2 F -> id
E + F * id3 F T -> F
E + T * id3 id3 F -> id
E + T * F T * F T -> T * F
E + T E + T E -> E + T
410/510 5 of 21
Stack implementation of a shift reduce Parser
Configurations of a shift-reduce parser on input id1 + id2 * id3
Actions - shift, reduce, accept
410/510 6 of 21
Shift-reduce conflicts
Stmt -> if Expr then Stmt| if Expr then Stmt else Stmtother
STACK…if Expr then Stmt
INPUTElse …$
410/510 7 of 21
LR(k) Parsers
• LR – Left to right scan of the input, Rightmost derivation, k number of symbols lookahead
• Most general nonbacktracking shift-reduce parsing method
• Parses all grammars that predictive parsers can and more
• Too much work to construct by hand
410/510 8 of 21
The LR Parser
LRParsing Program
$a1 … ai … an
action gotos0
…
Xm-1
sm-1
Xm
smSTACK
INPUT
OUTPUT
Aho, Sethi and Ullman (p.217)
LR Parsing Table
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 r7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
State action goto
Page 219 Aho, Sethi and Ullman
2
3
4
5
6 9
7
8
0 1
10
11
E + T* go to 7
F go to 3
go to 4
go to 5
(
idT * F
(
idgo to 4
go to 5F
(
idid
T
F
(
E )
+
go to 2
go to 3
go to 6
Page 226 Aho, Sethi and Ullman
410/510 11 of 21
Moves of an LR parser on id * id + id
(1) E -> E + T
(2) E -> T
(3) T -> T * F
(4) T -> F
(5) F -> (E)
(6) F -> id
410/510 12 of 21
Constructing SLR parsing tables
• Closure operation
• GOTO operation
• The set of items construction
• SLR parsing table
410/510 13 of 21
The Closure Operation
If I is the set of items for a grammar G, then closure(I) is the set of items constructed from I by:
• Initially, every item in I is added to closure(I)
• If A -> .B is in closure(I) and B -> is a production, then add the item B -> . to I if it is not already there. Apply this rule until no more new items can be added to closure(I)
E’ -> EE -> E + TT -> T * F | FF -> (E) | id
Initially I is the set of one item {[E’->.E]}
410/510 14 of 21
Closure of {[E’->.E]}
E’->.E
410/510 15 of 21
The GOTO operation
• Move the dot over one symbol• This becomes the kernel item for a new state• Compute its closure
I1 = {[E’ ->E .], [E -> E . + T]}
goto(I1 +) = {[E -> E + . T]}
Closure(I1) = ??
410/510 16 of 21
First and Follow Sets
• First and Follow sets tell when it is appropriate to put the right hand side of some production on the stack in predictive parsing
(i.e. for which input symbols)
• In LR parsing FOLLOW sets are used to tell us if we should reduce a handle or shift an input symbol to produce a bigger handle
410/510 17 of 21
First Sets
1. If X is a terminal, then FIRST(X) is {X}
2. IF X -> is a production, then add to FIRST(X)
3. IF X is a nonterminal and X -> Y1Y2…Yk is a
production, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and is in all of First(Y1), …First(Yi-1). If is in FIRST(Yj) for all j = 1, 2, …k, then add to
FIRST(X).
410/510 18 of 21
FIRST sets
(1) E’ -> E(2) E -> E +
T(3) E -> T(4) T -> T * F(5) F -> (E)(6) F -> id
410/510 19 of 21
The SLR parsing table
1. Construct C = {I0, I1, …., In}, the collection of sets of LR(0) items
2. State i is constructed from Ii. The parsng actions for state i are determined as followsa) If [A->.a] is in Ii and goto(Ii, a) = Ij, then set action[i,a] to
shift j. A must be a terminal.
b) If [A->.] is in Ii, then set action[I, a] to “reduce A -> ” for all a in FOLLOW(A); here A may not be S’
c) If [S’ -> S .] is in Ii then set action [i, $] to accept.
410/510 20 of 21
Follow Sets
• Apply the following rules until no more terminals can be added to any follow set1. Place $ in FOLLOW (S’), where S is the start symbol and $ is
the input right end marker
2. If there is a production A -> B, then everything in FIRST() except for is placed in FOLLOW(B)
3. If there is a production A -> B, or a production A -> B where FIRST() contains (i.e. B => ), then everything in FOLLOW(A) is in FOLLOW(B).
*
410/510 21 of 21
Follow Sets
(1) E’ -> E(2) E -> E + T(3) E -> T(4) T -> T * F(5) F -> (E)(6) F -> id