Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."— Presentation transcript:

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

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

3 CREATING SLR PARSING TABLES 3

4 Repetition: Shift-reduce parsing StackInputAction $ 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 4 id * id ⇐ F * id ⇐ T * id ⇐ T * F ⇐ T ⇐ E

5 (1)E → E + T (2)E → T (3)T → T * F (4)T → F (5)F → ( E ) (6)F → id 5 State ACTIONGOTO id+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5

6 Repetition: LR parsing StackInputAction $ 0id * id $sh. 5 $ 0 5 id * id $red. by (6) F → id $ 0 3 F * id $red. by (4) T → F $ 0 2 T * id $sh. 7 $ 0 2 T 7 * id $sh. 5 $ 0 2 T 7 * 5 id $red. by (6) F → id $ 0 2 T 7 * 10 F $red. by (3) T → T * F $ 0 2 T $red. by (2) E → T $ 0 1 E $accept 6

7 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

8 LR(0) items Grammar: decl→ type id ; | type id [ num ] ; type → int | float | class class → id 8

9 LR(0) items Set of items I 1 : decl→ type id ; | type id [ num ] ; type → int | float | class class → id 9

10 Computing CLOSURE CLOSURE(I) J = I repeat foreach item in J if it has the form A → αBβ add all B → γ to J until no more items were added in an iteration return J 10

11 The GOTO function GOTO(I 1, int) = I 2 : type → int GOTO(I 1, float) = I 3 : type → float GOTO(I 1, id) = I 4 : class → id 11

12 The GOTO function GOTO(I 1, type) = I 5 : decl→ type id ; | type id [ num ] ; GOTO(I 1, class) = I 6 type → class 12

13 Computing GOTO GOTO(I, X) J = Ø foreach item A → αXβ in I add A → αXβ to J return CLOSURE(J) 13

14 Reductions State: type → int Pop one state –I 1 appears on the stack Push type and go to GOTO(I 1, type) 14

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

16 LR(0) automata Grammar: S → A B S → A c A → A a A → a B → b B→ ε Augmented grammar: S' → S S → A B S → A c A → A a A → a B → b B→ ε 16

17 LR(0) automata 17 I 0 : S‘ → S S → A B S → A c A → A a A → a I 7 : S‘ → S I 2 : S → A B S → A c A → A a B → b B → I 1 : A → a I 6 : S → A B I 5 : B → b I 4 : A → A a I 3 : S → A c S a A c a b B

18 Computing the canonical collection of sets of items items(G') C = CLOSURE({ S' → S }) repeat foreach item I in C foreach grammar symbol X if GOTO(I, X) is non-empty, add it to C until no more sets were added in an iteration return C 18

19 Exercise (1) Compute a)CLOSURE({ F → id }) b)CLOSURE({ S' → E }) c)CLOSURE({ E → T E’ }) S' → E E → T E' E' → + T E’ | ε T → F T' T' → * F T’ | ε F → ( E ) | id 19

20 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' → E E → T E' E' → + T E’ | ε T → F T' T' → * F T’ | ε F → ( E ) | id 20

21 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

22 Constructing the parsing table table(C) foreach set of item I i in C if I i has an item [A → αaβ], where a is a terminal set ACTION[i, a] to a “shift j”, where j is the state representing I j = GOTO(I i, a) if I i has an item [A → αBβ] set GOTO[i, B] to j, where j is the state representing I j = GOTO(I i, B) if I i has an item [A → α] set ACTION[i, a] to “red. by. A → α” for all a in FOLLOW(A) if I i has the item [S' → S] set ACTION[i, $] to “accept” 22

23 Constructing the parsing table 23 I 0 : S‘ → S S → A B S → A c A → A a A → a I 7 : S‘ → S I 2 : S → A B S → A c A → A a B → b B → I 1 : A → a I 6 : S → A B I 5 : B → b I 4 : A → A a I 3 : S → A c S a A c a b B

24 The complete parsing table 24 States ACTIONGOTO abc$SAB 0s172 1r4 2s4s5s3r66 3r2 4r3 5r5 6r1 7acc

25 Exercise (3) Construct a parsing table for the grammar here to the right. Recall: you need to 1.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

26 Shift/reduce conflicts Grammar: stmt→ if ( expr ) stmt | if ( expr ) stmt else stmt | other Problematic configuration: StackInput $ … if ( expr ) stmtelse … $ 26

27 Reduce/reduce conflicts Grammar: stmt → id ( param_list ) stmt → expr := expr param_list → param_list, param param_list → param param → id expr → id ( expr_list ) expr → id expr_list → expr_list, expr expr_list → expr Problematic configuration: StackInput $ … id ( id, id ) … 27

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

29 Next time Parser generator tools Syntax-directed definitions/translation Abstract syntax trees 29


Download ppt "Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."

Similar presentations


Ads by Google