SLR Parser
SLR Parser
(cont.)
CS2210
Lecture 6
LR Grammars
■ = a grammar for which a LR parsing table can be
constructed
■ LR(0) and LR(1) typically of interest
■ What about LL(0)?
■ Parsing tables from LR grammars
■ SLR (simple LR) tables
■ Many grammars for which it is not possible
■ Canonical LR tables
■ Works on “most” grammars
■ LALR (= lookahead LR) tables
■ Often used in practice because significantly smaller than
canonical LR
LR(0) items
■ A->XYZ
■ Items:
■ A->.XYZ
■ A->X.YZ
■ A->XY.Z
■ A->XYZ.
■ Represents how much of a production
we have seen in parsing
1
Parse Table Construction
■ Build a DFA to recognize viable prefixes
■ = set of prefixes of right sentential forms that can appear on
the stack of a shift-reduce parser
■ Items are ~ states of an NFA for viable prefixes
■ Group items together (subset construction) to get DFA
■ Define a canonical collection of LR(0) items
■ Helpers:
■ Augmented grammar
■ S start symbol of G, S’->S new start symbol S’
■ Closure function
■ Goto function
Closure
■ I set of items of G
■ Closure(I)
■ Initially I
■ Repeat
■ If A->α.Bβ in closure(I) and B->γ is a production add
B->.γ to closure
■ Until no new items get added
Goto
■ Goto(I,X), I set of items, X grammar
symbol
■ Goto(I,X) :=
closure({A->αX.β| A->α.Xβ ∈ I})
■ For valid items I for viable prefix γ ,
then goto(I,X) = valid items for viable
prefix γX
2
Sets of Items Construction
procedure items(G’);
begin
C := closure({S->.S});
repeat
for each set of items I in C and each
grammar symbol X such that goto(I,X) is not
empty and not in C do
add goto(I,X) to C
until no more changes to C
end
3
SLR Table Example
Same grammar…
(1) E->E+T
Parsing Example (2) E->T
State id + * ( ) $ E T F
(3) T->T*F
0 (4) T->F
1 (5) F->(E)
2
(6) F->id
3
4
5
6
7
8
9
10
11
Action Goto
CS2210 Compiler Design 2004/5
4
Building LR Parsing Tables
■ Problem in SLR
■ If [A -> α.] is in Items set and
a ∈ Follow(A) then we reduce by A -> α but βα
on the stack is such that βA cannot be followed by
a.
■ Solution
■ Carry more information in state on stack
5
LR(1) Items
■ New item form: [A -> α. β, a] a
terminal (including $)
■ a represents (1 token) lookahead
■ Used only if β=ε, i.e., we use lookahead to
decide whether to reduce (or shift/error)
■ a ∈ Follow(A)
Example
■ Consider same grammar again:
S -> L = R
S -> R
L -> *R
L -> id
R -> L
6
LALR Parsing
■ Advantages
■ Smaller parsing tables than canonical LR
■ 100s versus 1,000s for typical language
■ Most PL constructs can be parsed with LALR
■ Algorithm of choice in practice
■ Construction idea
■ Merge different items with same core, e.g.
L->id., $ and L->id.,)
■ May reduce where an error should be reported
■ But reduction will lead to a parse error later
■ Merging does not produce new shift-reduce conflicts
■ Possible reduce-reduce conflicts; merge only if no conflicts
CS2210 Compiler Design 2004/5
7
Parsing with more Lookahead
■ LR(k)
■ Closure: A->α.Bβ, a predict B->.γ, y where
y∈Firstk(βx)
■ Reduce: A-> α.,x reduce on lookahead x
■ Shift on lookahead x for A-> α.aβ, y, where a is a
terminal and x ∈ Firstk(aβy)
■ SLR(k)
■ Reduce: A-> α. if lookahead x ∈ Followk(A)
■ Shift on lookahead x for A-> α.aβ, a terminal and
x ∈ Firstk(aβFollow k(A))
■ LALR(k): merge LR(k) machine states with
same core CS2210 Compiler Design 2004/5
LR(k) in Practice
■ LR(k), SLR(k) are not used in practice
■ Tables too large
■ Not necessary in practice since most
grammars can be made LR(1) and even
LALR(1)
■ Some parser generators for LALR(2)
■ Useful if too lazy too rewrite the grammar
8
Parsing with Ambiguous
Grammars
■ Use non-grammatical means to resolve
conflict
■ Want to have just ONE parse tree!
■ Useful because grammar can be
■ “more natural”
■ Smaller
■ Conflict resolution:
■ Precedence
■ Associativity
■ Shift versus reduce
CS2210 Compiler Design 2004/5
Precedence Example
E->E+E | E*E | (E) | I0={E’->.E, E->.E+E, E->.E*E, E->.(E),
E->.id}
id I1={E’->E., E->E.+E, E->E.*E}
I2={E->(.E), E->.E+E, E->.E*E, E->.(E),
versus E->.id}
I3={E->id.}
E-> E+T | T I4={E->E+.E, E->.E+E, E->.E*E, E->.(E),
E->id}
T -> T*F | F I5={E->E*.E, E->.E+E, E->.E*E, E->.(E),
E->.id}
Parser Generators
■ Yacc, bison, LLGen, LRGen etc
■ Specify grammar
■ Produce a table-drive parser
■ Typically can use precedence & associativity rules
and allow shift-reduce conflicts
■ Project uses Cup a parser generator for Java