Unit III - Infix to Postfix Conversion
Unit III - Infix to Postfix Conversion
UNIT-3
Topic : Infix to Postfix Conversion
Infix to Postfix Conversion
Infix Notation
Example: A + B
Infix to Postfix Conversion
Postfix Notation
Step 1
• Scan the Infix expression from left to right for tokens
(Operators, operands and Parenthesis) and perform the steps 2
to 5 for each token in the expression.
Step 2
• If token is operand, Append it in postfix expression.
Step 3
• If token is left parenthesis “(“, push it in stack.
Infix to Postfix Conversion
Step 4
Step 5
Step 6
• When all tokens of infix expression have been scanned. Pop all
the elements from the stack and append them to the output
string.
• The output string is the corresponding postfix notation.
Infix to Postfix Conversion
Example
Example
Example
Stage 2:
The first token is operand A. Operands are appended to the
output as it is.
Infix to Postfix Conversion
Example
Stage 3:
Next token is * since stack is empty it is pushed into stack
Infix to Postfix Conversion
Example
Stage 4:
• Next token is ( the precedence of open parenthesis which is
maximum.
• But when another operator is to come on the top of ‘(‘ then its
precedence is least.
Infix to Postfix Conversion
Example
Stage 5:
• Next token, B is an operand which will go to the output string
Infix to Postfix Conversion
Example
Stage 6:
• Next token, + is an operator, we consider the precedence of
top element in the stack. ‘(‘ . The outgoing precedence of open
parenthesis is the least
• So + gets pushed into the stack.
Infix to Postfix Conversion
Example
Stage 7:
• Now token, C, is appended to the output
Infix to Postfix Conversion
Example
Stage 8:
• Now token, ) , means that pop all the elements from stack and
append them to the output till we read an opening parenthesis.
Infix to Postfix Conversion
Example
Stage 9:
• Now token, - , is an operator. The precedence of operator on
the top of stack ‘*’ is more than that of ‘-’. So we pop multiply
and append it to output. Then push ‘– ‘ into stack.
Infix to Postfix Conversion
Example
Stage 10:
• Now token, D, is appended to the output
Infix to Postfix Conversion
Example
Stage 11:
• Next we insert the ‘/’ operator into the stack because its
precedence is more that the minus.
Infix to Postfix Conversion
Example
Stage 12:
• Now token, E, is appended to the output
Infix to Postfix Conversion
Example
Stage 13:
• The input expression is complete. So pop all the elements from
stack and append to the output.
Thank You