0% found this document useful (0 votes)
764 views

Lecture 12 Polish Notation PDF

The document discusses converting infix expressions to postfix notation using stacks. It explains how a stack can be used to translate an infix expression step-by-step by pushing operators and popping elements based on precedence. An example of converting the expression "a-(b+c)*d/f" to postfix is provided. The key steps are to push operators to the stack and pop them out to the output when a higher precedence operator is seen or when reaching a closing parenthesis.

Uploaded by

Muhammad Usman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
764 views

Lecture 12 Polish Notation PDF

The document discusses converting infix expressions to postfix notation using stacks. It explains how a stack can be used to translate an infix expression step-by-step by pushing operators and popping elements based on precedence. An example of converting the expression "a-(b+c)*d/f" to postfix is provided. The key steps are to push operators to the stack and pop them out to the output when a higher precedence operator is seen or when reaching a closing parenthesis.

Uploaded by

Muhammad Usman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Lecture : 12

POLISH NOTATION
Instructor: Anum Masood

Contents of todays 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, then
report an error.

5.

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

Polish Notation
ab/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/c
2. d*e
3. a a1
4. t2 + a2

/a1 = b/c /
/ t2 = a b/c /

/a2 = d*e/

Infix, Postfix, Prefix


infix = a- b * c / d + e / f
postfix =a bc* / d + e / f
a bc*d/ + e / f
a bc*d/ + ef/
abc*d/- + ef/
abc*d/-ef/+
prefix =a - *bc / d + e / f
a - /*bcd + e / f
a - /*bcd + /ef
-a/*bcd + /ef
+-a/*bcd/ef

Infix, Postfix, Prefix


Infix = a * b + c
((a*b) +c)
Precedence:
1. a * b
2. a1 + c / a1 = a * b /
Prefix =
* a b , +a1 c
+*abc
postfix =
ab* , a1c+
ab*c+

Infix, Postfix, Prefix


Infix:
a+b*cd/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.


3
6523+8*+3+*
1.

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

2
5
6

stack

Applications of Stack
evaluating the following postfix
expression.
3
6523+8*+3+*
2

5
6
2.

stack

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

stack

Applications of Stack

evaluating the following postfix expression.


6523+8*+3+*
5
5
6

stack
3.

Next 8 is read and pushed.

8
5
5
6

stack

Applications of Stack

evaluating the following postfix expression.


6523+8*+3+*
8
5
5
6

4.

stack

Next a * is seen so 8 and 5 are popped as 8 * 5 = 40 is pushed


40
5
6

stack

Applications of Stack

evaluating the following postfix expression.


6523+8*+3+*
40
5
6

stack
5.

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


45
6

stack

Applications of Stack

evaluating the following postfix expression.


6523+8*+3+*
45
6

stack
6.

Now 3 is pushed
3
45
6

stack

Applications of Stack

evaluating the following postfix expression.


6523+8*+3+*
3
45
6

7.

stack

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


48
6

stack

Applications of Stack

evaluating the following postfix expression.


6523+8*+3+*
48
6

stack
7.

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

8.

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


288

stack

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

First the symbol a is read, so it is passed through to the output

Then + is read and pushed onto the stack.

output
+
stack

3.
4.

Next b is read and passed through to the output.

ab
output

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

*
+

stack

Applications of Stack
Converting the following infix expression to postfix
expression.
a+b*c+(d*e+f)*g
abc
5. Next, c is read and output.
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

Applications of Stack
Converting 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*+d
output

Applications of Stack
Converting the following infix expression to postfix
expression.
a+b*c+(d*e+f)*g
9.

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
*
(
+

stack
10. Next, e is read and output.

abc*+de
output

Applications of Stack
Converting the following infix expression to postfix
expression.
a+b*c+(d*e+f)*g

11. 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 Stack
Converting 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 +.

abc*+de*f+
+
stack

output

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

*
+

stack
15. Now, g is read and output.

abc*+de*f+g
output

Applications of Stack
Converting the following infix expression to postfix
expression.
*
a+b*c+(d*e+f)*g
+

stack

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

abc*+de*f+g*+
stack

output

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.
2.
3.

Infix to Postfix conversion


Postfix expression evaluation
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 infix


notation is determined by inserting parentheses in the
expression according to the precedence and associativity
of operators.
(2)The expression in the innermost parentheses is converted
into postfix notation by placing the operator after the
operands on which it operates.
(3)Step 2 is repeated until the entire expression is converted
into a postfix notation.

Conversion of Infix to Postfix Notation


For example, to convert the expression a+b*c into
equivalent postfix notation, these steps are followed:
(1) Since the precedence of * is higher than +. the expression
b* c has to be evaluated first. Hence, the expression is
written as (a+(b*c))
(2) The expression in the innermost parentheses, that is, b*c
is converted into its postfix notation. Hence, it is written
as 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 expression
bc*.
The expression now becomes (abc*+)

When expressions are complex, manual conversion becomes


difficult then postfix notation is used. On the other hand, the
conversion of an infix expression into a postfix expression is simple
when it is implemented through stacks.
For example, consider the conversion of the following infix
expression 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 only
operator in the stack above the ( ` is +, which is popped and put to postfix. ( ` is
popped and removed from the stack.

After this, the next element * is an operator and hence, it is pushed


onto 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 / is
pushed 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy