0% found this document useful (0 votes)
27 views23 pages

Arithmetic Expressions Conversion & Evaluation: Unit-2

Uploaded by

Hàmmád Khân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views23 pages

Arithmetic Expressions Conversion & Evaluation: Unit-2

Uploaded by

Hàmmád Khân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Arithmetic Expressions

Conversion & Evaluation

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

Following is an operator precedence and associativity table (highest to lowest) −

Sr.No Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative

2 Multiplication ( ∗ ) & Second Highest Left Associative


Division ( / )

3 Addition ( + ) & Subtraction Lowest Left Associative


(−)
Comparison of Prefix,Postfix and Infix
•‘Infix’ notation is easy to read than ‘Prefix’ and ‘Postfix’
notations
•‘Infix’ notation requires additional information to resolve
ambiguities
•If operator of lower precedence is needed to be applied before

than we need to put parenthesis in ‘Infix’ notation. Whereas it is


not needed in ‘Prefix’ and‘Postfix’ notations.
•For machine it is easier to parse ‘Prefix’ and ‘Postfix’ notations

than ‘Infix’ notation.


Postfix Evaluation Algorithm

Step 1 − scan the expression from left to right


Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and
perform operation
Step 4 − store the output of step 3, back to stack Step 5
− scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation
Example
4, 5, 6,*,+
Example of Postfix expression
A. 6 2 3 + - 3 8 2 / + * 2 ^ 3 + result =52
B. 12 7 3 - / 2 1 5 + * + result=15
Question: use a stack to evaluate the following postfix
arithmetic expression. Show the changing status in tabular
form.
XYZP^*+AB/C+- for X =1, Y = 5, Z = 2, P
= 3, A = 15, B = 3, C = 8
Conversion from Postfix to Infix Expression
Example

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

1. Scan the expression from left to right.

2. if the element is an operand, then store the element into the target
area.

3. if the element is an operator, then push it into a stack with following


rules:

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.

1. Push “)” onto STACK, and add “(“ to end of the A

2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK
is empty

3. If an operand is encountered add it to B

4. If a right parenthesis is encountered push it onto STACK

5. If an operator is encountered then:


a. Repeatedly pop from STACK and add to B each operator (on the top of
STACK) which has same or higher precedence than the operator.

b. Add operator to STACK

6. If left parenthesis is encontered then


a. Repeatedly pop from the STACK and add to B (each operator on top of
stack until a left parenthesis is encounterd)

b. Remove the left parenthesis


7. Exit
Infix to prefix conversion
Expression = (A+B^C)*D+E^5
Step 1. Reverse the infix expression.
5^E+D*)C^B+A(

Step 2. Make Every '(' as ')' and every ')' as '('


5^E+D*(C^B+A)

Step 3. Convert expression to postfix form.


A+(B*C-(D/E-F)*G)*H

Step 4. Reverse the expression.


+*+A^BCD^E5

Result:
+*+A^BCD^E5
Expression Stack Output Comment

5^E+D*(C^B+A) Empty – Initial

^E+D*(C^B+A) Empty 5 Print

E+D*(C^B+A) ^ 5 Push

+D*(C^B+A) ^ 5E Push

D*(C^B+A) + 5E^ Pop And Push

*(C^B+A) + 5E^D Print

(C^B+A) +* 5E^D Push

C^B+A) +*( 5E^D Push

^B+A) +*( 5E^DC Print

B+A) +*(^ 5E^DC Push

+A) +*(^ 5E^DCB Print

A) +*(+ 5E^DCB^ Pop And Push

) +*(+ 5E^DCB^A Print

End +* 5E^DCB^A+ Pop Until ‘(‘

End Empty 5E^DCB^A+*+ Pop Every element


Convert Given expressions into postfix and
prefix form using stack

• A/B*(C^D+E)+F*G-(H%I)
• A/B*C*D+F-(H^I)
• A/B*C/D*F/G
Thank You

You might also like