You are on page 1of 2

Handout # 11: LR(0) and SLR(1)

March 2, 2010

1 Key Points
• Item Automata

• LR(0) & SLR(1) grammar

• LR(0) & SLR(1) parsing table

• Shift-reduce Parsing

2 Typical Questions and Common Strategies


If given a grammar, you need to know:

1. HOWTO: build the LR(0) item automata

(a) Add a new start symbol s0 which produces the original start symbol s
(b) Computer the closure of s0 → ·s as the initial state
(c) Make transitions from the initial state until reach the complete item

Exercise 1. Consider the following grammar:

E → (L)|a
L → L, E|E

Construct the LR(0) item automata for this grammar.

2. HOWTO: determine if a grammar is LR(0) or SLR(1) grammar

• A grammar is LR(0) grammar iff for any state in the item automata that contains a
complete item x → A·, then it can contain no other items
• A grammar is SLR(1) grammar iff in the item automata,
– any state contains item x → α · Xβ should not contain item y → γ· where X ∈
follow(y), and,
– any state contains complete items A → α and B → β, follow(a) ∩ follow(b) = ∅.

Exercise 2. According to the item automata constructed in Exercise 1., determine if the gram-
mar is LR(0) and SLR(1) grammar.

1
3. HOWTO: draw the LR(0) or SLR(1) parsing table

• When draw the SLR(1) parsing table, the number after the reduce action is the rule
number, specifying which rule to use to do reducing, and,
• the number after the shift action is the state number, specifying which state is transfered
to.

Exercise 3. Draw the LR(0) and SLR(1) parsing table for the grammar in Exercise 1..

4. HOWTO: use of parsing table

Exercise 4. Show the SLR(1) parsing of ((a), a, (a, a)) according to the SLR(1) parsing table
created in Exercise 3.