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
Introduction
SDT: Syntax-Directed Translation
1
Introduction
2
SDT: Syntax-Directed Translation
3
SDD: Syntax-Directed Definition
4
HACS
Review
S-attributed SDDs
Recursive Translation Schemes
Eva Rose, Kristoffer Rose
SDD: Syntax-Directed Definition
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
HACS
September 25, 2014
2 / 62
Introduction
SDT: Syntax-Directed Translation
1
Introduction
2
SDT: Syntax-Directed Translation
3
SDD: Syntax-Directed Definition
4
HACS
Review
S-attributed SDDs
Recursive Translation Schemes
Eva Rose, Kristoffer Rose
SDD: Syntax-Directed Definition
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
HACS
September 25, 2014
3 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Context
source program
- Lexical Analysis
- Syntax Analysis
Tokens
Tree
Tree
IR
IR
IR
Eva Rose, Kristoffer Rose
Symbol
Table
- Semantic Analysis
- Intermediate Representation Generator
- Optimizer
- Code Generator
- Machine-Dependent Code Optimizer
target
machine code
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
4 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
The Trees
T REE
I NTERIOR NODES
G RAMMAR
parse tree
nonterminals
concrete syntax
abstract syntax tree programming constructs abstract syntax
Example (abstract syntax)
+
−
9
Eva Rose, Kristoffer Rose
2
E → E + E | E − E | digit
5
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
5 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
The Trees
T REE
I NTERIOR NODES
G RAMMAR
parse tree
nonterminals
concrete syntax
abstract syntax tree programming constructs abstract syntax
Example (abstract syntax)
+
−
9
Eva Rose, Kristoffer Rose
2
E → E + E | E − E | digit
5
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
5 / 62
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
From Abstract Syntax Tree (AST)
position = initial + rate * 60
parsed into abstract syntax tree:
=
hid, 1i
+
∗
hid, 2i
hid, 3i
Eva Rose, Kristoffer Rose
hnum, 60i
id
1
2
3
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
lexeme
position
initial
rate
September 25, 2014
7 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
. . . to Annotated AST. . .
= t = float
hid, 1i
t = float
+ t = float
∗ t = float
hid, 2i
t = float
hid, 3i
t = float
Eva Rose, Kristoffer Rose
id
1
2
3
lexeme
position
initial
rate
t
float
float
float
hnum, 60i
t = int
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
8 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
. . . to “Fixed” AST
= t = float
hid, 1i
t = float
+ t = float
∗ t = float
hid, 2i
t = float
hid, 3i
t = float
Eva Rose, Kristoffer Rose
id
1
2
3
lexeme
position
initial
rate
t
float
float
float
int-to-float t = float
hnum, 60i
t = int
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
9 / 62
Introduction
SDT: Syntax-Directed Translation
1
Introduction
2
SDT: Syntax-Directed Translation
3
SDD: Syntax-Directed Definition
4
HACS
Review
S-attributed SDDs
Recursive Translation Schemes
Eva Rose, Kristoffer Rose
SDD: Syntax-Directed Definition
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
HACS
September 25, 2014
10 / 62
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 this
tree and production rules help the translation?
Intuitively, we need to associate something with each
production and each tree node:
I
I
something that evaluates the meaning at each node,
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
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 this
tree and production rules help the translation?
Intuitively, we need to associate something with each
production and each tree node:
I
I
something that evaluates the meaning at each node,
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
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 associated
for computing the attributes.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
12 / 62
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: SDD Infix to Postfix Notation
P RODUCTION
expr → expr1 + term2
expr → expr1 − term2
expr → term1
term → 0
term → 1
...
term → 9
− ||−
Eva Rose, Kristoffer Rose
S EMANTIC R ULE
expr.t = expr1 .t || term2 .t || 0 +0
expr.t = expr1 .t || term2 .t || 0 −0
expr.t = term1 .t
term.t =0 00
term.t =0 10
...
term.t =0 90
means concatenation, − 1 , − 2 disambiguates
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
14 / 62
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
−
+
term.t = 2
term.t = 5
2
5
9
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
15 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Translation Schemes
I
I
I
I
Equivalent mechanism.
Attaching program fragments to productions.
The program fragments are called semantic
actions/side-effects.
They “emit” the program fragments during “tree traversal”.
Example (rest → +term {print(0 +0 )} rest1 )
rest
+
Eva Rose, Kristoffer Rose
term
{print(0 +0 )}
rest1
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
16 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Translation Schemes
I
I
I
I
Equivalent mechanism.
Attaching program fragments to productions.
The program fragments are called semantic
actions/side-effects.
They “emit” the program fragments during “tree traversal”.
Example (rest → +term {print(0 +0 )} rest1 )
rest
+
Eva Rose, Kristoffer Rose
term
{print(0 +0 )}
rest1
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
16 / 62
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(‘+0 )}
expr → expr1 − term2 {print(‘−0 )}
expr → term
term → 0 {print(‘00 )}
term → 1 {print(‘10 )}
term → 2 {print(‘20 )}
term → 3 {print(‘30 )}
term → 4 {print(‘40 )}
term → 5 {print(‘50 )}
term → 6 {print(‘60 )}
term → 7 {print(‘70 )}
term → 8 {print(‘80 )}
term → 9 {print(‘90 )}
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
17 / 62
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Semantic Actions/Side-Effects in Parse Tree
expr
{print(0 +0 )}
expr
expr
−
+
{print(0 −0 )}
term
2
0 0
{print( 5 )}
term
{print(0 20 )}
term
5
0 0
{print( 9 )}
9
Input: 9 − 5 + 2. Output (print): 9 5 − 2 +. Single depth-first
traversal.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
19 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Semantic Actions/Side-Effects in Parse Tree
expr
{print(0 +0 )}
expr
expr
−
+
{print(0 −0 )}
term
2
0 0
{print( 5 )}
term
{print(0 20 )}
term
5
0 0
{print( 9 )}
9
Input: 9 − 5 + 2. Output (print): 9 5 − 2 +. Single depth-first
traversal.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
19 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Semantic Actions/Side-Effects in Parse Tree
expr
{print(0 +0 )}
expr
expr
−
+
{print(0 −0 )}
term
2
0 0
{print( 5 )}
term
{print(0 20 )}
term
5
0 0
{print( 9 )}
9
Input: 9 − 5 + 2. Output (print): 9 5 − 2 +. Single depth-first
traversal.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
19 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Summary
expr.t = 95 − 2+
expr.t = 95−
+
term.t = 2
−
term.t = 5
2
expr.t = 9
term.t = 9
attributes/semantic rules (HACS)
5
9
expr
{print(0 +0 )}
expr
+
{print(0 −0 )}
expr
−
term
2
{print(0 50 )}
term
{print(0 20 )}
semantic actions/side-effects (yacc)
term
5
{print(0 90 )}
9
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
20 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Tree Traversal and Translation Schemes
When translation is defined in terms of semantic actions, the
tree traversal order, that is the order in which the nodes are
visited, becomes essential.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
21 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Exercise
Construct an SDD that generates an attribute t in prefix
notation for each expression expr, for arithmetic expressions.
(You only need to consider: ‘+’,’−’, and ’∗’ expressions). Prefix
notation 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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Solution: SDD for Infix Notation into Prefix Notation
P RODUCTION
expr → expr1 + term2
expr → expr1 − term2
expr → expr1 ∗ term2
expr → term
term → 0
term → 1
...
term → 9
Eva Rose, Kristoffer Rose
S EMANTIC R ULE
expr.t = 0 +0 || expr1 .t || term2 .t
expr.t = 0 −0 || expr1 .t || term2 .t
expr.t = 0 ∗0 || expr1 .t || term2 .t
expr.t = term.t
term.t =0 00
term.t =0 10
...
term.t =0 90
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
23 / 62
Introduction
SDT: Syntax-Directed Translation
1
Introduction
2
SDT: Syntax-Directed Translation
3
SDD: Syntax-Directed Definition
4
HACS
Review
S-attributed SDDs
Recursive Translation Schemes
Eva Rose, Kristoffer Rose
SDD: Syntax-Directed Definition
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
HACS
September 25, 2014
24 / 62
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 of
a at node X.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
25 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Synthesized and Inherited Attributes
Synthesized attributes at node N are defined only in terms of
the attribute values of the children of N, and N itself.
Inherited attributes at node N is defined only in terms of
attribute values at N’s parent, N itself, and N’s
siblings.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
26 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: SDD for Desk Calculator
1.
2.
3.
4.
5.
6.
7.
P RODUCTION
L→E$
E → E1 + T
E→T
T → T1 ∗ F
T→F
F → (E)
F → digit
S EMANTIC R ULE
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’ and ‘lexval’ are synthesized attributes.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
27 / 62
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
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Exercise: Desk Calculator
Give annotated parse trees for the following expressions:
I
I
(3 + 4) ∗ (5 + 6) $
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Exercise: Mingling with Inherited Attributes. . .
1.
2.
3.
4.
P RODUCTION
T → F T0
T 0 → ∗ F T10
T0 → F → digit
S EMANTIC R ULES
T 0 .inh = F.val; T.val = T 0 .syn
T10 .inh = T 0 .inh × F.val; T 0 .syn = T10 .syn
T 0 .syn = T 0 .inh
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Dependency Graphs
Dependency graphs depicts the flow of information among
attributes.
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
32 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Attributes
Parse tree for 3 ∗ 5 based on SDD:
T
F
3O
val
digit 1 lexval
9 i val
inh
∗
F
'5
4O
T0
8 i syn
val
inh
digit 2 lexval
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
7) 6
T0
;7
syn
September 25, 2014
33 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Attributes with Dependency Graph
Parse tree for 3 ∗ 5 based on SDD:
T
F
3O
val
digit 1 lexval
9 i val
inh
∗
F
'5
4O
T0
8 i syn
val
inh
digit 2 lexval
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
7) 6
T0
;7
syn
September 25, 2014
33 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Dependency Graphs
Central problem: Determining the efficient evaluation order for
the 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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Calculating Attributes
Cycles possible!
P RODUCTION S EMANTIC R ULE
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
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 whose
dependency graphs have cycles!
Safe way to go: use classes of SDDs that guarantee an
evaluation 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
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 whose
dependency graphs have cycles!
Safe way to go: use classes of SDDs that guarantee an
evaluation 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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
L-Attributed Definitions
Suppose an attribute rule for A → X1 X2 . . . Xi . . . Xn produces
inherited 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 dependency
graph 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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Mingling with Inherited Attributes. . .
Decide if the SDD is L-attributed and argue :
1.
2.
3.
4.
P RODUCTION
T → F T0
T 0 → ∗ F T10
T0 → F → digit
S EMANTIC R ULES
T 0 .inh = F.val; T.val = T 0 .syn
T10 .inh = T 0 .inh × F.val; T 0 .syn = T10 .syn
T 0 .syn = T 0 .inh
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Mingling with Inherited Attributes. . .
Decide if the SDD is L-attributed and argue :
1.
2.
3.
4.
P RODUCTION
T → F T0
T 0 → ∗ F T10
T0 → F → digit
S EMANTIC R ULES
T 0 .inh = F.val; T.val = T 0 .syn
T10 .inh = T 0 .inh × F.val; T 0 .syn = T10 .syn
T 0 .syn = T 0 .inh
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Mingling with Inherited Attributes. . .
Decide if the SDD is L-attributed and argue :
1.
2.
3.
4.
P RODUCTION
T → F T0
T 0 → ∗ F T10
T0 → F → digit
S EMANTIC R ULES
T 0 .inh = F.val; T.val = T 0 .syn
T10 .inh = T 0 .inh × F.val; T 0 .syn = T10 .syn
T 0 .syn = T 0 .inh
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Another example
The following SDD is NOT L-attributed. Why?
P RODUCTION S EMANTIC R ULES
A → BC
A.s = B.b
B.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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Another example
The following SDD is NOT L-attributed. Why?
P RODUCTION S EMANTIC R ULES
A → BC
A.s = B.b
B.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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Construction of Abstract Syntax Trees
T REE
I NTERIOR NODES
G RAMMAR
parse tree
nonterminals
concrete syntax
abstract syntax tree programming constructs abstract syntax
Example (abstract syntax)
+
−
9
Eva Rose, Kristoffer Rose
2
E → E + E | E − E | digit
5
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
43 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Construction of AST
Example 1: S-attributed SDD for simple expresssions.
1.
2.
3.
4.
5.
6.
I
P RODUCTION
E → E1 + T2
E → E1 − T2
E→T
T → (E)
T → id
T → num
S EMANTIC R ULE
E.node = new Node(0 +0 , E1 .node, T2 .node)
E.node = new Node(0 −0 , E1 .node, T2 .node)
E.node = T.node
T.node = E.node
T.node = new Leaf (id, id.entry)
T.node = new Leaf (num, num.entry)
Show the AST for a − 4 + c
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
44 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Construction of AST
Example 2: L-attributed SDD for simple expresssions.
I
1.
2.
P RODUCTION
E → T E0
E0 → + T E10
3.
E0 → − T E10
4.
5.
6.
7.
E0 → T → (E)
T → id
T → num
S EMANTIC R ULE
E.node = E0 .syn; E0 .inh = T.node
E10 .inh = new Node(0 +0 , E0 .node, T.node)
E0 .syn = E10 .syn
E10 .inh = new Node(0 −0 , E0 .node, T.node)
E0 .syn = E10 .syn
E0 .syn = E0 .inh
T.node = E.node
T.node = new Leaf (id, id.entry)
T.node = new Leaf (num, num.entry)
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Syntax-Directed Translation of Type Expressions
P RODUCTION
1 T → BC
2 B → int
3 B → float
4 C → [num]C1
5 C→
I
S EMANTIC R ULE
T.syn = C.syn
C.inh = B.syn
B.syn = integer
B.syn = integer
C.syn = array(num.val, C1 .syn)
C1 .inh = C.inh
C.syn = C.inh
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
Introduction
SDT: Syntax-Directed Translation
1
Introduction
2
SDT: Syntax-Directed Translation
3
SDD: Syntax-Directed Definition
4
HACS
Review
S-attributed SDDs
Recursive Translation Schemes
Eva Rose, Kristoffer Rose
SDD: Syntax-Directed Definition
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
HACS
September 25, 2014
47 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
HACS Review: Lexical Specification
space [ \t\n] ;
token Int | hDigiti+ ;
token Float | hInti "." hInti ;
token fragment Digit | [0−9] ;
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
48 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
HACS Review: Parser Specification
E→E+T |T
T →T∗F |F
F → ( E ) | int | float
(4.1)
sort Exp | JhExp@1i + hExp@2iK@1
| JhExp@2i ∗ hExp@3iK@2
| JhIntiK@3 | JhFloatiK@3
| sugar J(hExp#1@1i)K@3 →Exp#1 ;
E → E + E | E ∗ E | int | float
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
49 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
HACS Review: Parser Specification
E→E+T |T
T →T∗F |F
F → ( E ) | int | float
(4.1)
sort Exp | JhExp@1i + hExp@2iK@1
| JhExp@2i ∗ hExp@3iK@2
| JhIntiK@3 | JhFloatiK@3
| sugar J(hExp#1@1i)K@3 →Exp#1 ;
E → E + E | E ∗ E | int | float
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
49 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
HACS Review: Parser Specification
E→E+T |T
T →T∗F |F
F → ( E ) | int | float
(4.1)
sort Exp | JhExp@1i + hExp@2iK@1
| JhExp@2i ∗ hExp@3iK@2
| JhIntiK@3 | JhFloatiK@3
| sugar J(hExp#1@1i)K@3 →Exp#1 ;
E → E + E | E ∗ E | int | float
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
49 / 62
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
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Type Synthesis SDD
P RODUCTION
E → E1 + E2
| E1 ∗ E2
| int
| float
Eva Rose, Kristoffer Rose
S EMANTIC R ULES
E.t = Unif(E1 .t, E2 .t)
E.t = Unif(E1 .t, E2 .t)
E.t = Int
E.t = Float
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
(1)
(2)
(3)
(4)
September 25, 2014
52 / 62
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
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
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)
JhExp#1 ↑ type(#t1)i+hExp#2 ↑ type(#t2)iK ↑ type(Unif (#t1, #t2))
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
55 / 62
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)
JhExp#1 ↑ type(#t1)i+hExp#2 ↑ type(#t2)iK ↑ type(Unif (#t1, #t2))
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
55 / 62
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)
JhExp#1 ↑ type(#t1)i+hExp#2 ↑ type(#t2)iK ↑ type(Unif (#t1, #t2))
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
55 / 62
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)
JhExp#1 ↑ type(#t1)i+hExp#2 ↑ type(#t2)iK ↑ type(Unif (#t1, #t2))
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
55 / 62
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)
JhExp#1 ↑ type(#t1)i+hExp#2 ↑ type(#t2)iK ↑ type(Unif (#t1, #t2))
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
55 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Example: Type Synthesis HACS
attribute ↑type(Type);
sort Exp; ↑type;
J
J
J
J
hExp#1 ↑type(#t1)i + hExp#2 ↑type(#t2)i K↑type(Unif(#t1,#t2));
hExp#1 ↑type(#t1)i ∗ hExp#2 ↑type(#t2)i K↑type(Unif(#t1,#t2));
hInt#i K↑type(Int);
hFloat#i K↑type(Float);
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
56 / 62
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 hExpiK;
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
57 / 62
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
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Recursive Translation Scheme (III)
// Cases for +:
I2F(JhExp#1↑type(Int)i+hExp#2↑type(Int)iK↑type(#t))
→ JhExp I2F(#1)i + hExp I2F(#2)iK↑type(#t);
I2F(JhExp#1↑type(Float)i+hExp#2↑type(Int)iK↑type(#t))
→ JhExp I2F(#1)i + (floathExp I2F(#2)i)K↑type(#t);
I2F(JhExp#1↑type(Int)i+hExp#2↑type(Float)iK↑type(#t))
→ J(floathExp I2F(#1)i) + hExp I2F(#2)iK↑type(#t);
I2F(JhExp#1↑type(Float)i+hExp#2↑type(Float)iK↑type(#t))
→ JhExp I2F(#1)i + hExp I2F(#2)iK↑type(#t);
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
59 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Recursive Translation Scheme (IV)
// Cases for *:
I2F(JhExp#1↑type(Int)i∗hExp#2↑type(Int)iK↑type(#t))
→ JhExp I2F(#1)i ∗ hExp I2F(#2)iK↑type(#t);
I2F(JhExp#1↑type(Float)i∗hExp#2↑type(Int)iK↑type(#t))
→ JhExp I2F(#1)i ∗ (floathExp I2F(#2)i)K↑type(#t);
I2F(JhExp#1↑type(Int)i∗hExp#2↑type(Float)iK↑type(#t))
→ J(floathExp I2F(#1)i) ∗ hExp I2F(#2)iK↑type(#t);
I2F(JhExp#1↑type(Float)i∗hExp#2↑type(Float)iK↑type(#t))
→ JhExp I2F(#1)i ∗ hExp I2F(#2)iK↑type(#t);
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
60 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Recursive Translation Scheme (II)
// Cases for literals:
I2F(JhInt#1iK↑type(#t)) →JhInt#1iK↑type(#t);
I2F(JhFloat#1iK↑type(#t)) →JhFloat#1iK↑type(#t);
Eva Rose, Kristoffer Rose
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
61 / 62
Introduction
SDT: Syntax-Directed Translation
SDD: Syntax-Directed Definition
HACS
Questions?
[email protected]
Eva Rose, Kristoffer Rose
[email protected]
Compiler Construction (CSCI-GA.2130-001) 4. Syntax-Directed Translation
September 25, 2014
62 / 62
© Copyright 2025 Paperzz