Docsity

Compilers - LR Pressing - Fall 2008 | CS 414, Study notes of Computer Science

Toggle sidebar
bg1
CS414-2008-05 LR Parsing 1
05-0: Parsing
•LL(1) – Left-to-right, Leftmost derivation, 1-symbol lookahead parsers
•Need to guess which rule to apply after looking at only the first element in the rule
•LR parsers – Left-to-right, Rightmost derivation parsers
•Look at the entire right-hand side of the rule before deciding which rule to apply
05-1: LR Parsing
•Maintain a stack
•Shift terminals from the input stream to the stack, until the top of the stack is the same as the right-hand side of
a rule
•When the top of the stack is the same as the right-hand side of a rule reduce by that rule – replace the right-hand
side of the rule on the stack with the left-hand side of the rule.
•Continue shifting elements and reducing by rules, until the input has been consumed and the stack contains only
the initial symbol
05-2: LR Parsing Example
(0) E′→E$
(1) E→E+T
(2) E→T
(3) T→T∗num
(4) T→num
3 + 4 * 5$
3 * 4 + 5$
05-3: LR Parsing
•How do we know when to shift, and when to reduce?
•Use a Deterministic Finite Automaton
•Combination of DFA and a stack is called a Push-down automaton
•We will put both states and symbols on the stack
•When the end-of-file marker is shifted, accept the string
05-4: LR Parsing Example (0) E′→E$
(1) E→E+T
(2) E→T
(3) T→T∗num
(4) T→num
num + * $ E T
1 s2 g3 g4
2 r(4) r(4) r(4)
3 s7 a
4 r(2) s5 r(2)
5 s6
6 r(3) r(3) r(3)
7 s2 g8
8 r(1) s(5) r(1)
05-5: LR Parsing
•LR(0) Parsers. Reduce as soon as the top of the stack is the same as the left-hand side of a rule
•SLR(1) Parsers. More powerful than LR(0) – adds some lookahead information
•LR(1) Parsers. More powerful than SLR(1) – adds more sophisticated lookahead information
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Compilers - LR Pressing - Fall 2008 | CS 414 and more Study notes Computer Science in PDF only on Docsity!

05-0: Parsing

  • LL(1) – Left-to-right, Leftmost derivation, 1-symbol lookahead parsers
    • Need to guess which rule to apply after looking at only the first element in the rule
  • LR parsers – Left-to-right, Rightmost derivation parsers
    • Look at the entire right-hand side of the rule before deciding which rule to apply

05-1: LR Parsing

  • Maintain a stack
  • Shift terminals from the input stream to the stack, until the top of the stack is the same as the right-hand side of a rule
  • When the top of the stack is the same as the right-hand side of a rule reduce by that rule – replace the right-hand side of the rule on the stack with the left-hand side of the rule.
  • Continue shifting elements and reducing by rules, until the input has been consumed and the stack contains only the initial symbol

05-2: LR Parsing Example (0) E′^ → E$ (1) E → E + T (2) E → T (3) T → T ∗ num (4) T → num

05-3: LR Parsing

  • How do we know when to shift, and when to reduce?
  • Use a Deterministic Finite Automaton
    • Combination of DFA and a stack is called a Push-down automaton
  • We will put both states and symbols on the stack
  • When the end-of-file marker is shifted, accept the string

05-4: LR Parsing Example

(0) E′ → E$ (1) E → E + T (2) E → T (3) T → T ∗ num (4) T → num

num + * $ E T 1 s2 g3 g 2 r(4) r(4) r(4) 3 s7 a 4 r(2) s5 r(2) 5 s 6 r(3) r(3) r(3) 7 s2 g 8 r(1) s(5) r(1) 05-5: LR Parsing

  • LR(0) Parsers. Reduce as soon as the top of the stack is the same as the left-hand side of a rule
  • SLR(1) Parsers. More powerful than LR(0) – adds some lookahead information
  • LR(1) Parsers. More powerful than SLR(1) – adds more sophisticated lookahead information
  • LALR Parsers. Almost as powerful as LR(1), but uses much less memory (smaller table sizes)

05-6: LR(0) Parsing

  • Reads the input file Left-to-Right LR(0)
  • Creates a Rightmost derivation LR(0)
  • No Lookahead (0-symbol lookahead) LR(0)

LR(0) parsers are the simplest of the LR parsers 05-7: LR Parsing Example (0) S′^ → S$ (1) S → AA (2) S → bc (3) A → baA (4) A → c 05-8: LR Parsing Example S′^ → S$ S → AA S → bc A → baA A → c a b c S′^ S′^ → S$ S′^ → S$ S S → bc S → AA S → AA A A → baA A → c

Not LL(1)! 05-9: LR(0) Items

  • An LR(0) item consists of
    • A rule from the CFG
    • A “.” in the rule, which indicates where we currently are in the rule
  • S → ab. c
    • Trying to parse the rule S → abc
    • Already seen “ab”, looking for a “c”

05-10: LR(0) States & Transitions (0) S′^ → S$ (1) S → AA (2) S → bc (3) A → baA (4) A → c 05-11: LR(0) States & Transitions

05-16: Finding Transitions

  1. If the end-of-file terminal $ appears before the “.” in some item in the original state, create a transition from the original state to an “accept” state, transitioning on $.
  2. For each terminal a (other than $) that appears before the “.” in some item in the original state:
    • Create a new empty state.
    • For each item in the original state of the form S → α. a γ, where α and γ are (possibly empty) strings of terminals an non-terminals, add the item S → αa. γ to the new state.
    • Find the closure of the new state.
    • Add a transition from the original state to the new state, labeled with a.
  3. For each non-terminal S that appears before the “.” in some item in the original state:
    • Create a new empty state.
    • For each items in the original state of the form S 1 → α. Sγ, where α and γ are (possibly empty) strings of terminals an non-terminals, add the item S 1 → αS. γ to the new state.
    • Find the closure of the new state.
    • Add a transition from the original state to the new state, labeled with S.

05-17: Finding Transitions

  • If a “.” appears just before a terminal a in at least one item:
    • Create a new state
    • Add all items where the “.” is just before an a
    • Move the “.” past the a in the new state
    • Find the closure of the new state
    • Add a transition to the new state, labeled with an a.

05-18: Another LR(0) Example (0) E′^ → E$ (1) E → E + T (2) E → T (3) T → T * id (4) T → id 05-19: LR(0) States & Transitions

E’->. E$ E ->. E + T E ->. T T ->. T * id T ->. id

id

E

State 1

accept

$

id

E -> E + T. T -> T. * id

State 7

T -> T *. id

State 6 T -> T * id.

State 8

E’ -> E. $ E -> E. + T

State 2

T -> id.

State 4

E -> T. T -> T. * id

State 3

T

id E -> E +. T T ->. T * id T ->. id

State 5

T

05-20: LR(0) Parse Table id + * $ E T 1 s4 g2 g 2 s5 accept 3 r(2) r(2) r(2),s6 r(2) 4 r(4) r(4) r(4) r(4) 5 s4 g 6 s 7 r(1) r(1) r(1),s6 r(1) 8 r(3) r(3) r(3) r(3) 05-21: Shift-Reduce Conflict

  • In state 3, on a *, should we shift, or reduce? Why?

05-22: Shift-Reduce Conflict

  • In state 3, on a *, should we shift, or reduce? Why?
    • If we reduce, then we’re stuck – since the top of the stack will contain E, the next symbol in the input stream is *, and * cannot follow E in any partial derivation!
  • If a state contains the item:

S → γ.

we should only reduce if the next terminal can follow S

05-23: SLR(1)

  • Add simple lookahead (the S in SLR(1) is for simple
  • In LR(0) parsers, if state k contains the item “S → γ .” (where S → γ is rule (n))
    • Put r(n) in state k, in all columns

S’ -> S. $

State 2

S -> L = R.

State 10

R -> L.

State 9

L -> * R.

State 8

S -> L =. R R ->. L L ->. *R L ->. id

State 7

L -> *. R R ->. L L ->. * R L ->. id

State 5

S -> R.

State 4

S -> L. = R R -> L.

State 3

S’ ->. S$ S ->. L = R S ->. R L ->. *R L ->. id R ->. L

State 1

L -> id.

State 6

S accept

L

R

id *

id L

R

$

=

R

id

L

05-30: SLR(1) Parse Table id = * $ S L R 1 s6 s5 g2 g3 g 2 accept 3 r(5),s7 r(5) 4 r(2) 5 s6 s5 g9 g 6 r(4) r(4) 7 s6 s5 g9 g 8 r(3) r(3) 9 r(5) r(5) 10 r(1) 05-31: Why SLR(1) Fails S → L. = R R → L.

  • In this state, on a =, should be shift or reduce?
  • An = can follow an R – only if the R is preceded by a *
  • We need a more sophisticated lookahead scheme to disambiguate this situation

05-32: LR(1) Items

  • Like LR(0) items, contain a rule with a “.”
  • Also contain lookahead information – the terminals that could follow this rule, in the current derivation
    • More sophisticated than SLR(1), which only look at what terminals could follow the LHS of the rule in any derivation

05-33: LR(1) Example

(0) S′^ → S$

(1) S → L = R

(2) S → R

(3) L → *R

(4) L → id (5) R → L 05-34: LR(1) States and Transitions

id State 6 L -> id. =, $

State 1 S’ ->. S$S ->. L = R $ S ->. RL ->. *R $=, $ L ->. idR ->. L =, $$

State 2 S’ -> S. $

State 3 SR ->-> LL .. = R $$

S -> R. $ State 4

LR ->-> *. .L R =,=, $$ LL ->-> .. *id R =,=, $$

State 5

S -> L =. RR ->. L $$ L ->. *RL ->. id $$

State 7

L -> * R. =, $ State 8

State 9 R -> L. =, $ S -> L = R. $ State 10

Accept

L -> *. RR ->. L $$ L ->. * RL ->. id $$

State 12

R -> L. $ State 11 State 13 L -> id. $

L -> * R. $ State 14

S L R

$

=

id

R L

R * L id

id

R L

05-35: LR(1) Parse Table id = * $ S L R 1 s6 s5 g2 g3 g 2 accept 3 s7 r(5) 4 r(2) 5 s6 s5 g9 g 6 r(4) r(4) 7 s13 s12 g11 g 8 r(3) r(3) 9 r(5) r(5) 10 r(1) 11 r(5) 12 s13 s12 g11 g 13 r(4) 14 r(3) r(3) 05-36: More LR(1) Examples (0) S′^ → S$ (1) S → BC (2) S → b (3) B → bB (4) B → a (5) C → ǫ (6) C → cC 05-37: LR(1) States & Transitions

id State 6 L -> id. =, $

State 1 S’S ->-> .. LS$ = (^) R $ SL ->-> .. R (^) *R $=, $ LR ->-> .. idL =,$ $

State 2 S’ -> S. $

State 3 SR ->-> LL .. = R $$

State 4 S -> R. $

LR ->-> *. .L R =,=, $$ LL ->-> .. *id R =,=, $$

State 5

SR ->-> L. =L. R $$ LL ->-> .. *Rid $$

State 7

State 8 L -> * R. =, $

State 9 R -> L. =, $ State 10 S -> L = R. $

Accept

LR ->-> *. .L R $$ LL ->-> .. *id R $$

State 12

State 11 R -> L. $ State 13 L -> id. $

State 14 L -> * R. $

S L R

$

=

id

R L

R L *

id

id

R L

Can combine 5 & 12, 6 & 13, 8 & 14, 9 & 11 05-42: LALR Parser Example

id State 6- L -> id. =, $

State 1 S’ ->. S$ S ->. L = R $ S ->. R $ L ->. *R =, $ L ->. id =, $ R ->. L $

S’ -> S. $

State 2

S -> L. = R $ R -> L. $

State 3

S -> R. $

State 4

L -> *. R =, $ R ->. L =, $ L ->. * R =, $ L ->. id =, $

State 5-

S -> L =. R $ R ->. L $ L ->. *R $ L ->. id $

State 7

L -> * R. =, $

State 8-

R -> L. =, $

State 9-

S -> L = R. $

State 10

Accept

S L R

$

=

id

R

L

R * L id

05-43: LALR Parser Example id = * $ S L R 1 s6-13 s5-12 g2 g3 g 2 accept 3 s7 r(5) 4 r(2) 5-12 s6-13 s5-12 g9-11 g8- 6-13 r(4) r(4) 7 s6-13 s5-12 g9-11 g 8-14 r(3) r(3) 9-11 r(5) r(5) 10 r(1) 05-44: More LALR Examples

(0) S′^ → S$

(1) S → Aa (2) S →bAc (3) S → Bc (4) S →bBa (5) A →d (6) B →d 05-45: More LALR Examples

S’ ->. S $ - S ->. Aa $ S ->. bAc $ S ->. Bc $ S ->. bBa $ A ->. d a B ->. d c

S’ -> S. $ -

S -> A. a $

S -> b. Ac $ S -> b. Ba $ A ->. d c S -> B. c $ B ->. d a

A -> d. a B -> d. c A -> d .B -> d.^ ca

S -> bA. c $

S -> bB. a $

S -> bAc. $

S -> bBa. $

State 1 State 4

State 5

State 6

State 2

State 3 (^) State 7

S -> Aa. $

State 8

State 12

State 10

State 9

State 11

d (^) B

S

A

b

d

a

A

B

c

a

05-46: More LALR Examples

S’ ->. S $ - S ->. Aa $ S ->. bAc $ S ->. Bc $ S ->. bBa $ A ->. d a B ->. d c

S’ -> S. $ -

S -> A. a $

S -> b. Ac $ S -> b. Ba $ A ->. d c S -> B. c $ B ->. d a

A -> d. a, c B -> d. a, c

S -> bA. c $

S -> bB. a $

S -> bAc. $

S -> bBa. $

State 1 State 4

State 5

State 6

State 2

State 3-

S -> Aa. $

State 8

State 12

State 10

State 9

State 11

d (^) B

S

A

b

d

a

A

B

c

a

05-47: More LALR Examples