Lecture 9

23
Joey Paquet, 2000-2014 1 Lecture 9 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)

description

Lecture 9. Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up). Part I. Top-Down Table-Driven Syntax-Directed Translation. Top-Down Table-Driven SDT. Augment the parser algorithm to implement attribute migration - PowerPoint PPT Presentation

Transcript of Lecture 9

Page 1: Lecture 9

Joey Paquet, 2000-2014 1

Lecture 9

Table-Driven Syntax-Directed Translation

(Top-Down and Bottom-Up)

Page 2: Lecture 9

Joey Paquet, 2000-2014 2

Part I

Top-Down Table-Driven Syntax-Directed Translation

Page 3: Lecture 9

Joey Paquet, 2000-2014 3

Top-Down Table-Driven SDT

• Augment the parser algorithm to implement attribute migration– Introduce additional symbols in the grammar’s right

hand sides for semantic actions that process semantic attributes

– When such a symbol is on top of the stack, execute the semantic action

• Problem: the attributes have to be pushed and popped at a different pace compared to symbols on the parsing stack

• Solution: use an additional stack (the semantic stack) to store the attributes

Page 4: Lecture 9

Joey Paquet, 2000-2014 4

Top-Down Table-Driven SDT

r1: E TAQB

r2: Q1 +TCQ2D

r3: Q E

r4: T FFRG

r5: R1 *FHR2I

r6: R J

r7: F idK

r8: F (E)L

A: {Qi = Ts}

B: {Es = Qs}

C: {Q2i = Q1i+Ts}

D: {Q1s = Q2s}

E: {Qs = Qi}

F: {Ri = Fs}

G: {Ts = Rs}

H: {R2i = R1i * Fs}

I: {R1s = R2s}

J: {Rs = Ri}

K: {Fs = id.val}

L: {Fs = Es}

id ( ) + * $

E r1 r1

Q r3 r2 r3

T r4 r4

R r6 r6 r5 r6

F r7 r8

Page 5: Lecture 9

Joey Paquet, 2000-2014 5

Attribute migration

E

T

T

R

*

F

id

R

F

id

Q

R

F

id

Q

+

K:{Fs = id.val}

F:{Ri = Fs}

J:{Rs = Ri}

G:{Ts = Rs}

A:{Qi = Ts}

B:{Es = Qs}

D:{Qs = Qs}

C:{Qi = Qi+Ts}

E:{Qs = Qi}

I:{Rs = Rs}

J:{Rs = Ri}K:{Fs = id.val}

H:{Ri = Ri * Fs}

F:{Ri = Fs}

K:{Fs = id.val}

G:{Ts = Rs}

id1+id2*id3$

Page 6: Lecture 9

Joey Paquet, 2000-2014 6

Attribute migration

E

T

T

R

*

F

idF

id

Q

R

F

id

Q

+

K:{Fs = id.val}

F:{Ri = Fs}

G:{Ts = Rs}

A:{Qi = Ts}

B:{Es = Qs}

D:{Qs = Qs}

C:{Qi = Qi+Ts}

E:{Qs = Qi}

I:{Rs = Rs}

J:{Rs = Ri}K:{Fs = id.val}

H:{Ri = Ri * Fs}F:{Ri = Fs}

K:{Fs = id.val}

G:{Ts = Rs}

R

J:{Rs = Ri}

id1*id2+id3$

Page 7: Lecture 9

Joey Paquet, 2000-2014 7

Pars

ing E

xam

ple

parsing stack input action semantic stack

1 $E id1*id2+id3$ r1

2 $BQAT id1*id2+id3$ r4

3 $BQAGRFF id1*id2+id3$ r7

4 $BQAGRFKid id1*id2+id3$

5 $BQAGRFK *id2+id3$ K F1s[id1.val]

6 $BQAGRF *id2+id3$ F R1i[id1.val]

7 $BQAGR *id2+id3$ r5 R1i[id1.val]

8 $BQAGIRHF* *id2+id3$ R1i[id1.val]

9 $BQAGIRHF id2+id3$ r7 R1i[id1.val]

10 $BQAGIRHKid id2+id3$ R1i[id1.val]

11 $BQAGIRHK +id3$ K R1i[id1.val] F2s[id2.val]

12 $BQAGIRH +id3$ H R2i[id1.val * id2.val]

13 $BQAGIR +id3$ r6 R2i[id1.val * id2.val]

14 $BQAGIJ +id3$ J R2s[id1.val * id2.val]

15 $BQAGI +id3$ I R1s[id1.val * id2.val]

16 $BQAG +id3$ G T1s[id1.val * id2.val]

17 $BQA +id3$ A Q1i[id1.val * id2.val]

Page 8: Lecture 9

Joey Paquet, 2000-2014 8

Pars

ing E

xam

ple

parsing stack input action semantic stack

18 $BQ +id3$ r2 Q1i[id1.val * id2.val]

19 $BDQCT+ +id3$ Q1i[id1.val * id2.val]

20 $BDQCT id3$ r4 Q1i[id1.val * id2.val]

21 $BDQCGRFF id3$ r7 Q1i[id1.val * id2.val]

22 $BDQCGRFKid id3$ Q1i[id1.val * id2.val]

23 $BDQCGRFK $ K Q1i[id1.val * id2.val] F3s[id3.val]

24 $BDQCGRF $ F Q1i[id1.val * id2.val] R3i[id3.val]

25 $BDQCGR $ r6 Q1i[id1.val * id2.val] R3i[id3.val]

26 $BDQCGJ $ J Q1i[id1.val * id2.val] R3s[id3.val]

27 $BDQCG $ G Q1i[id1.val * id2.val] T2s[id3.val]

28 $BDQC $ C Q2i[id1.val * id2.val + id3.val]

29 $BDQ $ r3 Q2i[id1.val * id2.val + id3.val]

30 $BDE $ E Q2s[id1.val * id2.val + id3.val]

31 $BD $ D Q1s[id1.val * id2.val + id3.val]

32 $B $ B E1s[id1.val * id2.val + id3.val]

33 $ $ accept E1s[id1.val * id2.val + id3.val]

Page 9: Lecture 9

Joey Paquet, 2000-2014 9

Part II

Top-Down Syntax-Directed TranslationGrammar Transformation

Page 10: Lecture 9

Joey Paquet, 2000-2014 10

Top-Down Translation

• Problem: Left recursion is not allowed in predictive top-down parsing. We must transform the grammar. We saw how to transform a context-free grammar to remove left recursions. How is an attribute grammar transformed?

Page 11: Lecture 9

Joey Paquet, 2000-2014 11

Top-Down Translation: Problem

E

T1

T2

T’2

*

F1

id (va : )

T’1

F2

id (vb : )

E’2

T’3

F3

id (vc : )

E’1

+

a+b*c

E TE’E’ +TE’ | T FT’T’ FT’ | F id

Page 12: Lecture 9

With Left Recursions

Joey Paquet, 2000-2014 12

E

E

F

id (va : )

T

F

id (vb : )

+

{Fs = lookup(a)}

{Ts = Fs}

{Es = Ts} T

T{Es = Ts}

{Fs = lookup(b)}

* F

id (vc : )

{Fs = lookup(c)}

{Ts = Ts * Fs}

{Es = Es * Ts}

E E+T {Es = Es*Ts}E T {Es = Ts}T T*F {Ts = Ts*Fs}T F {Ts = Fs}F id {Fs = lookup(id)}

a+b*c

Page 13: Lecture 9

Joey Paquet, 2000-2014 13

E

T

T

T’

*

F

id (va : )

T’

F

id (vb : )

E’

T’

F

id (vc : )

E’

+

E T{E’i=Ts}E’{Es=E’s}E’ +T{E’i=Ts}E’{E’s=E’i+E’s}E’ {E’s=E’i}T F{T’i=Fs}T’{Ts=T’s}T’ F{T’i=Fs}T’ {T’s=Ti*T’s}T’ {T’s=Ti}F id{Fs=lookup(id)}

{Fs = lookup(a)}

{Fs = lookup(b)}

{Fs = lookup(c)}

{E’i = Ts}

{T’i = Fs} {T’s = Ti}

{Ts = T’s}

{T’i = Fs}{T’s = Ti}

{T’i = Fs} {T’s = Ti*T’s}

{Ts = T’s}{E’i = Ts}

{E’s = E’i}

{E’s = E’i+E’s}

{Es = E’s}

Without Left Recursionsa+b*c

Page 14: Lecture 9

Joey Paquet, 2000-2014 14

Top-Down Translation

• Solution: The grammar is transformed as we saw before. But changing the grammar spreads some attributes over multiple rules, thus introducing attribute inheritance. The following transformation should be applied:

A1 A2 A1 Q1

A3 Q2 Q3

Q4

A1 A2 {A1s = f(A2s, )} A1 {Q1i = g()}Q1{A1s = Q1s}A3 {A3s = g()} Q2 {Q3i = f(Q2i, )}Q3{Q2s = Q3s}

Q4 {Q4s = Q4i}

Page 15: Lecture 9

Joey Paquet, 2000-2014 15

Top-Down Translation: Example

E1 E2+T1 {E1s = E2s+T1s} E1 T2{E1’i = T2s}E1’{E1s = E1’s}E3 T2 {E3s = T2s} E2’ +T1{E3’i = E2’i+T1s}E3’{E2’s = E3’s}

E4’ {E4’s = E4’i}

E1 E2+T1 E1 T2E1’E3 T2 E2’ +T1E3’

E4’

where:A1,2,3 E1,2,3

Q1,2,3,4 E’1,2,3,4 +T1

T2

f(A2s, ) E2s+T1sg() T2s

A1 A2 A1 Q1

A3 Q2 Q3

Q4

A1 A2 {A1s = f(A2s, )} A1 {Q1i = g()}Q1{A1s = Q1s}A3 {A3s = g()} Q2 {Q3i = f(Q2i, )}Q3{Q2s = Q3s}

Q4 {Q4s = Q4i}

Page 16: Lecture 9

Joey Paquet, 2000-2014 16

Part III

Bottom-Up Table-Driven Syntax-Directed Translation

Page 17: Lecture 9

Joey Paquet, 2000-2014 17

Bottom-Up SDT • Syntax-directed translation is much easier to implement

bottom-up than top-down.• Synthetized attributes are propagated from the bottom-

up, so we have this propagation mechanism for free in a bottom-up parse

• The presence of inherited attributes generally comes from the elimination of left-recursions and ambiguities. As these are not a problem in bottom-up parsing, we seldom need to process inherited attributes in bottom-up translation

• In bottom-up translation, the parse and semantic stacks move in synchronism.

• Semantic rules are triggered as handles are popped from the stack

Page 18: Lecture 9

Joey Paquet, 2000-2014 18

Bottom-Up SDT: Building a Tree• The tree is built by grafting subtrees (handles) to the

nodes• The semantic actions build the subtrees by grafting

generally through simple pointer manipulations• Generally, all nodes (internal or leaves) are of the same

type. Differences can be managed by using a variant record structure:

nodeKind = (internal,leaf)treeNode = record token : tokenType case kind : nodeKind of internal : (left,right : *treeNode) leaf : (location : integer)

end

Page 19: Lecture 9

Joey Paquet, 2000-2014 19

Bottom-Up SDT: Building a Tree• We need a makeTree function that will be used to

create subtrees and a makeLeaf function that will be

used to create tree leaves: nodePtr makeTree(op tokenType, rightSon,leftSon nodePtr){ rootPtr = new(nodePtr) rootPtr.kind = internal rootPtr.token = op rootPtr.left = leftSon rootPtr.right = rightSon return rootPtr}

nodePtr makeLeaf(tok tokenType, location integer{ leafPtr = new(nodePtr) leafPtr.kind = leaf leafPtr.token = tok leafPtr.location = location return leafPtr}

Page 20: Lecture 9

Joey Paquet, 2000-2014 20

Bottom-Up SDT: Example

r1: S id = EA

r2: E1 E2 + E3B

r3: E1 E2 * E3C

r4: E1 (E2)D

r5: E idE

A: {S.root = makeTree(‘=‘, id.location, E.root)}

B: {E1.root = makeTree(‘+’, E2.root, E3.root)}

C: {E1.root = makeTree(‘*’, E2.root, E3.root)}

D: {E1.root = E2.root}

E: {E1.root = makeLeaf(id, id.location)}

Page 21: Lecture 9

Joey Paquet, 2000-2014 21

Pars

ing E

xam

ple

parsing stack input action

1 $ id = (id+id)*id$

2 $id = (id+id)*id$

3 $id= (id+id)*id$

4 $id=( id+id)*id$

5 $id=(id +id)*id$ r5 E

6 $id=(E1 +id)*id$

7 $id=(E1 + id)*id$

8 $id=(E1 + id )*id$ r5 E

9 $id=(E1 + E2 )*id$

10 $id=(E1 + E2) *id$ r2 B

11 $id=(E3) *id$ r4 D

12 $id=E4 *id$

13 $id=E4* id$

14 $id=E4* id $ r5 E

15 $id=E4* E5 $ r3 C

16 $id=E6 $ r1 A

17 $S $ accept

Page 22: Lecture 9

Joey Paquet, 2000-2014 22

E1

id1

E2

id2

E3

E1E2

+

E3

+

id1 id2

E4

+

id1 id2

E5

id3

E6

E4E5

*

+

id1 id2

E6

*

id3

+

id1 id2

*

id3

=

S

id4

A

B

C

DE

5 8 10 11

14 15 16

E

E

Page 23: Lecture 9

Joey Paquet, 2000-2014 23

Bottom-Up SDT• We can use a similar process to build other kinds of

intermediate representations• A similar process can also be used to generate target

code directly, but that diminishes the possibilities of high-level code optimization