Beruflich Dokumente
Kultur Dokumente
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).
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:
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:
4. Error:
A situation in which parser cannot either shift or reduce the symbols and also cannot
even perform the accept action.
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.
***************
Problem-01:
Solution:
$ id id * id $ Shift
$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:
Solution:
$ (a,(a,a))$ Shift
$( a,(a,a))$ Shift
$(a ,(a,a))$ Reduce S a
$(L,S )$ Reduce L L , S
$(L )$ Shift
$S $ Accept
Problem-03:
$ int id , id ; $ Shift
$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:
S 0S0 / 1S1 / 2
Solution:
$1 0201$ Shift
$10S0 1$ Reduce S 0 S 0
$1S 1$ Shift
$1S1 $ Reduce S 1 S 1
$S $ Accept
Advantages:
Executes faster
Efficient memory usage
Yields better performance
A) Constant Folding:
Example:
length = (22/7) * d
B) Constant Propagation:
Example:
pi = 3.14
r=5
Area = pi * r * r
Example:
T1 = 4 * i
T2 = a [T1]
T3 = 4 * j
T4 = 4 * i // Redundant Expression
T5 = n
T6 = b[T4] + T5
T1 = 4 * i
T2 = a [T1]
T3 = 4 * j
T5 = n
T6 = b[T1] + T5
3: Code Movement-
Example:
x = y + z;
for (int i = 0 ; i < n ; i ++)
{
a[i] = 6 * i ;
}
Dead Code Elimination includes eliminating those code statements which are either
never executed or unreachable or if executed their output is never used.
Example:
i=0
5: Strength Reduction:
It is the replacement of expressions that are expensive with cheaper and simple
ones.
Example:
B = A* 2
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.