1 / 24

Lesson 8

Lesson 8. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. Bottom-up parsing: Derivations and reductions Shift-reduce parsing LR parsing. Bottom-up parsing. Derivations and reductions. Grammar : E → E + T | T T → T * F | F F → ( E ) | id.

caine
Download Presentation

Lesson 8

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 8 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg

  2. Outline • Bottom-up parsing: • Derivations and reductions • Shift-reduce parsing • LR parsing

  3. Bottom-up parsing

  4. Derivations and reductions • Grammar: E → E + T | T T → T * F | F F → ( E ) | id

  5. Derivations and reductions E E E E E E T T T T T T T T T T T T F F F F F F F F F F F F id * id id * id id * id id * id id * id id * id E ⇒ T ⇒ T * F ⇒ F * F ⇒ id * F ⇒ id * id

  6. Derivations and reductions • Leftmost derivation, E ⇒*lmid * id: E ⇒ T ⇒ T * F ⇒ F * F ⇒ id * F ⇒ id * id • Rightmost derivation, E ⇒*rmid * id: E ⇒ T ⇒ T * F ⇒ T * id ⇒ F * id ⇒ id * id

  7. Derivations and reductions • Reduction = reverse derivation • Rightmost derivation: E ⇒ T ⇒ T * F ⇒ T * id ⇒ F * id ⇒ id * id • Leftmostreduction: id * id ⇐ F * id ⇐ T * id ⇐ T * F ⇐ T ⇐ E

  8. Handles

  9. Derivations and reductions E E E E E E T T T T T T T T T T T T F F F F F F F F F F F F id * id id * id id * id id * id id * id id * id id * id ⇐ F * id ⇐ T * id ⇐ T * F ⇐ T ⇐ E

  10. Exercise (1) Given the previous expression grammar, E → E + T | T T → T * F | F F → ( E ) | id • write down the leftmost reduction of the string (id + id). • write down the handles in all the sentential forms along the reduction.

  11. Shift-reduce parsers • Performs a leftmost reduction • Morepowerfulthantop-down parsers • Commonly generated by parser generators

  12. Shift-reduce parsers • Four actions: • Shift: consume and “shift” a terminal onto a stack • Reduce: pop a handle from the stack and push the nonterminal • Accept • Error

  13. Shift-reduce parsing in action Stack 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 id * id ⇐ F * id ⇐ T * id ⇐ T * F ⇐ T ⇐ E

  14. Exercise (2) Similar to the previous demonstration, do a shift-reduce parse of the same string,(id + id), as in the previous exercise, using the same grammar: E → E + T | T T → T * F | F F → ( E ) | id Tip: reuse your leftmost reduction from the previous exercise.

  15. LR(k) parsing • Implementation of shift-reduce parsing • Left-to-right scanning of the input,Rightmost derivation in reverse • k = 0 or k = 1: nr of lookahead tokens

  16. Why LR parsing? • LR(k) morepowerfulthan LL(k) • Efficient • Earlydetection of syntax errors

  17. Overview of LR parsing • Table-driven • Shiftsstatesonto the stack • One staterepresents: symbol + context

  18. (1) E → E + T (2) E → T (3) T → T * F (4) T → F (5) F → ( E ) (6) F → id

  19. LR parsing algorithm pushstate 0 repeatuntilsuccess: s ← state on stack top a ← lookahead token caseaction[s, a] of shift t: consume a push t reduce by A → β: pop |β| states t ← state on stack top pushgoto[t, A] accept: success ← true empty: handleerror

  20. LR parsing in action Stack 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

  21. Exercise (3) Parse the string “hxe”. (1) S → h B e (2) B → B A (3) B → ε (4) A → x (5) A → t

  22. Constructing the parsing table • Severalmethods: • Simple LR (SLR) • Canonical LR • LookAhead LR (LALR) • Next time: SLR

  23. Conclusion • Bottom-up parsing vs. top-down parsing • Derivations and reductions • Sentential forms • Handles • Shift-reduce parsing • LR parsing

  24. Next time • Creating LR parsing tablesusing the simple LR method

More Related