4. syntax-directed translation · when translation is defined in terms ofsemantic actions, the...

75
Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS 4. Syntax-Directed Translation Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA.2130-001) http://cs.nyu.edu/courses/fall14/CSCI-GA.2130-001/lecture-4.pdf September 25, 2014 Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 1 / 62

Upload: others

Post on 03-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

4. Syntax-Directed Translation

Eva Rose Kristoffer Rose

NYU Courant InstituteCompiler Construction (CSCI-GA.2130-001)

http://cs.nyu.edu/courses/fall14/CSCI-GA.2130-001/lecture-4.pdf

September 25, 2014

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 1 / 62

Page 2: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

1 Introduction

2 SDT: Syntax-Directed Translation

3 SDD: Syntax-Directed Definition

4 HACSReviewS-attributed SDDsRecursive Translation Schemes

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 2 / 62

Page 3: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

1 Introduction

2 SDT: Syntax-Directed Translation

3 SDD: Syntax-Directed Definition

4 HACSReviewS-attributed SDDsRecursive Translation Schemes

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 3 / 62

Page 4: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Contextsource program

Lexical Analysis--

Syntax AnalysisTokens

--

Semantic AnalysisTree

--

Intermediate Representation GeneratorTree

--

SymbolTable

OptimizerIR--

Code GeneratorIR--

Machine-Dependent Code OptimizerIR--

target machine code��

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 4 / 62

Page 5: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

The Trees

TREE INTERIOR NODES GRAMMAR

parse tree nonterminals concrete syntaxabstract syntax tree programming constructs abstract syntax

Example (abstract syntax)+

9 5

2 E→ E + E | E− E | digit

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 5 / 62

Page 6: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

The Trees

TREE INTERIOR NODES GRAMMAR

parse tree nonterminals concrete syntaxabstract syntax tree programming constructs abstract syntax

Example (abstract syntax)+

9 5

2 E→ E + E | E− E | digit

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 5 / 62

Page 7: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Back to the introductory example...

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 6 / 62

Page 8: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

From Abstract Syntax Tree (AST)

position = initial + rate * 60

parsed into abstract syntax tree:

=

〈id,1〉 +

〈id,2〉 ∗〈id,3〉 〈num,60〉

id lexeme1 position2 initial3 rate

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 7 / 62

Page 9: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

. . . to Annotated AST. . .

= t = float

〈id,1〉

t = float

+ t = float

〈id,2〉

t = float

∗ t = float

〈id,3〉

t = float

〈num,60〉

t = int

id lexeme t1 position float2 initial float3 rate float

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 8 / 62

Page 10: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

. . . to “Fixed” AST

= t = float

〈id,1〉

t = float

+ t = float

〈id,2〉

t = float

∗ t = float

〈id,3〉

t = float

int-to-float t = float

〈num,60〉 t = int

id lexeme t1 position float2 initial float3 rate float

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 9 / 62

Page 11: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

1 Introduction

2 SDT: Syntax-Directed Translation

3 SDD: Syntax-Directed Definition

4 HACSReviewS-attributed SDDsRecursive Translation Schemes

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 10 / 62

Page 12: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Syntax-Directed Translation

We have built a parse (or AST) tree, now what? How will thistree and production rules help the translation?

Intuitively, we need to associate something with eachproduction and each tree node:

I something that evaluates the meaning at each node,I something that emits the meaning as program fragments.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 11 / 62

Page 13: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Syntax-Directed Translation

We have built a parse (or AST) tree, now what? How will thistree and production rules help the translation?

Intuitively, we need to associate something with eachproduction and each tree node:

I something that evaluates the meaning at each node,I something that emits the meaning as program fragments.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 11 / 62

Page 14: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Syntax-Directed Definition (SDD)

Attributes Each grammar symbol (terminal or nonterminal)has an attribute (“meaning”) associated.

Semantic Rules Each production has semantic rules associatedfor computing the attributes.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 12 / 62

Page 15: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: SDD for Infix to Postfix Notation

Consider the grammar:

expr→ expr + term| expr− term| term

term→ 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

expr has an attribute expr.t, term has an attribute term.t.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 13 / 62

Page 16: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: SDD Infix to Postfix Notation

PRODUCTION SEMANTIC RULE

expr→ expr1 + term2 expr.t = expr1.t || term2.t || ′+′expr→ expr1 − term2 expr.t = expr1.t || term2.t || ′−′expr→ term1 expr.t = term1.tterm→ 0 term.t =′ 0′

term→ 1 term.t =′ 1′

. . . . . .term→ 9 term.t =′ 9′

−||− means concatenation, −1, −2 disambiguates

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 14 / 62

Page 17: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Attribute Values in Parse Trees

expr.t = 9 5−2+

expr.t = 9 5−

expr.t = 9

term.t = 9

9

− term.t = 5

5

+ term.t = 2

2

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 15 / 62

Page 18: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Translation Schemes

I Equivalent mechanism.I Attaching program fragments to productions.I The program fragments are called semantic

actions/side-effects.I They “emit” the program fragments during “tree traversal”.

Example (rest→ +term {print(′+′)} rest1)

rest

+ term {print(′+′)} rest1

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 16 / 62

Page 19: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Translation Schemes

I Equivalent mechanism.I Attaching program fragments to productions.I The program fragments are called semantic

actions/side-effects.I They “emit” the program fragments during “tree traversal”.

Example (rest→ +term {print(′+′)} rest1)

rest

+ term {print(′+′)} rest1

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 16 / 62

Page 20: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Semantic Action Table: Infix to Postfix Notation

PRODUCTIONS WITH SIDE-EFFECTS/ACTIONS

expr→ expr1 + term2 {print(‘+′)}expr→ expr1 − term2 {print(‘−′)}expr→ termterm→ 0 {print(‘0′)}term→ 1 {print(‘1′)}term→ 2 {print(‘2′)}term→ 3 {print(‘3′)}term→ 4 {print(‘4′)}term→ 5 {print(‘5′)}term→ 6 {print(‘6′)}term→ 7 {print(‘7′)}term→ 8 {print(‘8′)}term→ 9 {print(‘9′)}

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 17 / 62

Page 21: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Tree Traversal in Translation Schemes: Depth-First

◦ ◦

__

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 18 / 62

Page 22: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Semantic Actions/Side-Effects in Parse Tree

expr

expr

expr

term

9{print(′9′)}

− term

5{print(′5′)}

{print(′−′)}+ term

2{print(′2′)}

{print(′+′)}

Input: 9− 5 + 2. Output (print): 9 5−2+. Single depth-firsttraversal.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 19 / 62

Page 23: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Semantic Actions/Side-Effects in Parse Tree

expr

expr

expr

term

9{print(′9′)}

− term

5{print(′5′)}

{print(′−′)}+ term

2{print(′2′)}

{print(′+′)}

Input: 9− 5 + 2. Output (print): 9 5−2+. Single depth-firsttraversal.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 19 / 62

Page 24: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Semantic Actions/Side-Effects in Parse Tree

expr

expr

expr

term

9{print(′9′)}

− term

5{print(′5′)}

{print(′−′)}+ term

2{print(′2′)}

{print(′+′)}

Input: 9− 5 + 2. Output (print): 9 5−2+. Single depth-firsttraversal.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 19 / 62

Page 25: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Summaryexpr.t = 95− 2+

expr.t = 95−

expr.t = 9

term.t = 9

9

− term.t = 5

5

+ term.t = 2

2 attributes/semantic rules (HACS)

expr

expr

expr

term

9

{print(′9′)}

− term

5

{print(′5′)}

{print(′−′)}+ term

2

{print(′2′)}

{print(′+′)}

semantic actions/side-effects (yacc)

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 20 / 62

Page 26: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Tree Traversal and Translation Schemes

When translation is defined in terms of semantic actions, thetree traversal order, that is the order in which the nodes arevisited, becomes essential.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 21 / 62

Page 27: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Exercise

Construct an SDD that generates an attribute t in prefixnotation for each expression expr, for arithmetic expressions.(You only need to consider: ‘+’,’−’, and ’∗’ expressions). Prefixnotation is where the operator comes before its operands; e.g.,− x y is the prefix notation for x − y.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 22 / 62

Page 28: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Solution: SDD for Infix Notation into Prefix Notation

PRODUCTION SEMANTIC RULE

expr→ expr1 + term2 expr.t = ′ +′ || expr1.t || term2.texpr→ expr1 − term2 expr.t = ′ −′ || expr1.t || term2.texpr→ expr1 ∗ term2 expr.t = ′ ∗′ || expr1.t || term2.texpr→ term expr.t = term.tterm→ 0 term.t =′ 0′

term→ 1 term.t =′ 1′

. . . . . .term→ 9 term.t =′ 9′

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 23 / 62

Page 29: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

1 Introduction

2 SDT: Syntax-Directed Translation

3 SDD: Syntax-Directed Definition

4 HACSReviewS-attributed SDDsRecursive Translation Schemes

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 24 / 62

Page 30: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Syntax-Directed Definition (SDD)

Recall the key definition:

Grammar Given a context-free grammar.Attributes Each grammar symbol (terminal or nonterminal)

has a set of attributes associated.Semantic Rules Each production has a set of semantic rules

associated for computing the attributes.

If X is a symbol, a is an attribute, then X.a denotes the value ofa at node X.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 25 / 62

Page 31: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Synthesized and Inherited Attributes

Synthesized attributes at node N are defined only in terms ofthe attribute values of the children of N, and N itself.

Inherited attributes at node N is defined only in terms ofattribute values at N’s parent, N itself, and N’ssiblings.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 26 / 62

Page 32: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: SDD for Desk Calculator

PRODUCTION SEMANTIC RULE

1. L→ E $ L.val = E.val2. E→ E1 + T E.val = E1.val + T.val3. E→ T E.val = T.val4. T → T1 ∗ F T.val = T1.val× F.val5. T → F T.val = F.val6. F → (E) F.val = E.val7. F → digit F.val = digit.lexval

‘val’ and ‘lexval’ are synthesized attributes.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 27 / 62

Page 33: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Characteristics of Desk Calculator

I S-attributed (only synthesized attributes)

I attribute grammar (without side-effects)

Order of attribute evaluation: any bottom-up traversal.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 28 / 62

Page 34: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Tree Traversal: Depth-First

◦ ◦

__

Post-order (default). Pre-order. Binary trees: also in-order.Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 29 / 62

Page 35: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Exercise: Desk Calculator

Give annotated parse trees for the following expressions:

I (3 + 4) ∗ (5 + 6) $I 1 ∗ 2 ∗ 3 ∗ (4 + 5) $

(Parse trees showing attributes and their values)

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 30 / 62

Page 36: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Exercise: Mingling with Inherited Attributes. . .

PRODUCTION SEMANTIC RULES

1. T → F T′ T′.inh = F.val; T.val = T′.syn2. T′ → ∗ F T′1 T′1.inh = T′.inh× F.val; T′.syn = T′1.syn3. T′ → ε T′.syn = T′.inh4. F → digit F.val = digit.lexval

‘inh’ is inherited, and ‘val’, ‘syn’ are synthesized.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 31 / 62

Page 37: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Dependency Graphs

Dependency graphs depicts the flow of information amongattributes.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 32 / 62

Page 38: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Attributes

Parse tree for 3 ∗ 5 based on SDD:

T 9 val

F 3 val

digit 1 lexval

T′5inh 8 syn

∗ F 4 val

digit 2 lexval

T′6inh 7 syn

ε

OO

''

))

OO77 ;;

ii

ii

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 33 / 62

Page 39: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Attributes with Dependency Graph

Parse tree for 3 ∗ 5 based on SDD:

T 9 val

F 3 val

digit 1 lexval

T′5inh 8 syn

∗ F 4 val

digit 2 lexval

T′6inh 7 syn

ε

OO

''

))

OO77 ;;

ii

ii

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 33 / 62

Page 40: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Dependency Graphs

Central problem: Determining the efficient evaluation order forthe attribute instances in a given parse tree.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 34 / 62

Page 41: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Calculating Attributes

Cycles possible!

PRODUCTION SEMANTIC RULE

A→ B A.s = B.i; B.i = A.s + 1

When dependency graphs have cycles, evaluation is “stuck”!

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 35 / 62

Page 42: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Well-behaved SDD Classes

Given an SDD, hard to tell if there exists a parse tree whosedependency graphs have cycles!

Safe way to go: use classes of SDDs that guarantee anevaluation order:

S-Attributed Definitions all attributes are synthesized.L-Attributed Definitions “left” restrictions on dependency

graph edges.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 36 / 62

Page 43: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Well-behaved SDD Classes

Given an SDD, hard to tell if there exists a parse tree whosedependency graphs have cycles!

Safe way to go: use classes of SDDs that guarantee anevaluation order:

S-Attributed Definitions all attributes are synthesized.L-Attributed Definitions “left” restrictions on dependency

graph edges.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 36 / 62

Page 44: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

L-Attributed Definitions

Suppose an attribute rule for A→ X1 X2 . . . Xi . . . Xn producesinherited Xi.a attribute. The rule may only use:

I inherited attributes associated with the head (A),I inherited or synthesized attributes associated with the

occurences of symbols X1,X2, . . . ,Xi−1 located left of Xi.I Inherited or synthesized attributes associated with the

occurence of Xi itself, as long as not part of a dependencygraph cycle!

or, the produced attribute is synthesized.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 37 / 62

Page 45: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Mingling with Inherited Attributes. . .

Decide if the SDD is L-attributed and argue :

PRODUCTION SEMANTIC RULES

1. T → F T′ T′.inh = F.val; T.val = T′.syn2. T′ → ∗ F T′1 T′1.inh = T′.inh× F.val; T′.syn = T′1.syn3. T′ → ε T′.syn = T′.inh4. F → digit F.val = digit.lexval

‘inh’ is inherited, and ‘val’, ‘syn’ are synthesized.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 38 / 62

Page 46: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Mingling with Inherited Attributes. . .

Decide if the SDD is L-attributed and argue :

PRODUCTION SEMANTIC RULES

1. T → F T′ T′.inh = F.val; T.val = T′.syn2. T′ → ∗ F T′1 T′1.inh = T′.inh× F.val; T′.syn = T′1.syn3. T′ → ε T′.syn = T′.inh4. F → digit F.val = digit.lexval

‘inh’ is inherited, and ‘val’, ‘syn’ are synthesized.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 39 / 62

Page 47: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Mingling with Inherited Attributes. . .

Decide if the SDD is L-attributed and argue :

PRODUCTION SEMANTIC RULES

1. T → F T′ T′.inh = F.val; T.val = T′.syn2. T′ → ∗ F T′1 T′1.inh = T′.inh× F.val; T′.syn = T′1.syn3. T′ → ε T′.syn = T′.inh4. F → digit F.val = digit.lexval

‘inh’ is inherited, and ‘val’, ‘syn’ are synthesized.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 40 / 62

Page 48: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Another example

The following SDD is NOT L-attributed. Why?

PRODUCTION SEMANTIC RULES

A→ B C A.s = B.bB.i = F(C.c,A.s)

where B.i is inherited, and A.s, C.c are synthesized.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 41 / 62

Page 49: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Another example

The following SDD is NOT L-attributed. Why?

PRODUCTION SEMANTIC RULES

A→ B C A.s = B.bB.i = F(C.c,A.s)

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 42 / 62

Page 50: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Construction of Abstract Syntax Trees

TREE INTERIOR NODES GRAMMAR

parse tree nonterminals concrete syntaxabstract syntax tree programming constructs abstract syntax

Example (abstract syntax)+

9 5

2 E→ E + E | E− E | digit

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 43 / 62

Page 51: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Construction of AST

Example 1: S-attributed SDD for simple expresssions.

PRODUCTION SEMANTIC RULE

1. E→ E1 + T2 E.node = new Node(′+′, E1.node, T2.node)2. E→ E1 − T2 E.node = new Node(′−′, E1.node, T2.node)3. E→ T E.node = T.node4. T → ( E ) T.node = E.node5. T → id T.node = new Leaf(id, id.entry)6. T → num T.node = new Leaf(num,num.entry)

I Show the AST for a− 4 + cEva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 44 / 62

Page 52: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Construction of AST

Example 2: L-attributed SDD for simple expresssions.

PRODUCTION SEMANTIC RULE

1. E→ T E′ E.node = E′.syn; E′.inh = T.node2. E′ → +T E′

1 E′1.inh = new Node(′+′, E′.node, T.node)

E′.syn = E′1.syn

3. E′ → −T E′1 E′

1.inh = new Node(′−′, E′.node, T.node)E′.syn = E′

1.syn4. E′ → ε E′.syn = E′.inh5. T → ( E ) T.node = E.node6. T → id T.node = new Leaf(id, id.entry)7. T → num T.node = new Leaf(num,num.entry)

I Show AST and dependency graph for a− 4 + c

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 45 / 62

Page 53: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Syntax-Directed Translation of Type Expressions

PRODUCTION SEMANTIC RULE

1 T → B C T.syn = C.synC.inh = B.syn

2 B→ int B.syn = integer3 B→ float B.syn = integer4 C→ [num]C1 C.syn = array(num.val, C1.syn)

C1.inh = C.inh5 C→ ε C.syn = C.inh

I Show AST and dependencies for int[2][3]

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 46 / 62

Page 54: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

1 Introduction

2 SDT: Syntax-Directed Translation

3 SDD: Syntax-Directed Definition

4 HACSReviewS-attributed SDDsRecursive Translation Schemes

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 47 / 62

Page 55: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

HACS Review: Lexical Specification

space [ \t\n] ;

token Int | 〈Digit〉+ ;token Float | 〈Int〉 "." 〈Int〉 ;

token fragment Digit | [0−9] ;

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 48 / 62

Page 56: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

HACS Review: Parser Specification

E→ E + T | TT → T ∗ F | F (4.1)F → ( E ) | int | float

sort Exp | J〈Exp@1〉 + 〈Exp@2〉K@1| J〈Exp@2〉 ∗ 〈Exp@3〉K@2| J〈Int〉K@3 | J〈Float〉K@3| sugar J(〈Exp#1@1〉)K@3 →Exp#1 ;

E→ E + E | E ∗ E | int | floatEva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 49 / 62

Page 57: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

HACS Review: Parser Specification

E→ E + T | TT → T ∗ F | F (4.1)F → ( E ) | int | float

sort Exp | J〈Exp@1〉 + 〈Exp@2〉K@1| J〈Exp@2〉 ∗ 〈Exp@3〉K@2| J〈Int〉K@3 | J〈Float〉K@3| sugar J(〈Exp#1@1〉)K@3 →Exp#1 ;

E→ E + E | E ∗ E | int | floatEva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 49 / 62

Page 58: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

HACS Review: Parser Specification

E→ E + T | TT → T ∗ F | F (4.1)F → ( E ) | int | float

sort Exp | J〈Exp@1〉 + 〈Exp@2〉K@1| J〈Exp@2〉 ∗ 〈Exp@3〉K@2| J〈Int〉K@3 | J〈Float〉K@3| sugar J(〈Exp#1@1〉)K@3 →Exp#1 ;

E→ E + E | E ∗ E | int | floatEva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 49 / 62

Page 59: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

This Week

I S-attributed SDDs.

I Recursive Translation Schemes.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 50 / 62

Page 60: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

S-attributed SDD

Only synthesized attributes.

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 51 / 62

Page 61: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis SDD

PRODUCTION SEMANTIC RULES

E→ E1 + E2 E.t = Unif(E1.t, E2.t) (1)| E1 ∗ E2 E.t = Unif(E1.t, E2.t) (2)| int E.t = Int (3)| float E.t = Float (4)

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 52 / 62

Page 62: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis HACS Sorts

sort Type | Int | Float;

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 53 / 62

Page 63: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis HACS Unification Rules

sort Type | scheme Unif(Type,Type);Unif(Int, Int) →Int;Unif(Float, #) →Float;Unif(#, Float) →Float;

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 54 / 62

Page 64: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis SDD Rule (1)

E→ E1 + E2 | E.t = Unif(E1.t, E2.t) (1)

J〈Exp#1 ↑ type(#t1)〉+〈Exp#2 ↑ type(#t2)〉K ↑ type(Unif(#t1,#t2))

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 55 / 62

Page 65: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis SDD Rule (1)

E→ E1 + E2 | E.t = Unif(E1.t, E2.t) (1)

J〈Exp#1 ↑ type(#t1)〉+〈Exp#2 ↑ type(#t2)〉K ↑ type(Unif(#t1,#t2))

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 55 / 62

Page 66: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis SDD Rule (1)

E→ E1 + E2 | E.t = Unif(E1.t, E2.t) (1)

J〈Exp#1 ↑ type(#t1)〉+〈Exp#2 ↑ type(#t2)〉K ↑ type(Unif(#t1,#t2))

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 55 / 62

Page 67: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis SDD Rule (1)

E→ E1 + E2 | E.t = Unif(E1.t, E2.t) (1)

J〈Exp#1 ↑ type(#t1)〉+〈Exp#2 ↑ type(#t2)〉K ↑ type(Unif(#t1,#t2))

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 55 / 62

Page 68: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis SDD Rule (1)

E→ E1 + E2 | E.t = Unif(E1.t, E2.t) (1)

J〈Exp#1 ↑ type(#t1)〉+〈Exp#2 ↑ type(#t2)〉K ↑ type(Unif(#t1,#t2))

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 55 / 62

Page 69: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Example: Type Synthesis HACS

attribute ↑type(Type);

sort Exp; ↑type;

J 〈Exp#1 ↑type(#t1)〉 + 〈Exp#2 ↑type(#t2)〉 K↑type(Unif(#t1,#t2));J 〈Exp#1 ↑type(#t1)〉 ∗ 〈Exp#2 ↑type(#t2)〉 K↑type(Unif(#t1,#t2));J 〈Int#〉 K↑type(Int);J 〈Float#〉 K↑type(Float);

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 56 / 62

Page 70: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Recursive Translation Scheme

// New syntax for value conversion from Int to Float:

sort Exp | Jfloat 〈Exp〉K;

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 57 / 62

Page 71: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Recursive Translation Scheme (II)

// New scheme for inserting all needed int−to−float conversions:

sort Exp | scheme I2F(Exp);

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 58 / 62

Page 72: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Recursive Translation Scheme (III)

// Cases for +:

I2F(J〈Exp#1↑type(Int)〉+〈Exp#2↑type(Int)〉K↑type(#t))→ J〈Exp I2F(#1)〉 + 〈Exp I2F(#2)〉K↑type(#t);

I2F(J〈Exp#1↑type(Float)〉+〈Exp#2↑type(Int)〉K↑type(#t))→ J〈Exp I2F(#1)〉 + (float〈Exp I2F(#2)〉)K↑type(#t);

I2F(J〈Exp#1↑type(Int)〉+〈Exp#2↑type(Float)〉K↑type(#t))→ J(float〈Exp I2F(#1)〉) + 〈Exp I2F(#2)〉K↑type(#t);

I2F(J〈Exp#1↑type(Float)〉+〈Exp#2↑type(Float)〉K↑type(#t))→ J〈Exp I2F(#1)〉 + 〈Exp I2F(#2)〉K↑type(#t);

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 59 / 62

Page 73: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Recursive Translation Scheme (IV)

// Cases for *:

I2F(J〈Exp#1↑type(Int)〉∗〈Exp#2↑type(Int)〉K↑type(#t))→ J〈Exp I2F(#1)〉 ∗ 〈Exp I2F(#2)〉K↑type(#t);

I2F(J〈Exp#1↑type(Float)〉∗〈Exp#2↑type(Int)〉K↑type(#t))→ J〈Exp I2F(#1)〉 ∗ (float〈Exp I2F(#2)〉)K↑type(#t);

I2F(J〈Exp#1↑type(Int)〉∗〈Exp#2↑type(Float)〉K↑type(#t))→ J(float〈Exp I2F(#1)〉) ∗ 〈Exp I2F(#2)〉K↑type(#t);

I2F(J〈Exp#1↑type(Float)〉∗〈Exp#2↑type(Float)〉K↑type(#t))→ J〈Exp I2F(#1)〉 ∗ 〈Exp I2F(#2)〉K↑type(#t);

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 60 / 62

Page 74: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Recursive Translation Scheme (II)

// Cases for literals:

I2F(J〈Int#1〉K↑type(#t)) →J〈Int#1〉K↑type(#t);I2F(J〈Float#1〉K↑type(#t)) →J〈Float#1〉K↑type(#t);

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 61 / 62

Page 75: 4. Syntax-Directed Translation · When translation is defined in terms ofsemantic actions, the tree traversal order, that is the order in which the nodes are visited, becomesessential

Introduction SDT: Syntax-Directed Translation SDD: Syntax-Directed Definition HACS

Questions?

[email protected] [email protected]

Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation September 25, 2014 62 / 62