You are on page 1of 20

Arithmetic Expressions

DR. SHWETA SHARMA


Arithmetic Expressions
Operators: * + - /
( A + B )*C or 3*5-4 Operands: a b c

The way to write arithmetic expression is known as a notation

An arithmetic expression can be written in three different but equivalent notations, i.e.,
without changing the essence or output of an expression. These notations are −

 Infix Notation
 Prefix (Polish) Notation
 Postfix (Reverse-Polish) Notation

DR. SHWETA SHARMA, PEC CHANDIGARH 2


Infix Notation
 We write expression in infix notation, e.g. a - b + c, where operators are
used in-between operands

 It is easy for humans to read, write, and speak in infix notation but the
same does not go well with computing devices

 We distinguish between ( A + B )*C and A + ( B * C ) by using either


parentheses or some operator-precedence convention

 An algorithm to process infix notation could be difficult and costly in


terms of time and space consumption
DR. SHWETA SHARMA, PEC CHANDIGARH 3
Prefix Notation
 In this notation, operator is prefixed to operands, i.e. operator is
written ahead of operands.

 For example, +ab

 This is equivalent to its infix notation a + b

 Prefix notation is also known as Polish Notation

 No parentheses are required


DR. SHWETA SHARMA, PEC CHANDIGARH 4
Postfix (Suffix) Notation
 The operator is post-fixed to the operands i.e., the operator is written
after the operands

 For example, ab+

 This is equivalent to its infix notation a + b

 This notation style is known as Reversed Polish Notation

 No parentheses are required


DR. SHWETA SHARMA, PEC CHANDIGARH 5
Evaluation of Arithmetic Expression by
Computer
 Stack-organized computers are better suited for post-fix notation
than the traditional infix notation

 The infix notation must be converted to the postfix notation

 The conversion from infix notation to postfix notation must take


into consideration the operational hierarchy

DR. SHWETA SHARMA, PEC CHANDIGARH 6


Difference
S. No. Infix Notation Prefix Notation Postfix Notation
1 a+b +ab ab+
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
4 a/b+c/d +/ab/cd ab/cd/+
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-

DR. SHWETA SHARMA, PEC CHANDIGARH 7


Parsing Expressions
 As we have discussed, it is not a very efficient way to design an
algorithm or program to parse infix notations

 Instead, these infix notations are first converted into either


postfix or prefix notations and then computed

 To parse any arithmetic expression, we need to take care of


operator precedence and associativity also

DR. SHWETA SHARMA, PEC CHANDIGARH 8


Precedence and Associativity

S. No. Operator Precedence Associativity


1 Exponentiation ( ^ ) Highest Right Associative

2 Multiplication ( ∗ ) Second Highest Left Associative


Division ( / )
3 Addition ( + ) Lowest Left Associative
Subtraction ( − )

DR. SHWETA SHARMA, PEC CHANDIGARH 9


DR. SHWETA SHARMA, PEC CHANDIGARH 10
Precedence
When an operand is in between two different operators, which
operator will take the operand first, is decided by the precedence of
an operator over others

For example −

As multiplication operation has precedence over addition, b * c will


be evaluated first

DR. SHWETA SHARMA, PEC CHANDIGARH 11


Associativity
Associativity describes the rule where operators with the same precedence
appear in an expression

For example, in expression a + b − c, both + and – have the same


precedence, then which part of the expression will be evaluated first, is
determined by associativity of those operators

Here, both + and − are left associative, so the expression will be evaluated
as (a + b) − c

DR. SHWETA SHARMA, PEC CHANDIGARH 12


Algorithm for Evaluation of Postfix Expression
Step 1 − Add a right parenthesis “)” at the end of STACK (acts as a sentinel)
Step 2 − Scan the expression from left to right until the “)” is encountered
Step 3 − If it is an operand, push it to STACK
Step 4 − If it is an operator, pull two operands from STACK and perform operation
Step 5 − Store the output of step 4, back on STACK
Step 6 − Scan the expression until all operands are consumed or the “)” is encountered
Step 7 − Set Final Value equal to the top element on STACK

DR. SHWETA SHARMA, PEC CHANDIGARH 13


Example
Post-fix notation: 4 5 6 * +

DR. SHWETA SHARMA, PEC CHANDIGARH 14


Questions on Evaluation of Postfix Expression
537*+4- Ans: 22

5 3 + 6 2 / *3 5 * + Ans: 39

Ans: -4
2 3 1*+ 9 -

Ans: 59
123*+45*6+2*+

DR. SHWETA SHARMA, PEC CHANDIGARH 15


Algorithm for Conversion of Infix to Postfix Expressions
Step 1 − If we have an opening parenthesis "(", we push it into the stack

Step 2 − If we have an operand, we append it to our postfix expression

Step 3 − If we have a closing parenthesis ")" we keep popping out elements from the top of the stack and append
them to our postfix expression until we encounter an opening parenthesis. We pop out the left parenthesis
without appending it

Step 4 − If we encounter an operator:-


4.1. If the operator has higher precedence than the one on top of the stack (We can compare ), we push
it in the stack
4.2. If the operator has lower or equal precedence than the one on top of the stack, we keep popping
out and appending it to the postfix expression

Step 5 − When the last token of infix expression has been scanned, we pop the remaining elements from stack
and append them to our postfix expression
DR. SHWETA SHARMA, PEC CHANDIGARH 16
Example Element Stack contents Postfix Expression
K K
+ +
Infix expression: L + KL
- - KL+
K + L - M*N + (O^P) * W M - KL+M
* -* KL+M
N -* KL+MN
+ + KL+MN*-
( +( KL+MN*-
Postfix expression: O +(^ KL+MN*-O
^ +(^ KL+MN*-O
KL+MN∗−OP^W*+ P +(^ KL+MN*-OP
) + KL+MN*-OP^
* +* K L + M N* - O P ^
W +* K L + M N* - O P ^ W
KL+MN∗−OP^W*+
DR. SHWETA SHARMA, PEC CHANDIGARH 17
Questions on Conversion to Postfix Expression
Infix Notation Post-fix Notation

(A-B)*[C/(D+E)+F] AB- CDE +/F +*

x^y/(5*z)+2 xy^5z*/2+

(x+y) * (p-q) / (m+n) xy + pq -*mn+/

DR. SHWETA SHARMA, PEC CHANDIGARH 18


Questions on Conversion to Postfix Expression

( (a+(b*c)) - (d^(e^f)) )

abc*+ def^^-

DR. SHWETA SHARMA, PEC CHANDIGARH 19


Thank you!

DR. SHWETA SHARMA, PEC CHANDIGARH 20

You might also like