CD unit 3
CD unit 3
polishattributedandL-
statements,Intermediate
implementation,
CO7 CO6 the MODULE-I|I:SYNTAX-DIRECTED
TRANSLATION
end
of
notation
the Boolean
translations
Classify
Semantic
Analysis.
Describe code
unit
students
syntax attributed
andTranslation:
the syntax expressions generation:
into
different three
Intermediate directed are
directed
definitions;
Directed
Syntax
Outcomes
Course able address
intermediate Syntax
definitions andIntermediate
t0: translation
Code. flow-of-
code, directed
GENERATION
forms
&
translations
79 for Control intotypesforms
definitions,
conversion
three-address of
for statements. of
source
Translation
performing three
of
syntax programs-constructionINTERMEDIATE
CODE
AND
address
code,
schemes.
translation abstract
statements of
Knowledge
Level
Taxonomy)
Remember(Bloom's
syntax
Analyze
syntax
of trees,
simple and
trec,
its S
SYNTAX-DIRECTED TRANSLATION AND INTERMEDIATECODE GENERATION
a: f(bl,b2....bk)
function f.
Where a is an attributes obtained from the
The process of computing the attribute values at the node is called annotating or decorating
the parse tree.Terminals can have synthesized attributes, but not inherited attributes.
" Aparse tree showing the values of attributes at each node is called an Annotated parse
tree.
" The process of computing the attributes values at the nodes is called annotating (or
decorating) of the parse tree.
" Of course, the order of these computations depends on the dependency graph induced by
: the semantic rules.
Exl:1) Synthesized Attributes:
Ex: Consider the CFG:
S’ EN
E’E+T
E-E-T
E’I
T-’T*F
T+T/F
T-F
F’ (E)
F-digit
N’;
Solution: The syntax directed definition can be written for the above grammar by using
semantic actions for cach production.
Production rule Semantic actions
S-EN S.val-E.val
E -El+T E.val -El.val + T.val
E’E1-T E.val = El.val -T.val
E+T E.val -T.val
T’T*F T.val = T.val * F.val
TTF T.val =T.val F.val
81
F’ (E) F.val-E.val
T F T.val -F.val
F-digit F.val -digit.lexval
N; can be ignored by lexical Analyzer as: I
is terminating symbol
For the Non-terminals E,T and F the values can be obtained using the attribute "Val".
The taken digit has synthesized attribute "lexval".
In S-EN. symbol S is the start symbol. This rule is to print the final answer of expressed.
Following steps are followed to Compute S attributed definition
1. Write the SDD using the appropriate semantic actions for corresponding production rule of
the given Grammar.
2. The annotated parse tree is generated and attribute values are computed. The Computation
is done in bottom up manner.
Consider the string 5*6+7; Construct Syntax tree, parse tree and annotated tree.
Solution:
The corresponding annotated parse tree is shown below for the string 5*6+7;
Syntax tree:
82
Annotated
F-digit E+TE+E1+T L’En 3*5+4n. PROBLEM: Ex2: Advantages:
the Disadvantages:vernoty lexval=s
Digit Fval-s Tval=s
T’F T+T1*F Semantic
Eval=30
Ival30 parse
actionConsider SDDs
tree
and are digtlexyal-6 Fval-6 E.val=37
the :
also more
grammar efficient.
the readable I.val=7
annotated Fig: Jexyal=7
digit Fval-7
that Annotated
parse
tree N:
and
is
83 parse used hence
tree for useful
for Simple
the for
string desk specifications
calculator.
Obtain
Solution:
L’En L.val=E.val
BT E.val-T.val
T’T1*F T.val=T1.val*F.val
T+F T.val-F.val
F-E) F.val-E.val
Fdigit F.val-digit.lexval
The corresponding annotated parse tree U shown below, for the string 3*5+4n.
L.ral = 19n
E.val<19
E.val=15 T.ral =4
T.val=1S F.val=4
Digitlexval=s
Digitlexval=3
Dependency Graphs:
val val
84
sort: dependency
Dependency graph and topological node labeled by grammar symbol X. the
say a
For each parse-tree node, attribute associated with X. attribute
graph has a node for each production p defines the value of synthesized A.b
associated with a to
graph has an edge from X.cattribute
Ifa semantic rule Then the dependency
of X.c. of inherited
A.b in terms of the value production p defines the value B.c.
associated with a graph has an edge from X.a
to
If a semantic rule X.a. Then, the dependency
B.c in terms of the value
Translation
Applications of Syntax-Directed
Construction of syntax Trees represented by objects witha suitable number of
fields.
tree are
The nodes of the syntax op field that is the label of the node.
an
Each object will have additional fields as follows
leaf. A constructor
The objects will have additional field holds the lexical value for the
an
" If the node is a leaf,
val) creates a leaf object. for a leaf.
function Leaf (op, Leaf returns a pointer to a new record
"If nodes are viewed as records, the node has children
node, there are as many additional fields as the
Ifthe node is an interior
constructor function
in the syntax tree. A arguments:
fields for the k
Node takes two or more
creates an object with first field op and &additional
Node (op,cl,c2,...ck)
children cl,c2,....ck
Syntax-Directed Translation Schemes embedded within
context-free grammar with progranm fragments any
À SDT scheme is a program fragments are called semantic actions and can appear at
production bodies.The
body.
position within the production first building a parse tree and then pre-forming the actions
Any SDT can be implemented by
first order. i.e during preorder traversal.
in a lett-to-right depth two important classes of SDD's
The use of SDT's to implement
parsable, then SDD is S-attributed.
1. If the grammar is LR parsable, then SDD is L-attributed.
2. Ifthe grammar is LL
The ordinary (infix) way of writing the sum of a and b is with the operator in the middle:
atb. the postfix (or postfix polish)notation for the same expression places the operator at the
right end, as ab+.
In general, if el and e2 are any postfix expressions, and Øto the values enoted by el and e2
is indicated in postfix notation nby ele20.no parentheses are needed in postfix notation
because the position and priority (number of arguments) of the operators permits only one
way to decode a postfix expression.
Example:
1. (a+b)*c in postfix notation is ab+c*,since ab+ represents the infix expression(atb).
2. a*(btc)is abc+* in postfix.
3. (atb)*(c+d) is abtcd+* in postfix.
Postfix notation can be generalized to k-ary operators for any k>=1.if k-ary operator is
applied to postfix expression el,e2,.......,then the result is denoted by ele2.....k.o.
if we know the priority of each operator then we can uniquely decipher any postfix
expression by scanning it from either end.
Example:
Consider the postfix string abtc*.
The right hand * says that there are two arguments to its left. since the next
-to-rightmost
symbol is c, simple operand, we knowc must be the second operand of *.continuing to the
left, we encounter the operator +.we know the sub expression ending in + makes up the first
operand of *.continuing in this way ,we deduce that abtc* is "parsed" as (((a.b)+),c)*.
a. Syntax tree:
If.-hen--else
Three-AddressCode:
" In three-address code, there is at most one operator on the right side of aninstruction; that is,
no built-up arithmetic expressions are permitted.
xty*z tl=y *zt2
=x+ tl
"Example
t} = b-c
t: = a * t
ts = a + t:
d
tË = t) * d
t.; = t. t ti
"Example Three-address code is built from two concepts: addresses and instructions.
An address can be one of the following:
Aname: Asource name is replaced by a pointer to its symbol table entry.
"A name: For convenience, alloW Source-program names to
Appear as addresses in three-address code. In an
Implementation, a source name is replaced by a pointer to
its symbol-table entry, where all information about the name is kept.
A constant
" A constant: In practice, a compiler must deal with many different types of constants and
variables
Acompiler-generated temporary
" A compiler-generated temporary. It is useful, especially in optimizing compilers, to
create a distinct name each time a temporary is needed. These temporaries can be combined,
if possible, when registers are allocated to variables.
A list of common three-address instruction forms:
Assignment statements
- X=y op z, where op is a binary operation
x= op y, where op is a unary operation
-Copy statement: x=y
Indexed assignments: x=y[i] and x[il=y
Pointer assignments: x-&y, *x=y and x=*y
Control flow statements
- Unconditional jump: goto L
Conditional jump: ifx relop y goto L; if x goto L; if False x goto L
Procedure calls: call procedure p with n parameters and return y, is
Optional
param xl
param x2
param Xn
call p, n
- do i=i+1; while (a[il<v);
89
L: tË = i + 1 100: tË = i + 1
i = tË 101: i = t1
t = i * 8 102: t = i * 8
tg = a [ t ] 103: t3 = a [ t ]
if t: < v goto L 104: if t3 < v goto 100
t3 = minus c 2minusi c t3
tË = b* t3 3 t3 t4
t t t4 ts
t5 =
a ts
d. Triples:
"A triple has only three fields: op. argl, and arg2
90
an
" Using triples, we refer to the result of an operation x op y by its position, rather by
explicit temporary name.
Example
op
0 minus c
a Tb(0)
2 minus C
3 b2)
minus b minus 4 + (1) (3)
5 a T(4)
C C
92