CD 2
CD 2
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
2
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504
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
Rc parse the string “c+c” to observe reduce-reduce conflict
Stack Input Action Stack Input Action
$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
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
(0) + a b
(1) - (0)
(2) + c d
(4) + (0) c
4
COMPILER DESIGN
ASSIGNMENT – II
Name: V. Amrutha Roll.no: 21691A0504
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
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