Professional Documents
Culture Documents
CD WINTER 2021 Solution
CD WINTER 2021 Solution
[ Q.1]
i. Compiler:
A compiler is a computer program (or a set of programs) that transforms source code written
in a programming language (the source language) into another computer language (the target
language).
Typically, from high level source code to low level machine code or object code.
ii. interpreter:
iii. Token: Token is a sequence of characters that can be treated as a single logical entity.
1 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Explain activation tree?
2 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Explain a rule of Left factoring a grammar and give Example.
o E⟶TE
o E’ ⟶ +TE’ / Є
o T ⟶ FT’
o T’ ⟶ FT’ / Є
4 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
[ Q.2 ]
Buffer pair
Sentinels
1. Buffer pair:
The lexical analysis scans the input string from left to right one character at a time.
So, specialized buffering techniques have been developed to reduce the amount of
overhead required to process a single input character.
We use a buffer divided into number of character on one disk block
We read N input character into each half of the buffer.
Two pointers to the input are maintained and string between two pointers is the current
lexemes.
Pointer Lexeme Begin
Pointer Forward, scans ahead until a pattern
If forward pointer is at the end of first buffer half then second is filled with N input
character.
If forward pointer is at the end of second buffer half then first is filled with N input
character
2. Sentinels:
If we use the scheme of Buffer pairs we must check, each time we pointer that we have
not moved off one of the buffers; if we do, then we must reload the other buffer. Thus,
for each character read, we make two tests.
We can combine the buffer-end test with the test for the character if we extend each
buffer to hold a sentinel character at the end.
(b) Define the following terms and give suitable example for it.
6 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
7 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Draw the DFA for the regular expression (a|b)*abb using set construction
method only.
OR
8 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Draw NFA from regular expression using Thomson’s construction and
convert it into DFA.
(a | b)* a b* a
9 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
10 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
11 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
12 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
13 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
[ Q.3]
(b) Design FIRST and FOLLOW set for the following grammar.
S→ 1AB | ε
A→1AC | 0C
14 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
B→0S
C→1
E-> E A E | id
A-> + | *
The above grammar is not an operator grammar but we can convert that grammar into
operator grammar like −
E-> E + E | E * E | id
Relation Meaning
15 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Precedence Table
id + * $
id ⋗ ⋗ ⋗
+ ⋖ ⋗ ⋖ ⋗
* ⋖ ⋗ ⋗ ⋗
$ ⋖ ⋖ ⋖ ⋗
Example
<⋅id1⋅>+<⋅id2⋅>∗<⋅id3⋅>
Basic Principle
Scan the string from left until seeing ·> and put a pointer.
Scan backwards the string from right to left until seeing <·
Everything between the two relations <· and ·> forms the handle.
Replace handle with the head of the production.
Repeat − Let b be the top stack symbol, a is the input symbol pointed to by P.
16 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
if (a is andbisandbis)
return
else
if a ·> b or a =· b then
repeat
else error
end
id + * $
id ⋗ ⋗ ⋗
+ ⋖ ⋗ ⋖ ⋗
* ⋖ ⋗ ⋗ ⋗
$ ⋖ ⋖ ⋖ ⋗
Example
17 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
By seeing this, we can extract the precedence function like −
id + * $
f 4 2 4 0
g 5 1 3 0
18 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
OR [ Q.3]
S ->aSA | €
A ->bS | c
X
[ Q.4]
19 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Explain the following parameter passing methods.
1. Call-by-value
2. Call-by-reference
3. Copy-Restore
4. Call-by-Name
20 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Peephole Optimization Techniques
A. Redundant load and store elimination: In this technique, redundancy is eliminated.
Initial code:
y = x + 5;
i = y;
z = i;
w = z * 3;
Optimized code:
y = x + 5;
i = y;
w = y * 3;
B. Constant folding: The code that can be simplified by the user itself, is simplified.
Initial code:
x = 2 * 3;
Optimized code:
x = 6;
C. Strength Reduction: The operators that consume higher execution time are replaced by
the operators consuming less execution time.
Initial code:
y = x * 2;
Optimized code:
y = x + x; or y = x << 1;
Initial code:
y = x / 2;
Optimized code:
21 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
y = x >> 1;
D. Null sequences: Useless operations are deleted.
E. Combine operations: Several operations are replaced by a single equivalent operation.
OR [ Q.4 ]
( a * b ) + ( c + d ) - ( a + b + c + d ) into
1] Quadruples
2] Triple
3] Indirect Triple
22 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
23 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
24 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
[ Q.5]
(a) Explain symbol table. For what purpose , compiler uses symbol table?
Basic-Block Scheduling
1 Data-Dependence Graphs
2 List Scheduling of Basic Blocks
3 Prioritized Topological Orders
1. Data-Dependence Graphs
25 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
3. Prioritized Topological Orders
List scheduling does not backtrack; it schedules each node once and only once. It
uses a heuristic priority function to choose among the nodes that are ready to be
scheduled next.
Synthesized attributes
A Synthesized attribute is an attribute of the nonterminal on the left-hand side of a
production.
All of the attributes that we have used so far have been synthesized.
Synthesized attributes represent information that is being passed up the parse tree.
These attributes get values from the attribute values of their child nodes. To illustrate,
assume the following production:
S → ABC
If S is taking values from its child nodes (A,B,C), then it is said to be a synthesized attribute,
as the values of ABC are synthesized to S.
For example (E → E + T), the parent node E gets its value from its child node. Synthesized
attributes never take values from their parent nodes or any sibling nodes.
Synthesized attributes can be contained by both the terminals or non-terminals.
26 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
OR
[ Q.5 ]
(a) Define a following: i. Basic block ii. Constant folding iii. Handle.
i. Basic block:
Basic block contains a sequence of statement. The flow of control enters at the beginning of the
statement and leave at the end without any halt (except may be the last instruction of the block).
1. t1:= x * x
2. t2:= x * y
3. t3:= 2 * t2
4. t4:= t1 + t3
5. t5:= y * y
6. t6:= t4 + t5
ii. Constant folding:
This is an optimization technique which eliminates expressions that calculate a value that
can be determined before code execution.
If operands are known at compile time, then the compiler performs the operations statically.
An Example,
1. int x = (2 + 3) * y → int x = 5 * y
Optimizations are commonly applied at the intermediate representation phase (4th phase
in the compiler).
27 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Constant folding is applicable for boolean values, integers with the exception of a division
by zero and floating points.
iii. Handle
A sub string which is the right side of a production such that replacement of that sub string by the
production left side leads eventually to a reduction to the start symbol, by the reverse of a right most
derivation.
28 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Explain Pass structure of assembler.
29 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
***********
30 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA