Infix to Postfix Conversion
EvaIuation of Expressions
• How an expression is evaluated
• An expression contains operands and operators
A-B+C*D/E
• First C*D will be evaluated
• The result will be divided by E
• B will be subtracted from A
• To this the first result will be added
EvaIuation of Expressions
• An expression is evaluated by assigning a priority to each
operator
• The highest priority operator will be evaluated first
• In an expression where two adjacent operators have the same
priority it is generally executed from left-to-right.
• If an expression contains parenthesis the inner most
parenthesis is evaluated first
Expressions and their Priorities
Priority Operator
4 Unary +, Unary -, ^, !
3 *, /, %
2 +,-
1 =, <,>,<=, >=
EvaIuation of Expressions
• How does a compiler evaluate an input expression
• The compiler converts an infix expression to a postfix
expression and evaluates it
• An infix expression may have brackets but a postfix
expression will not have brackets
Infix, Postfix and Prefix Expressions
• The expression where the operators come in between the
operands is called the infix expression
A*B-C
• The expression where the operators appear after its operands
is called postfix expression
AB*C-
• The expression where the operators appear before its
operands is called prefix expression
-*ABC
Rule of Converting Infix Expression to
Postfix Expression
• The rule is Operators are taken out of the
stack as long as their in-stack priority is
greater than or equal to the in-coming
priority of the new operator
Infix to Postfix Conversion
A-B+C*D/E
Infix to Postfix Conversion
Input A-B+C*D/E
Infix to Postfix Conversion
Input A-B+C*D/E
Output A
Infix to Postfix Conversion
Input A-B+C*D/E
Output A
-
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB
-
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-C
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-C
*
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-CD
*
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-CD*
/
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-CD*E
/
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-CD*E/
+
Infix to Postfix Conversion
Input A-B+C*D/E
Output AB-CD*E/+
Infix to Postfix Conversion
Input A-(B+H)*D/E
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output A
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output A
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output A
(
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output AB
(
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
+
Output AB
(
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
+
Output ABH
(
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+
(
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+
*
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+D
*
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+D*
/
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+D*E
/
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+D*E/
-
Infix to Postfix Conversion
Input A-(B+H)*D/E
Output ABH+D*E/-
Convert the following Infix expressions
to corresponding Postfix expressions
A+B*D
A^B-D*E
A-B+H*D/E
A-B/H+D*E
A*(B+E)*D
A-(B+H)*D/E
(A+B)*M+D/(E+F*G)+H
Procedure to convert an infix
expression to a postfix expression
void postfix(string e)
{
token x,y;
top=0;
for( token x=newtoken(e); x!= ‘ ‘; x=nexttoken(e))
{
if(x is an operand) printf(x);
else if (x==’)’) //unstuck until ‘(‘
{
while(stack[top]!=’(‘)
Procedure to convert an infix
expression to a postfix expression
{ delete(y); printf(y);}
delete(y); //delete ‘(‘
}
else {while(isp(stack[top])>=icp(x))
{delete(y); printf(y);}
add(x);
} }
while(top>0)
{ delete(y); printf(y); }
}
Algorithm to convert infix expression
to postfix expression
• The infix expression is processed according to the following
rules
1. The input expression is processed by taking each element
by element from left to right
2. If the element is an operand it is directly copied to the
output
3. If the element is a left parenthesis then it is pushed onto
the stack
4. If the element is a right parenthesis then the symbol at the
top of the stack is popped off the stack and copied to the
output until the symbol at the top of the stack is a left
parenthesis. When that occurs both parentheses are
discarded
Algorithm to convert infix expression
to postfix expression
5. If the symbol being scanned has a higher precedence than
the symbol at the top of the stack the symbol being
scanned is pushed onto the stack
6. If the precedence of the symbol being scanned is lower
than or equal to the precedence of the symbol at the top of
the stack the stack is popped to the output until the
element at the stack top has a precedence less than the
precedence of the symbol being scanned. Then the symbol
being scanned is pushed onto the stack
7. When the end of the input expression is reached the stack is
popped to the output until the stack becomes empty and
the algorithm terminates
Convert the following Infix expressions
to corresponding Postfix expressions
A+B*D ABD*+
A^B-D*E AB^DE*-
A-B+H*D/E AB-HD*E/+
A-B/H+D*E ABH/-DE*+
A*(B+E)*D ABE+*D*
A-(B+H)*D/E ABH+D*E/-
(A+B)*M+D/(E+F*G)+H AB+M*DEFG*+/+H+