Beruflich Dokumente
Kultur Dokumente
Introduction
• We can associate information with a language
construct by attaching attributes to the grammar
symbols.
• A syntax directed definition specifies the values of
attributes by associating semantic rules with the
grammar productions.
Production Semantic Rule
E->E1+T E.code=E1.code||T.code||’+’
Q-> What is the postfix expression for this infix expression 2+3*4?
• It can be done in two ways:
• For top down parsing we need to write the action numbers in the parse
tree and whenever action proceeds, it will execute it.
• For Bottom up parsing whenever it reduces, action is performed.
Q2
S xxw {printf(1);}
|y {printf(2);}
wsz {printf(3);}
For String xxxxyzz, what will be the SDT?
Q3
E E*T {E.val= E.val * T.val;}
|T {E.val = T.val;}
T F-T {T.val = F.val –T.val;}
|F {T.val = F.val;}
F2 {F.val = 2;}
|4 {F.val = 4;}
How many reductions are there for the string w= 4-2-4*2?
Q4.
E E#T {E.val = E.val *T.val;}
|T { E.val = T.val;}
T T&F { T.val = T.val +F.val;}
|F {T.val = F.val;}
Fnum {F.val=num.lvalue;}
String is 2#3&5#6&4
Types of Attributes
In SDD, attributes are of two types:
1. Synthesized – Which are dependent on child and itself
2. Inherited – Depends on parent node, left sibling and itself
T FT’ T’inh = F.val
T.val = T’.syn
T’ *FT’ T’,.inh = T’.inh *F.val
T’.syn = T’,.syn
T’ ε T’.syn = T’.inh
F num F.val = num.lval
String is 3*5
Evaluation orders for SDD’s
Dependency graphs are a useful tool for determining an evaluation
order for the attribute instances in a given parse tree.
While an annotated parse tree shows the values of attributes, a
dependency graph helps us determining how those values can be
computed.
Dependency graph depicts the flow of information among the attribute
instances in a particular parse tree; an edge from one attribute
instance to another means that the value of the first is needed to
compute the second.
Topological Sort
• An ordering that embeds a directed graph into a linear order is known
as a topological sort of the graph.
• If there is any cycle in the graph, than there are no topological sorts;
that is there is no way to evaluate the SDD on this parse tree.
Example with Inherited Attributes
L
T in=real
type=real
addtype(id1,real)
L
in=real
id , id , id
real entry=id1 entry=id2 entry=id3
Syntax Tree
The main application for Syntax Directed Translation is the construction
of the syntax tree.
Eg: E E1+T E.node= new Node(‘+’,E1.node,T.node)
EE1-T E.node = new Node(‘-’,E1.node,T.node)
ET E.node=T.node
T(E) T.node = E.node
Tid T.node = new Leaf(id,id.entry)
Tnum T.node= new Leaf(num,num.val)
String a-4+c
S and L Attribute Definitions
• An SDD is S-attributed if every attribute is synthesized
• Implemented through Bottom up parsing