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

Lesson 8

  • Upload
    caine

  • View
    43

  • Download
    1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Lesson 8

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

2

Outline

• Bottom-up parsing:– Derivations and reductions– Shift-reduce parsing– LR parsing

BOTTOM-UP PARSING

3

Derivations and reductions

• Grammar:E → E + T | TT → T * F | FF → ( E ) | id

4

Derivations and reductions

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

E ⇒ T ⇒ T * F ⇒ F * F ⇒ id * F ⇒ id * id

Derivations and reductions

• Leftmost derivation, E *⇒ lm id * id:E ⇒ T ⇒ T * F ⇒ F * F ⇒ id * F ⇒ id * id

• Rightmost derivation, E *⇒ rm id * id:E ⇒ T T * ⇒ F ⇒ T * id ⇒ F * id ⇒ id * id

6

Derivations and reductions

• Reduction = reverse derivation• Rightmost derivation:E ⇒ T T * ⇒ F ⇒ T * id ⇒ F * id ⇒ id * id• Leftmost reduction:id * id ⇐ F * id T * ⇐ id ⇐ T * F ⇐ T E⇐

7

Handles

8

Reduction Handleid * id ⇐ F * id

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

F → idT → FF → idT → T * F E → T

Derivations and reductions

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

id * id

F

T

F

T

E

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

Exercise (1)

Given the previous expression grammar,E → E + T | TT → T * F | FF → ( E ) | id

a) write down the leftmost reduction of the string (id + id).

b) write down the handles in all the sentential forms along the reduction. 10

Shift-reduce parsers

• Performs a leftmost reduction• More powerful than top-down parsers• Commonly generated by parser generators

11

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

12

Shift-reduce parsing in actionStack InputAction$ 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

13id * 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 | TT → T * F | FF → ( E ) | id

Tip: reuse your leftmost reduction from the previous exercise.

14

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

15

Why LR parsing?

• LR(k) more powerful than LL(k)• Efficient• Early detection of syntax errors

16

Overview of LR parsing

• Table-driven• Shifts states onto the stack

– One state represents: symbol + context

17

(1) E → E + T

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

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

18

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

LR parsing algorithmpush state 0repeat until success:

s ← state on stack top a ← lookahead tokencase ACTION[s, a] ofshift t:

consume apush t

reduce by A → β:pop |β| statest ← state on stack toppush GOTO[t, A]

accept:success ← true

empty:handle error

19

LR parsing in actionStack InputAction$ 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

20

Exercise (3)Parse the string

“hxe”.

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

21

StateACTION GOTO

h x t e $ S B A0 s2 11 acc2 r3 r3 r3 33 s6 s7 s4 54 r15 r2 r2 r26 r4 r4 r47 r5 r5 r5

Constructing the parsing table

• Several methods:– Simple LR (SLR)– Canonical LR– LookAhead LR (LALR)

• Next time: SLR

22

Conclusion

• Bottom-up parsing vs. top-down parsing• Derivations and reductions

– Sentential forms– Handles

• Shift-reduce parsing• LR parsing

23

Next time

• Creating LR parsing tables using the simple LR method

24