The document discusses syntax directed translation (SDT). It defines SDT as grammar plus semantic rules. It provides examples of using SDT for arithmetic expressions, infix to postfix conversion, and generating intermediate code. It also discusses how to evaluate an SDT using a top-down and bottom-up approach. Finally, it defines the differences between synthesized and inherited attributes, and S-attributed and L-attributed SDT.
Seal of Good Local Governance (SGLG) 2024Final.pptx
compiler design ujjwal matoliya 2nd sem MCA.pptx
1. Submitted By
Ujjwal matoliya
Atal Bihari Vajpayee Vishwavidyalaya
Department of Computer Science and Information
Session 2022-23
(SDT)Syntax Directed Translation
2. INDEX
Semantic Analizer
Application of Syntax Directed Translation
How to Evaluate a SDT
Differences between Synthesized and Inherited
Attributes
S-attributed SDT
L-attributed SDT
7. Syntax directed translation
In syntax directed translation, along with the
grammar we associate some informal
notations and these notations are called as
semantic rules.
So we can say that
Grammar + semantic rule = SDT (syntax directed translati
on)
8. Application of Syntax Directed Translation
We use SDT(Syntax Directed Translation) for
Executing Arithmetic Expressions
Conversion from infix to postfix expression
Conversion from infix to prefix expression
For Binary to decimal conversion
Counting the number of Reductions
Creating a Syntax tree
Generating intermediate code
Storing information into the symbol table
Type checking
9. Production Semantic Rules
E → E # T / T E.val := E.val * T.val
T → T & F / F T.val := T.val + F.val
F →id F.val := id.l.val
15. S
A S 1
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
16. S
A S 1
A B 2
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
17. S
A S 1
A B 2
d B 5
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
18. S
A S 1
A B 2
d B 5
b C 4
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
19. S
A S 1
A B 2
d B 5
b C 4
C 6
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
20. S
A S 1
A B 2
d B 5
b C 4
C 6
Top Down
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
21. S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
S
A S 1
A B 2
d B 5
b C 4
C 6
Top Down
Bottom Up
23. S
A S 1
A B 2
d B 5
b C 4
C 6
Top Down
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
A 3
A 3
24. S
A S 1
A B 2
d B 5
b C 4
C 6
Top Down
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
A 3
A 3
25. S
A S 1
A B 2
d B 5
b C 4
C 6
Bottom Up
S->AS {printf (1)}
S->AB {printf (2)}
A->a {printf (3)}
B->bc {printf (4)}
B->dB {printf (5)}
C->c {printf (6)}
A 3
A 3
31. S-attributed SDT :
If an SDT uses only synthesized attributes, it is called as S-attributed
SDT.
S-attributed SDTs are evaluated in bottom-up parsing, as the values
of the parent nodes depend upon the values of the child nodes.
Semantic actions are placed in rightmost place of RHS.
L-attributed SDT:
If an SDT uses both synthesized attributes and inherited attributes
with a restriction that inherited attribute can inherit values from left
siblings only, it is called as L-attributed SDT.
Attributes in L-attributed SDTs are evaluated by depth-first and left-
to-right parsing manner.
Semantic actions are placed anywhere in RHS.
Example : S->ABC, Here attribute B can only obtain its value either
from the parent – S or its left sibling A but It can’t inherit from its
right sibling C. Same goes for A & C – A can only get its value from
its parent & C can get its value from S, A, & B as well because C is
the rightmost attribute in the given production.
33. S.NO Synthesized Attributes Inherited Attributes
1.
An attribute is said to be Synthesized attribute if its
parse tree node value is determined by the attribute
value at child nodes.
An attribute is said to be Inherited
attribute if its parse tree node value is
determined by the attribute value at
parent and/or siblings node.
2. The production must have non-terminal as its head.
The production must have non-
terminal as a symbol in its body.
3.
A synthesized attribute at node n is defined only in
terms of attribute values at the children of n itself.
A Inherited attribute at node n is
defined only in terms of attribute
values of n’s parent, n itself, and n’s
siblings.
4.
It can be evaluated during a single bottom-up
traversal of parse tree.
It can be evaluated during a single
top-down and sideways traversal of
parse tree.
5.
Synthesized attributes can be contained by both the
terminals or non-terminals.
Inherited attributes can’t be
contained by both, It is only
contained by non-terminals.
6.
Synthesized attribute is used by both S-attributed
SDT and L-attributed SDT.
Inherited attribute is used by only L-
attributed SDT.