0% found this document useful (0 votes)
16 views12 pages

In Fix To Post Fix Rules

Uploaded by

yoyo36685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

In Fix To Post Fix Rules

Uploaded by

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

Precedence order

Operators Symbols { }, ( ), [ ]
Parenthesis
Exponential notation ^
Multiplication and Division *, /
Addition and Subtraction +, -
Associativity order
^ Right to Left
*, / Left to Right
+, - Left to Right

For example:
2^2^3 = 2 ^ 8
= 256
Rules for the conversion from infix to postfix
expression

• Print the operand as they arrive.


• If the stack is empty or contains a left parenthesis
on top, push the incoming operator on to the
stack.
• If the incoming symbol is '(', push it on to the
stack.
• If the incoming symbol is ')', pop the stack and
print the operators until the left parenthesis is
found.
• If the incoming symbol has higher precedence
than the top of the stack, push it on the stack.
• If the incoming symbol has lower precedence than the
top of the stack, pop and print the top of the stack.
Then test the incoming operator against the new top
of the stack.
• If the incoming operator has the same precedence
with the top of the stack then use the associativity
rules.
– If the associativity is from left to right then pop and print
the top of the stack then push the incoming operator.
– If the associativity is from right to left then push the
incoming operator.
• At the end of the expression, pop and print all the
operators of the stack.
Infix expression:
K + L - M*N + (O^P) * W/U/V * T + Q
Input Expression Stack Postfix Expression
K K
+ +
L + KL
- - K L+
M - K L+ M
* -* K L+ M
N -* KL+MN
K L + M N*
+ +
K L + M N* -
( +( K L + M N *-
O +( KL+MN*-O
^ +(^ K L + M N* - O
P +(^ K L + M N* - O P
) + K L + M N* - O P ^
Input Expression Stack Postfix Expression
* +* K L + M N* - O P ^
W +* K L + M N* - O P ^ W
/ +/ K L + M N* - O P ^ W *
U +/ K L + M N* - O P ^W*U
/ +/ K L + M N* - O P ^W*U/
V +/ KL + MN*-OP^W*U/V
* +* KL+MN*-OP^W*U/V/
T +* KL+MN*-OP^W*U/V/T
KL+MN*-OP^W*U/V/T*
+ +
KL+MN*-OP^W*U/V/T*+
Q + KL+MN*-OP^W*U/V/T*Q
KL+MN*-OP^W*U/V/T*+Q+

The final postfix expression of infix expression(K + L - M*N + (O^P) * W/U/V * T


+ Q) is KL+MN*-OP^W*U/V/T*+Q+.
Rules for the conversion of infix to
prefix expression:
• First, reverse the infix expression given in the
problem.
• Scan the expression from left to right. Whenever
the operands arrive, print them.
• If the operator arrives and the stack is found to be
empty, then simply push the operator into the
stack.
• If the incoming operator has higher precedence
than the TOP of the stack, push the incoming
operator into the stack.
• If the incoming operator has the same precedence with a
TOP of the stack, push the incoming operator into the stack.
• If the incoming operator has lower precedence than the
TOP of the stack, pop, and print the top of the stack. Test
the incoming operator against the top of the stack again
and pop the operator from the stack till it finds the operator
of a lower precedence or same precedence.
• If the incoming operator has the same precedence with the
top of the stack and the incoming operator is ^, then pop
the top of the stack till the condition is true. If the condition
is not true, push the ^ operator.
• When we reach the end of the expression, pop,
and print all the operators from the top of the
stack.
• If the operator is ')', then push it into the stack.
• If the operator is '(', then pop all the operators
from the stack till it finds ) opening bracket in the
stack.
• If the top of the stack is ')', push the operator on
the stack.
• At the end, reverse the output.
Conversion of Infix to Prefix using Stack

• K + L - M * N + (O^P) * W/U/V * T + Q

• If we are converting the expression from infix


to prefix, we need first to reverse the
expression.
• The Reverse expression would be:
• Q + T * V/U/W * ) P^O(+ N*M - L + K
Input expression Stack Prefix expression
Q Q
+ + Q
T + QT
* +* QT
V +* QTV
/ +*/ QTV
U +*/ QTVU
/ +*// QTVU
W +*// QTVUW
* +*//* QTVUW
) +*//*) QTVUW
P +*//*) QTVUWP
^ +*//*)^ QTVUWP
O +*//*)^ QTVUWPO
( +*//* QTVUWPO^
+ ++ QTVUWPO^*//*
Input expression Stack Prefix expression
N ++ QTVUWPO^*//*N
* ++* QTVUWPO^*//*N
M ++* QTVUWPO^*//*NM
- ++- QTVUWPO^*//*NM*
L ++- QTVUWPO^*//*NM*L
+ ++-+ QTVUWPO^*//*NM*L
K ++-+ QTVUWPO^*//*NM*LK
QTVUWPO^*//*NM*LK+-++

The above expression, i.e., QTVUWPO^*//*NM*LK+-++, is not a


final expression. We need to reverse this expression to obtain
the prefix expression.

You might also like