Infix expression: The expression of the form “a operator b” (a + b) i.e.
, when an
operator is in-between every pair of operands.
Postfix expression: The expression of the form “a b operator” (ab+) i.e., When
every pair of operands is followed by an operator.
Examples:
Input: A + B * C + D
Output: ABC*+D+
Input: ((A + B) – C * (D / E)) + F
Output: AB+CDE/*-F+
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the expression: a + b * c + d
• The compiler first scans the expression to evaluate the expression b * c,
then again scans the expression to add a to it.
• The result is then added to d after another scan.
The repeated scanning makes it very inefficient. Infix expressions are easily readable
and solvable by humans whereas the computer cannot differentiate the operators and
parenthesis easily so, it is better to convert the expression to postfix(or prefix) form
before evaluation.
The corresponding expression in postfix form is abc*+d+. The postfix expressions
can be evaluated easily using a stack.
How to convert an Infix expression to a Postfix
expression?
To convert infix expression to postfix expression, use the stack data structure. Scan
the infix expression from left to right. Whenever we get an operand, add it to the
postfix expression and if we get an operator or parenthesis add it to the stack by
maintaining their precedence.
Below are the steps to implement the above idea:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, put it in the postfix expression.
3. Otherwise, do the following
• If the precedence and associativity of the scanned operator are
greater than the precedence and associativity of the operator in
the stack [or the stack is empty or the stack contains a ‘(‘ ], then
push it in the stack. [‘^‘ operator is right associative and other
operators like ‘+‘,’–‘,’*‘ and ‘/‘ are left-associative].
• Check especially for a condition when the operator at
the top of the stack and the scanned operator both are
‘^‘. In this condition, the precedence of the scanned
operator is higher due to its right associativity. So it
will be pushed into the operator stack.
• In all the other cases when the top of the operator
stack is the same as the scanned operator, then pop the
operator from the stack because of left associativity
due to which the scanned operator has less
precedence.
• Else, Pop all the operators from the stack which are greater than
or equal to in precedence than that of the scanned operator.
• After doing that Push the scanned operator to the
stack. (If you encounter parenthesis while popping
then stop there and push the scanned operator in the
stack.)
4. If the scanned character is a ‘(‘, push it to the stack.
5. If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is
encountered, and discard both the parenthesis.
6. Repeat steps 2-5 until the infix expression is scanned.
7. Once the scanning is over, Pop the stack and add the operators in the
postfix expression until it is not empty.
8. Finally, print the postfix expression.
Illustration:
Follow the below illustration for a better understanding
Consider the infix expression exp = “a+b*c+d”
and the infix expression is scanned using the iterator i, which is initialized as i = 0.
1st Step: Here i = 0 and exp[i] = ‘a’ i.e., an operand. So add this in the postfix
expression. Therefore, postfix = “a”.
Add ‘a’ in the postfix
2nd Step: Here i = 1 and exp[i] = ‘+’ i.e., an operator. Push this into the
stack. postfix = “a” and stack = {+}.
Push ‘+’ in the stack
3rd Step: Now i = 2 and exp[i] = ‘b’ i.e., an operand. So add this in the postfix
expression. postfix = “ab” and stack = {+}.
Add ‘b’ in the postfix
4th Step: Now i = 3 and exp[i] = ‘*’ i.e., an operator. Push this into the
stack. postfix = “ab” and stack = {+, *}.
Push ‘*’ in the stack
5th Step: Now i = 4 and exp[i] = ‘c’ i.e., an operand. Add this in the postfix
expression. postfix = “abc” and stack = {+, *}.
Add ‘c’ in the postfix
6th Step: Now i = 5 and exp[i] = ‘+’ i.e., an operator. The topmost element of the
stack has higher precedence. So pop until the stack becomes empty or the top
element has less precedence. ‘*’ is popped and added in postfix. So postfix =
“abc*” and stack = {+}.
Pop ‘*’ and add in postfix
Now top element is ‘+‘ that also doesn’t have less precedence. Pop it. postfix =
“abc*+”.
Pop ‘+’ and add it in postfix
Now stack is empty. So push ‘+’ in the stack. stack = {+}.
Push ‘+’ in the stack
7th Step: Now i = 6 and exp[i] = ‘d’ i.e., an operand. Add this in the postfix
expression. postfix = “abc*+d”.
Add ‘d’ in the postfix
Final Step: Now no element is left. So empty the stack and add it in the postfix
expression. postfix = “abc*+d+”.
Pop ‘+’ and add it in postfix
Program 4
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized expressions with the operators: +,
-, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
#include<stdio.h>
#include<ctype.h>
void infixpostfix(char infix[],char postfix[]);
int precedense(char ch);
void main()
{
char infix[20],postfix[20];
printf("\nEnter the infix expression : ");
gets(infix);
infixpostfix(infix,postfix);
printf("\npost fix expression is : %s\n",postfix);
}
int precedense(char ch)
{
switch(ch)
{
case '^': return 4; break;
case '*':
case '/': return 3; break;
case '%': return 2; break;
case '+':
case '-': return 1; break;
}
return -1;
}
void infixpostfix(char infix[],char postfix[])
{
int i=0,j=0,k=0,top=-1;
char symbol,s[10];
printf("\n Infix char \t Stack top \t Postfix char\n");
for(i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
printf(" %c \t %c \t \t %c\n",symbol,s[top],postfix[j-1]);
if(isalpha(symbol)||isdigit(symbol))
{
postfix[j++] = symbol;
}
else if(symbol == '(')
{
s[++top] = symbol;
}
else if(symbol == ')')
{
while(s[top]!='(' && s[top]!= '(')
postfix[j++] = s[top--];
top--;
}
else
{
while(top!=-1 && s[top]!='(' && precedense(symbol)<=precedense(s[top])){
if(symbol=='^' && s[top]=='^') // Right associativity
break;
else
postfix[j++]=s[top--];
}
s[++top]=symbol;
}
}
while(top>=0)
postfix[j++] = s[top--];
postfix[j]='\0';
}
OUTPUT
Example 1:
Enter the infix expression : a*(b+4)^(c-2)%6
Infix char Stack top Postfix char
a
* a
( * a
b ( a
+ ( b
4 + b
) + 4
^ * +
( ^ +
c ( +
- ( c
2 - c
) - 2
% ^ -
6 % *
post fix expression is : ab4+c2-^*6%
Process returned 38 (0x26) execution time : 3.476 s
Press any key to continue.
Example 2:
Enter the infix expression : (a+b)*c^d^e
Infix char Stack top Postfix char
(
a (
+ ( a
b + a
) + b
* +
c * +
^ * c
d ^ c
^ ^ d
e ^ d
post fix expression is : ab+cde^^*
Process returned 36 (0x24) execution time : 16.898 s
Press any key to continue.