Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

B.

Sc Part-I Paper-B Section (B)


Data Structure

 Arithmetic Expression:
The arithmetic expression consists of operands and operators. An operand may be a variable or
constant. The operator is a symbol that performs some operation on operands. So the combination of operands
and operators is called an arithmetic expression.

Example of Arithmetic expression:

 A+B/C

ABC +/
Operands Operators

 2+7 %6-8*9

27689 +%-*
Operands Operators

 Notations of Arithmetic Expression:


There are different method / ways of writing arithmetic expression, which are called Arithmetic notations or
simply notations.

Following are three major notations of arithmetic expressions.

 Infix notation
 Postfix notation
 Prefix notation

Infix, postfix and prefix notations are three different but equivalent ways of writing expressions.

Now we will explain each of the above one by one.

 Infix Notation:
A way of writing expressions in which operator comes between their operands is called infix notation. This
is the usual way we write expression. Infix notation need extra information to make the order of evaluation
of operators clear: rules are built to evaluate the operators such as BDMAS rule.
If one ignores that rules then the expressions will not be evaluated properly.

Chapter# 5 | Simple Syntax Analysis 1


Examples of infix notation:
 A+B
 a+b*c
 2*73/7

Note:  or ^ means exponent or power

 Postfix Notation:
Postfix notation is also known as Reverse Polish or suffix notation. A way of writing expressions in which
operator comes after their operands is called postfix (reverse Polish or suffix) notation.
Examples of postfix notation:
 AB+
 abc*+
 39*
 Prefix Notation:
Prefix notation is also known as Polish notation. A way of writing expressions in which operator comes
before their operands is called prefix (Polish) notation.
Examples of prefix notation:
 +AB
 *56
 +*abc

Note: Polish notations are named after the Polish mathematician Jan Lukasiewicz, who introduced this
notation.

 Operator Precedence and Rule for calculating arithmetic expressions:


Arithmetic expression is always calculated from left to right. Operators are evaluated according to their priority.
If expressions have same priority operators then the left most is evaluated first. The uses of brackets increase
operator’s priority. So if expressions have brackets they are evaluated first.

The operators and their precedence table is:

Priority Operators
level Symbol Name
Highest  or ^ Power
Next highest *, / , % Multiply , Divide , Modulus
Lowest +, - Addition , Subtraction

Chapter# 5 | Simple Syntax Analysis 2


Notation Conversion:
Usually expressions are represented in infix notations but why conversion of infix to postfix and infix to prefix
take place? The answer is that the infix notation is a convention which humans are familiar with and is easier to
read. But for a computer, this form is difficult. Hence the need arises to find another suitable system for
representing arithmetic expressions. So for this purpose the prefix and postfix notations are introduced. Thus we
convert infix notation into postfix and prefix notations. Postfix or prefix notation has several advantage over
infix notation. That is, in postfix or prefix form the operators are evaluated according to their positions; thus
operator precedence is no longer require.

So advantage of postfix and prefix notation are:

1. The need for parenthesis is eliminated


2. The precedence rule is no longer relevant
3. The expression is evaluated by making left to right scan for postfix right to left scan for prefix by the
use of stacking.
There are following methods of notations conversion:

1) Hand Inspection Method


2) Stack Method
3) Binary Tree Traversal

Note: Binary Tree Traversal Method will be discussed in next chapter.

 Hand Inspection Method


Hand inspection method is an easy method of notation conversion. This method uses the rules of
mathematics. All the operators’ precedence’s are used in the same manner as in mathematics. We will
explain hand inspection method here below for different notations conversions.

 Conversion of Infix Notation to Postfix Notation Using Hand Inspection Method:


Steps:
 Scan the given expression from left to right.
 Select the high priority operators with its operand
 Shift the operator to the right of the operands
 Repeat the above process until no operator left

Example #1: Convert into postfix notation: A+B*C


Sol: A+B*C
=A+ B*C => B*C= BC*
= A+ BC* => A+BC* = ABC*+
=ABC*+
Required postfix notation.

Chapter# 5 | Simple Syntax Analysis 3


Example #2: Convert into postfix notation: (A+B)*C
Sol: (A+B)*C
= (A+B)*C => A+B=AB+
= AB+ *C => AB+ *C= AB+C*
=AB+C*
Required postfix notation.

Example #3: Convert into postfix notation: 5*(6+2)-12/4


Sol: 5*(6+2)-12/4
= 5* (6+2)-12/4 => 6+2= 6, 2, +
= 5 * 6, 2, + -12/4 => 5* 6, 2+ = 5, 6, 2, +, *
= 5, 6, 2, +, * - 12 / 4 => 12 / 4= 12, 4/
= 5, 6, 2, +, * - 12, 4/ => 5, 6, 2+* - 12, 4/ = 5, 6, 2, +, *, 12, 4, /, -
= 5, 6, 2, +, *, 12, 4, /, -
Required postfix notation.

Example #4: Convert into postfix notation: E= ((A-(B+C))*D)*(E+F)


Sol: E= ((A-(B+C))*D)*(E+F)
= ((A- (B+C))*D)*(E+F) => B+C = BC+
= ((A- BC+)*D)*(E+F) => A- BC+ = ABC+-
= (ABC+-*D)*(E+F) => ABC+- * D = ABC+-D*
= ABC+-D* * (E+F) => E+F= EF+
= ABC+-D* * EF+ => ABC+-D* * EF+ = ABC+-D*EF+*
E= ABC+-D*EF+*
Required postfix notation.

Example #5: Convert into postfix notation: A+ (B*C- (D / E  F) *G) * H


Sol: A+ (B*C- (D / E  F) *G) * H
= A+ (B*C- (D / E  F) *G) * H => E  F= EF
= A+ (B*C- (D / EF) *G) * H => D / EF = DEF/
= A+ (B*C- DEF/ *G) * H => B*C= BC*
= A+ (BC* - DEF/ * G) * H => DEF/ * G = DEF/G*
=A+ (BC* - DEF/ G*) * H => BC* - DEF/G*= BC*DEF/G*-
= A+ BC*DEF/G*- * H => BC*DEF/G*- * H = BC*DEF/G*-H*
= A+ BC*DEF/G*-H* => A+ BC*DEF/G*-H*= ABC*DEF/G*-H*+
= ABC*DEF/G*-H*+
Required postfix notation.

Chapter# 5 | Simple Syntax Analysis 4


 Assignment: Convert the following into postfix notation:

5.1 : (A-B) * (D/E)


5.2 : (A + B D) / (E-F) +G
5.3 : A* (B+D)/E-F*(G+H/K)
5.4 : 12 / (7-3)+2* (1+5)
Answers:
5.1 : AB-DE/*
5.2 : ABD+EF-/G+
5.3 : ABD+*E/FGHK/+*-
5.4 : 12, 7, 3, -, /, 2, 1, 5, +, *, +

 Conversion of Postfix Notation to Infix Notation Using Hand Inspection Method:

Steps:

 Scan the given expression from left to right.


 Select the first encounter operator and put them in between its closest left side operands, which
doesn’t have operator, by placing brackets before and after operand / sub expression.
 Repeat the above steps until no operator left

Example #1: Convert into infix notation: ABC*+


Sol:
ABC*+

= A (B*C) +
= (A+ (B*C))

Required infix notation.

Note: Although many brackets will be appearing in expression but they will appear in such a way that it doesn’t
affect the expression result. In above example the original expression is:

A+B*C

And our answer is: (A+ (B*C))

But if we convert them into postfix then they will give the same answer. So they are equal.

Chapter# 5 | Simple Syntax Analysis 5


Example #2: Convert into infix notation: AB+C*

Sol: AB+C*
= (A+B) C*
= ((A+B)*C)
Required infix notation.

Example #3: Convert into postfix notation: 5, 6, 2, +, *, 12, 4, /, -


Sol: 5, 6, 2, +, *, 12, 4, /, -
= 5, (6+2), *, 12, 4, /, -
= (5*(6+2)), 12, 4, /, -
= (5*(6+2)), (12/4), -
= ((5*(6+2)) - (12/4))
Required infix notation.

Example #4: Convert into infix notation: E= ABC+-D*EF+*


Sol: E= ABC+-D*EF+*
= A (B+C)-D*EF+*
= (A-(B+C)) D*EF+*
= ((A-(B+C)) *D) EF+*
= ((A-(B+C)) *D) (E+F)*
= (((A-(B+C)) *D)* (E+F))
Required infix notation.

Example #5: Convert into infix notation: ABC*DEF/G*-H*+


Sol: ABC*DEF/G*-H*+
= A (B*C) DEF/G*-H*+
= A (B*C) D (EF)/ G*-H*+
= A (B*C) (D/ (EF)) G*-H*+
= A (B*C) ((D/ (EF)) *G) -H*+
= A ((B*C) - ((D/ (EF)) *G)) H*+
= A ((B*C) – ((D/ (EF)) *G)) * H) +
= (A+ ((B*C) – ((D/ (EF)) *G)) * H)
Required infix notation.

Chapter# 5 | Simple Syntax Analysis 6


 Conversion of Infix Notation to Prefix Notation Using Hand Inspection Method:
Steps:
 Scan the given expression from left to right.
 Select the high priority operators with its operand
 Shift the operator to the left of the operands
 Repeat the above process until no operator left

Example #1: Convert into prefix notation: A+B*C


Sol: A+B*C
=A+ B*C => B*C= *BC
= A+ *BC => A+*BC = +A*BC
=+A*BC
Required prefix notation.

Example #2: Convert into prefix notation: (A+B)*C


Sol: (A+B)*C
= (A+B)*C => A+B= +AB
= +AB *C => +AB *C= *+ABC
=*+ABC
Required prefix notation.

Example #3: Convert into prefix notation: 5*(6+2)-12/4


Sol: 5*(6+2)-12/4
= 5* (6+2)-12/4 => 6+2= +, 6, 2
= 5 * +, 6, 2 -12/4 => 5* +, 6, 2= *, 5, +, 6, 2
= *, 5, +, 6, 2- 12 / 4 => 12 / 4= /, 12, 4
= *, 5, +, 6, 2- /, 12, 4 => *, 5, +, 6, 2- 12, 4/ = -, *, 5, +, 6, 2, /, 12, 4
= -, *, 5, +, 6, 2, /, 12, 4
Required prefix notation.

Chapter# 5 | Simple Syntax Analysis 7


Example #4: Convert into prefix notation: E= ((A-(B+C))*D)*(E+F)
Sol: E= ((A-(B+C))*D)*(E+F)
= ((A- (B+C))*D)*(E+F) => B+C = +BC
= ((A- +BC)*D)*(E+F) => A- +BC = -A+BC
= (-A+BC *D)*(E+F) => -A+BC * D = *-A+BCD
= *-A+BCD * (E+F) => E+F= +EF
= *-A+BCD * +EF => *-A+BCD * +EF = **-A+BCD+EF
E= **-A+BCD+EF
Required prefix notation.

Example #5: Convert into prefix notation: A+ (B*C- (D / E  F) *G) * H


Sol: A+ (B*C- (D / E  F) *G) * H
= A+ (B*C- (D / E  F) *G) * H => E  F= EF
= A+ (B*C- (D / EF) *G) * H => D / EF = /DEF
= A+ (B*C- /DEF *G) * H => B*C= *BC
= A+ (*BC - /DEF * G) * H => /DEF * G = */DEFG
=A+ (*BC - */DEFG) * H => *BC - */DEFG = -*BC*/DEFG
= A+ -*BC*/DEFG * H => -*BC*/DEFG * H = *-*BC*/DEFGH
= A+ *-*BC*/DEFGH => A+ *-*BC*/DEFGH = +A*-*BC*/DEFGH
= +A*-*BC*/DEFGH
Required prefix notation.

Example#6: Convert into prefix notation: + + ( − ).


Sol: This expression can be written as: A2+B2+ (C-D)
= A2+B2+ (C-D) => C-D= -CD
= A2+B2+ -CD => A2 = A2
= A2 + B2+ -CD => B2= B2
= A2 + B2 + -CD => A2 + B2= +A2B2
= +A2B2 + -CD => +A2B2 + -CD = ++A2B2-CD
= ++A2B2-CD
Required prefix notation.

Chapter# 5 | Simple Syntax Analysis 8


 Conversion of Prefix Notation to Infix Notation Using Hand Inspection Method:

Steps:

 Scan the given expression from right to left.


 Select the first encounter operator and put them in between its closest right side operands, which
doesn’t have operator, by placing brackets before and after operand / sub expression.
 Repeat the above steps until no operator left

Example #1: Convert into infix notation: +A*BC


Sol:
= +A*BC

= + A (B*C)
= (A+ (B*C))
Required infix notation.

Example #2: Convert into infix notation: *+ABC


Sol:
= *+ABC

= * (A+B) C
= ((A+B)*C)
Required infix notation.

Example #4: Convert into infix notation: E= **-A+BCD+EF


Sol:
E= **-A+BCD+EF
E= **-A+BCD (E+F)
E= **-A (B+C) D (E+F)
E= ** (A- (B+C)) D (E+F)
E= * ((A- (B+C)) * D) (E+F)
E= (((A- (B+C)) * D) * (E+F))

Required infix notation.

Chapter# 5 | Simple Syntax Analysis 9


Example#6: Convert into infix notation: ++A2B2-CD
Sol:
= ++A2B2-CD
= ++A2B2 (C-D)
= ++A2 (B2) (C-D)
= ++ (A2) (B2) (C-D)
= + ((A2) + (B2)) (C-D)
= (((A2) + (B2)) + (C-D))
Required infix notation.

 Assignment: Convert the following into prefix notation:

5.5 : (A- (B+C-D)+(E+F)


5.6 : A-B+C * (A*C)2
Convert the following into infix notation:
5.7 : +A*-*BC*/DEFGH
Answers:
5.5 : + - A - + BCD+EF
5.6 : - A +B *C  * CD2
5.7 : (A+ ((B*C) – ((D/ (EF)) *G)) * H)

 Stack Method:
The stack method is used to convert an infix notation into postfix notation. The stack is used to store
operands and then passed to postfix expressions according to their precedence. The infix notation is
converted into postfix notation according to the following rules:
a) The infix expression is scanned from left to right until end of the expression.
b) The operands are added to the output.
c) While operators and parenthesis (left parenthesis) are put into stack. If an operator of high priority
(as compared to one which is in stack) is put into stack, then no popping would be done. But if an
operator of low or same priority (as compared to one which is in stack) put in stack than then the
high priority or equal priority operator is popped and added to output.
d) If an operator is before left parenthesis ‘(’mean while right parenthesis ‘)’encounter then the operator
before left parenthesis is popped and added to the output. The left and right parenthesis will be
disappeared.

Chapter# 5 | Simple Syntax Analysis 10


e) When end of the expression is reached, then all the operators from stack are popped and added to
the output.

Example #1: Convert into postfix notation using stack method: A+B*C

Sol: A+B*C

* Output (Postfix Notation): ABC*+


+

Example #2: Convert into postfix notation using stack method: ((A-(B+C))*D)*(E+F)

Sol: ((A-(B+C))*D)*(E+F)

+
( +
- - (
( ( * * *
( ( ( ( (

Output (Postfix Notation): A B C + - DE F + *

Example #3: Convert into postfix notation using stack method: A+ (B*C- (D / E  F) *G) * H

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


/ /
( ( *
* - - - *
( ( ( ( (
+ + + + +

Output (Postfix Notation): A B C * D E F  / G * - H * +

Chapter# 5 | Simple Syntax Analysis 11


Example #4: Convert into postfix notation using stack method: A+B*C+ (D*E+F) *G.

Sol: A+B*C+ (D*E+F) *G

* +
* ( ( *
+ + + +

Output (Postfix Notation): A B C * + D E * F + G * +

 Assignment: Convert the following into postfix notation using stack method:

5.8 : (A-B) * (D / E)
5.9 : (A+B  D) / (E-F) +G
5.10 12 / (7-3) +2 * (1+5)
Answers:
5.8 : AB-DE/*
5.9 : ABD+EF-/G+
5.10 12, 7, 3, -, /, 2, 1, 5, +, *, +,

 
Prepared By: Muhammad Fayaz
M.Sc Computer Science, SBBU.
Contact#: 0342-5850786
E-mail: fayazsbbu786@gmail.com

Chapter# 5 | Simple Syntax Analysis 12

You might also like