MELJUN P. CORTES,
MBA,MPA,BSCS,ACS

Fall 2008

CSC 3130: Automata theory and formal languages

LR(k) grammars

MELJUN CORT...
LR(0) example from last time
a
1
A → •aAb
A→ •ab

a

A

2
A → a•Ab
A → a•b
A → •aAb
A → •ab

A → aAb | ab

4
A → aA•b

b

...
LR(0) parsing example revisited
A Stack
S

1
1a2

Input

S

aabb
abb

1
2

S

1a2a2

bb

2

S
R
S
R

1a2a2b3
1a2A4
1a2A4b5...
Meaning of LR(0) items

α • X

β

d
vere
isco
und

A

εNFA transitions to:
X → •γ

part

shift focus to
subtree rooted at ...
Outline of LR(0) parsing algorithm
• Algorithm can perform two actions:
no complete item
is valid

there is one valid item...
Definition of LR(0) grammar
• A grammar is LR(0) if S/R, R/R conflicts never occur
– LR means parsing happens left to righ...
Hierarchy of context-free grammars
context-free grammars
parse using CYK algorithm (slow)

LR(∞) grammars

…
LR(1) grammar...
A grammar that is not LR(0)
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

input: a
A grammar that is not LR(0)
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

input: a
possibilities:
shift (3), reduce ...
Lookahead
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

S

peek inside!

S

A

input: a

A
A

a•a

S
B
A

A
a

a• a
...
Lookahead
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

input: a a
peek inside!

S

valid LR(0) items:

A
A
…

a•a
p...
Lookahead
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

input: a a a
peek inside!

S

valid LR(0) items:

A
A
A
a a•...
Lookahead
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

input: a a a

S

valid LR(0) items:

A
A
A
a a a•
parse tree...
LR(0) items vs. LR(1) items
LR(0)

LR(1)

A

A

A
a

A

a•A b
a

b

a

a•A b
a

b

A → a•Ab

b

[A → a•Ab, b]
A → aAb | ab...
LR(1) items
• LR(1) items are of the form
[A → α•β, x]

or

[A → α•β, ε]

to represent this state in the parsing

A
α • β ...
Outline of LR(1) parsing algorithm
• Step 1: Build εNFA that describes valid item updates
• Step 2: Convert εNFA to DFA
– ...
Recall εNFA transitions for LR(0)
• States of εNFA will be items (plus a start state q0)
• For every item S → •α we have a...
εNFA transitions for LR(1)
• For every item [S → •α, ε] we have a transition
q0

ε

[S → •α, ε]

• For every item A → α•Xβ...
FIRST sets
FIRST(α) is the set of terminals that occur
on the left in some derivation starting from α
• Example
S → A(1) |...
Explaining the transitions
A
α• X

A
β x

α

X

[A → α•Xβ, x]

[A → αX•β, x]

C β

A
α• C

X• β x

•δ

β x

[A → α•Cβ, x]
...
Example
[S → A•, ε]

S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

ε
q0

A
[S → •A, ε]

ε

ε
ε
B

[S → •Bc, ε]

ε

[...
Convert NFA to DFA
• Each DFA state is a subset of LR(1) items, e.g.
[A → a•A, ε] [A → a•, ε]
[B → a•, c] [B → a•b, c]
[A ...
Example
S → A(1) | Bc(2)
A → aA(3) | a(4)
B → a(5) | ab(6)

look ahead!
A

stack input valid items
[S → •A, ε] [S → •Bc, ε...
LR(k) grammars
• A context-free grammar is LR(1) if all S/R, R/R
conflicts can be resolved with one lookahead
• More gener...
Upcoming SlideShare
Loading in...5
×

MELJUN CORTES Automata Theory (Automata14)

62

Published on

MELJUN CORTES Automata Theory (Automata14)

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
62
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Automata Theory (Automata14)

  1. 1. MELJUN P. CORTES, MBA,MPA,BSCS,ACS Fall 2008 CSC 3130: Automata theory and formal languages LR(k) grammars MELJUN CORTES
  2. 2. LR(0) example from last time a 1 A → •aAb A→ •ab a A 2 A → a•Ab A → a•b A → •aAb A → •ab A → aAb | ab 4 A → aA•b b 5 A → aAb• b 3 A → ab•
  3. 3. LR(0) parsing example revisited A Stack S 1 1a2 Input S aabb abb 1 2 S 1a2a2 bb 2 S R S R 1a2a2b3 1a2A4 1a2A4b5 1A b b ε ε 3 4 5 A → aAb | ab a 1 A → •aAb A→ •ab 3 A → ab• 4 A → aA•b a b A b 2 A → a•Ab A → a•b A → •aAb A → •ab 5 A → aAb• A• • a • A• b • a • b• A ⇒ aAb ⇒ aabb
  4. 4. Meaning of LR(0) items α • X β d vere isco und A εNFA transitions to: X → •γ part shift focus to subtree rooted at X (if X is nonterminal) focus A → α•Xβ A → αX•β move past subtree rooted at X
  5. 5. Outline of LR(0) parsing algorithm • Algorithm can perform two actions: no complete item is valid there is one valid item, and it is complete shift (S) reduce (R) some valid items complete, some not more than one valid complete item S / R conflict R / R conflict • What if:
  6. 6. Definition of LR(0) grammar • A grammar is LR(0) if S/R, R/R conflicts never occur – LR means parsing happens left to right and produces a rightmost derivation • LR(0) grammars are unambiguous and have a fast parsing algorithm • Unfortunately, they are not “expressive” enough to describe programming languages
  7. 7. Hierarchy of context-free grammars context-free grammars parse using CYK algorithm (slow) LR(∞) grammars … LR(1) grammars LR(0) grammars parse using LR(0) algorithm java perl python …
  8. 8. A grammar that is not LR(0) S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) input: a
  9. 9. A grammar that is not LR(0) S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) input: a possibilities: shift (3), reduce (4) reduce (5), shift (6) S S A A A a•a S B A A a a• a a•c valid LR(0) items: A → a•A, A → a• B → a•, B → a•b, A → •aA, A → •a S/R, R/R conflicts!
  10. 10. Lookahead S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) S peek inside! S A input: a A A a•a S B A A a a• a a•c valid LR(0) items: A → a•A, A → a• B → a•, B → a•b, A → •aA, A → •a
  11. 11. Lookahead S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) input: a a peek inside! S valid LR(0) items: A A … a•a parse tree must look like this A → a•A, A → a• B → a•, B → a•b, A → •aA, A → •a action: shift
  12. 12. Lookahead S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) input: a a a peek inside! S valid LR(0) items: A A A a a• … parse tree must look like this A → a•A, A → a• A → •aA, A → •a action: shift
  13. 13. Lookahead S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) input: a a a S valid LR(0) items: A A A a a a• parse tree must look like this A → a•A, A → a• A → •aA, A → •a action: reduce
  14. 14. LR(0) items vs. LR(1) items LR(0) LR(1) A A A a A a•A b a b a a•A b a b A → a•Ab b [A → a•Ab, b] A → aAb | ab b
  15. 15. LR(1) items • LR(1) items are of the form [A → α•β, x] or [A → α•β, ε] to represent this state in the parsing A α • β x A α • β
  16. 16. Outline of LR(1) parsing algorithm • Step 1: Build εNFA that describes valid item updates • Step 2: Convert εNFA to DFA – As in LR(0), DFA will have shift and reduce states • Step 3: Run DFA on input, using stack to remember sequence of states – Use lookahead to eliminate wrong reduce items
  17. 17. Recall εNFA transitions for LR(0) • States of εNFA will be items (plus a start state q0) • For every item S → •α we have a transition q0 ε S → •α • For every item A → α•Xβ we have a transition A → α•Xβ X A → αX•β • For every item A → α•Cβ and production C → •δ A → α•Cβ ε C → •δ
  18. 18. εNFA transitions for LR(1) • For every item [S → •α, ε] we have a transition q0 ε [S → •α, ε] • For every item A → α•Xβ we have a transition [A → α•Xβ, x] X [A → αX•β, x] • For every item [A → α•Cβ, x] and production C → δ [A → α•Cβ, x] ε for every y in FIRST(βx) [C → •δ, y]
  19. 19. FIRST sets FIRST(α) is the set of terminals that occur on the left in some derivation starting from α • Example S → A(1) | cB(2) A → aA(3) | a(4) B → a(5) | ab(6) FIRST(a) = {a} FIRST(A) = {a} FIRST(S) = {a, c} FIRST(bAc) = {b} FIRST(BA) = {a} FIRST(ε) = ∅
  20. 20. Explaining the transitions A α• X A β x α X [A → α•Xβ, x] [A → αX•β, x] C β A α• C X• β x •δ β x [A → α•Cβ, x] ε [C → •δ, y] y y ∈ FIRST(βx)
  21. 21. Example [S → A•, ε] S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) ε q0 A [S → •A, ε] ε ε ε B [S → •Bc, ε] ε [A → •aA, ε] [A → •a, ε] [S → B•c, ε] [B → •a, c] ε [B → •ab, c] ...
  22. 22. Convert NFA to DFA • Each DFA state is a subset of LR(1) items, e.g. [A → a•A, ε] [A → a•, ε] [B → a•, c] [B → a•b, c] [A → •aA, ε] [A → •a, ε] • States can contain S/R, R/R conflicts • But lookahead can always resolve such conflicts
  23. 23. Example S → A(1) | Bc(2) A → aA(3) | a(4) B → a(5) | ab(6) look ahead! A stack input valid items [S → •A, ε] [S → •Bc, ε] [A → •aA, ε] ε abc [A → •a, ε] [B → •a, c] [B → •ab, c] S a bc [A → a•A, ε] [A → a•, ε] [B → a•, c] [B → a•b, c] [A → •aA, ε] [A → •a, ε] S R S R ab B Bc S c c ε ε [B → ab•, c] [S → B•c, ε] [S → Bc•, ε]
  24. 24. LR(k) grammars • A context-free grammar is LR(1) if all S/R, R/R conflicts can be resolved with one lookahead • More generally, LR(k) grammars can resolve all conflicts with k lookahead symbols – Items have the form [A → α•β, x1...xk] • LR(1) grammars describe the semantics of most programming languages
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×