Lecture 11 - Syntax Directed Translation
Lecture 11 - Syntax Directed Translation
Lecture 11 - Syntax Directed Translation
Lecture 10
Section 5.1 - 5.3
Associate Professor
Dept of Computer Science and Engineering
University of Chittaong, Bangladesh
Email: rokan@cu.ac.bd
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
t = float
=
t = float id lexeme t
1 position float
hid, 1i + 2 initial float
t = float 3 rate float
t = float hid, 2i ⇤
t = float t = int
Eva Rose, Kristoffer Rose Compiler Construction (CSCI-GA.2130-001) 5. Syntax-directed Definitions/Type Analysis 6 / 89
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Parse Trees
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
lval = expr ; a +
num id b
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
The parse tree never really exists, except insofar as the parser
follows its logical order.
The AST builder builds the syntax tree from the information
obtained by the parser.
Then the code generator writes the assembly code from the syntax
tree.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Syntax-Directed Definitions
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Syntax-Directed Definitions
Definition
A syntax-directed definition (SDD) is a context-free grammar with
attributes added to the grammar symbols.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
E → E + E | num
Then E derives its value from the num tokens in the expression.
This is expressed formally by the rules
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Syntax-Directed Definitions
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Synthesized Attributes
Definition
A synthesized attribute of a grammar symbol is a property that is
determined by the attributes of the symbols below it in the parse tree.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Synthesized Attributes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
expr.val
expr1.val + expr2.val
num.lexval num.lexval
100 250
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
expr.val
expr1.val + expr2.val
num.lexval num.lexval
100 250
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
expr.val
expr1.val + expr2.val
synthesized synthesized
num.lexval num.lexval
100 250
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
expr.val
synthesized synthesized
expr1.val + expr2.val
synthesized synthesized
num.lexval num.lexval
100 250
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Definition
An inherited attribute is a property of a symbol (node) that is
determined by its parent node and its siblings in the parse tree.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
dcl
type.type list.type
float id1.type
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
dcl
type.type list.type
from lexer
float id1.type
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
dcl
type.type list.type
synthesized
from lexer
float id1.type
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
dcl
inherited
type.type list.type
synthesized
from lexer
float id1.type
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
dcl
inherited
type.type list.type
inherited inherited
synthesized
from lexer
float id1.type
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example
dcl
inherited
type.type list.type
inherited inherited
synthesized
float id1.type
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Some Questions
Questions
In an expression tree, is the type of the expression at the root
inherited or is it synthesized?
Is the type used in an arithmetic operation an inherited attribute
or an synthesized attribute of the operator?
In an assignment statement, is the type assigned by the operator
an inherited attribute or a synthesized attribute of the operator?
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Synthesized Attributes
E→E+T
E→T
T →T *F
T →F
F →(E)
F → num
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Synthesized Attributes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
E → T E′
E′ → + T E′
E′ → ε
T → F T′
T′ → * F T′
T′ → ε
F →(E)
F → num
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
For the production
F → num
we have the rule
F.val = num.lexval
F.val = E.val
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
3 ∗ 4 + 5.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
T E'
F T' + T E'
num ε num ε
Inherited Attributes
Example (Inherited Attributes)
T E'
F T' + T E'
num 4 ε num 5 ε
Inherited Attributes
Example (Inherited Attributes)
T E'
F 3 T' + T E'
num 4 ε num 5 ε
F.val = num.lexval . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
T ′ .inh = F.val . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
T E'
inh 3
F 3 T' 12 + T E'
*
4
num 3 * F 4 T' F 5 T' ε
num 4 ε num 5 ε
T ′ .inh = F.val . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
T E'
inh 3
F 3 T' + T E'
inh 12
num 4 ε num 5 ε
Inherited Attributes
Example (Inherited Attributes)
T E'
inh 3
F 3 T' + T E'
inh 12
num 4 ε num 5 ε
T ′ .syn = T ′ .inh . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
T E'
inh 3
F 3 T' + T E'
syn 12
num 4 ε num 5 ε
Inherited Attributes
Example (Inherited Attributes)
T E'
syn 12
inh 3
F 3 T' + T E'
syn 12
num 4 ε num 5 ε
T.val = T ′ .syn . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
Example (Inherited Attributes)
inh 12
T E'
syn 12
inh 3
F 3 T' + T E'
syn 12
num 4 ε num 5 ε
E ′ .inh = T.val . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Inherited Attributes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Outline
2 Syntax-Directed Definitions
3 Synthesized Attributes
4 Inherited Attributes
5 Examples
6 Assignment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Assignment
Homework
p. 309: 1, 2, 3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .