4-sdt
4-sdt
Rupesh Nasre.
Machine-Independent
Machine-Independent
Lexical
LexicalAnalyzer
Analyzer Code
CodeOptimizer
Optimizer
Intermediate representation
Backend
Token stream
Frontend
Syntax
SyntaxAnalyzer
Analyzer Code
CodeGenerator
Generator
Machine-Dependent
Machine-Dependent
Semantic
SemanticAnalyzer
Analyzer Code
CodeOptimizer
Optimizer
Intermediate
Intermediate
Code Symbol
CodeGenerator
Generator 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
●
… essentially, to add life to the skeleton.
3
Example
E→E+T $$.code = “”;
strcat($$.code, $1.code);
SDD
strcat($$.code, $3.code);
strcat($$.code, “+”);
Attributes
SDT
E→E+T { printf(“+”); }
SDTs may be viewed as
implementations of SDDs
and are important from
efficiency perspective.
Productions Actions
4
Syntax Directed Definition
●
An SDD is a CFG with attributes and rules.
– Attributes are associated with grammar symbols.
– Rules are associated with productions.
●
An SDD specifies the semantics of productions.
– It does not enforce a specific way of achieving the
semantics.
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
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$
** 44
33 55
●
This SDD uses a combination of synthesized
and inherited attributes.
●
A.s (head) is defined in terms of B.i (body non-
terminal). Hence, it is synthesized.
●
B.i (body non-terminal) is defined in terms of A.s
(head). Hence, it is inherited.
●
There exists a circular dependency between
their evaluations.
●
In practice, subclasses of SDDs required for our
purpose do have an order.
Synthesized
Classwork
●
Write semantic rules for the following grammar.
– It computes terms like 3 * 5 and 3 * 5 * 7.
●
Now write the annotated parse tree for 3 * 5 * 7.
●
What is the associativity of *?
3 T' → ε T'.val = 1
4 F → digit F.val = digit.lexval
Classwork
●
Write semantic rules for the following grammar.
– It computes terms like 3 * 5 and 3 * 5 * 7.
●
Now write the annotated parse tree for 3 * 5 * 7.
●
What is the associativity of *?
●
Can you make it left-associative?
Sr. No. Production Semantic Rules
1 T → F T' T'.inh = F.val
T.val = T'.syn
2 T' → * F T'1 T'1.inh = T'.inh * F.val
T'.syn = T'1.syn
3 T' → ε T'.syn = T'.inh ε
4 F → digit F.val = digit.lexval
Classwork
T.val = 15
T'.inh = 3
F.val = 3 T'.syn = 15
T'.inh = 3
F.val = 3 T'.syn = 15
What
Whatis
isthe
theorder
orderin
inwhich
whichrules
rulesare
areevaluated?
evaluated?
T'.inh = 3
F.val = 3 T'.syn = 15
●
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$
** 44
33 55
Can
Canwe
weallow
allowmore
moreorderings?
orderings? 18
Issues with S-attributed SDD
●
It is too strict!
●
There exist reasonable non-cyclic orders that it
disallows.
– If a non-terminal uses attributes of its parent only
(no sibling attributes)
– If a non-terminal uses attributes of its left-siblings
only (and not of right siblings).
●
The rules may use information “from above”
and “from left”.
L-attributed
19
L-attributed SDD
●
Each attribute must be either
– synthesized, or
– inherited, but with restriction.
For production A → X1 X2 … Xn with inherited attributes
Xi.a computed by an action, the rule may use only
●
inherited attributes of A.
● either inherited or synthesized attributes of X1, X2, …, Xi-1.
● inherited or synthesized attributes of Xi with no cyclic
dependence.
●
L is for left-to-right.
Have
Haveyou
youseen
seenany
anysuch
suchSDD?
SDD?
20
Example of L-attributed SDD
Sr. No. Production Semantic Rules
1 T → F T' T'.inh = F.val
T.val = T'.syn
2 T' → * F T'1 T'1.inh = T'.inh * F.val
T'.syn = T'1.syn
3 T' → ε T'.syn = T'.inh ε
4 F → digit F.val = digit.lexval
T.val = 15
T'.inh = 3
F.val = 3 T'.syn = 15
●
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:
SS →
→ LL.. LL|| LL ●
What does this grammar generate?
LL→→ LLBB || BB ●
Design L-attributed SDD to compute S.val, the decimal
BB →
→ 00 || 11 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. 22
SDT Applications
●
Creating an explicit syntax tree.
++
– e.g., a – 4 + c -- cc
p1 = new Leaf(ida); aa 44
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) 23
SDT Applications
●
Creating an explicit syntax tree.
++
– e.g., a – 4 + c -- cc
●
Classwork: aa 44
Classwork:
Classwork:Write
Writeproductions
productionsand
andsemantic
semanticrules
rulesfor
forcreating
creating
type
typeexpressions
expressionsfrom
fromarray
arraydeclarations.
declarations. 25
SDD for Calculator
Sr. Production Semantic Rules
No.
1 E' → E $ E'.val = 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
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
27
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. 28
Parsing Stack
A→XYZ
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; } 29
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
Classwork:
Classwork:Write
WriteSDT
SDTfor
forinfix-to-prefix
infix-to-prefixtranslation.
translation. 30
Infix-to-Prefix
●
What is the issue with this SDT?
●
The SDT has shift-reduce and reduce-reduce conflicts.
●
Recall that each marker is an empty non-terminal. Thus, the parser
doesn't know whether to reduce or reduce or shift on seeing a digit.
●
Note that the grammar had no conflicts prior to adding actions.
●
Such an SDT won’t work with top-down or bottom-up parsing.
E' → E $
E → { print '+'; } E1 + T
E→T
T → { print '*'; } T1 * F
T→F
F → (E)
F → digit { print digit.lexval; }
Classwork:
Classwork:Write
WriteSDT
SDTfor
forinfix-to-prefix
infix-to-prefixtranslation.
translation.
31
Code Generation for while
●
We want to generate code for while-construct
– S → while ( C ) S1
● We assume that code for S1 and C are available.
●
We also (for now) generate a single code string.
●
Classwork: What all do we require to generate
this code?
– This would give us an idea of what attributes we
need and their types.
label
labelL1:L1:
CC C.code
ififtrue
truejump
jumpL2
L2else
elseL3
L3 S
label
labelL2:L2:
SS1 S1.code
1
jump
jumpL1 L1 32
label
labelL3:L3: S.next
Code Generation for while
●
Assume we have the following mechanism.
– newLabel() returns a new label name.
You may have used a similar one for temporaries.
– Each statement has an attribute next, that points to
the next statement to be executed.
– Each conditional has two branches true and false.
– Each non-terminal has an attribute code.
33
SDD for while
S → while ( C ) S1 L1
L1 ==newLabel();
newLabel();
L2
L2 ==newLabel();
newLabel();
SS1.next
.next ==L1;
L1;
1
C.false
C.false ==S.next;
S.next;
C.true
C.true ==L2;
L2;
S.code
S.code ==“label”
“label”++L1L1++
C.code
C.code++
”label”
”label”++L2L2++
SS1.code;
.code;
1
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; }
34
What
Whatis
isthe
thetype
typeof
ofthis
thisSDD?
SDD?
SDD for while
S → while ( C ) S1 L1
L1 ==newLabel();
newLabel();
L2
L2 ==newLabel();
newLabel();
SS1.next
.next ==L1;
L1;
1
C.false
C.false ==S.next;
S.next;
C.true
C.true ==L2;
L2;
S.code
S.code ==“label”
“label”++L1L1++
C.code
C.code++
”label”
”label”++L2L2++
SS1.code;
.code;
1
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
35
What
Whatis
isthe
thetype
typeof
ofthis
thisSDD?
SDD?
Homework
●
Exercises 5.5.5 from ALSU book.
36