cd-unit-3-detailed-notes
cd-unit-3-detailed-notes
o 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:
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.
o The general approach to SDT is to construct a parse tree or a syntax tree and compute
the values of attributes at the nodes of the tree by visiting them in some order. In
many cases translation can be done during parsing without building an explicit tree.
EXAMPLE:
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
EXAMPLE:
1. Construct a Parse Tree for the input 2+3*4 by using the grammar and its
corresponding semantic rules given below:
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S→E$ { printE.VAL }
Tree Traversal for the grammar & semantic rules corresponding to the grammar:
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Intermediate code is used to translate the source code into the machine code.
Intermediate code lies between the high-level language and the machine language.
o If the compiler directly translates source code into the machine code without generating
intermediate code, then a full native compiler is required for each new machine.
o The intermediate code keeps the analysis portion same for all the compilers that's why
it doesn't need a full compiler for every unique machine.
o Intermediate code generator receives input from its predecessor phase and semantic
analyzer phase. It takes input in the form of an annotated syntax tree.
o Using the intermediate code, the second phase of the compiler synthesis phase is
changed according to the target machine.
o It simplifies the task of generation of optimal target code.
o It involves using at most one operation or one test in each statement.
o It introduces symbols to stand for temporary quantities.
o Intermediate representation:
Intermediate code can be represented in two ways:
(i) High Level intermediate code:
High level intermediate code can be represented as source code. To enhance
performance of source code, we can easily apply code modification. But to
optimize the target machine, it is less preferred.
(ii) Low Level intermediate code
Low level intermediate code is close to the target machine, which makes it suitable
for register and memory allocation etc. it is used for machine-dependent
optimizations.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
o Postfix notation is the useful form of intermediate code if the given language is
expressions.
o Postfix notation is also called as 'suffix notation' and 'reverse polish'.
o Postfix notation is a linear representation of a syntax tree.
o In the postfix notation, any expression can be written unambiguously without
parentheses.
o The ordinary (infix) way of writing the sum of x and y is with operator in the middle:
x * y. But in the postfix notation, we place the operator at the right end as xy *.
o In postfix notation, the operator follows the operand.
o 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:
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Solution:
t1 := -c
t2 := b*t1
t3 := -c
t4 := d * t3
t5 := t2 + t4
a := t5
-(a x b) + (c + d) – (a + b + c + d)
Solution:
(1) T1 = a x b
(2) T2 = uminus T1
(3) T3 = c + d
(4) T4 = T2 + T3
(5) T5 = a + b
(6) T6 = T3 + T5
(7) T7 = T4 – T6
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Solution:
(2) T1 = 0
(4) T1 = 1
(5) Stop
Solution:
(4) t = 0
(6) t = 1
(7) Stop
IX. QUADRUPLES
The quadruples have four fields to implement the three address code. The field of
quadruples contains the name of the operator, the first source operand, the second
source operand and the result respectively.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
EXAMPLE:
1. Write a Three Address Code for the given expression and construct a
quadruples table corresponding to that three address code.
a := -b * c + d
Solution:
(0) uminus b - t1
(1) + c d t2
(2) * t1 t2 t3
(3) := t3 - a
X. TRIPLES
The triples have three fields to implement the three address code. The field of triples
contains the name of the operator, the first source operand and the second source
operand.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
EXAMPLES:
1. Write a Three Address Code for the given expression and construct a quadruples
table corresponding to that three address code.
a := -b * c + d
Solution:
Three address code is as follows:
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
These statements are represented by triples as follows:
(0) minus b -
(1) + c d
(3) := (2) -
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Quadruple Representation-
(0) ↑ e f T1
(1) x b c T2
(2) / T2 T1 T3
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
(3) x b a T4
(4) + a T3 T5
(5) + T5 T4 T6
Triple Representation-
(0) ↑ e f
(1) x b c
(3) x b a
(4) + a (2)
Statement
35 (0)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
36 (1)
37 (2)
38 (3)
39 (4)
40 (5)
(0) ↑ e f
(1) x b e
(3) x b a
(4) + a (2)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
(1) T1 = uminus c
(2) T2 = b x T1
(3) T3 = uminus c
(4) T4 = b x T3
(5) T5 = T2 + T4
(6) a = T5
Now, we write the required representations-
Quadruple Representation-
(1) uminus c T1
(2) x b T1 T2
(3) uminus c T3
(4) x b T3 T4
(5) + T2 T4 T5
(6) = T5 a
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
(1) uminus c
(2) x b (1)
(3) uminus c
(4) x b (3)
(6) = a (5)
Statement
35 (1)
36 (2)
37 (3)
38 (4)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
39 (5)
40 (6)
(1) uminus c
(2) x b (1)
(3) uminus c
(4) x b (3)
(6) = a (5)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → id :=E {p = look_up(id.name);
If p ≠ nil then
Emit(p = E.place)
Else
Error;
}
E → E1 + E2 {E.place = newtemp();
Emit(E.place = E1.place '+' E2.place)
}
E → E1 * E2 {E.place = newtemp();
Emit (E.place = E1.place '*' E2.place)
}
E → id {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}
Boolean expressions have two primary purposes. They are used for computing the
logical values. They are also used as conditional expression using if-then-else or while-
do.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
E → E1 OR E2 {E.place = newtemp();
Emit (E.place ':=' E1.place 'OR' E2.place)
}
E → E1 + E2 {E.place = newtemp();
Emit (E.place ':=' E1.place 'AND' E2.place)
}
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
o The EMIT function is used to generate the three address code and the newtemp( )
function is used to generate the temporary variables.
o The E → id relop id2 contains the next_state and it gives the index of next three
address statements in the output sequence.
o Here is the example which generates the three address code using the above translation
scheme:
p>q AND r<s OR u>r
100: if p>q goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if r>s goto 107
105: t2:=0
106: goto 108
107: t2:=1
108: if u>v goto 111
109: t3:=0
110: goto 112
111: t3:= 1
112: t4:= t1 AND t2
113: t5:= t4 OR t3
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Multi-dimensional arrays:
Row major or column major forms
Row major: a[1,1], a[1,2], a[1,3], a[2,1], a[2,2], a[2,3]
Column major: a[1,1], a[2,1], a[1, 2], a[2, 2],a[1, 3],a[2,3]
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → L := E
E → E+E
E → (E)
E → L
L → Elist ]
L → id
Elist → Elist, E
Elist → id[E
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
L → id {L.place = lookup(id.name);
L.offset = null;
}
Where:
o ndim denotes the number of dimensions.
o limit(array, i) function returns the upper limit along with the dimension of array
o width(array) returns the number of byte for one element of array.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida