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" would be written *AB  This is Prefix notation.B " and write +AB  "Multiply A.

The big advantage in prefix/postfix notation is that there never arise any questions like operator precedence. we don't know what those operators mean. Without knowing the rules governing the use of these operators.Why we need to convert a INFIX notation into a Prefix or Postfix  Infix notation is easy to read for humans. the infix expression is essentially worthless. consider the infix expression 1 # 2 \$ 3. 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. whereas Question: prefix/postfix notation is easier to parse for a machine.  For example. Now. so there are two possible corresponding postfix expressions: 1 2 # 3 \$ and 1 2 3 \$ #. Or. but the same isn't true for infix expressions Find out: How a parse tree will be generated .

the resulting code would be larger the needed and very inefficient.  postfix is very easy to process left-to-right. An operand is pushed onto a stack.  This is the reason. an operator pops its operand(s) from the stack and pushes the result. . the compiler convert the infix expression into a postfix expression. It's used by some calculators (HP calculators are noted for using RPN). Little or no parsing is necessary. if a compiler allowed infix expressions into the binary code used in the compiled version of a program.

• 1. . the operand associates with the operator that has higher priority. priority(*) = priority(/) > priority(+) = priority(-) • When an operand lies between two operators. 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.

Tie Breaker  When an operand lies between two operators that have the same priority. the operand associates with the operator on the left. 2. a+b-c a*b/c/d .

 3. (a + b) * (c – d) / (e – f) . independent from the remainder of the expression. Delimiters  Sub expression within delimiters is treated as a single operand.

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

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 .

(Check it) 234*-5+ (replaced with numbers) 23-45+* 234*5+345*6/+ (Is it Correct?) 32*25 5 /+ (Is it Correct) .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 out here…… .Why do we need stacks ? …….

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 a ( .

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+c–d* - .

stack Infix expression e+f) Postfix expression ab+c–d* ( - .

stack Infix expression +f) Postfix expression ab+c–d*e ( - .

stack Infix expression f) Postfix expression + ( ab+c–d*e - .

stack Infix expression ) Postfix expression + ( ab+c–d*ef - .

stack Infix expression Postfix expression ab+c–d*ef+ - .

stack Infix expression Postfix expression ab+c–d*ef+- .

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

How a stack is used here…. .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.

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*+Dis postfix conversion Let us check by representing in stack……….D Step 3: ABC*+ . ..D ---- ABC*+ .

Infix String: A+B*C-D Infix expression A+B*C-D Postfix expression .

the first character scanned is ‘A'.Initially the Stack is empty and our Postfix string has no characters. ‘A' is added to the Postfix string. Infix expression +B*C-D Postfix expression A . Now.

it is pushed to the stack.The next character scanned is '+'. It being an operator. 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 + .

Infix expression C-D Postfix expression AB * + .Next character is '*' which is an operator.

Infix expression -D Postfix expression ABC * + .The next character is ‘C' which is placed in the Postfix string.

Infix expression D Postfix expression * + Now. The topmost character in the stack is '*' which has a higher precedence than '-'. What we are going to do……. ? ABC ..Next character scanned is '-'.

Infix expression D Postfix expression + Still some thing is there……..Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. ? 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. Infix expression D Postfix expression ABC*+ - . The '-' will be pushed to the stack.

Infix expression Postfix expression ABC*+D - .Next character is ‘D' which is added to Postfix string.

Infix expression Postfix expression ABC*+DInfix to Postfix expression completed .Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string.

.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 4 .

 Infix Expression: (4+5) * (7-2) Postfix Expression value is: 45+72-* Insert 5(value / operand) into stack 5 4 .

pop the last top two from stack. Infix Expression: (4+5) * (7-2) Postfix Expression value is: 45+72-* The next character is +(operator) So. Store the result on top of stack 4+5 = 9 9 . perform + .

 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 .

Stack has…. 2 7 9 Now. Perform 7-2. 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. Push the result to stack.

 Infix Expression: (4+5) * (7-2) Postfix Expression value is: 45+72-* The next character is * (operator) Pop top two values of stack. 5 9 Now. Perform 9*5 Push the result to stack. 45 . Stack has….

It is your result of the expression 45 The value of (4+5) * (7-2) = 45 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…..

Convert into postfix.Exercises 1. Go for postfix evaluation 3+4*5/6 Is post fix is 345*6/+ Check the answer . What is the value of 623+-382/+* (Check it is 7 or not) 2.