0% found this document useful (0 votes)
14 views

CD 2

Compiler Design Assignment-2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CD 2

Compiler Design Assignment-2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

COMPILER DESIGN

ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

1. Explain the Shift reduce parsing. What are the conflicts of Shift
reduce parsing?
A. Definition: Shift- reduce parsing is a form of bottom-up parsing in which a stack
holds grammar symbols and an input buffer will hold the rest of string to be
parsed.
 Here, the handle always appear at the top of the stack.
 We use $ to bookmark the bottom of the stack as well as the right end of input.
 During a left to right scan of the input, parser shifts zero or more input
symbols onto stack, until any reduction is possible.
 When any reduction is possible it will reduce it to the head of production i.e.
the Non Terminal.
 The parser will repeat this process until it detects an error or until the stack
contains start symbol and input is empty.
 The primary operations are Shift and Reduce and there are two more also
possible they are accept and error.
Actions in Shift – reduce parser:
1. Shift  The next input symbol is shifted onto the top of the stack.
2. Reduce  The right end of string to be reduced must be at the top of stack
and replace it with left end of string within stack and decide with what non-
terminal to replace.
3. Accept  It announces successfully completion of parsing.
4. Error  It discovers a syntax error and call an error recovery routine.
 The use of a stack in shift-reduce parsing is justified by an important fact that is
the handle will always eventually appear on top of stack.
Example:
G: E  E+E | E*E | id Parse the grammar with the string “id*id+id”
Sol :
Given grammar is E  E+E | E*E | id
Given string to parse is “id*id+id”
Stack INPUT Action
$ id*id+id$ Shift id
$id *id+id$ Reduce E  id
$E *id+id$ Shift *
$E* id+id$ Shift id

1
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

$E*id +id$ Reduce E id


$E*E +id$ Reduce E E*E
$E +id$ Shift +
$E+ id$ Shift id
$E+id $ Reduce E id
$E+E $ Reduce E E+E
$E $ Accept

Conflicts of Shift reduce parsing:


 There are CFGs (Context Free Grammars) for which the shift – reduce parsing
cannot be used.
 In such scenarios the parser falls in a situation in which it cannot decide whether to
shift or reduce (Shift-Reduce conflict) otherwise it cannot be able to choose which
reduction is to be selected from available ( Reduce-Reduce conflict).
Types of Conflicts:
1. Shift – Reduce conflict
2. Reduce – Reduce conflict
Shift – Reduce conflict:
 This conflict arises when the parser cannot decide whether to shift or to reduce.
 It is possible when the parser encounters a terminal in the input string and there are
various possible production rules that can be applied.
 It may also arise due to the precedence and associativity ambiguities.
Let us consider the above example itself.
Stack Input Action Stack Input Action
$ id*id+id$ Shift id $ id*id+id$ Shift id
$id *id+id$ Reduce E  id $id *id+id$ Reduce E  id
$E *id+id$ Shift * $E *id+id$ Shift *
$E* id+id$ Shift id $E* id+id$ Shift id
$E*id +id$ Reduce E id $E*id +id$ Reduce E id
$E*E +id$ Reduce E E*E $E*E +id$ Shift +
$E +id$ Shift + $E*E+ id$ Shift id

2
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

$E+ id$ Shift id $E*E+id $ Reduce E id


$E+id $ Reduce E id $E*E+E $ Reduce E
E+E
$E+E $ Reduce E E+E $E*E $ Reduce E
E*E
$E $ Accept $E $ Accept

Here, there are two possible ways of parsing and both gives same correct output so the
parser gets confused whether to shift + or reduce E E*E , this type of conflict is
shift-reduce conflict.
Reduce – Reduce conflict:
 This conflict arises when the parser cannot decide out of several possible reductions
which one to select.
 It is possible when the given grammar is ambiguous or multiple productions can be
possible to reduce.
 Overlapping of production rules is also a chance of getting reduce-reduce conflict.
Example: G: M R+R | R+c | R
Rc parse the string “c+c” to observe reduce-reduce conflict
Stack Input Action Stack Input Action

$ c+c$ Shift c $ c+c$ Shift c

$c +c$ Reduce R  c $c +c$ Reduce R  c

$R +c$ Shift + $R +c$ Shift +

$R+ c$ Shift c $R+ c$ Shift c

$R+c $ Reduce R c $R+c $ Reduce M


R+c
$R+R $ Reduce M R*R $M $ Accept

$M $ Accept

Here, there are two possible ways of reduction so the parser may get confused which
rule to choose Reduce R  c or Reduce M  R+c , this type of conflict is reduce-
reduce conflict.

3
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

2. Translate the expression –(a+b)*(c+d)+(a+b+c) into quadruples,


triples, and indirect triples?
A.
 Quadruple is a record structure which consists of four fields like op, arg1, arg2
and result
Translating given expression “ –( a+b)*(c+d)+(a+b+c)” into Quadruples

Locatio op arg1 arg2 result


n
(0) + a b T1
(1) - T1 T2
(2) + c d T3
(3) * T2 T3 T4
(4) + T1 c T5
(5) + T4 T5 T6

 Triples is a record structure which consists of three fields like op, arg1, arg2
 Since there is no temporary variable we might have to refer to the temporary
value i.e. location to compute.
Translating given expression “ –( a+b)*(c+d)+(a+b+c)” into Triples

Location op arg1 arg2

(0) + a b

(1) - (0)

(2) + c d

(3) * (1) (2)

(4) + (0) c

(5) + (3) (4)

 Indirect Triples consists of listing pointers to triples, rather than a listing of


triples themselves
Translating given expression “ –( a+b)*(c+d)+(a+b+c)” into Indirect triples

4
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

Location Statement Location op arg1 arg2

(0) (31) (31) + a b

(1) (32) (32) - (31)

(2) (33) (33) + c d

(3) (34) (34) * (32) (33)

(4) (35) (35) + (31) c

(5) (36) (36) + (34) (35)

Thus, the given expression is translated into quadruples, triples and indirect triples.
3. Explain Peephole Optimization with example.
A. Definition: Peephole Optimization is a type of code optimization technique that
is being performed on a small part of code. It is actually performed on a very small
set of instructions in the entire code.
 It is a machine-dependent optimization.
5
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

 These small sections of code are referred to as “Peepholes” or “windows”.


 This technique works on the principle of code replacement while preserving the
actual output.
 It generally replaces the inefficient or unnecessary instructions with efficient,
shorter and faster equivalents.
Objectives of Peephole optimization:
 Improving Performance
 Reducing Memory Footprint
 Minimizing Code Size
Peephole optimization Techniques:
1. Redundant load and store elimination: It eliminates the redundancy in the
memory operations like removing the unnecessary load, store operations.

Example:
Initial code:
k=j+4;
a=k;
c=a;
l=c*4
 We could see a, c are just copying the values from one another so they can
be eliminated
Optimized code:
k=j+4;
l=k*4;
2. Constant folding: The simplification of expression which is involving
constants is performed at compile-time itself to avoid computational time.
Example:
Initial code:
e=2+2;
 2+2 is easy to compute by the user itself so it can be simplified
Optimized code:
e=4;
3. Strength Reduction: The costlier operators(which take more execution time)
that consume more execution time are to be replaced with the cheaper
operators(which take less execution time).

6
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

Example:
Initial code:
e=i*2;
 Any number multiplied with 2 is nothing but adding the number and itself
and here multiplication takes more time than addition so it can be replaced
Optimized code:
e=i+i;
4. Null sequences / Simplify Algebraic Expressions: The operations which are
useless will be deleted from the code to reduce the computation time
Initial code:
e=5;
e=e*1;
e=e-0;
 Here, multiplying with 1 and substracting 0 from the number don’t really
have any impact on the value of e so they can be eliminated
Optimized code:
e=5;
5. Combine operations: Several operations are replaced by a single equivalent
operation thus reducing the no. of. instructions and optimizing the code.
Initial code:
if(a>0)
{
g=a;
}
else
{
g=0;
}
 Here, we can observe a single ternary operator will give the same output as
of the block of code
Optimized code:
g=(a>0)? a:0;
6. Deadcode Elimination: Eliminating the block of code which never gets
executed or the instructions which does not affect program’s output is done for
reducing computation time as well as memory usage.
Initial code:

7
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504

A()
{
x=0;
}
int main()
{
int n=50;
printf(“%d”,n);
int B=38;
}

 Here, we can see that function A is not getting executed and B=38 is also
not useful as it is not affecting output so they can be eliminated

Optimized code:
int main()
{
int n=50;
printf(“%d”,n);
return 0;
}
Advantages of Peephole optimization:
 Localized Optimization
 Efficiency
 Compatibility
 Easy to Implement

You might also like