Lecture 12 Polish Notation.pdf

36
Lecture : 12 POLISH NOTATION Instructor: Anum Masood 1

Transcript of Lecture 12 Polish Notation.pdf

Lecture : 12POLISH NOTATION

Instructor: Anum Masood

1

Contents of today’s Lecture

• Applications of stack

– Balancing symbols

– Postfix expression evaluation

– Translating infix expression to postfix expression

Applications of Stack• Balancing Symbols

Compilers uses a program that checks whether every symbol (like parenthesis, brackets etc) in a program is balanced. Algorithm for this purpose is:

1. Make an empty stack.

2. Read the characters until end of file.

3. If the character is an open bracket, push it onto the stack.

4. If it is a close bracket, then

if the stack is empty report an error.

Otherwise Pop the Stack.

If the popped symbol is not the corresponding opening symbol, thenreport an error.

5. At the end of the file, if the stack is not empty report an error.

Polish Notationa – b / c + d * e

Precedence? Level 2 ^ (Exponentiation)Level 1 * (Multiplication) & / (Division) Level 0 + (Addition) & - (Subtraction)

Same Level then scan from left to right (First one processed first)

Solution:1. b/c2. d*e3. a – a1 /a1 = b/c / 4. t2 + a2 / t2 = a – b/c / /a2 = d*e/

Infix, Postfix, Prefixinfix = a- b * c / d + e / f

postfix =a – bc* / d + e / fa – bc*d/ + e / fa – bc*d/ + ef/abc*d/- + ef/abc*d/-ef/+

prefix =a - *bc / d + e / fa - /*bcd + e / fa - /*bcd + /ef-a/*bcd + /ef+-a/*bcd/ef

Infix, Postfix, PrefixInfix = a * b + c

((a*b) +c)

Precedence:1. a * b2. a1 + c / a1 = a * b /

Prefix = * a b , +a1 c+*abc

postfix =

ab* , a1c+ab*c+

Infix, Postfix, Prefix

Infix: a+b*c–d/f+e

postfix: abc*+df/-e+

Prefix: +-+a*bc/dfe

Applications of Stack

Postfix Expression Evaluation

– When a number is seen, it is pushed onto the stack

– When an operator is seen, then pop two elements from stack and push the result onto the stack.

Now we evaluate the following postfix expression.6 5 2 3 + 8 * + 3 + *

1. The first four are placed on the stack. The resulting stack is

3

2

5

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

3

2

5

6

stack2. Next a + is read, so 3 and 2 are popped from the stack and their sum 5 is

pushed. 5

5

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

5

5

6

stack3. Next 8 is read and pushed. 8

5

5

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

4. Next a * is seen so 8 and 5 are popped as 8 * 5 = 40 is pushed40

5

6

stack

8

5

5

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

5. Next a + is read so 40 and 5 are popped and 40 + 5 = 45 is pushed.

40

5

6

stack

45

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

6. Now 3 is pushed

45

6

stack

3

45

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

7. Next symbol is + so pops 3 and 45 and pushes 45 + 3 = 48, so push 48 in stack.

3

45

6

stack

48

6

stack

Applications of Stack

• evaluating the following postfix expression.6 5 2 3 + 8 * + 3 + *

7. Finally a * is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed.

288

stack

48

6

stack

8. As there is no input, so pop the stack and we get the result.

Applications of Stack• Translating infix expressions to postfix expression

– When an operand is read, it is immediately placed onto the output.

– When an operator or left parenthesis comes then save it in the stack initially stack is empty.

– If we see a right parenthesis, then we pop the stack, writing symbols until we encounter a (corresponding) left parenthesis, which is popped but not output.

– If we see any other symbol (‘+’, ‘*’, ‘(‘, etc) then we pop entries form the stack until we find an entry of lower priority. One exception is that we never remove a ‘(‘ from the stack except when processing a ‘)’.

– When the popping is done, we push the operand onto the stack.

– Finally, if we read the end of input, we pop the stack until it is empty, writing symbols onto the output.

Applications of Stack• Translating infix expressions to postfix expression

Convert the following infix expression to postfix expression.a+b*c+(d*e+f)*g

1. First the symbol a is read, so it is passed through to the output aoutput2. Then + is read and pushed onto the stack.

+stack

4. Next a * is read. The top entry on the operator stack has lower precedence than *, so nothing is output and * is put on the .

3. Next b is read and passed through to the output. aboutput *

+

stack

Applications of StackConverting the following infix expression to postfix

expression.a+b*c+(d*e+f)*g5. Next, c is read and output.

6. The next symbol is a +. Checking the stack, we find that priority of stack top symbol * is higher than + . So we pop a * and place it on the output, Pop the other +, which is not of lower but equal priority, and then push +.

+stack

abc*+output

*

+

stack

abcoutput

Applications of StackConverting the following infix expression to postfix

expression.a+b*c+(d*e+f)*g

7. The next symbol read is an ‘(‘, which, being of highest precedence, is placed on the stack.

(

+stack

8. Then d is read and output. abc*+doutput

Applications of StackConverting the following infix expression to postfix

expression.a+b*c+(d*e+f)*g9. We continue by reading a *. Since open parenthesis do not get removed except

when a closed parenthesis is being processed, there is no output and we push * in stack

*

(

+

stack10. Next, e is read and output.

abc*+deoutput

Applications of StackConverting the following infix expression to postfix

expression.a+b*c+(d*e+f)*g11. The next symbol read is a +, since priority of stack top value is higher so we pop

* and push +.

abc*+de*output

+

(

+

stack

12. Now we read f and output f.abc*+de*f

output

Applications of StackConverting the following infix expression to postfix

expression.a+b*c+(d*e+f)*g

13. Now we read a ‘)’, so the stack is emptied back to the ‘(‘, we output a +.

14. We read a * next; it is pushed onto the stack.

abc*+de*f+

output+

stack

*

+

stack15. Now, g is read and output. abc*+de*f+g

output

Applications of StackConverting the following infix expression to postfix

expression.a+b*c+(d*e+f)*g

16. The input is now empty, so pop output symbols from the stack until it is empty.

abc*+de*f+g*+

outputstack

*

+

stack

Application of Stacks:Postfix Expression Calculation

Application of Stacks:Postfix Expression Calculator

Stack after pushing 6

Stack after pushing 3

Stack after retrieving the top two elements and popping twice

Stack after pushing the result of op1 + op2, which is 9

Application of Stacks:Postfix Expression Calculator

Stack after pushing 2

Stack after retrieving the top two elements and popping twice

Stack after pushing the result of op1 * op2, which is 18

Stack after popping the element

EXAMPLE

Convert following expression into Prefix:

(1) (a+b)*(c-d)(2) a+(b*c-(d/e^f)*)*h(3) a/(b-c)*d+g(4) (a+b)*c-(d-e)^(f+g)

Lab Assignment• Implement Applications of stack

1. Infix to Postfix conversion2. Postfix expression evaluation3. Infix to Prefix conversion

• Each application will be implemented as a separate function. Function will use stack, which is already implemented.

Conversion of Infix to Postfix Notation

• The steps for converting the expression manually are given here.

(1) The actual order of evaluation of the expression in infixnotation is determined by inserting parentheses in theexpression according to the precedence and associativityof operators.

(2)The expression in the innermost parentheses is convertedinto postfix notation by placing the operator after theoperands on which it operates.

(3)Step 2 is repeated until the entire expression is convertedinto a postfix notation.

Conversion of Infix to Postfix NotationFor example, to convert the expression a+b*c intoequivalent postfix notation, these steps are followed:

(1) Since the precedence of * is higher than +. the expressionb* c has to be evaluated first. Hence, the expression iswritten as (a+(b*c))

(2) The expression in the innermost parentheses, that is, b*cis converted into its postfix notation. Hence, it is writtenas bc*. The expression now becomes (a+bc*)

(3) Now the operator + has to be placed after its operands.The two operands for + operator are a and the expressionbc*.

The expression now becomes (abc*+)

• When expressions are complex, manual conversion becomesdifficult then postfix notation is used. On the other hand, theconversion of an infix expression into a postfix expression is simplewhen it is implemented through stacks.

• For example, consider the conversion of the following infixexpression to postfix expression:

a-(b+c)*d/f• Initially, a left parenthesis ‘(` is pushed onto the stack and the infix

expression is appended with a right parenthesis ‘) `.

Infix is read from left to right and the following steps are performed.

• The operand a is encountered, which is directly put to postfix.• The operator - is pushed onto the stack.• The left parenthesis ‘ ( ' is pushed onto the stack.• The next element is b which being an operand is directly put to postfix.• + being an operator is pushed onto the stack.• Next, c is put to postfix.• The next element is the right parenthesis’)` and hence, the operators on the top

of s t ac k are popped untill ’ (` is encountered in stack. Till now, the onlyoperator in the stack above the ‘ ( ` is +, which is popped and put to postfix.’ ( ` ispopped and removed from the stack.

• After this, the next element * is an operator and hence, it is pushedonto the stack.

• Then, d is put to postfix.• The next element is /. Since the precedence of / is same as the

precedence of *, the operator * is popped from the stack and / ispushed onto the stack (see Figure 3.7).

• The operand f is directly put to postfix after which,’)' is encountered .• On reaching ‘)` the operators in stack before the next ‘ (' is reached are

popped. Hence, / and - are popped and put to postfix• ‘)`is removed from the stack. Since stack is empty, the algorithm is

terminated and postfix is printed.

Conversion of Infix to Prefix Notation• The conversion of infix expression to prefix expression is similar to

conversion of infix to postfix expression.

• The only difference is that the expression in the infix notation is scanned in the reverse order, that is, from right to left. Therefore, the stack in this case stores the operators and the closing (right) parenthesis.

Questions

36