Download presentation
Presentation is loading. Please wait.
Published by달환 채 Modified over 6 years ago
1
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
LALR(1) parsing COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
2
topics Non SLR(1) Grammars Calculating LALR(1) lookahead sets
LALR(1) examples. Summary of Parsing (top-down vs. bottom-up)
3
LALR(1) parsing LR(0) Automaton: SLR(1) Analysis: (State 5)
2 S 6 3 7 A 10 b a A → a 4 8 B A → AbAa 9 11 5 S → Ba B → a Grammar is not LR(0): reduce-reduce conflict. 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} Conflict not resolved. Grammar not SLR(1). Need Follow(A) ’in the context of’ State 1, not state 7. State a b 5 R/A→a R/B→a State a b 5 R/A→a R/B→a LR(0) table. SLR(1) table.
4
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.
5
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. p1 A q pn A → ω ω .
6
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 ! p A p2 A → w1…wn … w2 wn w1
7
Simple LALR(1) parsing Grammar: Not LR(0). Not SLR(1). S’ → S
S → AbAa → Ba A → a B → a Not LR(0). Not SLR(1). 1 2 S 6 3 7 A 10 b a A → a 4 8 B A → AbAa 9 11 5 S → Ba B → a 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)! 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) These have split ! State a b 5 R/B→a R/A→a LALR(1) table.
8
Simple LALR(1) parsing 1 2 5 8 11 7 4 6 3 12 10 9 13 Grammar:
S’ → S B → A S → bBb A → c → aBa → acb Simple LALR(1) parsing 1 2 S 5 8 11 b A → c 7 4 6 3 a S → bBb S → aBa 12 10 9 A c B B → A 13 S → acb 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}. {a} 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). Since b ∉ {a}, the grammar is LALR(1).
9
Summary of parsing S part of tree known stack w
β part of tree known stack part of tree left to predict remaining input input already parsed α Top-Down Parsing Hand-written or Table Driven:LL(1) S w β unknown (left to predict) stack known remaining input input already parsed α Bottom-up Parsing Usually Table-Driven: LR(0), SLR(1), LALR(1).
10
summary Non SLR(1) Grammars Calculating LALR(1) lookahead sets
LALR(1) examples. Summary of Parsing (top-down vs. bottom-up)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.