A New Look at LR(k) Bill McKeeman, MathWorks Fellow for Worcester Polytechnic Computer Science...

Post on 31-Dec-2015

217 views 3 download

Tags:

Transcript of A New Look at LR(k) Bill McKeeman, MathWorks Fellow for Worcester Polytechnic Computer Science...

A New Look at LR(k)

Bill McKeeman,

MathWorks Fellow

for

Worcester Polytechnic

Computer Science Colloquium

February 20, 2004

Abstract

Knuth offered LR(k) as an algorithm for parsing computer languages (Information and Control, Nov. 1965). Most work since has concentrated on parser usability and space/time optimization.

This talk presents LR(k) from first principles, teasing apart some complex formalisms applied by Knuth into simpler components.

AssumptionsThe audience is comfortable with mathematical notation

and computer programming.

Points to Ponder

• A context-free grammar can describe meaning in addition to syntax.

• The set of reducible parse stacks is a regular language.

• Any finite automata can be described by a context-free grammar.

• The LR(0) machine can be constructed in two stages, first an NFA, then a DFA.

• In most cases, the LALR(1) lookahead can be teased out of the LR(0) machine.

A context-free grammar <N, T, G, R> is a four-tuple describing a language L.

N is a set of non-terminal symbols (naming syntactic structure like expression).

T is a set of terminal symbols (naming the written symbols like +, for, int…)

V = N T, N T = { }.

G N, is a set of symbols describing the language (usually just sentence or translation-unit).

R is a set of rewriting rules, R N V*.

Given an input text from language L, we repeatedly substitute the LHS of a rewriting rule for its RHS, leading eventually to something in G. The sequence of substitutions is called the parse.

Why Use Context-free Grammars?N = {P D C B}, T = { | & t f}, G = {P}.

1. P→D

2. D→D|C3. D→C4. C→C&B5. C→B6. B→t

7. B→f

t&t|f&f

B&t|f&f

C&t|f&f

C&B|f&f

C|f&f

D|f&f

D|B&f

D|C&f

D|C&B

D|C

D

P

6

5

6

4

3

7

5

7

4

2

1

t

t

&

f

f

&

|

7 rewriting rules: rewritings rule consumed terminals

tt&ff&|pcode

Grammar for boolean expressions

How to Get a Parse?

• Recursive Descent (top down)– Write a C function for each nonterminal– Report rule applications– Pro: no tools needed, Con: can be buggy

• YACC (bottom up)– Write a CFG– Implement rule applications– Pro: reliable, Con: deal with YACC diagnostics

Parse Stack Input S/R sequence

• t&t|f&f start• t &t|f&f shift t• B &t|f&f rule 6• C &t|f&f rule 5• C& t|f&f shift &• C&t |f&f shift t• C&B |f&f rule 6• C |f&f rule 4• D |f&f rule 3• D| f&f shift |• D|f &f shift f• D|B &f rule 7• D|C &f rule 5• D|C& f shift &• D|C&f shift f• D|C&B rule 5• D|C rule 4• D rule 2• D shift • P rule 1

(quit)

The red parse stacks are reducible

S/R is useful for languages where a lexer is needed.

The set L of all reducible parse stacks for L is a regular language (recognizable with a finite automaton). L has a grammar that can be derived from the CFG of the original language L. The terminal symbols of L are the things than can appear on parse stacks (that is, all of T and N). The nonterminal symbols of L are all the partial rules of L (that is, leave off zero or more symbols on the end of a rule of L and get a nonterminal of L). The resulting rules R are all of the forms A→Bb A→B A→Using finite automata terminology, rules A→Bb are read as “in state B if you see b, eat it and go to state A”. Rules A→B are the same but no b. Rule A→ says “create A from nothing”, which is how we get started.

This definition leaves undefined the situation if an unacceptable symbol appears in the input. One can make the NFA 1-1 by adding an error state E and rules of the form E→Bb to fill out the tables. In practice the error state triggers a syntax diagnostic. It is too verbose for use here.

LR(0) NFA Construction notation: , V*

L defined by <N, T, G, R>

L defined by <N, T, G, R>

T = V // anything on the parse stack

N = {A→ | A→ R} // any partial rule

G = R // any completed rule

R = {A→a → A→ a | A→a R}

{B→ → A→ | A→B R A N}

{A→ → | A G}

P→D D→D | CD→CC→C & BC→BB→tB→f

R for L N for L

0 P→ 1 P→D 2 P→D 3 D→ 4 D→D 5 D→D| 6 D→D|C 7 D→C 8 C→ 9 C→C10 C→C&11 C→C&B12 C→B13 B→14 B→t15 B→f

R for L

0→ 1→0D 2→1 4→3D 5→4| 6→5C 7→3C 9→8C10→9&11→10B12→8B14→13t15→13f 3→0 8→3 8→513→813→10

G for L

2 6 7 11 12 14 15

Apply LR(0) NFA

D|C&B0D|C&B3D|C&B4|C&B5C&B8C&B9&B10B11

For rule 11 we rewrite C→C&B and start over:

D|C

elements of N renamed for readability

NFA to DFA

The NFA is not efficient. So we apply the (textbook) NFA-to-DFA transformation, getting yet another context-free grammar.

In this case the nonterminals are sets of partial rules, and the terminals are the same as the for the NFA (since they describe the same language).

LR(0) DFA Construction

E(M) = M {X | Y E(M) X→Y R} // tx on empty

S(M, b) = E({X | Y M X→Yb R}) // tx on b from M

0 = E({0}) // start state

N = {0} {S(M,b) | M N b T}

G = {M | M N M G ≠ {}} // contains a rule

P = {0→} {N→Mb | M N b T N = S(M,b)}

Note: this construction gives just the states reachable from 0. The textbook formalism is simpler but computes useless states

The LR(0) DFA

T for L

P D C B | & t f

N for L

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

T for L

P D C B | & t f

N for L

0 0 3 8 13 1 1 4 2 2 3 5 8 13 4 6 9 5 10 13 6 11 7 14 8 15 9 7 910 12

R for L

0→ 1→0D 2→1 3→1| 4→3C 5→4& 6→5B 7→5t 8→5f10→3B 7→3t 8→3f 9→0C10→0B 7→0t 8→0f

N renamed for readability

D|C&B0D|C&B1|C&B3C&B4&B5B6

Apply LR(0) DFA

G for L

2 4 6 7 8 9 10

The LR(0) DFA0 1

9

10

7

8

2

3

5

4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

D|C&B0D|C&B1|C&B3C&B4&B5B6

Apply LR(0) DFA

States 2, 6, 7, 8, 10 are obviously in G (no place to go).

States 4 and 9 are in G but also can shift.

States 4 and 9 show LR(0) shift/reduce conflicts.

For this CFG, shift if you can resolves the conflicts.

Note that is what we did for state 4 above.

C→C D→D|CC→C D→C

The LALR(1) Lookahead

0 1

9

10

7

8

2

3

5

4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

The LALR(1) lookahead for D→D|C in state 4 is found by stepping back from 4, over the RHS of the rule (D|C), then stepping forward (from 0 in this case) over the LHS of the rule (D) and collecting the transition symbols ( |). Since the transition symbols out of 1 are different from the transition symbols out of 4 (&), there is no LALR(1) shift/reduce conflict.

If we erroneously did the reduction D→D|C while looking at something other than or |, we would immediately fail on the next step.

P D C B | & t f

0 1 9 10 7 8

1 2 3

2 R1 R1 R1 R1 R1 R1 R1 R1 R1

3 4 10 7 8

4 R2 R2 5

5 6 7 8

6 R4 R4 R4

7 R6 R6 R6

8 R7 R7 R7

9 R3 R3 5

10 R5 R5 R5

Conventional LALR(1) parsing table

in state:

see:

NQLALR vs LALR

The simple trick for LALR(1) lookahead worked because the grammar for L had no erasing rules (rules with empty RHS). One way to insure the no-erasing property is to transform the user’s grammar, removing the erasing rules.

The trick is to remove each erasing rule, say F→, and everywhere in the grammar that F is used, create a new rule with F erased.

F→

F →i

H→hFj

F→i

H→hFj

H→hj

Putting it all together (1)

0 1

9

10

7

8

2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

0 t&t|f&f

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

reduce states

parse stack input action

start

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (2)

0 1

9

10

7

8

2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

7 t0

&t|f&f

reduce states

parse stack input action

shift t

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (3)

0 1

9

10

7

8

2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

B0

&t|f&f

reduce states

parse stack input action

reduce B→t

10 B0

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (4)

0 1

9

10

7

8

2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

&t|f&f

reduce states

parse stack input action

shift B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (5)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

C0

&t|f&f

reduce states

parse stack input action

reduce C→B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (6)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

9 C0

&t|f&f

reduce states

parse stack input action

shift C

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (7)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

5 &9 C0

t|f&f

reduce states

parse stack input action

shift &

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (8)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

7 t5 &9 C0

|f&f

reduce states

parse stack input action

shift t

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (9)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

B5 &9 C0

|f&f

reduce states

parse stack input action

reduce B→t

Note: we do not have to go all the way back to state 0.

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (10)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

6 B5 &9 C0

|f&f

reduce states

parse stack input action

shift B

Note: we do not have to go all the way back to state 0.

C0

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (11)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

|f&f

reduce states

parse stack input action

reduce C→C&B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (12)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

9 C0

|f&f

reduce states

parse stack input action

shift C

D0

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (13)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

|f&f

reduce states

parse stack input action

reduce D→C

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (14)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

1 D0

|f&f

reduce states

parse stack input action

shift D

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (15)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

3 |1 D0

f&f

reduce states

parse stack input action

shift |

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (16)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

8 f3 |1 D0

&f

reduce states

parse stack input action

shift f

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (17)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

B3 |1 D0

&f

reduce states

parse stack input action

reduce B→f

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (18)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

10 B3 |1 D0

&f

reduce states

parse stack input action

shift B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (19)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

C3 |1 D0

&f

reduce states

parse stack input action

reduce C→B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (20)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

4 C3 |1 D0

&f

reduce states

parse stack input action

shift C

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (21)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

5 &4 C3 |1

f

reduce states

parse stack input action

shift &

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (22)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

8 f5 &4 C3

reduce states

parse stack input action

shift f

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (23)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

B5 &4 C3 |

reduce states

parse stack input action

reduce B→f

6 B5 &4 C3

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (24)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

reduce states

parse stack input action

shift B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (25)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

C3 |1 D0

reduce states

parse stack input action

reduce C→C&B

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (26)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

4 C3 |1 D0

reduce states

parse stack input action

shift C

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (27)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

D0

reduce states

parse stack input action

reduce D→D|C

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (28)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

1 D0

reduce states

parse stack input action

shift D

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (29)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

2 1 D0

reduce states

parse stack input action

shift

2 P→D

4 D→D|C

6 C→C&B

7 B→t

8 B→f

9 D→C

10 C→B

Putting it all together (30)

1

9

10

7

8

0 2

3 4

10

7

8

5 6

7

8

D

C

B

t

f

|

&

C

B

t

f

& B

t

f

P0

reduce states

parse stack input action

reduce P→D

quit