0% found this document useful (0 votes)
135 views18 pages

Syntax Directed Translations Explained

Syntax directed translations associate semantic rules with the productions of a context-free grammar. This allows attaching semantic information to syntax trees generated by parses. A syntax directed definition specifies how attribute values are computed by rules associated with grammar productions. Attributes can store values like numbers, types, strings, and references. The document provides examples of syntax directed definitions for expression evaluation and discusses evaluation orders, attribute types, and constructing syntax trees.

Uploaded by

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

Syntax Directed Translations Explained

Syntax directed translations associate semantic rules with the productions of a context-free grammar. This allows attaching semantic information to syntax trees generated by parses. A syntax directed definition specifies how attribute values are computed by rules associated with grammar productions. Attributes can store values like numbers, types, strings, and references. The document provides examples of syntax directed definitions for expression evaluation and discusses evaluation orders, attribute types, and constructing syntax trees.

Uploaded by

Ayush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Syntax Directed Translations

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||’+’

• We may alternatively insert the semantic actions inside the grammar


E -> E1+T {print ‘+’}
Syntax Directed Definitions
• A SDD is a context free grammar with attributes and rules
• Attributes are associated with grammar symbols and rules with
productions
• Attributes may be of many kinds: numbers, types, table references,
strings, etc.
• Grammar + Semantic Rules = SDD
• Eg-> SDD for evaluation of expression

E E+T|T { E.value = E.value+T.value} {E.value=T.value}


T T*F|F {T.value = T.value*F.value} {T.value=F.value}
Fnum {F.val =num.lvalue}
How to carry out SDD on the parse tree for the string 2+3*4?
EE+T { printf(“+”); }
|T { }
T T*F { printf(“*”);}
|F { }
F num {printf(num.lval);}

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);}
wsz {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;}
F2 {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;}
Fnum {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

PRODUCTION SEMANTIC RULE


DTL L.in = T.type
T  int T.type = integer
T  real T.type = real
L  L1 , id L1.in = L.in
addtype(id.entry, L.in)
L  id addtype(id.entry, L.in)
Draw the Tree
Example real id1, id2 , id3 D
addtype(id3,real)
L
in=real
addtype(id2,real)

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)
EE1-T E.node = new Node(‘-’,E1.node,T.node)
ET E.node=T.node
T(E) T.node = E.node
Tid T.node = new Leaf(id,id.entry)
Tnum 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

• An SDD is L-attributed if each attribute is synthesized as well as


inherited.
• Implemented through Top Down Parsing.

You might also like