LR Parsing Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

lr parsing
SMART_READER_LITE
LIVE PREVIEW

LR Parsing Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

Introduction The Automata LR Parsing Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction The Automata Objectives You should be able to ... Explain the difference between an LL and LR


slide-1
SLIDE 1

Introduction The Automata

LR Parsing

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

slide-2
SLIDE 2

Introduction The Automata

Objectives

You should be able to ...

◮ Explain the difference between an LL and LR parser. ◮ Generate the fjnite state machine from an LR grammar. ◮ Use the state machine to detect ambiguity. Further reading: See Dragon Book §4.x.

slide-3
SLIDE 3

Introduction The Automata

What Is LR Parsing?

◮ What is an LR parser?

◮ An LR parser uses a Left-to-right scan and produces a Rightmost derivation. ◮ A.k.a. bottom-up parsing ◮ Uses a push-down automata to do the work.

◮ There are four actions. Shift Consume a token from the input. Reduce Build a tree from components. Goto Jump to a different state, after a reduce. Accept Signal that we’re done.

slide-4
SLIDE 4

Introduction The Automata

Shifting

Shifting involves three steps.

  • 1. Consume a token from the input.
  • 2. Push the token and the current state to the stack.
  • 3. Go to the next state.

Example:

I0 S →

  • a b

I1 S → a • b I2 S → a b • {$} r1

a b Grammar S → a b Input • a b $ Stack (empty) Current State 0 We will shift the a and then we go to state 1.

slide-5
SLIDE 5

Introduction The Automata

Shifting

Shifting involves three steps.

  • 1. Consume a token from the input.
  • 2. Push the token and the current state to the stack.
  • 3. Go to the next state.

Example:

I0 S →

  • a b

I1 S → a • b I2 S → a b • {$} r1

a b Grammar S → a b Input a • b $ Stack 0, a Current State 1 We will shift the b and then we go to state 2.

slide-6
SLIDE 6

Introduction The Automata

Shifting

Shifting involves three steps.

  • 1. Consume a token from the input.
  • 2. Push the token and the current state to the stack.
  • 3. Go to the next state.

Example:

I0 S →

  • a b

I1 S → a • b I2 S → a b • {$} r1

a b Grammar S → a b Input a b • $ Stack 0, a, 1, b Current State 2 What should happen now?

slide-7
SLIDE 7

Introduction The Automata

Reducing

Reducing involves three steps.

  • 1. Pop the tokens and states from the stack. (How many?)
  • 2. Return to the last state popped.
  • 3. Construct a new tree from the popped tokens.

Example:

I0 S →

  • a b

I1 S → a • b I2 S → a b • {$} r1

a b Grammar S → a b Input a b • $ Stack 0, a, 1, b Current State 2 We are ready to reduce.

slide-8
SLIDE 8

Introduction The Automata

Reducing

Reducing involves three steps.

  • 1. Pop the tokens and states from the stack. (How many?)
  • 2. Return to the last state popped.
  • 3. Construct a new tree from the popped tokens.

Example:

I0 S →

  • a b

I1 S → a • b I2 S → a b • {$} r1

a b Grammar S → a b Input a b • $ Stack Current State 0 Now we have an S tree. Go To or Accept could happen here.

slide-9
SLIDE 9

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input • a x b $ Current State 0 Stack (Empty)

slide-10
SLIDE 10

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input a • x b $ Current State 1 Stack 0,a

slide-11
SLIDE 11

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input a x • b $ Current State 4 Stack 0,a,1,x

slide-12
SLIDE 12

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input a x • b $ Current State 1 Stack 0,a

slide-13
SLIDE 13

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input a x • b $ Current State 2 Stack 0,a, 1, E x

slide-14
SLIDE 14

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input a x b • $ Current State 3 Stack 0,a, 1, E x , 2, b

slide-15
SLIDE 15

Introduction The Automata

A More Complex Example

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x Grammar S → a E b E → x Input a x b • $ Current State 0 Stack (Empty) Now we have the result: S a E x b

slide-16
SLIDE 16

Introduction The Automata

Representing the Automata

We will represent the automata using two tables. Action Table Shift, Reduce n, Accept Goto Table Destination State The rows are the state numbers, the columns are the symbols.

slide-17
SLIDE 17

Introduction The Automata

The Algorithm

◮ To create the start state, add the transitive closure of the start symbol. Example 1 Start Example 2 Start S → x S e | E x E → a E | F x F → q S →

  • x S e

|

  • E x

E →

  • a E

|

  • F x

F →

  • q

S → x S e | F x E → a E | F x F → q S →

  • x S e

|

  • F x

F →

  • q
slide-18
SLIDE 18

Introduction The Automata

The Algorithm, ctd

◮ Let x be an arbitrary terminal, A be an aribtrary nonterminal, and α and β be arbitrary (possibly empty) strings of symbols. ◮ In an item set i, take every production of the form E → α • xβ and produce a new state j containing the transitive closure of E → αx • β. Add a shift in the action table for column x and state i, and destination state j in the goto table for column x and state i. ◮ In an item set i, take every production of the form E → α • Aβ and produce a new state j containing the transitive closure of E → αA • β. Add j to the goto table in column A and state i. ◮ In an item set i, take every rule of the form E → α• and add a reduce actions for state i for each terminal in the follow set of E. ◮ If an item set is recreated, reuse the original; do not create a duplicate.

slide-19
SLIDE 19

Introduction The Automata

Automata Example 1

Automata

I0 S →

  • a b

I1 S → a • b I2 S → a b • $ r1

a b

Tabular Representation

Grammar Action Goto S → a b a b $ s 1 s 2 r1 a b $ S 1 1 2 2

slide-20
SLIDE 20

Introduction The Automata

Automata Example 2

Automata

I0 S →

  • a E b

I1 S → a • E b E →

  • x

I2 S → a E • b I3 E → x • {b} r2 I4 S → a E b • {$} r1

a E b x

Grammar Action Goto S → a E b E → x a b x $ s 1 s 2 s 3 r2 4 r1 a b x $ S E 1 1 3 2 2 4 3 4

slide-21
SLIDE 21

Introduction The Automata

Automata Example 3.1

◮ Let’s build the table for this automata. S → a E b | a b S E → E x | b

slide-22
SLIDE 22

Introduction The Automata

Automata Example 3.2

I0 S →

  • a E b
  • a b S

Grammar Action Goto S → a E b | a b S E → E x | b a b x $ 1 2 3 4 5 6 a b x $ S E 1 2 3 4 5 6

slide-23
SLIDE 23

Introduction The Automata

Automata Example 3.2

I0 S →

  • a E b ⇐
  • a b S ⇐

Grammar Action Goto S → a E b | a b S E → E x | b a b x $ 1 2 3 4 5 6 a b x $ S E 1 2 3 4 5 6

slide-24
SLIDE 24

Introduction The Automata

Automata Example 3.3

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

a

S → a E b | a b S E → E x | b a b x $ s 1 2 3 4 5 6 a b x $ S E 1 1 2 3 4 5 6

slide-25
SLIDE 25

Introduction The Automata

Automata Example 3.3

I0 S →

  • a E b
  • a b S

I1 S → a • E b ⇐ a • b S E →

  • E x ⇐
  • b

a

S → a E b | a b S E → E x | b a b x $ s 1 2 3 4 5 6 a b x $ S E 1 1 2 3 4 5 6

slide-26
SLIDE 26

Introduction The Automata

Automata Example 3.4

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x

a E

S → a E b | a b S E → E x | b a b x $ s 1 2 3 4 5 6 a b x $ S E 1 1 2 2 3 4 5 6

slide-27
SLIDE 27

Introduction The Automata

Automata Example 3.4

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S ⇐ E →

  • E x
  • b ⇐

I2 S → a E • b E → E • x

a E

S → a E b | a b S E → E x | b a b x $ s 1 2 3 4 5 6 a b x $ S E 1 1 2 2 3 4 5 6

slide-28
SLIDE 28

Introduction The Automata

Automata Example 3.5

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

a E b

S → a E b | a b S E → E x | b a b x $ s 1 s 2 3 4 5 6 a b x $ S E 1 1 3 2 2 3 4 5 6

slide-29
SLIDE 29

Introduction The Automata

Automata Example 3.5

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b ⇐ E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

a E b

S → a E b | a b S E → E x | b a b x $ s 1 s 2 3 4 5 6 a b x $ S E 1 1 3 2 2 3 4 5 6

slide-30
SLIDE 30

Introduction The Automata

Automata Example 3.6

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

I4 S → a E b •

a E b b

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 4 5 6

slide-31
SLIDE 31

Introduction The Automata

Automata Example 3.6

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x ⇐ I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

I4 S → a E b •

a E b b

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 4 5 6

slide-32
SLIDE 32

Introduction The Automata

Automata Example 3.7

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

I4 S → a E b • I5 E → E x •

a E b b x

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 4 5 6

slide-33
SLIDE 33

Introduction The Automata

Automata Example 3.7

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S ⇐ E → b • S →

  • a E b

S →

  • a b S

I4 S → a E b • I5 E → E x •

a E b b x

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 4 5 6

slide-34
SLIDE 34

Introduction The Automata

Automata Example 3.8

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

I4 S → a E b • I5 E → E x • I6 S → a b S •

a E b b x S

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 6 4 5 6

slide-35
SLIDE 35

Introduction The Automata

Automata Example 3.8

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • ⇐ S →

  • a E b

S →

  • a b S

I4 S → a E b • I5 E → E x • I6 S → a b S •

a E b b x S

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 6 4 5 6

slide-36
SLIDE 36

Introduction The Automata

Automata Example 3.8

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

{x, b} r4 I4 S → a E b • I5 E → E x • I6 S → a b S •

a E b b x S

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 r4 r4 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 6 4 5 6

slide-37
SLIDE 37

Introduction The Automata

Automata Example 3.8

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b ⇐

S →

  • a b S ⇐

{x, b} r4 I4 S → a E b • I5 E → E x • I6 S → a b S •

a E b b x S

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 r4 r4 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 6 4 5 6

slide-38
SLIDE 38

Introduction The Automata

Automata Example 3.9

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

{x, b} r4 I4 S → a E b • I5 E → E x • I6 S → a b S •

a E b b x S a

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 s r4 r4 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 1 6 4 5 6

slide-39
SLIDE 39

Introduction The Automata

Automata Example 3.9

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

{x, b} r4 I4 S → a E b • ⇐ I5 E → E x • ⇐ I6 S → a b S • ⇐

a E b b x S a

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 s r4 r4 4 5 6 a b x $ S E 1 1 3 2 2 4 5 3 1 6 4 5 6

slide-40
SLIDE 40

Introduction The Automata

Automata Example 3.10

I0 S →

  • a E b
  • a b S

I1 S → a • E b a • b S E →

  • E x
  • b

I2 S → a E • b E → E • x I3 S → a b • S E → b • S →

  • a E b

S →

  • a b S

{x, b} r4 I4 S → a E b • {$} r1 I5 E → E x • {x, b} r3 I6 S → a b S • {$} r2

a E b b x S a

S → a E b | a b S E → E x | b a b x $ s 1 s 2 s s 3 s r4 r4 4 r1 5 r3 r3 6 r2 a b x $ S E 1 1 3 2 2 4 5 3 1 6 4 5 6

slide-41
SLIDE 41

Introduction The Automata

Activity

Your turn. Try to build the automata for this grammar. There’s a surprise waiting for you! S → a E b | x E → E x E | b