Arithmetic Expressions Conversion & Evaluation: Unit-2
Arithmetic Expressions Conversion & Evaluation: Unit-2
Unit-2
PREFIX (POLISH NOTATION), POSTFIX (REVERSE
POLISH NOTATION) AND INFIX NOTATIONS
• Prefix, Postfix and Infix notations are three different but
equivalent ways to represent expressions.
• Order of operands is same in these three notations but
the order of operators changes.
• Operator is in between the operands in ‘Infix’ notation,
after the operands in ‘Postfix’ notation and before
operands in ‘Prefix’ notation.
Infix notation: (A+B)
Postfix notation: (AB+)
Prefix notation: (+AB)
PREFIX (POLISH NOTATION), POSTFIX (REVERSE
POLISH NOTATION) AND INFIX NOTATIONS
• While evaluating expression in ‘Infix’ notation, it is not clear
that whether addition should be performed before or
multiplication.
• To resolve this ambiguity precedence of operators is required.
Operator with higher precedence will be applied before the
operator with lower precedence.
• This precedence of operators is not required in ‘Prefix’ and
‘Postfix’ notations. So these two do not have ambiguities as
in ‘Infix’ notation.
Precedence and associativity determines the order of evaluation of an
expression Precedence and associativity determines the order of
evaluation of an expression
1. 3 4 5 * 6 / +
2. 300 23 + 43 21 - * 84 7 + /
3. 4 8 + 6 5 - * 3 2 – 2 2 + * /
Example Solution:
1. 3+4*5/6
2. (300+23)*(43-21)/(84+7)
3. (4+8)*(6-5)/((3-2)*(2+2))
1. Post: ABC+DE/F-*-
Pre: -A*+BC-/DEF
2. Post: AB/CD^-EF/G-+
Pre: +-/AB^CD-/EFG
3. Post: AB-CD^EF/+G*+
Pre: +-AB*+^CD/EFG
4. Post: ABCD^%/EF*+
Pre: +/A%B^CD*EF
Algorithm to convert Infix To Postfix
Let, X is an arithmetic expression written in infix notation. This algorithm finds
the equivalent postfix expression Y.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X until
the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of
Stack) which has the same precedence as or higher precedence than
operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of
Stack) until a left parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
7. END.
Before conversion add symbol ‘ ; ‘ at the end of the expression.
Algorithm covert( )
Step
2. if the element is an operand, then store the element into the target
area.
3.1 While the incoming priority is less than or equal to instack priority, pop the
operators and store into the target area.
3.2 if the element is right hand parenthesis ‘ ) ‘, then pop all the elements from
the stack till left hand parenthesis ‘ ( ‘ is popped out. The popped element
are stored in the target area except ‘ ( ‘.
4. if the element is ‘ ; ‘, then pop all the elements till the stack is empty and store
them into target area.
5. Stop
}
Infix Expression: A+ (B*C-(D/E^F)*G)*H,
Infix: ((A-(B+C))*D)^(E+F)
Algorithm of Infix to Prefix
Suppose A is an arithmetic expression written in infix form. The algorithm finds
equivalent prefix expression B.
2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK
is empty
Result:
+*+A^BCD^E5
Expression Stack Output Comment
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Push
• A/B*(C^D+E)+F*G-(H%I)
• A/B*C*D+F-(H^I)
• A/B*C/D*F/G
Thank You