5 SDT
5 SDT
Character stream
Machine-Independent
Lexical Analyzer
Code Optimizer
Intermediate representation
Backend
Token stream
Frontend
Machine-Dependent
Semantic Analyzer Code Optimizer
Intermediate
Code Generator Symbol
Table
2
Intermediate representation
Role of SDT
● To associate actions with productions
● To associate attributes with non-terminals
● To create implicit or explicit syntax tree To
● perform semantic analysis
3
Example
E → E +T $$.code = “”;
strcat($$.code, $1.code);
SDD
strcat($$.code, $3.code);
strcat($$.code, “+”);
Attributes
E → E +T { printf(“+”); } SDT
5
Syntax Directed Translation
● An SDT is done by attaching rules or program
fragments to productions.
● The order induced by the syntax analysis
produces a translation of the input program.
6
Attributes
● Inherited
– In terms of the attributes of the
node, its parent and siblings.
– e.g., int x, y, z; or
Ishu's nested scoping
● Synthesized
– In terms of the attributes of the
node and its children.
– e.g., a + b * c or
most of the constructs from your
7
assignments
SDD for Calculator
Parse Tree
Input string
+
3*5+4$
* 4
3 5
T'.inh = 3
F.val = 3 T'.syn = 15
T' .inh = 15
digit.lexval = 3 * F.val = 5 1
T'1.syn = 15
digit.lexval = 5 ε
T'.inh = 3
F.val = 3 T'.syn = 15
T' .inh = 15
digit.lexval = 3 * F.val = 5 1
T'1.syn = 15
digit.lexval = 5 ε
T'.inh = 3
F.val = 3 T'.syn = 15
T' .inh = 15
digit.lexval = 3 * F.val = 5 1
T'1.syn = 15
digit.lexval = 5 ε
●
A dependency graph depicts the flow of information amongst attributes.
●
An edge attr1 → attr2 means that the value of attr1 is needed to
compute attr2.
●
Thus, allowable evaluation orders are those sequences of rules
N1, N2, …, Nk such that if Ni → Nj, then i < j.
● What are such allowable orders?
●
Topological sort
●
What about cycles?
Order of Evaluation
Inherited
● If there are only synthesized S-attributed
attributes in the SDD, there
exists an evaluation order.
●
Any bottom-up order would do;
for instance, post-order.
● Helpful for LR parsing.
●
How about when the attributes
are both synthesized as well as
inherited?
●
How about when the attributes
Synthesized
are only inherited?
SDD for Calculator
Parse Tree
Input string
S-attributed +
3*5+4$
* 4
3 5
preorder(N) {
for (each child C of N, from the left) preorder(C)
evaluate attributes of N
}
T.val = 15
T'.inh = 3
F.val = 3 T'.syn = 15
T' .inh = 15
digit.lexval = 3 * F.val = 5 1
T'1.syn = 15
digit.lexval = 5 ε 20
Example of non-L-attributed SDD
Production Semantic rule
A → BC A.s = B.b;
B.i = C.c + A.s
●
First rule uses synthesized attributes.
●
Second rule has inherited attributes.
●
However, B's attribute is dependent on C's attribute, which
is on the right.
●
Hence, it is not L-attributed SDD.
Classwork:
S → L . L |L ● What does this grammar generate?
L → L B |B ● Design L-attributed SDD to compute S.val, the decimal
B→0|1
value of an input string.
● For instance, 101.101 should output 5.625.
● Idea: Use an inherited attribute L.side that tells which side
(left or right) of the decimal point a bit is on. 21
SDT Applications
● Creating an explicit syntax tree.
+
– e.g., a –4 + c
- c
p1 = new Leaf(ida); 4
a
p2 = new Leaf(num4);
p3 = new Op(p1, '-', p2);
p4 = new Leaf(idc);
p5 = new Op(p3, '+', p4);
Production Semantic Rules
E→E+T $$.node = new Op($1.node, '+', $3.node)
E→E-T $$.node = new Op($1.node, '-', $3.node)
E→T $$.node = $1.node
T→(E) $$.node = $2.node
T → id $$.node = new Leaf($1)
T → num $$.node = new Leaf($1) 22
SDT Applications
● Creating an explicit syntax tree.
+
– e.g., a –4 + c
- c
● Classwork: a 4
25
SDT for Calculator
Sr. Production Semantic Rules
No.
1 E' → E $ print(E.val)
2 E → E1 + T E.val = E1.val + T.val
3 E→T ...
4 T → T1 *F ...
5 T→F ...
6 F → (E) ...
7 F → digit F.val = digit.lexval
26
SDT for Calculator
E' → E $ { print(E.val); }
E → E1 +T { E.val = E1.val + T.val; }
Postfix SDT
E→T ...
T → T1 *F ...
T→F ...
F → (E) ...
F → digit { F.val = digit.lexval; }
●
SDTs with all the actions at the right ends of the production bodies are
called postfix SDTs.
● Only synthesized attributes are useful here.
● Can be implemented during LR parsing by executing actions when
reductions occur.
● The attribute values can be put on a stack and can be retrieved. 27
Parsing Stack
A → X YZ
X Y Z State / grammar symbol
X.x Y.y Z.z Synthesized attribute
stack top
Compare with $1, $2, … in Yacc.
Production Actions
E' → E $ { print(stack[top – 1].val); --top; }
E → E1 +T { stack[top – 2].val += stack[top].val; top -= 2; }
E→T { stack[top].val = stack[top].val; }
T → T1 *F { stack[top – 2].val *= stack[top].val; top -= 2; }
T→F { stack[top].val = stack[top].val; }
F → (E) { stack[top – 2].val = stack[top – 1].val; top -= 2; }
F → digit { stack[top].val = stack[top].val; } 28
Actions within Productions
● Actions may be placed at any position within production
body. Considered as empty non-terminals called markers.
● For production B → X {action} Y, action is performed
– as soon as X appears on top of the parsing stack in
bottom-up parsing.
– just before expanding Y in top-down parsing if Y is a non-
terminal.
– just before we check for Y on the input in top-down parsing
if Y is a terminal.
● SDTs that can be implemented during parsing are
– Postfix SDTs (S-attributed definitions)
– SDTs implementing L-attributed definitions
E' → E $
E → { print '+'; } E1 + T
E→T
T → { print '*'; } T1 * F
T→F
F → (E)
F → digit { print digit.lexval; }
32
SDD for
while L1
S → while ( C ) = newLabel();
L2 = newLabel();
S1 S1.next = L1;
C.false = S.next;
C.true = L2;
S.code = “label” + L1 +
C.code +
”label” + L2 +
S1.code;
SDT
S → while ( { L1 = newLabel(); L2 = newLabel(); C. false = S.next; C.true = L2; }
C) { S1.next = L2; }
S1 { S.code = “label” + L1 + C.code + “label” + L2 + S1.code; }
33
What is the type of this SDD?
SDD for
while L1
S → while ( C ) = newLabel();
L2 = newLabel();
S1 S1.next = L1;
C.false = S.next;
C.true = L2;
S.code = “label” + L1 +
C.code +
”label” + L2 +
S1.code;
SDT
S → while ( { L1 = newLabel(); L2 = newLabel(); C. false = S.next; C.true = L2;
print(“label”, L1); }
C) { S1.next = L2; print(“label”, L2); } On-the-fly
code
S1 generation
34
What is the type of this SDD?
Homework
● Exercises 5.5.5 from ALSU book.
35