Bottom-Up Parsing (cont.


Lecture 6

CS2210 Compiler Design 2004/5

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)? SLR (simple LR) tables

Parsing tables from LR grammars

Many grammars for which it is not possible Works on “most” grammars Often used in practice because significantly smaller than canonical LR
CS2210 Compiler Design 2004/5

Canonical LR tables

LALR (= lookahead LR) tables

LR(0) items
■ ■

A->XYZ Items:
■ ■ ■ ■


Represents how much of a production we have seen in parsing
CS2210 Compiler Design 2004/5


Bβ in closure(I) and B->γ is a production add B->.β| A->α.Xβ ∈ I}) For valid items I for viable prefix γ . S’->S new start symbol S’ ■ ■ Closure function Goto function CS2210 Compiler Design 2004/5 Closure ■ ■ I set of items of G Closure(I) ■ ■ Initially I Repeat If A->α. I set of items.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. X grammar symbol Goto(I. then goto(I.X).X) = valid items for viable prefix γX CS2210 Compiler Design 2004/5 2 .X) := closure({A->αX.γ to closure ■ ■ Until no new items get added CS2210 Compiler Design 2004/5 Goto ■ ■ ■ Goto(I.

$] = accept Goto transitions for state I are constructed for all nonterminal A as follows: if goto(Ii.A] := j Undefined entries are set to error The initial state of the parser is the on constructed from the items containing S’->.a] := reduce A-> α for all a in Follow(A) c) if S’->S is in Ii then set action[I.aβ is in Ii and goto(I i. collection of LR(0) items State i is constructed from I i with parsing actions: a) if A->α.a) = Ij then action[i.….X) to C until no more changes to C end CS2210 Compiler Design 2004/5 Sets of Items Construction Example E’ -> E E -> E + T | T T -> T * F | F F -> (E) | id I0 = I1 = I2 = I3 = I4 = I5 = I6 = I7 = I8 = I9 = I10 = I11 = CS2210 Compiler Design 2004/5 SLR Table Construction Input G’ augmented grammar 1.A) = Ij then goto[i.In}. is in Iithen action[i. Construct C ={I 0 . repeat for each set of items I in C and each grammar symbol X such that goto(I. 3.a] := shift jγβ (a must be a terminal) b) if A-> α. 5. begin C := closure({S->. 2. 4.Sets of Items Construction procedure items(G’).S}).X) is not empty and not in C do add goto(I.S CS2210 Compiler Design 2004/5 3 .

20 in Aho Canonical LR ■ ■ Alternatives ■ Most powerful Smaller parsing tables CS2210 Compiler Design 2004/5 ■ LALR ■ 4 .SLR Table Example Same grammar… CS2210 Compiler Design 2004/5 Parsing Example State 0 1 2 3 4 5 6 7 8 9 10 11 id + * ( ) $ E T F (1) E->E+T (2) E->T (3) T->T*F (4) T->F (5) F->(E) (6) F->id Action CS2210 Compiler Design 2004/5 Goto Grammars that are not SLR ■ There are non-ambiguous grammars for which SLR tables have multiple entries ■ Grammar 4.

*R. L->. R->.} = ∈Follow(L) CS2210 Compiler Design 2004/5 5 . S->.id} I5={L->id.R. L->. L->.Building LR Parsing Tables ■ Problem in SLR ■ If [A -> α.id} I7={L->*R. L->.S.} I3={S->R.=R.} I9={S->L=R. L->. L->.L.*R.. S->.L} I1={S’->S.*R.} I8={R->} I5={L->id.} I6->{S->L=.} I9={S->L=R.} I6->{S-> L->.} I2={S->L. R->} I7={L->*R.} I4={L->*.} I4={L->*. L->.L.=R. S->.*R.R.L. R->. R->..} I8={R->L.*R.R. R->. L->.*R. L->.R.L. L->.R.L} I1={S’->S.} I3={S->R. R->L.} Shift-reduce conflict Viable prefix L=R should not reduce to R=R CS2210 Compiler Design 2004/5 SLR Problem Example (2) Parse id =id Stack s0 s0ids5 s0Ls2 s0Rs3 s0Ss1 Error (expect to see a $) Input id=id =id =id =id =id Items: I0= {S’-> . L->.id. Carry more information in state on stack ■ Solution ■ CS2210 Compiler Design 2004/5 SLR Problem Example Grammar: S -> L = R S -> R L -> *R L -> id R -> L Items: I0= {S’-> . R->.R. S->.] 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. R->L.} I2={S->L.L=R.

CS2210 Compiler Design 2004/5 Example ■ Consider same grammar again: S -> L = R S -> R L -> *R L -> id R -> L CS2210 Compiler Design 2004/5 6 . procedure items(G’). a] a terminal (including $) a represents (1 token) lookahead ■ ■ Used only if β=ε. begin let J be the set of items [A->a. we use lookahead to decide whether to reduce (or shift/error) a ∈ Follow(A) CS2210 Compiler Design 2004/5 LR(1) Items Construction function closure(I).e.X) is not empty do add goto(I.X) to C (if not there) until no more items can be added to C end function goto(I. a] in I. β.LR(1) Items ■ ■ New item form: [A -> α.Bβ.X). begin C := {closure({S’->. begin repeat for each item [A->α.Xb. i.S.$]})}.a] such that [A->a. repeat for each set of items I in C and each grammar symbol X such that goto(I. b] to I (if not present) until no more items can be added to I end. and each terminal b in First(βa) add [B->.γ. each production B->γ in G’.. end.Xb.a] is in I.

” not leftmost ■ ■ Can generate all items in I from kernel Can generate parsing actions from kernel ■ Details in the book CS2210 Compiler Design 2004/5 7 . $ and L->id... merge only if no conflicts CS2210 Compiler Design 2004/5 LALR Table Construction ■ Simple but space / time consuming ■ ■ Build LR tables Merge item sets with identical cores ■ If conflict results: grammar not LALR CS2210 Compiler Design 2004/5 Efficient LALR Construction ■ Ideas: ■ Represent only kernels for items set I. i.g.000s for typical language Algorithm of choice in practice ■ Most PL constructs can be parsed with LALR ■ ■ Construction idea ■ ■ Merge different items with same core. e.. 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.e.S or item with “.LALR Parsing ■ Advantages ■ Smaller parsing tables than canonical LR ■ 100s versus 1. either S’->.

Parsing with more Lookahead ■ LR(k) ■ ■ ■ Closure: A->α. a predict B->. 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) Useful if too lazy too rewrite the grammar ■ Some parser generators for LALR(2) ■ CS2210 Compiler Design 2004/5 Language Class Hierarchy CS2210 Compiler Design 2004/5 8 . a terminal and x ∈ Firstk(aβFollow k(A)) ■ SLR(k) ■ ■ ■ LALR(k): merge LR(k) machine states with same core CS2210 Compiler Design 2004/5 LR(k) in Practice ■ LR(k). where a is a terminal and x ∈ Firstk(aβy) Reduce: A-> α.aβ.Bβ.x reduce on lookahead x Shift on lookahead x for A-> α. if lookahead x ∈ Followk(A) Shift on lookahead x for A-> α.γ.aβ.. y. y where y∈Firstk(βx) Reduce: A-> α.

id} I1={E’->E.*E} I2={E->(. E->.id} I3={E->id.+E.). E->. E->.+E. 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 CS2210 Compiler Design 2004/5 9 .E+E.Parsing with Ambiguous Grammars ■ Use non-grammatical means to resolve conflict ■ Want to have just ONE parse tree! “more natural” Smaller Precedence Associativity Shift versus reduce CS2210 Compiler Design 2004/5 ■ Useful because grammar can be ■ ■ ■ Conflict resolution: ■ ■ ■ Precedence Example E->E+E | E*E | (E) | id versus E-> E+T | T T -> T*F | F F -> (E) | id Parse for id+id*id I0={E’->.(E). E->E.E). E->E.E+E.*E} I8={E->E*E. E->. E->.} CS2210 Compiler Design 2004/5 Parser Generators ■ ■ Yacc.*E} I7={E->E+E.(E). E->. E->.E+E.E.(E). E->. E->. E->E.E.. E->E.E*E. E->E. E->. E->.} I4={E->E+. E->id} I5={E->E*.E*E. LLGen.+E. E->.+E. E->E. E->. E->.E.. E->E. E->E.E*E.(E).id} I6={E->(E. E->.*E} I9={E->(E).E+E..E*E. bison.

Sign up to vote on this title
UsefulNot useful