lesson 9 cdt301 – compiler theory, spring 2011 teacher: linus källberg

29
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Upload: edmund-george

Post on 03-Jan-2016

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Lesson 9

CDT301 – Compiler Theory, Spring 2011Teacher: Linus Källberg

Page 2: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2

Outline

• Creating SLR parsing tables– LR(0) items– Canonical LR(0) collections– LR(0) automata

Page 3: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

CREATING SLR PARSING TABLES

3

Page 4: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Repetition: Shift-reduce parsingStack Input Action$ id * id $ sh. id$ id * id $ red. by F → id$ F * id $ red. by T → F$ T * id $ sh. *$ T * id $ sh. id$ T * id $ red. by F → id$ T * F $ red. by T → T * F$ T $ red. by E → T$ E $ accept

4id * id F * ⇐ id T * ⇐ id T * F T E⇐ ⇐ ⇐

Page 5: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

(1) E → E + T

(2) E → T(3) T → T *

F(4) T → F(5) F → ( E )(6) F → id

5

StateACTION GOTO

id + * ( ) $ E T F

0 s5 s4 1 2 3

1 s6 acc

2 r2 s7 r2 r2

3 r4 r4 r4 r4

4 s5 s4 8 2 3

5 r6 r6 r6 r6

6 s5 s4 9 3

7 s5 s4 10

8 s6 s11

9 r1 s7 r1 r1

10 r3 r3 r3 r3

11 r5 r5 r5 r5

Page 6: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Repetition: LR parsingStack Input Action$ 0 id * id $ sh. 5$ 0 5id * id $ red. by (6) F → id

$ 0 3F * id $ red. by (4) T → F

$ 0 2T * id $ sh. 7

$ 0 2T 7* id $ sh. 5

$ 0 2T 7* 5id $ red. by (6) F → id

$ 0 2T 7* 10F $ red. by (3) T → T * F

$ 0 2T $ red. by (2) E → T

$ 0 1E $ accept 6

Page 7: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LR(0) items

• One state represents: symbol + context– The set of handles the parser might be in the

process of reading in that state– The progress so far in reading those handles

• LR(0) item = handle with a “dot”– Example: E → E • + T

7

Page 8: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LR(0) items

• Grammar:decl → type id ;

| type id [ num ] ;type → int | float | classclass → id

8

Page 9: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LR(0) items

• Set of items I1:

decl → • type id ; | • type id [ num ] ;

type → • int | • float | • classclass → • id

9

Page 10: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Computing CLOSURE

CLOSURE(I)J = Irepeat

foreach item in Jif it has the form A → α•Bβ

add all B → •γ to Juntil no more items were added in an iterationreturn J 10

Page 11: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

The GOTO function

• GOTO(I1, int) = I2:

type → int •• GOTO(I1, float) = I3:

type → float •• GOTO(I1, id) = I4:

class → id •11

Page 12: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

The GOTO function

• GOTO(I1, type) = I5:

decl → type • id ; | type • id [ num ] ;

• GOTO(I1, class) = I6

type → class •

12

Page 13: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Computing GOTO

GOTO(I, X)J = Øforeach item A → α•Xβ in I

add A → αX•β to Jreturn CLOSURE(J)

13

Page 14: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Reductions

• State:type → int •

• Pop one state– I1 appears on the stack

• Push type and go to GOTO(I1, type)

14

Page 15: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LR(0) automata

• Based on the canonical LR(0) collection• States represent sets of items• Transitions defined by the GOTO function

15

Page 16: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LR(0) automata

• Grammar:S → A BS → A cA → A aA → aB → bB→ ε

• Augmented grammar:S' → SS → A BS → A cA → A aA → aB → bB→ ε 16

Page 17: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LR(0) automata

17

I0:S‘ → • SS → • A BS → • A cA → • A aA → • a

I7:S‘ → S •

I2:S → A • BS → A • cA → A • aB → • bB → •

I1:A → a • I6:

S → A B •

I5:B → b •

I4:A → A a •

I3:S → A c •S

aA

c

a

b

B

Page 18: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Computing the canonicalcollection of sets of items

items(G')C = CLOSURE({ S' → • S })repeat

foreach item I in Cforeach grammar symbol X

if GOTO(I, X) is non-empty, add it to Cuntil no more sets were added in an iterationreturn C

18

Page 19: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Exercise (1)

Computea) CLOSURE({ F → • id })b) CLOSURE({ S' → • E })c) CLOSURE({ E → T • E’ })

S' → EE → T E'E' → + T E’ | ε T → F T' T' → * F T’ | εF → ( E ) | id

19

Page 20: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Exercise (2)

Let I be the following set of items:

E → T • E'E' → • + T E'E' → •

Compute GOTO(I, X) for all relevant X.

S' → EE → T E'E' → + T E’ | ε T → F T' T' → * F T’ | εF → ( E ) | id

20

Page 21: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Constructing the parsing table

1. Enumerate the productions:

(0) S' → S(1) S → A B(2) S → A c(3) A → A a(4) A → a(5) B → b(6) B→ ε

2. Compute FOLLOW:FOLLOW(S') = { $ }FOLLOW(S) = { $ }FOLLOW(A) = { a, b, c, $ }FOLLOW(B) = { $ }

21

Page 22: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Constructing the parsing tabletable(C)

foreach set of item Ii in C

if Ii has an item [A → α•aβ], where a is a terminal

set ACTION[i, a] to a “shift j”,where j is the state representing Ij = GOTO(Ii, a)

if Ii has an item [A → α•Bβ]

set GOTO[i, B] to j,where j is the state representing Ij = GOTO(Ii, B)

if Ii has an item [A → α•]

set ACTION[i, a] to “red. by. A → α” for all a in FOLLOW(A)

if Ii has the item [S' → S•]

set ACTION[i, $] to “accept”

22

Page 23: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Constructing the parsing table

23

I0:S‘ → • SS → • A BS → • A cA → • A aA → • a

I7:S‘ → S •

I2:S → A • BS → A • cA → A • aB → • bB → •

I1:A → a • I6:

S → A B •

I5:B → b •

I4:A → A a •

I3:S → A c •S

aA

c

a

b

B

Page 24: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

The complete parsing table

24

StatesACTION GOTO

a b c $ S A B

0 s1 7 2

1 r4 r4 r4 r4

2 s4 s5 s3 r6 6

3 r2

4 r3 r3 r3 r3

5 r5

6 r1

7 acc

Page 25: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Exercise (3)Construct a parsing table for

the grammar here to the right.

Recall: you need to1. compute the canonical

collection of sets of items.2. compute FOLLOW for all

nonterminals in the grammar.

3. insert actions into the parsing table.

(0) S' → S(1) S → h B e(2) B → B A(3) B → ε(4) A → x(5) A → t

25

Page 26: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Shift/reduce conflicts

• Grammar:stmt → if ( expr ) stmt

| if ( expr ) stmt else stmt | other

• Problematic configuration:Stack Input$ … if ( expr ) stmt else … $ 26

Page 27: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Reduce/reduce conflicts• Grammar:stmt → id ( param_list )stmt → expr := exprparam_list → param_list , paramparam_list → paramparam → idexpr → id ( expr_list )expr → idexpr_list → expr_list , exprexpr_list → expr

• Problematic configuration:

Stack Input

$ … id ( id , id ) …

27

Page 28: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Conclusion– LR(0) items– Canonical LR(0) collections– LR(0) automata

28

Page 29: Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Next time

• Parser generator tools• Syntax-directed definitions/translation• Abstract syntax trees

29