Sie sind auf Seite 1von 11

What is Shift-Reduce Parsing?

A shift-reduce parser attempts to construct a parse tree for an input string beginning
at the leaves (the bottom) and working up towards the root (the top).

String ----Reduced to------> Start Symbol

Thus, it works on the same principle of bottom-up parser.

Implementation of Shift-Reduce Parser:

To implement shift-reduce parser, we use a stack to hold grammar symbols and an


input buffer to hold the string w to be parsed.

The initial configuration of shift-reduce parse is-

The parser operates by shifting zero or more input symbols onto the stack until a
handle is on top of the stack.

The parser repeats this cycle until it has detected an error or until the stack contains
the start symbol and the input is empty.

After entering this state or configuration, the parser halts and announces successful
completion of parsing.

Possible Actions:

There are 4 possible actions that a shift-reduce parser can make-


1. Shift:

In a shift action, the next symbol is shifted onto the top of the stack.

2. Reduce:

In a reduce action, the handle that appears on the top of the stack is replaced with
appropriate non-terminal.

3. Accept:

In an accept action, the parser announces successful completion of parsing.

4. Error:

A situation in which parser cannot either shift or reduce the symbols and also cannot
even perform the accept action.

A rule to remember to perform shift-reduce parsing:

If the incoming operator has more priority than in stack operator, then perform
shift.
If in stack operator has same or less priority than the priority of incoming
operator, then perform reduce.

***************

NUMERICAL PROBLEMS ON SHIFT-REDUCE PARSING:

Problem-01:

Consider the grammar-


EE-E
EE*E
E id

Perform shift-reduce parsing of the input string: id - id * id

Solution:

Remember this priority order: id > * > -


Stack Input Buffer Parsing Action

$ id id * id $ Shift

$id -id * id $ Reduce E id

$E -id * id $ Shift

$E- id * id $ Shift

$ E - id * id $ Reduce E id

$E-E * id $ Shift

$EE* id $ Shift

$ E E * id $ Reduce E id

$EE*E $ Reduce E E * E

$EE $ Reduce E E - E

$E $ Accept

Problem-02:

Consider the grammar-


S (L) / a
L L, S / S

Parse the input string ( a , ( a , a ) ) using shift-reduce parser.

Solution:

Stack Input Buffer Parsing Action

$ (a,(a,a))$ Shift

$( a,(a,a))$ Shift
$(a ,(a,a))$ Reduce S a

$(S ,(a,a))$ Reduce L S

$(L ,(a,a))$ Shift

$(L, (a,a))$ Shift

$(L,( a,a))$ Shift

$(L,(a ,a))$ Reduce S a

$(L,(S ,a))$ Reduce L S

$(L,(L ,a))$ shift

$(L,(L, a))$ shift

$(L,(L,a ))$ Reduce S a

$(L,(L,S) ))$ Reduce L L , S

$(L,(L ))$ Shift

$(L,(L) )$ Reduce S (L)

$(L,S )$ Reduce L L , S

$(L )$ Shift

$(L) $ Reduce S (L)

$S $ Accept

Problem-03:

Consider the grammar-


S TL;
T int / float
L L , id / id

Using shift-reduce parser, parse the input string int id , id ;


Solution:

Stack Input Buffer Parsing Action

$ int id , id ; $ Shift

$ int id , id ; $ Reduce T int

$T id , id ; $ Shift

$ T id , id ; $ Reduce L id

$TL , id ; $ Shift

$TL, id ; $ Shift

$ T L , id ;$ Reduce L L , id

$TL ;$ Shift

$TL; $ Reduce S T L ;

$S $ Accept

Problem-04:

Design shift-reduce parser for the following grammar-

S 0S0 / 1S1 / 2

considering the string "10201"

Solution:

Stack Input Buffer Parsing Action


$ 10201$ Shift

$1 0201$ Shift

$10 201$ Shift

$102 01$ Reduce S 2

$10S 01$ Shift

$10S0 1$ Reduce S 0 S 0

$1S 1$ Shift

$1S1 $ Reduce S 1 S 1

$S $ Accept

What is Code Optimization?


Code Optimization is a technique which tries to improve the code by eliminating
unnecessary code lines and arranging the statements in such a sequence that
speed up the program execution without wasting the resources.

Advantages:

Executes faster
Efficient memory usage
Yields better performance

Machine independent optimizations

Enable other transformations


o Procedure in lining, cloning, loop unrolling
Eliminate redundancy
o Redundant expression elimination
Eliminate useless and unreachable code
o Dead code elimination
Specialization and strength reduction
o Constant propagation, peephole optimization
Move operations to less frequently executed place
o Loop invariant code motion
Machine dependent (scheduling) transformations
Take advantage of special hardware features
Instruction selection, prefetching
Manage or hide latency, introduce parallelism
Instruction scheduling, prefetching
Manage bounded machine resources
Register allocation

Various Code Optimization Techniques:

1. Compile Time Evaluation

(A) Constant Folding


(B) Constant Propagation

2. Common sub-expression elimination


3. Code Movement
4. Dead Code Elimination
5. Strength Reduction

1: Compile Time Evaluation-

Two techniques that comes under compile time evaluation are-

A) Constant Folding:

It refers to a technique of evaluating the expressions whose operands are known to


be constant at compile time evaluation.

Example:

length = (22/7) * d

B) Constant Propagation:

In constant propagation, if a variable is assigned a constant value, then subsequent


use of that variable can be replaced by a constant as long as no intervening
assignment has changed the value of the variable.

Example:

pi = 3.14
r=5
Area = pi * r * r

Here, the value of pi is replaced by 3.14 and r by 5, then computation of 3.14 * 5 * 5


is done during compilation.

2: Common sub-expression elimination-

The common sub-expression is an expression appearing repeatedly in the code


which is computed previously. This technique replaces redundant expression each
time it is encountered.

Example:

Code before Optimization:

T1 = 4 * i
T2 = a [T1]
T3 = 4 * j
T4 = 4 * i // Redundant Expression
T5 = n
T6 = b[T4] + T5

Code after Optimization:

T1 = 4 * i
T2 = a [T1]
T3 = 4 * j
T5 = n
T6 = b[T1] + T5

3: Code Movement-

It is a technique of moving a block of code outside a loop if it won't have any


difference if it is executed outside or inside the loop.

Example:

Code before Optimization:

for (int i = 0 ; i < n ; i ++)


{
x = y + z;
a[i] = 6 * i ;
}

Code after Optimization:

x = y + z;
for (int i = 0 ; i < n ; i ++)
{
a[i] = 6 * i ;
}

4. Dead Code Elimination:

Dead Code Elimination includes eliminating those code statements which are either
never executed or unreachable or if executed their output is never used.

Example:

Code before Optimization:


i=0
if ( i == 1)
{
a = x + 5;
}

Code after Optimization:

i=0

5: Strength Reduction:

It is the replacement of expressions that are expensive with cheaper and simple
ones.

Example:

Code before Optimization:

B = A* 2

Code after Optimization:

B = A+ A

Induction variable

It is the variable that gets incremented or decremented by fixed amount with every iteration of a
loop. Consider the following example which contains the induction variables j and k.

void main ( )
{ int a[10], b[10];
int i, j, k;
for (i=0,j=0,k=0 ; i<n ; i++)
{
a[j++] = b[k++];
}}
In this example, j and k are the induction variables. These variables can be eliminated and the
code can be-
void main ( )
{
int a[10], b[10];
int i, j, k;
for (i=0 ; i<n ; i++)
{
a[i] = b[i]; } }
Thus by eliminating the induction variables, the code can be optimized.

Das könnte Ihnen auch gefallen