INFIXTOPOSTFIX
INFIXTOPOSTFIX
03
03 EXAMPLES FOR INFIX TO POSTFIX CONVERSION
04
Infix notation
Infix notation is the common arithmetic and logical formula notation, in which
E.g. A+B
Postfix notation
For example, the Infix expression A+B will be written as AB+ in its Postfix Notation.
The Infix expression A+B will be written as +AB in its Prefix Notation.
Step1
Scan the Infix expression from left to right for tokens (Operators, Operands
& Parentheses) and perform the steps 2 to 5 for each token in the Expression
Algorithm
Step2
Step3
Step4
Check the stack top and Pop the operator if it is of higher or equal
precedence than the incoming token and append it (in the same order) to the
output Expression.
Pop all the operators from the Stack and append them to Output String, till you encounter
Pop the left parenthesis but don’t append it to the output string (Postfix notation does not
have brackets).
Algorithm
Step6
When all tokens of Infix expression have been scanned. Pop all the elements
A * (B + C) – D / E
.
Example
Stage 2
Stage 3
side, is maximum.
But when another operator is to come on the top of ‘(‘ then its preced
ence is least.
Example
Stage 5
it is
Example
Stage 6
Stage 7
Stage 8
Next token ), means that pop all the elements from Stack and appe
Stack ‘*‘ is more than that of Minus. So we pop multiply and append it
Stage 10
Stage 11
Next, we will insert the division operator into the Stack because its pr
Stage 12
sion as it is.
Example
Stage 13
Infix Expression
(a+b-c)*d–(e+f)
Postfix Expression
Stack
Infix Expression
a+b-c)*d–(e+f)
Postfix Expression
(
Stack
Infix Expression
+b-c)*d–(e+f)
Postfix Expression
a
(
Stack
Infix Expression
b-c)*d–(e+f)
Postfix Expression
a
+
(
Stack
Infix Expression
-c)*d–(e+f)
Postfix Expression
ab
+
(
Stack
Infix Expression
c)*d–(e+f)
Postfix Expression
ab+
-
(
Stack
Infix Expression
)*d–(e+f)
Postfix Expression
ab+c
-
(
Stack
Infix Expression
*d–(e+f)
Postfix Expression
ab+c-
Stack
Infix Expression
d–(e+f)
Postfix Expression
ab+c-
*
Stack
Infix Expression
–(e+f)
Postfix Expression
ab+c-d
*
Stack
Infix Expression
(e+f)
Postfix Expression
ab+c–d*
-
Stack
Infix Expression
e+f)
Postfix Expression
ab+c–d*
(
-
Stack
Infix Expression
+f)
Postfix Expression
ab+c–d*e
(
-
Stack
Infix Expression
f)
Postfix Expression
+ ab+c–d*e
(
-
Stack
Infix Expression
)
Postfix Expression
+ ab+c–d*ef
(
-
Stack
Infix Expression
Postfix Expression
ab+c–d*ef+
-
Stack
Infix Expression
Postfix Expression
ab+c–d*ef+-
Thank you