2. Expression Evaluation
Evaluate an expression represented by a String
Expression can contain parentheses, one can assume parentheses are
well-matched
For simplicity, we can assume only binary operations allowed are +, -,
*, and /.
Arithmetic Expressions can be written in one of three forms:
Infix Notation: Operators are written between the operands they operate
on, e.g. 3 + 4 .
Prefix Notation: Operators are written before the operands, e.g + 3 4
Postfix Notation: Operators are written after operands
2
3. Evaluation of Infix expressions
Infix notation is commonly used in arithmetic formula or statements, the
operators are written in-between their operands
Infix notation is commonly used in arithmetic formula or statements, the
operators are written in-between their operands
Let us assume ;
• Operands are real numbers.
• Permitted operators: +,-, *, /, ^(exponentiation)
• Blanks are permitted in expression.
• Parenthesis are permitted
3
4. Example:
A * ( B + C ) / D
2 * (5 + 3) / 4
Output: 4
We shall use two stacks :
Operand stack: This stack will be used to keep track of numbers.
Operator stack: This stack will be used to keep operations (+, -, *, /, ^)
Order of precedence of operations–
^ (Exponential)
/ *
+ –
Note: brackets ( ) are used to override these rules.
4
5. Process: (that will be used for the main algorithm)
1. Pop-out two values from the operand stack, let’s say it is A
and B
2. Pop-out operation from operator stack, let’s say it is ‘+’
3. Do A + B and push the result to the operand stack
5
6. Algorithm
Iterate through given expression, one character at a time
1. If the character is an operand, push it to the operand stack
2. If the character is an operator,
1. If the operator stack is empty then push it to the operator stack
2. Else If the operator stack is not empty,
• If the character’s precedence is greater than or equal to the
precedence of the stack top of the operator stack, then push the
character to the operator stack
• If the character’s precedence is less than the precedence of the stack
top of the operator stack then do Process (as explained above) until
character’s precedence is less or stack is not empty
3. If the character is “(“, then push it onto the operator stack
4. If the character is “)”, then do Process (as explained above) until the corresponding
“(” is encountered in operator stack. Now just pop out the “(“
5. Once the expression iteration is completed and the operator stack is not empty,
do Process until the operator stack is empty. The values left in the operand stack is
our final result
Courtesy : https://algorithms.tutorialhorizon.com/evaluation-of-infix-expressions/
6
7. Infix and Postfix Notations
• Infix: operators placed between operands:
A+B*C
• Postfix: operands appear before their operators:-
ABC*+
• There are no precedence rules to learn in postfix notation, and
parentheses are never needed
Courtesy: https://cse.iitkgp.ac.in/~pds/ 7
8. Infix Postfix
A + B A B +
A + B * C A B C * +
(A + B) * C A B + C *
A + B * C + D A B C * + D +
(A + B) * (C + D) A B + C D + *
A * B + C * D A B * C D * +
A + B * C (A + (B * C)) (A + (B C *) ) A B C * +
A + B * C + D ((A + (B * C)) + D ) ((A + (B C*) )+ D)
((A B C *+) + D) A B C * + D +
8
Infix to Postfix
9. Infix to postfix conversion
• Use a stack for processing operators (push and pop operations).
• Scan the sequence of operators and operands from left to right and
perform one of the following:
• output the operand,
• push an operator of higher precedence,
• pop an operator and output, till the stack top contains operator of a lower
precedence and push the present operator.
9
10. The algorithm steps
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming operator
onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on the
stack.
6. If the incoming symbol has equal precedence with the top of the stack, use association.
If the association is left to right, pop and print the top of the stack and then push the
incoming operator. If the association is right to left, push the incoming operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the stack,
pop the stack and print the top operator. Then test the incoming operator against the
new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No parentheses
should remain.)
10
11. Infix to Postfix Conversion
Requires operator precedence information
Operands:
Add to postfix expression.
Close parenthesis:
pop stack symbols until an open parenthesis appears.
Operators:
Pop all stack symbols until a symbol of lower precedence appears. Then push
the operator.
End of input:
Pop all remaining stack symbols and add to the expression.
11
12. Current
symbol
Operator
Stack
Postfix string
1 A A
2 * * A
3 ( * ( A
4 B * ( A B
5 + * ( + A B
6 C * ( + A B C
7 * * ( + * A B C
8 D * ( + * A B C D
9 ) * A B C D * +
10 + + A B C D * + *
11 E + A B C D * + * E
12 A B C D * + * E +
Expression:
A * (B + C * D) + E
becomes
A B C D * + * E +
Postfix notation
is also called as
Reverse Polish
Notation (RPN)
12
Infix to Postfix Rules