You are on page 1of 30

3170701 Complier DesignWINTER 2021

[ Q.1]

(a) Define following terms: i. Compiler ii. Interpreter iii. Token

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:

An Interpreter directly executes instructions written in a programming or scripting


language without previously converting them to an object code or machine code.

iii. Token: Token is a sequence of characters that can be treated as a single logical entity.

Typical tokens are,

1) Identifiers 2) keywords 3) operators 4) special symbols 5) constants

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.

 It is a process of factoring out the common prefixes of alternatives.


It is used when it is not clear that which of the two alternatives is used to
expand the non-terminal.
Rewrite the production rules without changing the meaning to avoid left factoring
A → αβ1 / αβ2 ——— (1)
1. A → αA’
2. A → β1 / β2
3 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Rewrite the given expression (1) using the 1 and 2 expressions.
Example:
Consider the grammar and eliminate the left recursion
E ⟶ E + T / T ——- (1)
T ⟶ T * F / F ——- (2)
First, let’s take equation (1) and rewrite it using the given formula
E⟶E+T/T
E ⟶ T E’ —— (3)
E’ ⟶ +TE’ / Є —— (4)
Productions 3 and 4 are the Left factoring equations of the given
production
T⟶T*F/F
T ⟶ FT’ —–(5)
T’ ⟶ FT’ / Є —— (6)
Productions 5 and 6 are the Left factoring equations of the given
production.
The final productions after left factoring are:

o E⟶TE
o E’ ⟶ +TE’ / Є
o T ⟶ FT’
o T’ ⟶ FT’ / Є

4 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
[ Q.2 ]

(a) Explain input buffering methods.

 Here are mainly two techniques for input buffering,

 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

code to advance forward pointer is given below :


if forward at end of first half then begin
reload second half;
forward := forward + 1
end
else if forward at end of second half then begin
5 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
reload first half
move forward to beginning of first half;
end
else forward := forward + 1;

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.

Look ahead code with sentinels is given below :


forward := forward + 1
if forward = eof then begin
if forward at end of first half then begin
reload second half;
forward := forward + 1;
end
else if forward at the second half then begin
reload first half;
move forward to beginning of first half;
end
else terminate lexical analysis;
end

(b) Define the following terms and give suitable example for it.

i. Augmented Grammar ii. LR(0) Item iii.LR(1) Item

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]

(a) Describe Ambiguous Grammar with example.

(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

(c) Explain operator grammar. Generate precedence function table for


following grammar. E EAE | id A+|*

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

There are three precedence relations, which are given below −

Relation Meaning

a⋖b a yields precedence to b

a=·b a has the same precedence as b

a⋗b atakes precedence over b

15 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Precedence Table

id + * $

id ⋗ ⋗ ⋗

+ ⋖ ⋗ ⋖ ⋗

* ⋖ ⋗ ⋗ ⋗

$ ⋖ ⋖ ⋖ ⋗

Example

The input string is as follows −

id1 + id2 * id3

After inserting precedence relations is−

<⋅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.

Operator Precedence Parsing Algorithm

The algorithm is as follows −

Initialize: Set P to point to the first symbol of the input string w$

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

push a onto the stack

advance P to the next input symbol

else if a <· b then

repeat

c -> pop the stack

until (c .> stack-top)

else error

end

id + * $

id ⋗ ⋗ ⋗

+ ⋖ ⋗ ⋖ ⋗

* ⋖ ⋗ ⋗ ⋗

$ ⋖ ⋖ ⋖ ⋗

Example

Construct a graph using the algorithm. This graph is as follows −

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]

(a) Differentiate Top Down Parsing and Bottom up parsing

(b) Explain error recovery strategies used by parser.

Refer : WINTER 2020 (Q : 4 (b))

(c) Construct CLR parsing table for following grammar.

S ->aSA | €

A ->bS | c

X

[ Q.4]

(a) Explain various issues in design of code generator.

Refer WINTER 2020 (Q : 7 (a))

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

 Refer WINTER 2020 (Q : 5 (b))

(c) Explain Peephole Optimization.

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) Draw a DAG for expression: a + a * (b – c) + (b – c) * d.

(b) Compare: Static v/s Dynamic Memory Allocation.

Refer WINTER 2020 (Q : 7 (b))

(c) Translate following arithmetic expression –

( 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?

Refer WINTER 2020 (Q : 8 (b))

(b) Explain Basic-Block Scheduling.

 Basic-Block Scheduling

1 Data-Dependence Graphs
2 List Scheduling of Basic Blocks
3 Prioritized Topological Orders

1. Data-Dependence Graphs

We represent each basic block of machine instructions by a data-


dependence graph, G = (N,E), having a set of nodes iV representing the operations
in the machine instructions in the block and a set of directed edges E representing
the data-dependence constraints among the operations.

2. List Scheduling of Basic Blocks


The simplest approach to scheduling basic blocks involves visiting each node of
the data-dependence graph in "prioritized topological order." Since there can be no
cycles in a data-dependence graph, there is always at least one topological order
for the nodes. However, among the possible topological orders, some may be
preferable to others.

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.

(c) Explain synthesized attributes with the help of an example.

 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.

Synthesized attribute is used by both S-attributed SDT and L-attributed STD.

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).

The following sequence of three address statements forms a basic 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

2. int z = 300 * 78 * 6 → int z = 140400


Compilers will not go ahead and generate two symbols addition and multiplication in the
first case or two multiplication symbols in the second case but instead will identify such
constructs and substitute the computed values as shown.

 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.

The set of strings to be replaced at each reduction step is called a handle.

(b) Write difference(s) between stack and heap memory allocation.

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

You might also like