Lecture 12 Polish Notation PDF
Lecture 12 Polish Notation PDF
POLISH NOTATION
Instructor: Anum Masood
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.
2.
3.
4.
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/
Applications of Stack
Postfix Expression Evaluation
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
stack
3.
8
5
5
6
stack
Applications of Stack
4.
stack
stack
Applications of Stack
stack
5.
stack
Applications of Stack
stack
6.
Now 3 is pushed
3
45
6
stack
Applications of Stack
7.
stack
stack
Applications of Stack
stack
7.
Finally a * is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed.
8.
stack
Applications of Stack
Translating infix expressions to postfix expression
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
output
+
stack
3.
4.
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.
(
+
stack
8.
abc*+d
output
Applications of Stack
Converting the following infix expression to postfix
expression.
a+b*c+(d*e+f)*g
9.
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
abc*+de*f+
+
stack
output
*
+
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
Application of Stacks:
Postfix Expression Calculator
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
Questions
36