105
Compiler Construction Lecture 10: Syntax Analysis VI (LR (0) and SLR (1) Parsing) Thomas Noll Lehrstuhl f¨ ur Informatik 2 (Software Modeling and Verification) [email protected] http://moves.rwth-aachen.de/teaching/ss-14/cc14/ Summer Semester 2014

Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Compiler ConstructionLecture 10: Syntax Analysis VI (LR(0) and SLR(1) Parsing)

Thomas Noll

Lehrstuhl fur Informatik 2(Software Modeling and Verification)

[email protected]

http://moves.rwth-aachen.de/teaching/ss-14/cc14/

Summer Semester 2014

Page 2: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Outline

1 Recap: LR(0) Grammars

2 The LR(0) Parsing Automaton

3 SLR(1) Parsing

4 LR(1) Parsing

Compiler Construction Summer Semester 2014 10.2

Page 3: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(0) Grammars

The case k = 0 is relevant (in contrast to LL(0)): here the decision is justbased on the contents of the pushdown, without any lookahead.

Corollary (LR(0) grammar)

G ∈ CFGΣ has the LR(0) property if for all rightmost derivations of theform

S

{

⇒∗r αAw ⇒r αβw

⇒∗r γBx ⇒r αβy

it follows that α = γ, A = B, and x = y .

Goal: derive a finite information from the pushdown which suffices toresolve the nondeterminism (similar to abstraction of right context in LLparsing by fo-sets)

Compiler Construction Summer Semester 2014 10.3

Page 4: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(0) Items and Sets

Definition (LR(0) items and sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S andS ′ ⇒∗

r αAw ⇒r αβ1β2w (i.e., A→ β1β2 ∈ P).

[A→ β1 · β2] is called an LR(0) item for αβ1.Given γ ∈ X ∗, LR(0)(γ) denotes the set of all LR(0) items for γ,called the LR(0) set (or: LR(0) information) of γ.LR(0)(G ) := {LR(0)(γ) | γ ∈ X ∗}.

Corollary

1 For every γ ∈ X ∗, LR(0)(γ) is finite.2 LR(0)(G ) is finite.3 The item [A→ β·] ∈ LR(0)(γ) indicates the possible reduction

(w , αβ, z) ⊢ (w , αA, zi) where πi = A→ β and γ = αβ.4 The item [A→ β1 · Y β2] ∈ LR(0)(γ) indicates an incomplete handle

β1 (to be completed by shift operations or ε-steps).

Compiler Construction Summer Semester 2014 10.4

Page 5: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(0) Conflicts

Definition (LR(0) conflicts)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ and I ∈ LR(0)(G ).

I has a shift/reduce conflict if there exist A→ α1aα2,B → β ∈ Psuch that

[A→ α1 · aα2], [B → β·] ∈ I .

I has a reduce/reduce conflict if there exist A→ α,B → β ∈ P withA 6= B or α 6= β such that

[A→ α·], [B → β·] ∈ I .

Lemma

G ∈ LR(0) iff no I ∈ LR(0)(G ) contains conflicting items.

Proof.

omitted

Compiler Construction Summer Semester 2014 10.5

Page 6: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Action Function

The parsing automaton will be defined using another table, the actionfunction, which determines the shift/reduce decision.(Reminder: π0 = S ′ → S)

Definition (LR(0) action function)

The LR(0) action function

act : LR(0)(G )→ {red i | i ∈ [p]} ∪ {shift, accept, error}

is defined by

act(I ) :=

red i if i 6= 0, πi = A→ α and [A→ α·] ∈ Ishift if [A→ α1 · aα2] ∈ Iaccept if [S ′ → S ·] ∈ Ierror if I = ∅

Corollary

For every G ∈ CFGΣ, G ∈ LR(0) iff act is well defined.

Together, act and goto form the LR(0) parsing table of G .Compiler Construction Summer Semester 2014 10.6

Page 7: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Outline

1 Recap: LR(0) Grammars

2 The LR(0) Parsing Automaton

3 SLR(1) Parsing

4 LR(1) Parsing

Compiler Construction Summer Semester 2014 10.7

Page 8: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Table

Example 10.1 (cf. Example 9.15)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

I0 := LR(0)(ε) : [S ′ → ·S ][S → ·B] [S → ·C ][B → ·aB] [B → ·b][C → ·aC ] [C → ·c]

I1 := LR(0)(S) : [S ′ → S ·]I2 := LR(0)(B) : [S → B·]I3 := LR(0)(C) : [S → C ·]I4 := LR(0)(a) : [B → a · B] [C → a · C ]

[B → ·aB] [B → ·b][C → ·aC ] [C → ·c]

I5 := LR(0)(b) : [B → b·]I6 := LR(0)(c) : [C → c·]I7 := LR(0)(aB) : [B → aB·]I8 := LR(0)(aC) : [C → aC ·]I9 := ∅

Compiler Construction Summer Semester 2014 10.8

Page 9: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Table

Example 10.1 (cf. Example 9.15)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

I0 := LR(0)(ε) : [S ′ → ·S ][S → ·B] [S → ·C ][B → ·aB] [B → ·b][C → ·aC ] [C → ·c]

I1 := LR(0)(S) : [S ′ → S ·]I2 := LR(0)(B) : [S → B·]I3 := LR(0)(C) : [S → C ·]I4 := LR(0)(a) : [B → a · B] [C → a · C ]

[B → ·aB] [B → ·b][C → ·aC ] [C → ·c]

I5 := LR(0)(b) : [B → b·]I6 := LR(0)(c) : [C → c·]I7 := LR(0)(aB) : [B → aB·]I8 := LR(0)(aC) : [C → aC ·]I9 := ∅

Compiler Construction Summer Semester 2014 10.8

Page 10: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton I

Definition 10.2 (LR(0) parsing automaton)

Let G = 〈N ,Σ,P , S〉 ∈ LR(0). The (deterministic) LR(0) parsing automaton ofG is defined by the following components.

Input alphabet Σ

Pushdown alphabet Γ := LR(0)(G)

Output alphabet ∆ := [p] ∪ {0, error}

Configurations Σ∗ × Γ∗ ×∆∗

Initial configuration (w , I0, ε) where I0 := LR(0)(ε)

Final configurations {ε} × {ε} ×∆∗

Transitions:

shift: (aw , αI , z) ⊢ (w , αII ′, z) if act(I ) = shift and goto(I , a) = I ′

reduce: (w , αII1 . . . In, z) ⊢ (w , αII ′, zi) if act(In) = red i , πi = A→ Y1 . . .Yn,and goto(I ,A) = I ′

accept: (ε, I0I , z) ⊢ (ε, ε, z 0) if act(I ) = accepterror: (w , αI , z) ⊢ (ε, ε, z error) if act(I ) = error

Compiler Construction Summer Semester 2014 10.9

Page 11: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)Compiler Construction Summer Semester 2014 10.10

Page 12: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )

Compiler Construction Summer Semester 2014 10.10

Page 13: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )

Compiler Construction Summer Semester 2014 10.10

Page 14: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )

Compiler Construction Summer Semester 2014 10.10

Page 15: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )

Compiler Construction Summer Semester 2014 10.10

Page 16: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )

Compiler Construction Summer Semester 2014 10.10

Page 17: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )(∗)

⊢ ( ε, I0I4I8 , 65 )

Compiler Construction Summer Semester 2014 10.10

Page 18: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )(∗)

⊢ ( ε, I0I4I8 , 65 )⊢ ( ε, I0I3 , 655 )

Compiler Construction Summer Semester 2014 10.10

Page 19: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )(∗)

⊢ ( ε, I0I4I8 , 65 )⊢ ( ε, I0I3 , 655 )⊢ ( ε, I0I1 , 6552 )

Compiler Construction Summer Semester 2014 10.10

Page 20: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )(∗)

⊢ ( ε, I0I4I8 , 65 )⊢ ( ε, I0I3 , 655 )⊢ ( ε, I0I1 , 6552 )⊢ ( ε, ε , 65520)

Compiler Construction Summer Semester 2014 10.10

Page 21: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )(∗)

⊢ ( ε, I0I4I8 , 65 )⊢ ( ε, I0I3 , 655 )⊢ ( ε, I0I1 , 6552 )⊢ ( ε, ε , 65520)

Check by rightmost derivation(on the board)

Compiler Construction Summer Semester 2014 10.10

Page 22: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton II

Example 10.3 (cf. Example 10.1)

G : S ′ → S (0)S → B | C (1, 2)B → aB | b (3, 4)C → aC | c (5, 6)

LR(0)(G ) act gotoS B C a b c

I0 shift I1 I2 I3 I4 I5 I6I1 acceptI2 red 1I3 red 2I4 shift I7 I8 I4 I5 I6I5 red 4I6 red 6I7 red 3I8 red 5I9 error

(empty = I9)

LR(0) parsing of aac :

(aac , I0 , ε )⊢ ( ac , I0I4 , ε )⊢ ( c , I0I4I4 , ε )⊢ ( ε, I0I4I4I6, ε )⊢ ( ε, I0I4I4I8, 6 )(∗)

⊢ ( ε, I0I4I8 , 65 )⊢ ( ε, I0I3 , 655 )⊢ ( ε, I0I1 , 6552 )⊢ ( ε, ε , 65520)

Check by rightmost derivation(on the board)

Remark: in the correspondingcomputation of NBA(G), (∗) isnondeterministic

Compiler Construction Summer Semester 2014 10.10

Page 23: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(0) Parsing Automaton III

Theorem 10.4 (Correctness of LR(0) Parsing Automaton)

If G ∈ LR(0), then the LR(0) parsing automaton of G is deterministic,and for every w ∈ Σ∗ and z ∈ {0, . . . , p}∗:

(w , I0, ε) ⊢∗ (ε, ε, z) iff ←−z is a rightmost analysis of w

Proof.

omitted

Compiler Construction Summer Semester 2014 10.11

Page 24: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Outline

1 Recap: LR(0) Grammars

2 The LR(0) Parsing Automaton

3 SLR(1) Parsing

4 LR(1) Parsing

Compiler Construction Summer Semester 2014 10.12

Page 25: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Removing Conflicts in LR(0) Parsing

In practice: often G /∈ LR(0)

Example 10.5

GAE : E ′ → E E → E+T | TT → T*F | F F → (E) | a | b

Compiler Construction Summer Semester 2014 10.13

Page 26: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Removing Conflicts in LR(0) Parsing

In practice: often G /∈ LR(0)

Example 10.5

GAE : E ′ → E E → E+T | TT → T*F | F F → (E) | a | b

LR(0)(GAE ) with conflicts:

I0 : [E ′ → ·E ] [E → ·E+T ] I1 : [E ′ → E ·] [E → E · +T ][E → ·T ] [T → ·T*F ] I2 : [E → T ·] [T → T · *F ][T → ·F ] [F → ·(E)] I3 : [T → F ·][F → ·a] [F → ·b]

I4 : [F → ( · E)] [E → ·E+T ] I5 : [F → a·][E → ·T ] [T → ·T*F ] I6 : [F → b·][T → ·F ] [F → ·(E)] I7 : [E → E+ · T ] [T → ·T*F ][F → ·a] [F → ·b] [T → ·F ] [F → ·(E)]

[F → ·a] [F → ·b]I8 : [T → T* · F ] [F → ·(E)] I9 : [F → (E · )] [E → E · +T ]

[F → ·a] [F → ·b] I10 : [E → E+T ·] [T → T · *F ]I11 : [T → T*F ·] I12 : [F → (E)·]

Compiler Construction Summer Semester 2014 10.13

Page 27: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead I

Goal: resolving conflicts by considering next input symbol

Compiler Construction Summer Semester 2014 10.14

Page 28: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead I

Goal: resolving conflicts by considering next input symbol

Observations:

[A→ β1 · aβ2] ∈ LR(0)(αβ1)=⇒ S ′ ⇒∗

r αAw ⇒r αβ1ր

pushdown

aβ2wտnext input symbol

Thus: shift only on lookahead a

Compiler Construction Summer Semester 2014 10.14

Page 29: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead I

Goal: resolving conflicts by considering next input symbol

Observations:

[A→ β1 · aβ2] ∈ LR(0)(αβ1)=⇒ S ′ ⇒∗

r αAw ⇒r αβ1ր

pushdown

aβ2wտnext input symbol

Thus: shift only on lookahead a

[A→ β·] ∈ LR(0)(αβ)=⇒ S ′ ⇒∗

r αAxw ⇒r αβր

pushdown

xwտinput

=⇒ x ∈ fo(A) ⊆ Σε (x = ε only if w = ε)

Thus: reduce with A→ β only if lookahead x ∈ fo(A)

Compiler Construction Summer Semester 2014 10.14

Page 30: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead II

Example 10.6 (cf. Example 10.5)

GAE : E ′ → E (0)E → E+T | T (1, 2)T → T*F | F (3, 4)F → (E) | a | b (5, 6, 7)

A ∈ N fo(A)E ′ {ε}E {+, ), ε}

Compiler Construction Summer Semester 2014 10.15

Page 31: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead II

Example 10.6 (cf. Example 10.5)

GAE : E ′ → E (0)E → E+T | T (1, 2)T → T*F | F (3, 4)F → (E) | a | b (5, 6, 7)

A ∈ N fo(A)E ′ {ε}E {+, ), ε}

I1 = {[E′ → E ·], [E → E · +T ]}:

accept on lookahead εshift on lookahead +

Compiler Construction Summer Semester 2014 10.15

Page 32: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead II

Example 10.6 (cf. Example 10.5)

GAE : E ′ → E (0)E → E+T | T (1, 2)T → T*F | F (3, 4)F → (E) | a | b (5, 6, 7)

A ∈ N fo(A)E ′ {ε}E {+, ), ε}

I1 = {[E′ → E ·], [E → E · +T ]}:

accept on lookahead εshift on lookahead +

I2 = {[E → T ·], [T → T · *F ]}:red 2 on lookahead +/)/εshift on lookahead *

Compiler Construction Summer Semester 2014 10.15

Page 33: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead II

Example 10.6 (cf. Example 10.5)

GAE : E ′ → E (0)E → E+T | T (1, 2)T → T*F | F (3, 4)F → (E) | a | b (5, 6, 7)

A ∈ N fo(A)E ′ {ε}E {+, ), ε}

I1 = {[E′ → E ·], [E → E · +T ]}:

accept on lookahead εshift on lookahead +

I2 = {[E → T ·], [T → T · *F ]}:red 2 on lookahead +/)/εshift on lookahead *

I10 = {[E → E+T ·], [T → T · *F ]}:red 1 on lookahead +/)/εshift on lookahead *

Compiler Construction Summer Semester 2014 10.15

Page 34: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Adding Lookahead II

Example 10.6 (cf. Example 10.5)

GAE : E ′ → E (0)E → E+T | T (1, 2)T → T*F | F (3, 4)F → (E) | a | b (5, 6, 7)

A ∈ N fo(A)E ′ {ε}E {+, ), ε}

I1 = {[E′ → E ·], [E → E · +T ]}:

accept on lookahead εshift on lookahead +

I2 = {[E → T ·], [T → T · *F ]}:red 2 on lookahead +/)/εshift on lookahead *

I10 = {[E → E+T ·], [T → T · *F ]}:red 1 on lookahead +/)/εshift on lookahead *

=⇒ SLR(1) parsing (Simple LR(1))

Compiler Construction Summer Semester 2014 10.15

Page 35: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The SLR(1) Action Function

Definition 10.7 (SLR(1) action function)

The SLR(1) action function

act : LR(0)(G ) ×Σε → {red i | i ∈ [p]} ∪ {shift, accept, error}

is defined by

act(I , x) :=

red i if i 6= 0, πi = A→ α, [A→ α·] ∈ I ,and x ∈ fo(A)

shift if [A→ α1 · xα2] ∈ I and x ∈ Σaccept if [S ′ → S ·] ∈ I and x = εerror otherwise

Compiler Construction Summer Semester 2014 10.16

Page 36: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The SLR(1) Action Function

Definition 10.7 (SLR(1) action function)

The SLR(1) action function

act : LR(0)(G ) ×Σε → {red i | i ∈ [p]} ∪ {shift, accept, error}

is defined by

act(I , x) :=

red i if i 6= 0, πi = A→ α, [A→ α·] ∈ I ,and x ∈ fo(A)

shift if [A→ α1 · xα2] ∈ I and x ∈ Σaccept if [S ′ → S ·] ∈ I and x = εerror otherwise

Definition 10.8 (SLR(1) grammar)

A grammar G ∈ CFGΣ has the SLR(1) property (notation: G ∈ SLR(1))if its SLR(1) action function is well defined.

Compiler Construction Summer Semester 2014 10.16

Page 37: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The SLR(1) Action Function

Definition 10.7 (SLR(1) action function)

The SLR(1) action function

act : LR(0)(G ) ×Σε → {red i | i ∈ [p]} ∪ {shift, accept, error}

is defined by

act(I , x) :=

red i if i 6= 0, πi = A→ α, [A→ α·] ∈ I ,and x ∈ fo(A)

shift if [A→ α1 · xα2] ∈ I and x ∈ Σaccept if [S ′ → S ·] ∈ I and x = εerror otherwise

Definition 10.8 (SLR(1) grammar)

A grammar G ∈ CFGΣ has the SLR(1) property (notation: G ∈ SLR(1))if its SLR(1) action function is well defined.

Together, act and the LR(0) goto function (cf. Definition 9.14) form theSLR(1) parsing table of G .

Compiler Construction Summer Semester 2014 10.16

Page 38: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The SLR(1) Parsing Table

Example 10.9 (cf. Example 10.5)

I0 : [E ′ → ·E ] [E → ·E+T ] I1 : [E ′ → E ·] [E → E · +T ][E → ·T ] [T → ·T*F ] I2 : [E → T ·] [T → T · *F ][T → ·F ] [F → ·(E)] I3 : [T → F ·][F → ·a] [F → ·b]

I4 : [F → ( · E)] [E → ·E+T ] I5 : [F → a·][E → ·T ] [T → ·T*F ] I6 : [F → b·][T → ·F ] [F → ·(E)] I7 : [E → E+ · T ] [T → ·T*F ][F → ·a] [F → ·b] [T → ·F ] [F → ·(E)]

[F → ·a] [F → ·b]I8 : [T → T* · F ] [F → ·(E)] I9 : [F → (E · )] [E → E · +T ]

[F → ·a] [F → ·b] I10 : [E → E+T ·] [T → T · *F ]I11 : [T → T*F ·] I12 : [F → (E)·]

A ∈ N fo(A)E ′ {ε}E {+, ), ε}T {+, *, ), ε}F {+, *, ), ε}

Compiler Construction Summer Semester 2014 10.17

Page 39: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The SLR(1) Parsing Table

Example 10.9 (cf. Example 10.5)

I0 : [E ′ → ·E ] [E → ·E+T ] I1 : [E ′ → E ·] [E → E · +T ][E → ·T ] [T → ·T*F ] I2 : [E → T ·] [T → T · *F ][T → ·F ] [F → ·(E)] I3 : [T → F ·][F → ·a] [F → ·b]

I4 : [F → ( · E)] [E → ·E+T ] I5 : [F → a·][E → ·T ] [T → ·T*F ] I6 : [F → b·][T → ·F ] [F → ·(E)] I7 : [E → E+ · T ] [T → ·T*F ][F → ·a] [F → ·b] [T → ·F ] [F → ·(E)]

[F → ·a] [F → ·b]I8 : [T → T* · F ] [F → ·(E)] I9 : [F → (E · )] [E → E · +T ]

[F → ·a] [F → ·b] I10 : [E → E+T ·] [T → T · *F ]I11 : [T → T*F ·] I12 : [F → (E)·]

A ∈ N fo(A)E ′ {ε}E {+, ), ε}T {+, *, ), ε}F {+, *, ), ε}

LR(0)(GAE ) act goto+ * ( ) a b ε E T F + * ( ) a b

I0 shift shift shift I1 I2 I3 I4 I5 I6I1 shift accept I7I2 red 2 shift red 2 red 2 I8I3 red 4 red 4 red 4 red 4I4 shift shift shift I9 I2 I3 I4 I5 I6I5 red 6 red 6 red 6 red 6I6 red 7 red 7 red 7 red 7I7 shift shift shift I10 I3 I4 I5 I6I8 shift shift shift I11 I4 I5 I6I9 shift shift I7 I12I10 red 1 shift red 1 red 1 I8I11 red 3 red 3 red 3 red 3I12 red 5 red 5 red 5 red 5

Compiler Construction Summer Semester 2014 10.17

Page 40: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The SLR(1) Parsing Automaton

Definition 10.10 (SLR(1) parsing automaton)

The SLR(1) parsing automaton is defined as in the LR(0) case (seeDefinition 10.2), except for the transition relation:

shift: (aw , αI , z) ⊢ (w , αII ′, z) if act(I , a) = shift andgoto(I , a) = I ′

reducea: (aw , αII1 . . . In, z) ⊢ (aw , αII ′, zi) if act(In, a) = red i ,πi = A→ Y1 . . .Yn, and goto(I ,A) = I ′

reduceε: (ε, αII1 . . . In, z) ⊢ (ε, αII ′, zi) if act(In, ε) = red i ,πi = A→ Y1 . . .Yn, and goto(I ,A) = I ′

accept: (ε, I0I , z) ⊢ (ε, ε, z 0) if act(I , ε) = accept

errora: (aw , αI , z) ⊢ (ε, ε, z error) if act(I , a) = error

errorε: (ε, αI , z) ⊢ (ε, ε, z error) if act(I , ε) = error

Compiler Construction Summer Semester 2014 10.18

Page 41: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Outline

1 Recap: LR(0) Grammars

2 The LR(0) Parsing Automaton

3 SLR(1) Parsing

4 LR(1) Parsing

Compiler Construction Summer Semester 2014 10.19

Page 42: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

SLR(1) Conflicts

Problem: not all conflicts can be resolved using fo sets

Example 10.11

GLR : S ′ → S S → L=R | R L→ *R | a R → L

Compiler Construction Summer Semester 2014 10.20

Page 43: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

SLR(1) Conflicts

Problem: not all conflicts can be resolved using fo sets

Example 10.11

GLR : S ′ → S S → L=R | R L→ *R | a R → L

LR(0)(GLR ):

I0 := LR(0)(ε) : [S ′ → ·S ] [S → ·L=R ] [S → ·R ][L→ ·*R ] [L→ ·a] [R → ·L]

I1 := LR(0)(S) : [S ′ → S ·]I2 := LR(0)(L) : [S → L · =R ] [R → L·]I3 := LR(0)(R) : [S → R ·]I4 := LR(0)(*) : [L→ * · R ] [R → ·L] [L→ ·*R ] [L→ ·a]I5 := LR(0)(a) : [L→ a·]I6 := LR(0)(L=) : [S → L= · R ] [R → ·L] [L→ ·*R ] [L→ ·a]I7 := LR(0)(*R) : [L→ *R ·]I8 := LR(0)(*L) : [R → L·]I9 := LR(0)(L=R) : [S → L=R ·]

Compiler Construction Summer Semester 2014 10.20

Page 44: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

SLR(1) Conflicts

Problem: not all conflicts can be resolved using fo sets

Example 10.11

GLR : S ′ → S S → L=R | R L→ *R | a R → L

LR(0)(GLR ):

I0 := LR(0)(ε) : [S ′ → ·S ] [S → ·L=R ] [S → ·R ][L→ ·*R ] [L→ ·a] [R → ·L]

I1 := LR(0)(S) : [S ′ → S ·]I2 := LR(0)(L) : [S → L · =R ] [R → L·]I3 := LR(0)(R) : [S → R ·]I4 := LR(0)(*) : [L→ * · R ] [R → ·L] [L→ ·*R ] [L→ ·a]I5 := LR(0)(a) : [L→ a·]I6 := LR(0)(L=) : [S → L= · R ] [R → ·L] [L→ ·*R ] [L→ ·a]I7 := LR(0)(*R) : [L→ *R ·]I8 := LR(0)(*L) : [R → L·]I9 := LR(0)(L=R) : [S → L=R ·]

But: conflict in I2 not SLR(1)-solvable since = ∈ fo(R)

Compiler Construction Summer Semester 2014 10.20

Page 45: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets I

Observation: not every element of fo(A) can follow every occurrence of A=⇒ refinement of LR(0) items by adding possible lookahead symbols

Compiler Construction Summer Semester 2014 10.21

Page 46: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets I

Observation: not every element of fo(A) can follow every occurrence of A=⇒ refinement of LR(0) items by adding possible lookahead symbols

Definition 10.12 (LR(1) items and sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S .

If S ′ ⇒∗r αAaw ⇒r αβ1β2aw , then [A→ β1 · β2, a] is called an LR(1)

item for αβ1.

Compiler Construction Summer Semester 2014 10.21

Page 47: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets I

Observation: not every element of fo(A) can follow every occurrence of A=⇒ refinement of LR(0) items by adding possible lookahead symbols

Definition 10.12 (LR(1) items and sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S .

If S ′ ⇒∗r αAaw ⇒r αβ1β2aw , then [A→ β1 · β2, a] is called an LR(1)

item for αβ1.

If S ′ ⇒∗r αA⇒r αβ1β2, then [A→ β1 · β2, ε] is called an LR(1) item

for αβ1.

Compiler Construction Summer Semester 2014 10.21

Page 48: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets I

Observation: not every element of fo(A) can follow every occurrence of A=⇒ refinement of LR(0) items by adding possible lookahead symbols

Definition 10.12 (LR(1) items and sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S .

If S ′ ⇒∗r αAaw ⇒r αβ1β2aw , then [A→ β1 · β2, a] is called an LR(1)

item for αβ1.

If S ′ ⇒∗r αA⇒r αβ1β2, then [A→ β1 · β2, ε] is called an LR(1) item

for αβ1.

Given γ ∈ X ∗, LR(1)(γ) denotes the set of all LR(1) items for γ,called the LR(1) set (or: LR(1) information) of γ.

Compiler Construction Summer Semester 2014 10.21

Page 49: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets I

Observation: not every element of fo(A) can follow every occurrence of A=⇒ refinement of LR(0) items by adding possible lookahead symbols

Definition 10.12 (LR(1) items and sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S .

If S ′ ⇒∗r αAaw ⇒r αβ1β2aw , then [A→ β1 · β2, a] is called an LR(1)

item for αβ1.

If S ′ ⇒∗r αA⇒r αβ1β2, then [A→ β1 · β2, ε] is called an LR(1) item

for αβ1.

Given γ ∈ X ∗, LR(1)(γ) denotes the set of all LR(1) items for γ,called the LR(1) set (or: LR(1) information) of γ.

LR(1)(G ) := {LR(1)(γ) | γ ∈ X ∗}.

Compiler Construction Summer Semester 2014 10.21

Page 50: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets II

Corollary 10.13

1 For every γ ∈ X ∗, LR(1)(γ) is finite.

Compiler Construction Summer Semester 2014 10.22

Page 51: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets II

Corollary 10.13

1 For every γ ∈ X ∗, LR(1)(γ) is finite.2 LR(1)(G ) is finite.

Compiler Construction Summer Semester 2014 10.22

Page 52: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets II

Corollary 10.13

1 For every γ ∈ X ∗, LR(1)(γ) is finite.2 LR(1)(G ) is finite.3 For every γ ∈ X ∗, LR(1)(γ) “contains” LR(0)(γ), i.e.,

{[A→ β1 · β2] | [A→ β1 · β2, x ] ∈ LR(1)(γ)} = LR(0)(γ).

Compiler Construction Summer Semester 2014 10.22

Page 53: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Items and Sets II

Corollary 10.13

1 For every γ ∈ X ∗, LR(1)(γ) is finite.2 LR(1)(G ) is finite.3 For every γ ∈ X ∗, LR(1)(γ) “contains” LR(0)(γ), i.e.,

{[A→ β1 · β2] | [A→ β1 · β2, x ] ∈ LR(1)(γ)} = LR(0)(γ).

4 [A→ β1 · β2, x ] ∈ I ∈ LR(1)(G ) =⇒ x ∈ fo(A)

Compiler Construction Summer Semester 2014 10.22

Page 54: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Conflicts

Definition 10.14 (LR(1) conflicts)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ and I ∈ LR(1)(G ).

I has a shift/reduce conflict if there exist A→ α1aα2,B → β ∈ Pand x ∈ Σε such that

[A→ α1 · aα2, x ], [B → β·, a] ∈ I .

Compiler Construction Summer Semester 2014 10.23

Page 55: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Conflicts

Definition 10.14 (LR(1) conflicts)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ and I ∈ LR(1)(G ).

I has a shift/reduce conflict if there exist A→ α1aα2,B → β ∈ Pand x ∈ Σε such that

[A→ α1 · aα2, x ], [B → β·, a] ∈ I .

I has a reduce/reduce conflict if there exist x ∈ Σε andA→ α,B → β ∈ P with A 6= B or α 6= β such that

[A→ α·, x ], [B → β·, x ] ∈ I .

Compiler Construction Summer Semester 2014 10.23

Page 56: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

LR(1) Conflicts

Definition 10.14 (LR(1) conflicts)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ and I ∈ LR(1)(G ).

I has a shift/reduce conflict if there exist A→ α1aα2,B → β ∈ Pand x ∈ Σε such that

[A→ α1 · aα2, x ], [B → β·, a] ∈ I .

I has a reduce/reduce conflict if there exist x ∈ Σε andA→ α,B → β ∈ P with A 6= B or α 6= β such that

[A→ α·, x ], [B → β·, x ] ∈ I .

Lemma 10.15

G ∈ LR(1) iff no I ∈ LR(1)(G ) contains conflicting items.

Compiler Construction Summer Semester 2014 10.23

Page 57: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets I

The computation of LR(0) sets (cf. Theorem 9.10) can be extended tocover right contexts:

Theorem 10.16 (Computing LR(1) sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S and reduced.1 LR(1)(ε) is the least set such that

[S ′ → ·S , ε] ∈ LR(1)(ε) andif [A→ ·Bγ, x ] ∈ LR(1)(ε), B → β ∈ P, and y ∈ fi(γx), then[B → ·β, y ] ∈ LR(1)(ε).

Compiler Construction Summer Semester 2014 10.24

Page 58: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets I

The computation of LR(0) sets (cf. Theorem 9.10) can be extended tocover right contexts:

Theorem 10.16 (Computing LR(1) sets)

Let G = 〈N,Σ,P ,S〉 ∈ CFGΣ be start separated by S ′ → S and reduced.1 LR(1)(ε) is the least set such that

[S ′ → ·S , ε] ∈ LR(1)(ε) andif [A→ ·Bγ, x ] ∈ LR(1)(ε), B → β ∈ P, and y ∈ fi(γx), then[B → ·β, y ] ∈ LR(1)(ε).

2 LR(1)(αY ) (α ∈ X ∗,Y ∈ X) is the least set such that

if [A→ γ1 · Y γ2, x ] ∈ LR(1)(α),then [A→ γ1Y · γ2, x ] ∈ LR(1)(αY ) andif [A→ γ1 · Bγ2, x ] ∈ LR(1)(αY ), B → β ∈ P, and y ∈ fi(γ2x), then[B → ·β, y ] ∈ LR(1)(αY ).

Compiler Construction Summer Semester 2014 10.24

Page 59: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

Compiler Construction Summer Semester 2014 10.25

Page 60: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [S′ → ·S , ε] ∈ LR(1)(ε)

I ′0 := LR(1)(ε) : [S ′ → ·S , ε]

Compiler Construction Summer Semester 2014 10.25

Page 61: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → ·Bγ, x] ∈ LR(1)(ε),B → β ∈ P, y ∈ fi(γx)=⇒ [B → ·β, y ] ∈ LR(1)(ε)

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε]

Compiler Construction Summer Semester 2014 10.25

Page 62: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → ·Bγ, x] ∈ LR(1)(ε),B → β ∈ P, y ∈ fi(γx)=⇒ [B → ·β, y ] ∈ LR(1)(ε)

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =]

Compiler Construction Summer Semester 2014 10.25

Page 63: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → ·Bγ, x] ∈ LR(1)(ε),B → β ∈ P, y ∈ fi(γx)=⇒ [B → ·β, y ] ∈ LR(1)(ε)

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε]

Compiler Construction Summer Semester 2014 10.25

Page 64: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → ·Bγ, x] ∈ LR(1)(ε),B → β ∈ P, y ∈ fi(γx)=⇒ [B → ·β, y ] ∈ LR(1)(ε)

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

Compiler Construction Summer Semester 2014 10.25

Page 65: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 66: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε]

Compiler Construction Summer Semester 2014 10.25

Page 67: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 68: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 69: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε]

Compiler Construction Summer Semester 2014 10.25

Page 70: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Bγ2, x] ∈ LR(1)(αY ),B → β ∈ P, y ∈ fi(γ2x)=⇒ [B → ·β, y ] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

Compiler Construction Summer Semester 2014 10.25

Page 71: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Bγ2, x] ∈ LR(1)(αY ),B → β ∈ P, y ∈ fi(γ2x)=⇒ [B → ·β, y ] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]

Compiler Construction Summer Semester 2014 10.25

Page 72: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 73: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε]

Compiler Construction Summer Semester 2014 10.25

Page 74: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Bγ2, x] ∈ LR(1)(αY ),B → β ∈ P, y ∈ fi(γ2x)=⇒ [B → ·β, y ] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε]

Compiler Construction Summer Semester 2014 10.25

Page 75: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Bγ2, x] ∈ LR(1)(αY ),B → β ∈ P, y ∈ fi(γ2x)=⇒ [B → ·β, y ] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

Compiler Construction Summer Semester 2014 10.25

Page 76: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 77: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 78: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 79: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 80: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε]

Compiler Construction Summer Semester 2014 10.25

Page 81: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Bγ2, x] ∈ LR(1)(αY ),B → β ∈ P, y ∈ fi(γ2x)=⇒ [B → ·β, y ] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε] [R → ·L, ε]

Compiler Construction Summer Semester 2014 10.25

Page 82: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Bγ2, x] ∈ LR(1)(αY ),B → β ∈ P, y ∈ fi(γ2x)=⇒ [B → ·β, y ] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

Compiler Construction Summer Semester 2014 10.25

Page 83: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′12 := LR(1)(L=a) : [L → a·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 84: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR): [A → γ1 · Y γ2, x] ∈ LR(1)(α)=⇒ [A → γ1Y · γ2, x] ∈ LR(1)(αY )

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′12 := LR(1)(L=a) : [L → a·, ε]I ′13 := LR(1)(L=*R) : [L → *R·, ε]

Compiler Construction Summer Semester 2014 10.25

Page 85: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR):

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′12 := LR(1)(L=a) : [L → a·, ε]I ′13 := LR(1)(L=*R) : [L → *R·, ε]I ′14 := ∅

Compiler Construction Summer Semester 2014 10.25

Page 86: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

Computing LR(1) Sets II

Example 10.17 (cf. Example 10.11)

GLR : S ′ → S S → L=R | R L → *R | a R → L

LR(1)(GLR):

I ′0 := LR(1)(ε) : [S ′ → ·S , ε] [S → ·L=R, ε] [S → ·R, ε] [L → ·*R, =][L → ·a, =] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]

I ′1 := LR(1)(S) : [S ′ → S ·, ε]I ′2 := LR(1)(L) : [S → L · =R, ε] [R → L·, ε]I ′3 := LR(1)(R) : [S → R·, ε]I ′4 := LR(1)(*) : [L → * · R, =] [L → * · R, ε] [R → ·L, =] [R → ·L, ε]

[L → ·*R, =] [L → ·a, =] [L → ·*R, ε] [L → ·a, ε]I ′5 := LR(1)(a) : [L → a·, =] [L → a·, ε]I ′6 := LR(1)(L=) : [S → L= · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′7 := LR(1)(*R) : [L → *R·, =] [L → *R·, ε]I ′8 := LR(1)(*L) : [R → L·, =] [R → L·, ε]I ′9 := LR(1)(L=R) : [S → L=R·, ε]I ′10 := LR(1)(L=L) : [R → L·, ε]I ′11 := LR(1)(L=*) : [L → * · R, ε] [R → ·L, ε] [L → ·*R, ε] [L → ·a, ε]I ′12 := LR(1)(L=a) : [L → a·, ε]I ′13 := LR(1)(L=*R) : [L → *R·, ε]I ′14 := ∅

In I ′2: shift on =/reduce on ε =⇒ GLR ∈ LR(1)Compiler Construction Summer Semester 2014 10.25

Page 87: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Action Function

Definition 10.18 (LR(1) action function)

The LR(1) action function

act : LR(1)(G ) ×Σε → {red i | i ∈ [p]} ∪ {shift, accept, error}

is defined by

act(I , x) :=

red i if i 6= 0, πi = A→ α and [A→ α·, x ] ∈ Ishift if [A→ α1 · xα2, y ] ∈ I and x ∈ Σaccept if [S ′ → S ·, ε] ∈ I and x = εerror otherwise

Compiler Construction Summer Semester 2014 10.26

Page 88: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Action Function

Definition 10.18 (LR(1) action function)

The LR(1) action function

act : LR(1)(G ) ×Σε → {red i | i ∈ [p]} ∪ {shift, accept, error}

is defined by

act(I , x) :=

red i if i 6= 0, πi = A→ α and [A→ α·, x ] ∈ Ishift if [A→ α1 · xα2, y ] ∈ I and x ∈ Σaccept if [S ′ → S ·, ε] ∈ I and x = εerror otherwise

Corollary 10.19

For every G ∈ CFGΣ, G ∈ LR(1) iff its LR(1) action function is welldefined.

Compiler Construction Summer Semester 2014 10.26

Page 89: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) goto Function

The goto function is defined in analogy to the LR(0) case(Definition 9.14).

Definition 10.20 (LR(1) goto function)

The function goto : LR(1)(G )× X → LR(1)(G ) is determined by

goto(I ,Y ) = I ′ iff there exists γ ∈ X ∗ such thatI = LR(1)(γ) and I ′ = LR(1)(γY ).

Compiler Construction Summer Semester 2014 10.27

Page 90: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) goto Function

The goto function is defined in analogy to the LR(0) case(Definition 9.14).

Definition 10.20 (LR(1) goto function)

The function goto : LR(1)(G )× X → LR(1)(G ) is determined by

goto(I ,Y ) = I ′ iff there exists γ ∈ X ∗ such thatI = LR(1)(γ) and I ′ = LR(1)(γY ).

Again, act and goto form the LR(1) parsing table of G .

Compiler Construction Summer Semester 2014 10.27

Page 91: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Table

Example 10.21 (cf. Example 10.17)

LR(1)(GLR ) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I ′2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I ′9I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I ′13I ′12 red 4I ′13 red 3

(empty = error/∅)

Compiler Construction Summer Semester 2014 10.28

Page 92: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton I

Definition 10.22 (LR(1) parsing automaton)

The LR(1) parsing automaton is defined as in the LR(0) case (seeDefinition 10.2), except for the transition relation:

shift: (aw , αI , z) ⊢ (w , αII ′, z) if act(I , a) = shift andgoto(I , a) = I ′

reducea: (aw , αII1 . . . In, z) ⊢ (aw , αII ′, zi) if act(In, a) = red i ,πi = A→ Y1 . . .Yn, and goto(I ,A) = I ′

reduceε: (ε, αII1 . . . In, z) ⊢ (ε, αII ′, zi) if act(In, ε) = red i ,πi = A→ Y1 . . .Yn, and goto(I ,A) = I ′

accept: (ε, I0I , z) ⊢ (ε, ε, z 0) if act(I , ε) = accept

errora: (aw , αI , z) ⊢ (ε, ε, z error) if act(I , a) = error

errorε: (ε, αI , z) ⊢ (ε, ε, z error) if act(I , ε) = error

Compiler Construction Summer Semester 2014 10.29

Page 93: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

Compiler Construction Summer Semester 2014 10.30

Page 94: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )

Compiler Construction Summer Semester 2014 10.30

Page 95: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )

Compiler Construction Summer Semester 2014 10.30

Page 96: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )

Compiler Construction Summer Semester 2014 10.30

Page 97: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )

Compiler Construction Summer Semester 2014 10.30

Page 98: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )

Compiler Construction Summer Semester 2014 10.30

Page 99: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )

Compiler Construction Summer Semester 2014 10.30

Page 100: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

10, 44 )

Compiler Construction Summer Semester 2014 10.30

Page 101: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

10, 44 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

13, 445 )

Compiler Construction Summer Semester 2014 10.30

Page 102: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

10, 44 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

13, 445 )⊢ ( ε, I ′0I

2 I′

6 I′

10 , 4453 )

Compiler Construction Summer Semester 2014 10.30

Page 103: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

10, 44 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

13, 445 )⊢ ( ε, I ′0I

2 I′

6 I′

10 , 4453 )⊢ ( ε, I ′0I

2 I′

6 I′

9 , 44535 )

Compiler Construction Summer Semester 2014 10.30

Page 104: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

10, 44 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

13, 445 )⊢ ( ε, I ′0I

2 I′

6 I′

10 , 4453 )⊢ ( ε, I ′0I

2 I′

6 I′

9 , 44535 )⊢ ( ε, I ′0I

1 , 445351 )

Compiler Construction Summer Semester 2014 10.30

Page 105: Compiler Construction - Lecturemoves.rwth-aachen.de/wp-content/uploads/SS14/cc14/slides/... · 2014. 5. 23. · Compiler Construction Summer Semester 2014 10.5. The LR(0) Action Function

The LR(1) Parsing Automaton II

Example 10.23 (cf. Example 10.17)

GLR : S ′ → S (0) S → L=R | R (1, 2) L → *R | a (3, 4) R → L (5)

LR(1)(GLR) act/goto|Σ goto|N* = a ε S L R

I ′0 shift/I ′4 shift/I ′5 I ′1 I′

2 I ′3I ′1 acceptI ′2 shift/I ′6 red 5I ′3 red 2I ′4 shift/I ′4 shift/I ′5 I ′8 I ′7I ′5 red 4 red 4I ′6 shift/I ′11 shift/I ′12 I ′10 I

9

I ′7 red 3 red 3I ′8 red 5I ′9 red 1I ′10 red 5I ′11 shift/I ′11 shift/I ′12 I ′10 I

13

I ′12 red 4I ′13 red 3

(empty = error/∅)

LR(1) parsing of a=*a:

(a=*a, I ′0 , ε )⊢ ( =*a, I ′0I

5 , ε )⊢ ( =*a, I ′0I

2 , 4 )⊢ ( *a, I ′0I

2 I′

6 , 4 )⊢ ( a, I ′0I

2 I′

6 I′

11 , 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

12, 4 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

10, 44 )⊢ ( ε, I ′0I

2 I′

6 I′

11I′

13, 445 )⊢ ( ε, I ′0I

2 I′

6 I′

10 , 4453 )⊢ ( ε, I ′0I

2 I′

6 I′

9 , 44535 )⊢ ( ε, I ′0I

1 , 445351 )⊢ ( ε, ε , 4453510)

Compiler Construction Summer Semester 2014 10.30