




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
sHSqhsjKdawjidada5wihow to convert from Infix expression to postfix expression (with c++ code).
Typology: Study notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Dr B R Ambedkar National Institute of Technology, Jalandhar
Infix Notation
Postfix Notation
Operator Precedence
Infix to Postfix Conversion Algorithm
Example
C++ Code for Infix to Postfix Conversion
Conclusion
Used to represent algebraic expressions
This notation is easier to parse for a machine
Characterized by placement of operators after the operand
Example :
Token Operator Precedence Associativity
()
[]
.
Function call
Array element
Structure element
17 Left to Right
++
--
Increment
decrement
16 Left to Right
++
--
!
-+
Sizeof
Increment
Decrement
Logical not
Unary minus,plus
Size(in bytes)
15 Right to left
type Type cast 14 Right to Left
diff
12 Left to Right
<< >> shift 11 Left to Right
Step 1: Scan the infix expression from left to right for
tokens(operators, operands and parenthesis).
Step 2: If the token is operand append it in postfix expression
Step 3: If the token is a left parenthesis โ(โ, push it in the stack
Step 4: If token is an operator,
Pop all operators which are of higher or equal
precedence then the incoming token(in same order)
and append them to output expression
After popping out all such operators, push new token
on the stack.
Step 6: After all tokens of infix expression have been scanned.
Pop all elements from stack and append them to
output expression
Step 7: Output expression obtained is the required Postfix
expression
Let Infix expression be : A*(B+C)/D โ E
Stage 1: Initially stack is empty
Scan the infix expression from left to right
Infix Notation :
Postfix Notation :
top NULL
Stage 3 : Next token is operator * , Push it into the stack
Infix Notation :
Postfix Notation :
top
Stage 4: Next token is left parenthesis (, Push it into the stack
Infix Notation :
Postfix Notation :
top
Stage 6: Next token is operator +,
We consider the outgoing precedence of top element of
stack,(
Outgoing precedence of ( is least, so + is pushed into the
stack
Infix Notation :
Postfix Notation :
top
Stage 7: Next token is operand C, operands are appended to output
expression as it is
Infix Notation :
Postfix Notation :
top
Stage 9: Next token is operator /,
We consider the outgoing precedence of top element of
stack,*
Outgoing precedence of * is same as /,
So, pop * and push / into the stack
Infix Notation :
Postfix Notation :
top
Stage 10: Next token is operand D, operands are appended to
output expression as it is
Infix Notation :
Postfix Notation :
top