Sunteți pe pagina 1din 29

Syntax-Directed Translation

For these two weeks, we shall introduce syntax-directed translation.

Page 1 of 29

Syntax-Directed Definitions
L

We consider the following grammar PRODUCTION 1) L En 2) E E+T 3) E T 4) T E*F 5) T F 6) F (E) 7) F digit


E T T F digit *

E +

n T F

F digit

digit

Page 2 of 29

Attributes for Nonterminals


L

There are attributes for each nonterminal. PRODUCTION 1) L En 2) E E+T 3) E T 4) T E*F 5) T F 6) F (E) 7) F digit
val=15

val=19 val=19

n T F

E T

val=4

val=15

val=4

val=3

T F

val=5

lexval=4

digit

val=3

lexval=5

digit

lexval=3

digit

Page 3 of 29

Semantic Rules for Productions


L

There are semantic rules for productions. PRODUCTION 1) L En 2) E E+T 3) E T 4) T E*F 5) T F 6) F (E) 7) F digit SEMANTIC RULES L.val = E.val E.val = E.val + T.val E.val = T.val T.val = E.val F.val T.val = F.val F.val = E.val F.val = digit.lexval
val=15

val=19 val=19

n T F

E T

val=4

val=15

val=4

val=3

T F

val=5

lexval=4

digit

val=3

lexval=5

digit

lexval=3

digit

Page 4 of 29

Semantic Rules for Productions


L

There are semantic rules for productions. PRODUCTION 1) L En 2) E E1+T 3) E T 4) T T1*F 5) T F 6) F (E) 7) F digit SEMANTIC RULES L.val = E.val E.val = E1.val + T.val E.val = T.val T.val = T1.val F.val T.val = F.val F.val = E.val F.val = digit.lexval
val=15

val=19 val=19

n T F

E T

val=4

val=15

val=4

val=3

T F

val=5

lexval=4

digit

val=3

lexval=5

digit

lexval=3

digit

Page 5 of 29

Synthesised and Inherited Attributes


A synthesised attribute at node N is defined only in terms of attribute values at the children of N and at N itself.

N.x = N.y * C1.x + C2.y N.y = C1.x + C2.y

C1

Cn

Page 6 of 29

Synthesised and Inherited Attributes


An inherited attribute at node N is defined only in terms of attribute values at Ns parent, N itself, and Ns siblings.

N.x = P.y * S1.x + N.y N.y = S1.x + S2.y

S1

Sn

Page 7 of 29

S-Attributed SDD
A syntax-directed definition (SDD) that involves only synthesised attributes is called S-attributed.

Page 8 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult.

Page 9 of 29

Dependency Graphs
E.val is dependent on E1.val and T.val.

Page 10 of 29

Dependency Graphs
We do NOT say that E is dependent on E1 and T.

Page 11 of 29

Dependency Graphs

Page 12 of 29

Dependency Graphs

Page 13 of 29

Dependency Graphs
The dependency graph characterises the possible order of attribute evaluation (if there is no cycle).

Page 14 of 29

S-Attributed SDD
A syntax-directed definition (SDD) that involves only synthesised attributes is called S-attributed.

Page 15 of 29

S-Attributed SDDs
A syntax-directed definition (SDD) that involves only synthesised attributes is called S-attributed. An SDD is S-attributed if every attribute is synthesised. (Note the direction.)

Page 16 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 17 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 18 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 19 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 20 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 21 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 22 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 23 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 24 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 25 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 26 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 27 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 28 of 29

Evaluating an Annotated Parse Tree


The evaluation of an annotated parse tree is not difficult. (E.g., postorder traversal.)

Page 29 of 29

S-ar putea să vă placă și