0% found this document useful (0 votes)
18 views14 pages

CD unit 3

Uploaded by

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

CD unit 3

Uploaded by

sudha yamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

At Syntax-Directed

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

Syntax Directed Translation:


Aformalist called as syntax directed definition is used fort specifying translations for
programming language constructs.
Asyntax directed definition is a generalization of a context free gramnmar in which cach
is
grammar symbol has associated set of attributes and each and each productions
associated with a set of' semantic rules

Definition of (syntax Directed definition ) SDID:


associated with
SDD is a generalization of CFG in which cach grammar productions X->u is
it a set of semantic rules of the fornm

a: f(bl,b2....bk)
function f.
Where a is an attributes obtained from the

"A syntax-directed definition is a generalization of a


context-free grammar in which:
attributes.
Each grammar symbol is associated with a set of
partitioned into two subsets called
This set of attributes for a grammar symbol is
Synthesized and inherited attributes of that grammar symbol.
Each production rule is associated with a set of semantic rules.
which can be represented by a
" Semantic rules set up dependencies between attributes
dependency graph.
order of these semantic rules.
" This dependency graph determines the evaluation
semantic rule may also
"Evaluation of asemantic rule detines the value of an attribute. But a
have some side effects such as printing a value.
The two attributes for non terminal are:

1)Synthesized attribute (S-attribute) : (1)


parse tree node is determined
An attribute is said to be synthesized attribute if its value at a
from attribute values at the children of the node

2) Inherited attribute: (1,)


80
An inherited attribute is one whose value at parse tree node is
determined in terms of
attributes at the parent and or siblings of that node.
The attribute can be string. a number, a type, a. memory location or anything else.
The parse tree showing the value of attributes at each node is called an annotated parse
tree.

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.

Annotated Parse Tree

" 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.

3. The value obtained at the node is supposed to be final output.


PROBLEM 1:

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:

Production rule Semantic actions

L’En L.val=E.val

E+El+T E.val-El.val + T.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

Eal=s digit lexal=4

Digitlexval=s

Digitlexval=3

Dependency Graphs:

Figure 5.6. E. val is synthesized from Ej.val and E.val


val

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

Postfix Translation Schemes


change: the action for the
inmplements the desk calculator SDD with one
The postfix SDT SDD is S-attributed.
production prints the value. As the grammar is LR, and the
first
L+En print(E.val):}
}
E’El +T{E.val =El.val -+T.val
T.val }
E’EI-T{Eval = El.val
E’TE.val = T.val }
T’TI*F{ T.val = Tl.val *F.val }
T’F{T.val =F.val }
F’(E) {Fval =E.val }
F’ digit F.val = digit.lexval }
85
PRODUCTION SEMANTIC RULES
1) E- E+T E.node new Node('+', E.node, T.node)
2) E-’ E-T E.node new Node(',Enode, T.node)
3) E-T E.node Tnode
T.node E.node
4) T’ (E)
5) T> id T.node new Leaf (id, id.entry)
6) T-’ num T.node new Leaf(num, num.val)

Intermediate code forms:


a propram ereated by the
An intermediate code form of' source program is an intemal fomm of'
the proprann
compiler while translating the program ereated by the compiler while translating
code(machine code) an ntermediate
from a high level language to assembly code(or)object ssembly. An
code than does
source torm represents a more attractive fom of target produces an
optimizing Compiler performs optimizations on the intemediate source tom ad
object module.
Analysis tsyntheses translation

Creates n generate targe code


Intermediate code
In the analysis synthesis model of acompiler, the front-end translates a source program into
an intermediate representation trom which the back-end generates target code, in many
compilers the source code is translated into a language which is intemediate in complexity
between a HLL and machine code the usual intermediate code introduces symbols to stand
for various temporary quantities.

farser Static Intermediate Code


checker code generator generator

position of intermediate code generator


86
We assume that the source program has already been parsed and statically checked.. the
various intermediate code forms are:

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:

The parse tree itself is a useful


intermediate-language representation for a source program,
especially in optimizing compilers where the intermediate code needs to extensively
Yestructure.
A parse tree, however, often contains redundant information which can be eliminated, Thus
producing a more economical representation of the source program. One such variant ofa
87
parse tree is what is called an (abstract) syntax tree, a tree in which each leaf represents an
operand and cach interior node an operator.
Exmples:
I) Syntax tree for the expression a*(btcd

2) syntax tree for if a=b then a:=c+d else b:=c-d

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

(a) DAG (b) Three-address code

Figure 6.8: A DAG and its corresponding three-address code


88
Problems:
Write the 3-address code for the following expression
1. if(x +y * z>x* y +z)
a-0;
2. (2 + a * (b-c/d)) /e
3. A :=b *-c+b*-c

Address and Instructions

"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

(a) Symbolic labels. (b) Position numbers.


The multiplication i * 8is appropriate for an array of elements that each take 8 units of space.

C. quadruples: for the


" Three-address instructions can be implemented as objects or as record with fields
operator and operands.
" Three such representations
Quadruple. triples, and indirect triples
" Aquadruple (or quad) has four fields: op, argl, arg2, and result.
Example
D. Triples
"A triple has only three fields: op. argl, and arg2 position, rather by
" Using triples, we refer to the result ofan operation x op y by its
an explicit temporary name.
Example
op ary1 ar9» result
0minus t1
tË = minus c
t2 = b* t 1 t2

t3 = minus c 2minusi c t3
tË = b* t3 3 t3 t4

t t t4 ts
t5 =
a ts

(a) Three-address code (b) Quadruples

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

(a) Syntax tree (b) Triples

Fig: Representations of a=b* -c+b*-c

instruction argj aTg2


0minus i C
35 (0)
36| (1) 1 b (0)
37| (2) 2minus i C
38| (3) 3 b (2)
39 (4) 4 (1) T(3)
40 (5) 5 i. a i4)

Fig: Indirect triples representation of 3-address code


-> The benefit of Quadruples over Triples can be seen in an optimizing compiler, where
instructions are often moved around.
->With quadruples, if we move an instruction that computes a temporary t, then the
instructions that use t require no change. With triples, the result of an operation is referred to
by its position, so moving an instruction may require changing all references to that result.
This problem does not occur with indirect triples.
Single-Assignment Static Form
Static single assignment form (SSA) is an intermediate representation that facilitates certain
code optimization.
"Two distinct aspects distinguish SSA from three -address code.
91
All assignments in SSA are to variables with distinct names; hence the term static single
assignment.
p a + b P a +
q= p - c
p q*d d
P e P
q p+ q 9: P + 41
(a) Threddress code (b) Static single-aiument forn

Iutermediate programin three-address code and SSA

If (lag) x=-1;else x=1;


Y=x* a;
If(flag)xl =-1;else x2=1; x3=(x1,x2);

92

You might also like