100 likes | 141 Views
LALR(1) parsing. COP4620 – Programming Language Translators Dr. Manuel E. Bermudez. Non SLR(1) Grammars Calculating LALR(1) lookahead sets LALR(1) examples. Summary of Parsing (top-down vs. bottom-up). topics. S. . 1. 2. 6. LALR(1) parsing. A. b. a. 3. 7. 10. A → a.
E N D
LALR(1) parsing COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Non SLR(1) Grammars Calculating LALR(1) lookahead sets LALR(1) examples. Summary of Parsing (top-down vs. bottom-up) topics
S 1 2 6 LALR(1) parsing A b a 3 7 10 A → a Grammar: S’ → S S → AbAa → Ba A → a B → a LR(0) Automaton: SLR(1) Analysis: (State 5) Follow(A) = {a, b} Follow(B) = {a} A a 9 11 A → AbAa B a 4 8 S → Ba a A → a B → a 5 Grammar is not LR(0): reduce-reduce conflict. Conflict not resolved. Grammar not SLR(1). Need Follow(A) ’in the context of’ State 1, not state 7. LR(0) table. SLR(1) table.
LR Parsing history • LR parsing: D. Knuth seminal paper, 1965. • SLR(1): F. DeRemer, Ph.D. thesis, MIT, 1969. • LALR(1): various algorithms (from LR(1) DFA), 1970’s. • LALR(1): Efficient Computation of LALR(1) Lookahead sets, (from LR(0) DFA) F. DeRemer, T. Pennello, UCSC, 1981. 5. LALR(k): M. Bermudez, Ph.D. thesis, UCSC, 1984. 6. LALR(1): Simple Computation of LALR(1) Lookahead sets, M. Bermudez and G. Logothetis, UF, 1989.
Simple LALR(1) parsing I. For each conflicting reduction A → ω at each conflicting state q, find all nonterminal transitions (pi, A) such that II. Need the union of Follow(pi, A) for all i. . . . A p1 ω q A → ω ω A pn
Simple LALR(1) parsing Build G’, an ’expanded’ version of G: For each (p, A) and for each A → w1w2…wn, we have For each such situation, G’ has a production of the form: (p, A) → (p, w1)(p2, w2)…(pn, wn) • G’ structurally similar to G. • Uses vocabulary of LR(0) transitions, rather than symbols in G. • Follow sets in G’ are the key ! A p w1 w2 wn … p2 A → w1…wn
S 1 2 6 Simple LALR(1) parsing A b a Grammar: S’ → S S → AbAa → Ba A → a B → a Not LR(0). Not SLR(1). 3 7 10 A → a A a 9 11 A → AbAa B a 4 8 S → Ba G’: (1, S) → (1, A)(3, b)(7, A)(9, a) → (1, B)(4, a) (1, A) → (1, a) (7, A) → (7, a) (1, B) → (1, a) a A → a B → a 5 For the conflict in state 5, we need Follow(1, A) = {(3, b)} Follow(1, B) = {(4, a)}. Extract the symbols. {b} {a} Disjoint. Grammar is LALR(1)! These have split ! LALR(1) table.
Grammar: S’ → S B → A S → bBbA → c → aBa → acb Simple LALR(1) parsing LALR(1) Analysis: Need Follow(4, A). G’: (1,S) → (1, b)(3, B)(6, b) → (1, a)(4, B)(9, a) → (1, a)(4, c)(10, b) (3, B) → (3, A) (3, A) → (3, c) (4, B) → (4, A) (4, A) → (4, c) Follow(4, A)⊇Follow(4, B) = {(9, a)}. The lookahead set is {a}. S 1 2 5 8 A → c c b B b 3 6 11 S → bBb A 7 B → A A {a} a B a 4 9 12 S → aBa State 10: shift-reduce conflict. Grammar is not LR(0). SLR(1) Analysis, state 10: Follow(A) ⊇ Follow(B) ={a,b}. Grammar is not SLR(1). c b 10 13 S → acb A → c Since b ∉ {a}, the grammar is LALR(1).
Summaryof parsing Top-Down Parsing • Hand-written or Table Driven:LL(1) S S part of tree known unknown (left to predict) unknown part of tree known stack stack w w part of tree left to predict known Bottom-up Parsing • Usually Table-Driven: LR(0), SLR(1), LALR(1). β β α α remaining input remaining input input already parsed input already parsed
Non SLR(1) Grammars Calculating LALR(1) lookahead sets LALR(1) examples. Summary of Parsing (top-down vs. bottom-up) summary