0% found this document useful (0 votes)
36 views24 pages

CS 4300: Compiler Theory Syntax-Directed Translation: Xuejun Liang 2019 Fall

1) Syntax-directed definitions specify attribute values using semantic rules associated with grammar productions. Attributes typically represent values like numbers, types, and memory locations. 2) Evaluation orders for syntax-directed definitions are determined by the dependency graph, which cannot contain cycles. A topological sort of the dependency graph gives a valid evaluation order. 3) Applications of syntax-directed definitions include constructing syntax trees during parsing and representing types as type expressions for type checking. Attributes are evaluated in a bottom-up or left-to-right order depending on whether the definition is S-attributed or L-attributed.

Uploaded by

Deepanshu Punj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views24 pages

CS 4300: Compiler Theory Syntax-Directed Translation: Xuejun Liang 2019 Fall

1) Syntax-directed definitions specify attribute values using semantic rules associated with grammar productions. Attributes typically represent values like numbers, types, and memory locations. 2) Evaluation orders for syntax-directed definitions are determined by the dependency graph, which cannot contain cycles. A topological sort of the dependency graph gives a valid evaluation order. 3) Applications of syntax-directed definitions include constructing syntax trees during parsing and representing types as type expressions for type checking. Attributes are evaluated in a bottom-up or left-to-right order depending on whether the definition is S-attributed or L-attributed.

Uploaded by

Deepanshu Punj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS 4300: Compiler Theory

Chapter 5
Syntax-Directed Translation

Xuejun Liang
2019 Fall
2

Outlines (Sections)
1. Syntax-Directed Definitions
2. Evaluation Orders for SDD's
3. Applications of Syntax-Directed Definition
4. Syntax-Directed Translation Schemes
5. Implementing L-Attributed SDD's
3

1. Syntax-directed Definition
• A syntax-directed definition (SSD) specifies the values of
attributes by associating semantic rules with the grammar
productions

• A syntax-directed translation scheme embeds program


fragments called semantic actions within production bodies

• Between the two notations


– syntax-directed definitions can be more readable, and hence
more useful for specifications.
– However, translation schemes can be more efficient, and
hence more useful for implementations
4

Attributes
• A synthesized attribute at node N is defined only in terms
of attribute values at the children of N and at N itself.
• An inherited attribute at node N is defined only in terms of
attribute values at N's parent , N itself, and N's siblings
• Attribute values typically represent
– Numbers (literal constants)
– Strings (literal constants)
– Memory locations, such as a frame index of a local variable
or function argument
– A data type for type checking of expressions
– Scoping information for local declarations
– Intermediate program representations
5

Example Syntax-directed Definition


A simple desk calculator
Production Semantic Rule
An SDD with
LEn L.val = E.val only synthesized
E  E1 + T E.val = E1.val + T.val attributes is called
ET E.val = T.val S-attributed.
T  T1 * F T.val = T1.val * F.val
TF T.val = F.val An SDD without
F(E) F.val = E.val side effects is
F  digit F.val = digit.lexval called an
attribute grammar
Note: all attributes in this example
are of the synthesized type
6

Annotated Parse Tree for 3 * 5 + 4 n


A parse tree, showing
the value(s) of its
attribute(s) is called an
annotated parse tree.
7

Annotating a Parse Tree With


Depth-First Traversals
With synthesized attributes, we can evaluate
attributes in any bottom-up order, such as that
of a postorder traversal of the parse tree.

procedure visit(n : node);


begin
for each child m of n, from left to right do
visit(m);
evaluate semantic rules at node n
end
8

An SDD Based on a Grammar


Suitable for Top-down Parsing

TT*F
TF
F  digit

An inherited attribute for nonterminal T’


is used to pass the operand to the operator
9

Annotated Parse Tree for 3 * 5

An inherited attribute for nonterminal T’


is used to pass the operand to the operator
10

Example Attribute Grammar with


Synthesized & Inherited Attributes
Simple Type Declaration
Production Semantic Rule
treated as
DTL L.inh = T.type dummy
T  int T.type = ‘integer’ synthesized
T  float T.type = ‘float’ attribute
L  L1 , id L1.inh = L.inh;
addtype(id.entry, L.inh)
L  id addtype(id.entry, L.inh)

Synthesized: T.type, id.entry


Inherited: L.inh
11

2. Evaluation Orders for SDD 's


A dependency graph depicts the flow of information
among the attribute instances in a particular parse tree
A.a
AXY A.a = f(X.x, Y.y)
X.x Y.y
A.a
X.x = f(A.a, Y.y)
X.x Y.y

Direction of A.a
Y.y = f(A.a, X.x)
value dependence X.x Y.y
12

Evaluation Orders for SDD 's (Cont.)


• Edges in the dependency graph determine the
evaluation order for attribute values
– Dependency graphs cannot be cyclic
• So, dependency graph is a directed acyclic graph
(DAG)

AXY A.a A.a := f(X.x)


X.x := f(Y.y)
X.x Y.y Y.y := f(A.a)
Direction of
Error: cyclic dependence
value dependence
13

Example Annotated Parse Tree with


Dependency Graph

3*5
14

Example Annotated Parse Tree

T.type = ‘float’ L.inh = ‘float’

float L.inh = ‘float’, id3.entry

L.inh = ‘float’ , id2.entry

id1.entry float id1 , id2 , id3


15

Example Annotated Parse Tree with


Dependency Graph
D

T.type = ‘float’ L.inh = ‘float’

float L.inh = ‘float’, id3.entry

L.inh = ‘float’ , id2.entry

id1.entry float id1 , id2 , id3


16

Evaluation Order
• A topological sort of a directed acyclic graph
(DAG) is any ordering m1, m2, …, mn of the nodes
of the graph, such that if mimj is an edge, then mi
appears before mj
• Any topological sort of a dependency graph gives a
valid evaluation order of the semantic rules
• Example: Topological orders of DAG on slide 13
– 1, 2, 3, 4, 5, 6, 7, 8, 9.
– 1, 3, 5, 2, 4, 6, 7, 8, 9.
17

Example Parse Tree with


Topologically Sorted Actions
D
Topological sort:
4 5 1. Get id1.entry
T1.type = ‘float’ L1.inh = ‘float’ 6
2. Get id2.entry
3. Get id3.entry
7 8
float L2.inh = ‘float’, 3 id3.entry 4. T1.type=‘float’
5. L1.inh=T1.type
6. addtype(id3.entry, L1.inh)
9 10 7. L2.inh=L1.inh
L3.inh = ‘float’ , 2 id2.entry
8. addtype(id2.entry, L2.inh)
9. L3.inh=L2.inh
1 id1.entry 10. addtype(id1.entry, L3.inh)

float id1 , id2 , id3


18

L-Attributed Definitions
• A syntax-directed definition is L-attributed if each
inherited attribute of Xj on the right side of
production A  X1 X2 … Xn depends only on
1. the attributes of the symbols X1, X2, …, Xj-1
2. the inherited attributes of A
A.a
Shown: dependences
of inherited attributes X1.x X2.x
• L-attributed definitions allow for a natural order of
evaluating attributes: depth-first and left to right
• Note: every S-attributed syntax-directed definition is also
L-attributed
19

3. Applications of SDD
Construction of Syntax Trees
S-attributed Definition for Simple Expressions

Note: This is a S-attributed definition, then


can be done during bottom-up parsing
20

Example: Syntax Tree for a - 4 + c


Steps in the construction
of the syntax tree
21

Constructing Syntax Tree


During Top-Down Parsing
L-attributed Definition for Simple Expression
22

Example: Dependency Graph for a-4+c

Steps
2) p1 == new Leaf (id, entry-a) ;
4) p2 == new Leaf (num, 4) ;
6) p3 == new Node('-', pl, p2 ) ;
8) p4 == new Leaf (id, entry-c) ;
9) p5 == new Node('+', p3, p4 ) ;
23

The Structure of a Type


int[2][3] array(2, array(3, integer)) Type expression
for int[2][3]

T generates either a basic type or an array type


24

Annotated Parse Tree for int[2][3]

You might also like