Unit-3
Unit-3
Unit: 3
Subject:
Compiler Design
Rohit Chaudhary
Course Details Assistant Professor
B Tech CSE 5th Sem CSE Department
Rohit Chaudhary
Designation: Assistant Professor CSE Department
NIET, Grater Noida
Qualification:
B.Tech (CSE) in 2010
M.Tech (CSE) in 2013
Teaching experience: 10 year +
Research Publications
Computer Science
Compiler technology can be used to translate the binary code for
one machine to that of another, allowing a machine to run
programs originally compiled for another instruction set. Binary
translation technology has been used by various computer
companies to increase the availability of software for their machines
Sr. Course Outcome PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
No
1 ACSE0504.1 3 3 2 3 1 - - - 1 - - 2
2 ACSE0504.2 3 3 3 3 1 - - 1 1 - - 2
3 ACSE0504.3 3 3 3 3 - - - 1 1 - - 2
4 ACSE0 504.4 3 2 2 2 - - - 1 1 - - 2
5 ACSE0 504.5 3 3 3 3 - - - 1 1 - - 2
ACSE0504.1 3 2 3 2
ACSE0504.2 3 3 3 2
ACSE0504.3 3 3 3 2
ACSE0504.4 3 2 2 2
ACSE0504.5 3 2 3 2
Average
3 2.2 2.8 2
Compiler Design :
2022-2023 : Section D :100%
Rohit Chaudhary
7/23/2024 CD Unit -3 13
End Semester Question Paper
Template
B TECH
(SEM-V) THEORY EXAMINATION 20__-20__
COMPILER DESIGN
Time: 3 Hours Total Marks:
100
Note: 1. Attempt all Sections. If require any missing data; then choose
suitably.
SECTION A
1. Attempt all questions in brief. 2 x 10 = 20
Q.No. Question Marks CO
1 2
2 2
. .
10 2
1 10
2 10
. .
5 SECTION C 10
3. Attempt any one part of the following: 1 x 10 = 10
1 10
2 10
7/23/2024 Rohit Chaudhary CD Unit -3 15
End Semester Question Paper
Templates
4. Attempt any one part of the following: 1 x 10 =
10
Q.No. Question Marks CO
1 10
2 10
5. Q.No.
Attempt any one part of the following:
Question 1 x 10 =CO
Marks
101 10
2 10
6.Q.No.
Attempt any one part of Question
the following: Marks
1 x 10 CO
=
10
1 10
2 10
7/23/2024
Rohit Chaudhary CD Unit -3 16
End Semester Question Paper
Templates
7. Attempt any one part of the following: 1 x 10 = 10
1 10
2 10
• Algorithms
• Languages and machines
• Operating systems
• Computer architectures
• Theory of Automata
• Translation Schemes:
• indicate the order of evaluation of semantic actions associated
with a production rule.
• In other words, translation schemes give a little bit information
about implementation details.
• These attributes get values from the attribute values of their child
nodes.
• Each of the non-terminals has a single synthesized attribute, called
val.
• An SDD that involves only synthesized attributes is called S-
attributed
• Each rule computes an attribute for the non-terminal at the head
of a production from attributes taken from the body of the
production.
2. In syntax directed translation, along with the grammar we associate some informal
notation are called as:
✔ Semantic Rules
✔ Syntax Rules
✔ parsing Rules
✔ None of these
• Example Calculation:
• Given the expression 2+3∗(4−1), follow these steps:
• Compute 4−1
• 4−1=3
• Substitute into the original expression:
• 2+3∗3
• Perform multiplication:
• 3∗3=9
• Add to the initial number:
• 2+9=11
• Graphical representations
• e.g. Abstract Syntax Tree
• Postfix notation: operations on values stored on operand stack
(similar to JVM byte code)
• Three-address code: (e.g. triples and quads)
x := y op z
• Two-address code:
x := op y
▪ The triples have three fields to implement the ------ address code
✔ Three
✔ Two
✔ One
✔ None of these
+
t1 = b – c
+ *
t2 = a * t1
* t3 = a + t2
d
t4 = t1 * d
a
t5 = t3 + t4
b c
• x = y op z
• x = op y
• x=y
• goto L
• if x goto L and ifFalse x goto L
• if x relop y goto L
• Procedure calls using:
– param x
– call p,n
– y = call p,n
• x = y[i] and x[i] = y
• x = &y and x = *y and *x =y
• Quadruples
– Has four fields: op, arg1, arg2 and result
• Triples
– Temporaries are not used and instead references to instructions
are made
• Indirect triples
– In addition to triples we use a list of pointers to triples
a := b * -c + b * -c
(0) uminus c t1
(1) * b t1 t2
(2) uminus c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) := t5 a
a := b * -c + b * -c
op arg1 arg2
(0) uminus c
(1) * b (0)
(2) uminus c
(3) * b (2)
(4) + (1) (3)
(5) := a (4)
a := b * -c + b * -c
op op arg1 arg2
▪ Postfix notation of a + b * c is __ .
✔ abc*+
✔ abc+*
✔ ba+c*
✔ None of the above
x = a * b + c * d - e * f;
t1 = e * f;
t2 = c * d;
S
t3 = t2 - t1;
t4 = a * b;
id = E t5 = t4 + t3;
E +
x = t5;
E
x
E * E - E
E
id id E * E *
E E
id id id id
a b
c d e f
7/23/2024 Rohit Chaudhary CD Unit -3 59
Boolean Expressions: Example(CO3)
E E
or
id relop id E E
and
id relop id id relop id
a < b
c < d e < f
E E
or
id relop id E E
and
id relop id id relop id
a < b if a < b goto Ltrue
goto L1
L1: if < d L2
c < d e < f
c goto
L2: goto Lfalse Ltrue
if < f
e goto
goto Lfalse
x = y + z else
x = y - z
S.next =
Lnext S.
L1: if a < b goto L2
S begin = L1
goto Lnext
S.next = L1
while E E.true = L2 do S L2:
E.false = Lnext
if E then
id relop id S then S
id relop id
a < b
c < d
x = y + z else
x = y - z
S.next =
Lnext S. L1: if a < b goto
S begin = L1
L2 goto Lnext
S.next = L1
while E E.true = L2 do S L2: if c < d goto L3
E.false = Lnext
goto L4
if E S 2 .next = L1
id relop id E. then= L1
SS .next S L3:
E.false = L4 1
t h en
tru e = L3
id relop id
a < b
c < d
x = y + z else
x = y - z
S.next =
Lnext S. L1: if a < b goto
S begin = L1
L2 goto Lnext
S.next = L1 L2: if c < d goto
while E E.true = L2 do S
E.false = Lnext
L3 goto L4
if E S 2 .nex t = L1
id relop id E. then= L1
SS .next S L3: t1 = x + z
E.false = L4 1
t h en x = t1 goto L1
tru e = L3
id relop id
a < b
L4:
c < d
x = y + z else
x = y - z
S.next =
Lnext S. L1: if a < b goto
S begin = L1
L2 goto Lnext
S.next = L1 L2: if c < d goto
while E E.true = L2 do S
E.false = Lnext
L3 goto L4
if E S 2 .next = L1
id relop id E. then= L1
SS .next S L3: t1 = x + z
E.false = L4 1
t h en
tru e = L3 x = t1
id relop id goto L1
a < b
L4: t2 = x -
z x = t2
c < d
goto L1
7/23/2024 Rohit Chaudhary CD Unit -3 66
Lnext:
Combining Boolean and Control Flow
Statements
while a < b do
if c < d then
x = y + z else
x = y - z
c < d Lnext:
a ++;
}
Else
{
w=x-y;
b --;
}
}
• Consider the above program fragment and answer the following question.
(i) Write control flow representation in postfix notation.
(ii) Write three address notations.
7/23/2024 98
Rohit Chaudhary CD Unit -3
Recap of Unit
• A syntax-directed translation is defined by associating a translation
rule with each grammar rule.
• A translation rule defines the translation of the left-hand-side
nonterminal as a function of the right-hand-side nonterminals'
translations, and the values of the right-hand-side terminals.
• To compute the translation of a string, build the parse tree, and
use the translation rules to compute the translation of each
nonterminal in the tree, bottom-up; the translation of the string is
the translation of the root nonterminal.
Thank You