240 likes | 440 Views
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.
E N D
Lesson 8 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg
Outline • Bottom-up parsing: • Derivations and reductions • Shift-reduce parsing • LR parsing
Derivations and reductions • Grammar: E → E + T | T T → T * F | F F → ( E ) | id
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
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
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
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
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.
Shift-reduce parsers • Performs a leftmost reduction • Morepowerfulthantop-down parsers • Commonly generated by parser generators
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
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
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.
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
Why LR parsing? • LR(k) morepowerfulthan LL(k) • Efficient • Earlydetection of syntax errors
Overview of LR parsing • Table-driven • Shiftsstatesonto the stack • One staterepresents: symbol + context
(1) E → E + T (2) E → T (3) T → T * F (4) T → F (5) F → ( E ) (6) F → id
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
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
Exercise (3) Parse the string “hxe”. (1) S → h B e (2) B → B A (3) B → ε (4) A → x (5) A → t
Constructing the parsing table • Severalmethods: • Simple LR (SLR) • Canonical LR • LookAhead LR (LALR) • Next time: SLR
Conclusion • Bottom-up parsing vs. top-down parsing • Derivations and reductions • Sentential forms • Handles • Shift-reduce parsing • LR parsing
Next time • Creating LR parsing tablesusing the simple LR method