0% found this document useful (0 votes)
82 views

Syntax Directed Translation

Syntax directed translation attaches attributes and semantic actions to context-free grammar productions to help generate intermediate code from the parse tree. It defines semantic rules for computing attributes to evaluate them in a defined order like postorder traversal. Common intermediate representations include postfix notation, syntax trees, and three-address code using quadruples or triples.

Uploaded by

VIRENDRA VERMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Syntax Directed Translation

Syntax directed translation attaches attributes and semantic actions to context-free grammar productions to help generate intermediate code from the parse tree. It defines semantic rules for computing attributes to evaluate them in a defined order like postorder traversal. Common intermediate representations include postfix notation, syntax trees, and three-address code using quadruples or triples.

Uploaded by

VIRENDRA VERMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 5

Syntax Directed Translation


Outline
Syntax Directed Definitions
Evaluation Orders of SDD’s
Applications of Syntax Directed Translation
Syntax Directed Translation Schemes
Introduction

After implementing the Semantic Analysis, the source program is modified


to an intermediate form.
There is some information that is required by the Intermediate code
generation phase to convert the semantically checked parse tree into
intermediate code. But this information or attributes of variables cannot be
represented alone by Context- Free Grammar.
So, some semantic actions have to be attached with Context-Free grammar
which helps the intermediate code generation phase to generate intermediate
code.
So, Attaching attributes to the variables of the context Free Grammar and
defining semantic action (meaning) of each production of grammar is
called Syntax Directed Translation.
Conceptual view of SDT
Input String----->Parse Tree------->Dependency
Graph------>evaluation order for semantic rules
Semantic Actions − It is an action that is executed
whenever the Parser will recognize the input string generated
by context-free grammar.
For Example, A → BC {Semantic Action}

Semantic Action is written in curly braces Attached with a


production.
In Top-Down Parser, semantic action will be taken when
A will be expanded to derive BC which will further derive
string w.
In Bottom-Up Parser, Semantic Action is generated when
BC is reduced to A.
Semantic Action can perform −

•Computation of value of variables


S → S(1) + S(2) {S. VAL = S(1). VAL + S(2). VAL}
Here S. VAL will compute the sum of S(1) and S(2)values.

•Printing of Error Messages


A → BC {error ( ); }
Whenever A will be expanded to BC, an error function will be called
to print an error message.
Example
S.No Production Semantic Rules
1. E→E+T E.val := E.val + T.val
2. E→T E.val := T.val
3. T→T*F T.val := T.val * F.val
4. T→F T.val := F.val
5. F → (F) F.val := F.val
6. F → num F.val := num.lexval
Advantages of Syntax Directed Translation in Compiler Design
•SDT in compiler design provides an easy way to specific transitions and grammar rules. It makes
an easy method to translate= programming languages.

•SDT maintains the compiler and is easy to modify as it separates the translation process from the
parsing method.

•It optimises the translation process making it code-effective.


Disadvantages of Syntax Directed Translation in Compiler Design
•Other translation methods, like attribute grammar, have increased expressiveness. This decreases
the performance of SDT.

•Also, SDT does not perform well when the translations are complex.

•In addition to that, the process of error fixing and error location is not easy in SDT. This happens
in the case of complex translations.
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.
Synthesized attributes
A synthesized attribute at node N is defined only in
terms of attribute values of children of N and at N itself
Inherited attributes
An inherited attribute at node N is defined only in terms
of attribute values at N’s parent, N itself and N’s siblings
Example of S-attributed SDD
Production Semantic Rules
1) L -> E n L.val = E.val
2) E -> E1 + T E.val = E1.val + T.val
3) E -> T E.val = T.val
4) T -> T1 * F T.val = T1.val * F.val
5) T -> F T.val = F.val
6) F -> (E) F.val = E.val
7) F -> digit F.val = digit.lexval
Example of mixed attributes
Production Semantic Rules
1) T -> FT’ T’.inh = F.val
T.val = T’.syn
2) T’ -> *FT’1 T’1.inh = T’.inh*F.val
T’.syn = T’1.syn
3) T’ -> ε T’.syn = T’.inh
1) F -> digit F.val = digit.lexval
SDD with inherited attribute
Annotated parse tree
Parse tree with inherited
attribute
Evaluation orders for SDD’s
A dependency graph is used to determine the order of
computation of attributes
Dependency graph
For each parse tree node, the parse tree has a node for
each attribute associated with that node
If a semantic rule defines the value of synthesized
attribute A.b in terms of the value of X.c then the
dependency graph has an edge from X.c to A.b
If a semantic rule defines the value of inherited
attribute B.c in terms of the value of X.a then the
dependency graph has an edge from X.c to B.c
Ordering the evaluation of
attributes
If dependency graph has an edge from M to N then M
must be evaluated before the attribute of N
Thus the only allowable orders of evaluation are
those sequence of nodes N1,N2,…,Nk such that if
there is an edge from Ni to Nj then i<j
Such an ordering is called a topological sort of a
graph
S-Attributed definitions
An SDD is S-attributed if every attribute is synthesized
We can have a post-order traversal of parse-tree to
evaluate attributes in S-attributed definitions

postorder(N) {
for (each child C of N, from the left) postorder(C);
evaluate the attributes associated with node N;
}
S-Attributed definitions can be implemented during bottom-
up parsing without the need to explicitly create parse trees
L-Attributed definitions
A SDD is L-Attributed if the edges in dependency graph
goes from Left to Right but not from Right to Left.
More precisely, each attribute must be either
 Synthesized
 Inherited, but if there us a production A->X1X2…Xn and
there is an inherited attribute Xi.a computed by a rule
associated with this production, then the rule may only use:
 Inherited attributes associated with the head A
 Either inherited or synthesized attributes associated with the
occurrences of symbols X1,X2,…,Xi-1 located to the left of Xi
 Inherited or synthesized attributes associated with this occurrence
of Xi itself, but in such a way that there is no cycle in the graph
Intermediate Code Generation
The following are commonly used intermediate representations:

1. Postfix notation
2. Syntax tree
3. Three-address code
Postfix notation
Syntax Tree
The syntax tree is nothing more than a condensed form of the parse tree.

Parse tree for the string id+id*id. Syntax tree for id+id*id
Three Address Code
Three address code is a sequence of statements of the
form x = y op z. Since a statement involves no more
than three references, it is called a "three-address
statement," and a sequence of such statements is
referred to as three-address code. For example, the
three-address code for the expression A + B * C + D is:
Sometimes a statement might contain less than three
references; but it is still called a three-address
statement. The following are the three-address
statements used to represent various programming
language constructs:
 Used for representing arithmetic expressions:
Used for representing Boolean expressions:

 Used for representing array references and


dereferencing operations:
Representing 3-address
statements
 Quadruple Representation

Operator Operand1 Operand2 Result

(1) + a b t1
(2) - c t2

(3) * t1 t2 t3
(4) / t3 d t4
(5) = t4 x

Quadruple Representation of x = (a + b) *  c/d


 Triple Representation

Operator Operand1 Operand2


(1) + a b
(2) - c
(3) * (1) (2)
(4) / (3) d
(5) = x (4)

Triple Representation of x = (a + b) *  c/d


 Indirect Triple Representation
 uses an additional array to list the pointers to the triples in the desired order.
Operator Operand1 Operand2
(1) (1) + a b
(2) (2) - c
(3) (3) * (1) (2)
(4) (4) / (3) d
(5) (5) = x (4)

Indirect Triple Representation of x = (a + b) *  c/d


Comparison
By using quadruples, we can move a statement that computes A without requiring any
changes in the statements using A, because the result field is explicit. However, in a
triple representation, if we want to move a statement that defines a temporary value,
then we must change all of the pointers in the operand1 and operand2 fields of the
records in which this temporary value is used. Thus, quadruple representation is easier
to work with when using an optimizing compiler, which entails a lot of code
movement. Indirect triple representation presents no such problems, because a separate
list of pointers to the triple structure is maintained. When statements are moved, this
list is reordered, and no change in the triple structure is necessary; hence, the utility of
indirect triples is almost the same as that of quadruples.
 if a<b then x = y + z else p = q + r
 while a<b do x = y + z
 do x = y + z while a<b
 repeat x = y + z until a<b
 for(i = 1; i< = 20; i+ +) x = y + z
switch (i + j )
{
case 1: x = y + z
default: p = q + r
case 2: u = v + w
}
a[i] is an array of size 20 and 4 bytes per word (bpw)
Translation:

t1=i*4
t2=addr(a)-4
t3=t2[t1]
a[i,j] is an array of size 30X40 and 4 bytes per word
(bpw)
Translation:

t1=i*40
t1=t1+j
t1=t1*4
t2=addr(a)-164
t3=t2[t1]
main()
{ int i = 1;
int a[10];
while(i <= 10)
a[i] =0 ;
}
The three-address code for the above C program is:

1.i=1
2.if i <= 10 goto(4)

3.goto(8)

4.t1 = i * width

5.t2 = addr(a)  width

6.t2[t1] = 0

7.goto(2)

where width is the number of bytes required for each element.


Generate the three-address code for the following
program fragment:
while (A < C and B > D)
do if A = 1
then
C = C+1
else
while A <= D do
A=A+3
Consider the program fragment:
sum = 0
for(i = 1; i<= 20; i++)
sum = sum + a[i] + b[i];

and generate the three-address code for it. There are


four bytes per word.

You might also like