Presentation is loading. Please wait.

Presentation is loading. Please wait.

LR(k) grammars The Chinese University of Hong Kong Fall 2009

Similar presentations


Presentation on theme: "LR(k) grammars The Chinese University of Hong Kong Fall 2009"— Presentation transcript:

1 LR(k) grammars The Chinese University of Hong Kong Fall 2009
CSC 3130: Automata theory and formal languages LR(k) grammars Andrej Bogdanov

2 LR(0) example from last time
4 A  aA•b a A b 2 A  a•Ab A  a•b A  •aAb A  •ab 5 1 A  aAb• A  •aAb A •ab a b 3 A  ab• A  aAb | ab

3 LR(0) parsing example revisited
Stack Input S A  •aAb A •ab A  a•Ab A  a•b A  •ab A  aA•b A  aAb• A  ab• a b A 1 2 3 4 5 1 1a2 1a2a2 1a2a2b3 1a2A4 1a2A4b5 1A aabb abb bb b 1 2 3 4 5 S R A A a b a b A  aAb | ab A  aAb  aabb

4 Meaning of LR(0) items eNFA transitions to: X  •g A  aX•b A  a•Xb A
undiscovered part shift focus to subtree rooted at X (if X is nonterminal) a X b focus A  aX•b A  a•Xb move past subtree rooted at X

5 Outline of LR(0) parsing algorithm
Algorithm can perform two actions: What if: 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

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 Hierarchy of context-free grammars
parse using CYK algorithm (slow) LR(∞) grammars java perl python LR(1) grammars LR(0) grammars parse using LR(0) algorithm

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 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 S valid LR(0) items: A  a•A, A  a• B  a•, B  a•b, A  •aA, A  •a A A B A A A S/R, R/R conflicts! a a a a a a c

10 Lookahead input: valid LR(0) items:
S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) input: a peek inside! S S S valid LR(0) items: A  a•A, A  a• B  a•, B  a•b, A  •aA, A  •a A A B A A A a a a a a a c

11 parse tree must look like this
Lookahead S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) input: a peek inside! a A a S valid LR(0) items: A  a•A, A  a• B  a•, B  a•b, A  •aA, A  •a action: shift parse tree must look like this

12 parse tree must look like this
Lookahead S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) input: a a peek inside! a A a S valid LR(0) items: A  a•A, A  a• A  •aA, A  •a action: shift parse tree must look like this

13 parse tree must look like this
Lookahead S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) input: a a a A a S valid LR(0) items: A  a•A, A  a• A  •aA, A  •a action: reduce parse tree must look like this

14 LR(0) items vs. LR(1) items
A a b LR(1) A a b A  a•Ab [A  a•Ab, b] A  aAb | ab

15 LR(1) items LR(1) items are of the form to represent this state in the parsing [A  a•b, x] or [A  a•b, e] A A a b x a b

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 Recall eNFA transitions for LR(0)
States of eNFA will be items (plus a start state q0) For every item S  •a we have a transition For every item A  •X we have a transition For every item A  a•Cb and production C  •d e q0 S  •a X A  •X A  X• e A  •C C  •d

18 eNFA transitions for LR(1)
For every item [S  •a, e] we have a transition For every item A  •X we have a transition For every item [A  a•Cb, x] and production C  d for every y in FIRST(bx) e q0 [S  •a, e] X [A  •X, x] [A  X•, x] e [A  •C, x] [C  •d, y]

19 FIRST sets FIRST(a) is the set of terminals that occur on the left in some derivation starting from a Example FIRST(a) = {a} FIRST(A) = {a} FIRST(S) = {a, c} FIRST(bAc) = {b} FIRST(BA) = {a} FIRST(e) = ∅ S  A(1) | cB(2) A  aA(3) | a(4) B  a(5) | ab(6)

20 Explaining the transitions
X b x a X b x X [A  •X, x] [A  X•, x] C b A y a C b x d e [A  •C, x] [C  •d, y] y ∈ FIRST(bx)

21 Example: Constructing the NFA
S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) [S  A•, e] A [A  •aA, e] e [S  •A, e] [A  •a, e] e e . . . q0 [S  B•c, e] e B e [S  •Bc, e] [B  •a, c] e [B  •ab, c]

22 Example: Constructing the NFA
S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) [S  A•, e] A a e A [S  •A, e] [A  •aA, e] [A  a•A, e] [A  aA•, e] e e a e [A  •a, e] [A  a•, e] q0 e c [S  B•c, e] [S  Bc•, e] B e a [S  •Bc, e] [B  •a, c] [B  a•, c] e a b [B  •ab, c] [B  a•b, c] [B  ab•, c]

23 Example: Running the NFA
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, ] [A  •a, ] [B  •a, c] [B  •ab, c] abc bc [A  a•A, ] [A  a•, ] [B  a•, c] [B  a•b, c] [A  •aA, ] [A  •a, ] a S [B  ab•, c] c ab S [S  B•c, ] c B R [S  Bc•, ] Bc S S R

24 Convert NFA to DFA Each DFA state is a subset of LR(1) items, e.g.
States can contain S/R, R/R conflicts But lookahead can always resolve such conflicts [A  a•A, ] [A  a•, ] [B  a•, c] [B  a•b, c] [A  •aA, ] [A  •a, ]

25 Example: Convert NFA to DFA
LEGEND S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) shift variable shift terminal reduce A 2 1 [A  a•A, e] [S  •A, e] 3 [S  •Bc, e] [A  •aA, e] [A  a•A, e] 4 [A  •a, e] [A  •aA, e] A [A  •aA, e] a a [A  aA•, e] [B  a•b, c] [A  •a, e] [A  •a, e] [B  •a, c] [A  a•, e] [A  a•, e] [B  a•, c] [B  •ab, c] a b A B 5 6 7 8 c [S  A•, e] [S  B•c, e] [S  Bc•, e] [B  ab•, c]

26 Example: Reconstruct the parse tree
stack input S  A(1) | Bc(2) A  aA(3) | a(4) B  a(5) | ab(6) [A  a•A, e] [A  •a, e] [B  a•b, c] [A  •aA, e] [A  a•, e] [B  a•, c] b 2 8 [S  •A, e] [S  •Bc, e] [A  •aA, e] [A  •a, e] [B  •a, c] [B  •ab, c] 1 a 2 B 6 abc 1 bc 12 S [S  B•c, e] c 6 7 [S  Bc•, e] 7 c 128 S c 16 R 167 S [B  ab•, c] 8 S 1 R B a b c

27 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  •, c1...ck] LR(1) grammars describe the semantics of most programming languages


Download ppt "LR(k) grammars The Chinese University of Hong Kong Fall 2009"

Similar presentations


Ads by Google