You are on page 1of 1

Experiment 6

Name: Reverse Polish Notation (RPN)


Aim: Design and develop the program to represent given an expression into RPN notation.
Input: String of characters indicates an Expression.
Output: PN format of a given expression.
Theory:
Polish notation, also known as Polish prefix notation or simply prefix notation, is a form
of notation for logic, arithmetic, and algebra. Its distinguishing feature is that it
places operators to the left of their operands. The term Polish notation is sometimes taken (as
the opposite of infix notation) to also include Polish postfix notation, or Reverse Polish
notation, in which the operator is placed after the operands. When Polish notation is used as a
syntax for mathematical expressions by interpreters of programming languages, it is readily
parsed into abstract syntax trees and can, in fact, define a one-to-one representation for the
same. Because of this, Lisp and related programming languages define their entire syntax in
terms of prefix notation (and others use postfix notation).
For example, "1 2 add" would be postfix notation for adding the numbers 1 and 2.
Most programming languages use either prefix notation ("add(1, 2)" or "(add 1 2)") or infix
notation ("1 add 2" or "1 + 2"). Prefix and infix are more familiar to most people, as they are
the standard notations used for arithmetic and algebra. Many people wonder why anyone
would use this "weird" postfix notation.
The answer is that it is useful, especially for programming, because it clearly shows the order
in which operations are performed, and because it disambiguates operator groupings. For
example, look at this postfix expression:
12+3*6+23+/
This means "take 1 and 2, add them, take 3 and multiply, take 6 and add, take 2 and 3, add
them, and divide". In contrast, the equivalent expression in Infix Notation is
(((1 + 2) * 3) + 6) / (2 + 3)
This may seem more familiar, but note the need for parentheses to control the order of
evaluation. The prefix notation would be
(/ (+ (* (+ 1 2) 3) 6) (+ 2 3))
Which you to read "inside-out" to evaluate.
Example:

Input:
Output:

Enter infix notation: (a+b)*(c-d)


ab+cd-*

You might also like