Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

UNIT-3

Syntax directed translation


In syntax directed translation, along with the grammar we associate some informal
notations and these notations are called as semantic rules.

So, we can say that

Grammar + semantic rule = SDT (syntax directed translation)

o In syntax directed translation, every non-terminal can get one or more than one
attribute or sometimes 0 attribute depending on the type of the attribute. The
value of these attributes is evaluated by the semantic rules associated with the
production rule.

o In the semantic rule, attribute is VAL and an attribute may hold anything like a
string, a number, a memory location and a complex record.

o In Syntax directed translation, whenever a construct encounters in the


programming language then it is translated according to the semantic rules
define in that particular programming language.

Example

Production Semantic Rules

E→E+T E.val := E.val + T.val

E→T E.val := T.val

T→T*F T.val := T.val * F.val

T→F T.val := F.val

F → (F) F.val := F.val

F → num F.val := num.lexval

E.val is one of the attributes of E.

num.lexval is the attribute returned by the lexical analyzer.

Mr. Abhinav Gupta (CS&E Department) 1


Syntax directed translation scheme
o The Syntax directed translation scheme is a context -free grammar.
o The syntax directed translation scheme is used to evaluate the order of semantic
rules.
o In translation scheme, the semantic rules are embedded within the right side of
the productions.
o The position at which an action is to be executed is shown by enclosed between
braces. It is written within the right side of the production.

Production Semantic Rules

S→E$ { printE.VAL }

E→E+E {E.VAL := E.VAL + E.VAL }

E→E*E {E.VAL := E.VAL * E.VAL }

E → (E) {E.VAL := E.VAL }

E→I {E.VAL := I.VAL }

I → I digit {I.VAL := 10 * I.VAL + LEXVAL }

I → digit { I.VAL:= LEXVAL}

Implementation of Syntax directed translation


Syntax direct translation is implemented by constructing a parse tree and performing
the actions in a left to right top-down order.
SDT is implementing by parse the input and produce a parse tree as a result.

Production Semantic Rules

S→E$ { printE.VAL }

E→E+E {E.VAL := E.VAL + E.VAL }

E→E*E {E.VAL := E.VAL * E.VAL }

E → (E) {E.VAL := E.VAL }

E→I {E.VAL := I.VAL }

I → I digit {I.VAL := 10 * I.VAL + LEXVAL }

I → digit { I.VAL:= LEXVAL}

Mr. Abhinav Gupta (CS&E Department) 2


Parse tree for SDT:

Fig: Semantic Parse tree

Example
E -> E+T | T
T -> T*F | F
F -> id

This is a grammar to semantically validate an expression having additions and


multiplications in it. Now, to carry out semantic analysis we will augment SDT rules to
this grammar, in order to pass some information up the parse tree and check for
semantic errors, if any. In this example, we will focus on the evaluation of the given
expression, as we don’t have any semantic declaration to check in this very basic
example.

E -> E+T { E.val = E.val + T.val }


E -> T { E.val = T.val }
T -> T*F { T.val = T.val * F.val }
T -> F { T.val = F.val }
F -> id { F.val = id.lexval }

Mr. Abhinav Gupta (CS&E Department) 3


For understanding translation rules further, we take the first SDT augmented to [ E ->
E+T ] production rule. The translation rule in consideration has val as an attribute for
both the non-terminals – E & T. Right-hand side of the translation rule corresponds to
attribute values of the right-side nodes of the production rule and vice-versa.
Generalizing, SDT are augmented rules to a CFG that associate 1) set of attributes to
every node of the grammar and 2) a set of translation rules to every production rule
using attributes, constants, and lexical values.

Let’s take a string to see how semantic analysis happens:– S = 2+3*4.


Parse tree corresponding to S would be

To evaluate translation rules, we can employ one depth-first search traversal on the
parse tree. This is possible only because SDT rules don’t impose any specific order on
evaluation until children’s attributes are computed before parents for a grammar
having all synthesized attributes. Otherwise, we would have to figure out the best-
suited plan to traverse through the parse tree and evaluate all the attributes in one or
more traversals. For better understanding, we will move bottom-up in the left to right
for computing the translation rules of our example.

The above diagram shows how semantic analysis could happen.

Mr. Abhinav Gupta (CS&E Department) 4


Intermediate code
In the analysis-synthesis model of a compiler, the front end of a compiler
translates a source program into an independent intermediate code, then the
back end of the compiler uses this intermediate code to generate the target
code (which can be understood by the machine).

Commonly used intermediate code representation


Postfix Notation
The ordinary (infix) way of writing the sum of a and b is with an operator in the
middle: a + b. The postfix notation for the same expression places the operator
at the right end as ab +. Also known as reverse Polish notation or suffix
notation.

In general, if e1 and e2 are any postfix expressions, and + is any binary


operator, the result of applying + to the values denoted by e1 and e2 is postfix
notation by e1e2 +.

No parentheses are needed in postfix notation because the position and


number of arguments of the operators permit only one way to decode a postfix
expression. In postfix notation, the operator follows the operand.

Example 1: The postfix representation of the expression


(a + b) * c
is : ab + c *

Example 2: The postfix representation of the expression


(a – b) * (c + d) + (a – b)
is : ab – cd + *ab -+

Example 3:
We can also evaluate the postfix expression with the help of STACK
If String ab+c*, where a=1, b=3, c=5. So, string becomes 13+5*
Then
1. push 1
2. push 3
3. add the two topmost elements, and result is 4
4. push 5
5. multiply the two topmost elements and result is 20

Mr. Abhinav Gupta (CS&E Department) 5


Parse tree and Syntax tree
When you create a parse tree then it contains more details than actually needed. So, it
is very difficult to compiler to parse the parse tree.
Take the following parse tree as an example:

o In the parse tree, most of the leaf nodes are single child to their parent nodes.
o In the syntax tree, we can eliminate this extra information.
o Syntax tree is a variant of parse tree. In the syntax tree, interior nodes are
operators and leaves/leafs are operands.
o Syntax tree is usually used when represent a program in a tree structure.

A sentence id + id * id would have the following syntax tree:

Abstract syntax tree can be represented as:

Abstract syntax trees are important data structures in a compiler. It contains the least
unnecessary information.

Mr. Abhinav Gupta (CS&E Department) 6


Three address code
o Three-address code is an intermediate code. It is used by the optimizing
compilers.
o In three-address code, the given expression is broken down into several separate
instructions. These instructions can easily translate into assembly language.
o Each Three address code instruction has at most three operands. It is a
combination of assignment and a binary operator.

Example: Convert the expression a * – (b + c) into three address code.

Example: Convert the expression a := (-c * b) + (-c * d) into three address code.
Three-address code is as follows:
t1 := -c
t2 := t1*b
t3 := t1*d
t4 := t2 + t3
a := t4
t is used as registers in the target program.

Example: Convert the expression


while(a>b) and a<2*b-5
do a=a+b into three address code.
Three-address code is as follows:
1. if a>b goto 3
2. goto 8
3. t1 = 2 * b
4. a < t1 – 5 goto 6
5. goto 8
6. a=a+b
7. goto 1
8. STOP

Mr. Abhinav Gupta (CS&E Department) 7


Example: Convert the expression
while(a>b)
if(c>d)
c=c-d*e;
else
c=c+d*e; into three address code.
Three-address code is as follows:
1. if a>b goto3
2. goto11
3. if c>d goto5
4. goto8
5. t1=d*e
6. c=c-t1
7. goto1
8. t2=d*e
9. c=c+t2
10. goto1
11. STOP
Example: Convert the expression
while(a<10)
{
x=0;
a=a+1;
} into three address code.
Three-address code is as follows:
1. if a<10 goto 3
2. goto 6
3. x=0;
4. a=a+1;
5. goto 1
6. STOP
Example: Convert the expression
switch(x+y)
{
case 1: c=d;
break;
case 2: g=h;
case 3: i=j;
k=1;
break;
default: a=b;
break;
}
p=g; into three address code.

Mr. Abhinav Gupta (CS&E Department) 8


Three-address code is as follows:
t1=x+y
goto L0
label L0: if t1=1 goto L1
if t1=2 goto L2
if t1=3 goto L3
goto L4
label L1: c=d
goto L5
label L2: g=h
label L3: i=j
k=1
goto L5
label L4: a=b
goto L5 //optional
label L5: p=g

Implementation of Three Address Code –


There are 3 representations of three address code namely: 1. Quadruple
2. Triples
# denotes pointer location 3. Indirect Triples
1. Quadruple –
It is structure with consist of 4 fields namely op, arg1, arg2 and result.
op denotes the operator and arg1 and arg2 denotes the two operands and result is
used to store the result of the expression.
Example – Consider expression a = b * – c + b * – c.
The three address code is:
t1 = uminus c
t2 = b * t1
t3 = uminus c
t4 = b * t3
t5 = t2 + t4
a = t5

Mr. Abhinav Gupta (CS&E Department) 9


2. Triples –
This representation doesn’t make use of extra temporary variable to represent a
single operation instead when a reference to another triple’s value is needed, a
pointer to that triple is used. So, it consist of only three fields namely op, arg1 and
arg2.
Example – Consider expression a = b * – c + b * – c

3. Indirect Triples –
This representation makes use of pointer to the listing of all references to
computations which is made separately and stored. It’s similar in utility as compared
to quadruple representation but requires less space than it. Temporaries are implicit
and easier to rearrange code.

Example – Consider expression a = b * – c + b * – c

Mr. Abhinav Gupta (CS&E Department) 10


Question – Write quadruple, triples and indirect triples for following expression :
(x + y) * (y + z) + (x + y + z)

Explanation – The three address code is: t1 = x + y


t2 = y + z
t3 = t1 * t2
t4 = t1 + z
t5 = t3 + t4

Mr. Abhinav Gupta (CS&E Department) 11


Example: Write the quadruples, triple and indirect triple for the following expression:
(x+y)*(y+z)+(x+y+z)
Example: Translate the following arithmetic expression into quadruples and triples:
(i) x=y*z+y*-z
(ii) a=-b*(c+d)+b

Translation of Assignment Statements


In the syntax directed translation, assignment statement is mainly deals with
expressions. The expression can be of type real, integer, array…..
Consider the grammar
S → id := E
E → E1 + E2
E → E1 * E2
E → (E1)
E → id

The non-terminal E has two attributes:


 E.place, a name that will hold the value of E, and
 E.code, the sequence of three-address statements evaluating E.

The newtemp() is a function used to generate new temporary variables.

A function gen(…) to produce sequence of three address statements.


The translation scheme of above grammar is given below:

Production rule Semantic actions

S → id :=E S.code:= E.code || gen(id.place ‘=’ E.place)

E → E1 + E2 E.place := newtemp;
E.code := E1.code || E2.code
gen(E.place := E1.place '+' E2.place)

E → E1 * E2 E.place := newtemp;
E.code := E1.code || E2.code
gen(E.place := E1.place '*' E2.place)

E → (E1) E.code := E1.code


E.place := E1.place

E → id E.place:=id.place
E.code:= null

Mr. Abhinav Gupta (CS&E Department) 12

You might also like