COP4620-31-LALR(1)_Parsing

LALR(1) PARSING
COP4620 – Programming Language Translators
Dr. Manuel E. Bermudez
TOPICS

Non SLR(1) Grammars

Calculating LALR(1) lookahead sets

LALR(1) examples.

Summary of Parsing
(top-down vs. bottom-up)
LALR(1) PARSING
Grammar:
S’ → S 
S → AbAa
→ Ba
A→a
B→a
1
S
A
2
3

6
b
7
LR(0)
A
Automaton:
B
a
SLR(1) Analysis: (State 5)
Follow(A) = {a, b}
Follow(B) = {a}
a
4
5
a
10 A → a
9
11 A → AbAa
8 S → Ba
A→a
B→a
Conflict not
resolved.
Grammar not SLR(1).
Grammar is not LR(0):
reduce-reduce conflict.
Need Follow(A)
’in the context of’
State 1, not state 7.
State
a
b

State
a
b
5
R/A→a
R/B→a
R/A→a
R/B→a
R/A→a
R/B→a
5
R/A→a
R/B→a
R/A→a
LR(0) table.
a
SLR(1) table.

LR PARSING HISTORY
1. LR parsing: D. Knuth seminal paper, 1965.
2. SLR(1): F. DeRemer, Ph.D. thesis, MIT, 1969.
3. LALR(1): various algorithms (from LR(1) DFA), 1970’s.
4. 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
p1
.
.
.
pn
A
ω
ω
q A→ω
A
II. Need the union of Follow(pi, A) for all i.
SIMPLE LALR(1) PARSING
Build G’, an ’expanded’ version of G:
For each (p, A) and for each A → w1w2…wn, we have
p
A
w1
p2
w2
… wn
A → w1…wn
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 !
SIMPLE LALR(1) PARSING
1
S
A
2
3

b
6
a
7
A
B
a
4
5
a
10 A → a
a
9
11 A → AbAa
Disjoint.
Grammar is
LALR(1)!
G’: (1, S) → (1, A)(3, b)(7, A)(9, a)
→ (1, B)(4, a)
(7, A) → (7, a)
(1, B) → (1, a)
Not LR(0).
Not SLR(1).
8 S → Ba
A → a {b}
B → a {a}
(1, A) → (1, a)
Grammar:
S’ → S 
S → AbAa
→ Ba
A→a
B→a
These
have
split !
For the conflict in state 5, we need
Follow(1, A) = {(3, b)}
Follow(1, B) = {(4, a)}.
Extract the symbols.
State
a
b
5
R/B→a
R/A→a
LALR(1) table.

SIMPLE LALR(1) PARSING
1
S
2

5
c
b
a
3
4
B
A
A
B
c
8
A→c
6
b
7
B→A
9
a
12
S → aBa
10
b
13
S → acb
A → c {a}
11
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).
S → bBb
Grammar:
S’ → S 
S → bBb
→ aBa
→ acb
B→A
A→c
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}.
Since b ∉ {a}, the grammar is LALR(1).
SUMMARY
OF PARSING
S
part of tree known
input already parsed remaining input
S
Bottom-up Parsing
•
Usually Table-Driven:
LR(0), SLR(1),
LALR(1).
part of tree left to
predict
β
α
Hand-written or
Table Driven:LL(1)
stack
w
Top-Down Parsing
•
part of tree known
unknown
stack
unknown
(left to predict)
w
known
α
input already parsed
β
remaining input
SUMMARY

Non SLR(1) Grammars

Calculating LALR(1) lookahead sets

LALR(1) examples.

Summary of Parsing
(top-down vs. bottom-up)