You are on page 1of 57

Algebraic expression

An algebraic expression is a legal combination of

operands and the operators.

Operand is the quantity (unit of data) on which a

mathematical operation is performed.

Operand may be a variable like x, y, z or a constant

like 5, 4,0,9,1 ...

Operator is a symbol which signifies a mathematical or

logical operation between the operands.

Example of familiar operators include +,-,*, /, ^


Considering these definitions of operands and

operators now we can write an example of expression as A*B+C.

Stacks are used by compilers to help in the

process of evaluating expressions and generating machine language code. Humans generally write expressions like 3+4 and 9/3 in which the operator (+ or / here) is written between its operands ----- this is called infix notation.
Computers "prefer" postfix notation in which the

operator is written to the right of its two operands.

The preceding infix expressions would appear in

postfix notation as 3 4 + and 9 3 / respectively.

INFIX Expressions in which operands surround the operator, Example A-B, 6*3 This way of writing the Expressions is called infix notation.
To add A, B, we write A+B To multiply A, B, we write

A*B The operators ('+' and '*') go in between the operands ('A' and 'B') This is "Infix" notation.

POSTFIX Also called as Reverse Polish Notation

Put the operators after the operands as in

AB+ and

AB* This is Postfix notation.


To evaluate a complex infix expression, a compiler

would first convert the expression to postfix notation, and then evaluate the postfix version of the expression.

PREFIX Also known as polish notation Instead of saying "A plus B", we could say "add A,B " and write +AB "Multiply A,B" would be written *AB This is Prefix notation.

Why we need to convert a INFIX notation into a Prefix or Postfix


Infix notation is easy to read for humans, whereas

Question:

prefix/postfix notation is easier to parse for a machine. The big advantage in prefix/postfix notation is that there never arise any questions like operator precedence. For example, consider the infix expression 1 # 2 $ 3. Now, we don't know what those operators mean, so there are two possible corresponding postfix expressions: 1 2 # 3 $ and 1 2 3 $ #. Without knowing the rules governing the use of these operators, the infix expression is essentially worthless. Or, to put it in more general terms: it is possible to restore the original (parse) tree from a prefix/postfix expression without any additional knowledge, but the same isn't true for infix expressions
Find out: How a parse tree will be generated

if a compiler allowed infix expressions into the

binary code used in the compiled version of a program, the resulting code would be larger the needed and very inefficient. This is the reason, the compiler convert the infix expression into a postfix expression.
postfix is very easy to process left-to-right. An

operand is pushed onto a stack; an operator pops its operand(s) from the stack and pushes the result. Little or no parsing is necessary. It's used by some calculators (HP calculators are noted for using RPN).

1. Operator Priority How do you figure out the operands of an operator?


a+b*c a*b+c/d

This is done by assigning operator priorities.


priority(*) = priority(/)

> priority(+) = priority(-)

When an operand lies between two operators,

the operand associates with the operator that has higher priority.

2. Tie Breaker
When an operand lies between two operators

that have the same priority, the operand associates with the operator on the left.
a+b-c a*b/c/d

3. Delimiters
Sub expression within delimiters is treated as

a single operand, independent from the remainder of the expression.


(a + b) * (c d) / (e f)

Infix Expression Is Hard To Parse


Need operator priorities, tie breaker, and

delimiters. This makes computer evaluation more difficult than is necessary. Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier to evaluate expressions that are in these forms.

Examples
Infix
A+B (A+B) * (C + D) A-B/(C*D^E)

Postfix
AB+ AB+CD+* ABCDE^*/-

Prefix
+AB *+AB+CD -A/B*C^DE

Infix to Postfix
Suppose that we would like to rewrite

A+B*C in postfix
A+B*C A+(B*C) Parentheses for emphasis A+(BC*) Convert the multiplication, Let D=BC* A+D Convert the addition A(D)+ ABC*+ Postfix Form

Infix (A+B)/D (A+B) / (D+E) (A-B/C +E) / (A+B) A*(B+C)/D (A+B-C)*D(E+F) 2-3*4+5 (2-3)*(4+5) 2-(3*4+5) 3+4*5/6 3*2+25/5

Postfix AB+D/ AB+DE+/ ABC/-E+AB+/ (Check it once) ABC+*D/ AB + C D * E F + - (Check it) 234*-5+ (replaced with numbers) 23-45+* 234*5+345*6/+ (Is it Correct?) 32*25 5 /+ (Is it Correct)

Why do we need stacks ? .Check out here

Infix to postfix conversion


Infix expression

(a+b-c)*d(e+f)
Postfix expression

stack Infix expression

a+b-c)*d(e+f)
Postfix expression

stack Infix expression

+b-c)*d(e+f)
Postfix expression

stack Infix expression

b-c)*d(e+f)
Postfix expression

a
+

stack Infix expression

-c)*d(e+f)
Postfix expression

ab
+

stack Infix expression

c)*d(e+f)
Postfix expression

ab+
-

Stack Infix expression

)*d(e+f)
Postfix expression

ab+c
-

stack Infix expression

*d(e+f)
Postfix expression

ab+c-

stack Infix expression

d(e+f)
Postfix expression

ab+c-

stack Infix expression

(e+f)
Postfix expression

ab+c-d

stack Infix expression

(e+f)
Postfix expression

ab+cd*

stack Infix expression

e+f)
Postfix expression

ab+cd*
(

stack Infix expression

+f)
Postfix expression

ab+cd*e
(

stack Infix expression

f)
Postfix expression

+
(

ab+cd*e

stack Infix expression

)
Postfix expression

+
(

ab+cd*ef

stack Infix expression

Postfix expression

ab+cd*ef+

stack Infix expression

Postfix expression

ab+cd*ef+-

Algorithm (Infix to Postfix)


1) 2) 3) 4) Examine the next element in the input. If it is operand, output it. If it is opening parenthesis, push it on stack. If it is an operator, then i) If stack is empty, push operator on stack. ii) If the top of stack is opening parenthesis, push operator on stack iii) If it has higher priority than the top of stack, push operator on stack. iv) Else pop the operator from the stack and output it, repeat step 4 5) If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis. 6) If there is more input go to step 1 7) If there is no more input, pop the remaining operators to output.

Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form


Expression Stack Output

2 * 3 / ( 2 1 ) + 5 * 3

Empty * * / /( /( /(/(/ + + +* +* + Empty

2 2 23 23* 23* 23*2 23*2 23*21 23*2123*21-/ 23*21-/5 23*21-/5 23*21-/53 23*21-/53* 23*21-/53*+

Check it again, How a stack is used here.

Example
Infix String: A+B*C-D Postfix String: ABC*+D-

Step 1: A+B*C-D------ A+BC*-D Step 2: A + BC* - D ---- ABC*+ - D Step 3: ABC*+ - D ------- ABC*+Dis postfix conversion

Let us check by representing in stack..

Infix String:

A+B*C-D

Infix expression
A+B*C-D

Postfix expression

Initially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is A'. A' is added to the Postfix string.

Infix expression
+B*C-D

Postfix expression
A

The next character scanned is '+'. It being an operator, it is pushed to the stack.

Infix expression
B*C-D

Postfix expression
A

Next character scanned is B' which will be placed in the Postfix string.

Infix expression
*C-D

Postfix expression
AB

Next character is '*' which is an operator.

Infix expression
C-D

Postfix expression
AB
* +

The next character is C' which is placed in the Postfix string.

Infix expression
-D

Postfix expression
ABC
* +

Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'.

Infix expression
D

Postfix expression
* +
Now, What we are going to do..

ABC

Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty.

Infix expression
D

Postfix expression
+
Still some thing is there..

ABC*

Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.

Infix expression
D

Postfix expression
ABC*+

Next character is D' which is added to Postfix string.

Infix expression

Postfix expression
ABC*+D

Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string.

Infix expression

Postfix expression
ABC*+DInfix to Postfix expression completed

Exercises
Work out on

3+4*5/6
(Check the result 345*6/+ correct or wrong)
Work out on

(300+23)*(43-21)/(84+7) 300 23 + 43 21 -* 84 7 + / correct or wrong


Implementations: Conversion of Infix to Postfix expression. Postfix evaluation.

Evaluating Postfix Expression


Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*


Insert 4 (value / operand) into stack

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*

Insert 5(value / operand) into stack

5 4

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*

The next character is +(operator) So, pop the last top two from stack, perform + , Store the result on top of stack

4+5 = 9
9

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*

Insert 7 to the stack.

7 9

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*

Insert 2 to the stack

2 7 9

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*


The next character is (operator) Pop top two values of stack. Perform 7-2. Push the result to stack.

2 7 9
Now, Stack has.

5 9

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*


The next character is * (operator) Pop top two values of stack. Perform 9*5 Push the result to stack.

5 9

Now, Stack has.

45

Infix Expression:

(4+5) * (7-2) Postfix Expression value is: 45+72-*


All characters are completed Pop the top most value(you have only one) From the stack.. It is your result of the expression 45 The value of (4+5) * (7-2) = 45

45

Exercises
1. What is the value of 623+-382/+* (Check it is 7 or not)

2. Convert into postfix, Go for postfix evaluation 3+4*5/6 Is post fix is 345*6/+ Check the answer

You might also like