1 / 29

Lesson 9

Lesson 9. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. Creating SLR parsing tables LR(0) items Canonical LR(0) collections LR(0) automata. Creating SLR parsing tables. Repetition: Shift-reduce parsing. Stack Input Action $ id * id $ sh. id

cree
Download Presentation

Lesson 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lesson 9 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg

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

  3. Creating SLR parsing tables

  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 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

  6. Repetition: LR parsing StackInputAction $ 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

  7. LR(0) items • One staterepresents: 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

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

  9. LR(0) items • Set of items I1: decl → • typeid ; | • typeid [ num ] ; type → • int | • float | • class class → • id

  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

  11. The GOTO function • GOTO(I1, int) = I2: type → int • • GOTO(I1, float) = I3: type → float• • GOTO(I1, id) = I4: class→ id •

  12. The GOTO function • GOTO(I1, type) = I5: decl → type • id ; | type • id [ num ] ; • GOTO(I1, class) = I6 type → class •

  13. ComputingGOTO GOTO(I, X) J = Ø foreachitem A → α•Xβ in I add A → αX•β to J returnCLOSURE(J)

  14. Reductions • State: type → int • • Pop one state • I1 appears on the stack • Pushtype and go to GOTO(I1, type)

  15. LR(0) automata • Based on the canonical LR(0) collection • States represent sets of items • Transitionsdefined by the GOTOfunction

  16. LR(0) automata • Grammar: S → A B S → A c A → A a A → a B → b B→ ε • Augmentedgrammar: S' → S S → A B S → A c A → A a A → a B → b B→ ε

  17. LR(0) automata I0: S‘ → • S S → • A B S → • A c A → • A a A → • a I7: S‘ → S • I3: S → A c • S c I4: A → A a • a A I2: S → A • B S → A • c A → A • a B → • b B → • a I5: B → b • b I1: A → a • B I6: S → A B •

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

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

  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

  21. Constructing the parsing table • 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→ ε • Compute FOLLOW: FOLLOW(S') = { $ } FOLLOW(S) = { $ } FOLLOW(A) = { a, b, c, $ } FOLLOW(B) = { $ }

  22. Constructing the parsing table table(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”

  23. Constructing the parsing table I0: S‘ → • S S → • A B S → • A c A → • A a A → • a I7: S‘ → S • I3: S → A c • S c I4: A → A a • a A I2: S → A • B S → A • c A → A • a B → • b B → • a I5: B → b • b I1: A → a • B I6: S → A B •

  24. The complete parsing table

  25. Exercise (3) Construct a parsing table for the grammar here to the right. Recall: you need to • compute the canonical collection of sets of items. • compute FOLLOW for all nonterminals in the grammar. • 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

  26. Shift/reduceconflicts • Grammar: stmt→ if ( expr ) stmt | if ( expr ) stmtelsestmt | other • Problematicconfiguration: StackInput $ … if ( expr ) stmtelse … $

  27. Reduce/reduceconflicts • 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 • Problematicconfiguration: StackInput $ … id ( id , id ) …

  28. Conclusion • LR(0) items • Canonical LR(0) collections • LR(0) automata

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

More Related