# What is the significance of reverse polish notation?

I teach computing to 18 year olds. After having reverse polish notation explained to them one asked why is it significant enough to be in the public exam. I explained the historical significance of 70s calculators but this failed to really address the issue. So are there and concurrent practical or theorhetical applications of RPN.

-
I have been programming for almost ten years and have completed a masters in CS but I don't think I have ever seen or used reverse polish in any serious context. Are you certain it is relevant, in particular for teaching students? –  Raphael Sep 23 '12 at 14:08
Here is an implementation of a regular expressions engine that uses postfix notation: swtch.com/~rsc/regexp/regexp1.html#thompson. Ken Thompson used it in his first implementation also. Also, it's a popular technique for evaluating expressions at run-time (if you are building a compiler). –  saadtaame Sep 23 '12 at 15:01
In RPN you can write down any expression without using brackets. –  Seg Fault Sep 23 '12 at 15:09
The wikipedia article on RPN covers much ground. Logic texts sometimes refer to polish notation (because it is bracket free), when they explain what can be omitted from proofs as pure "syntactic sugar". However, RPN is important because of its close relationship to stacks. Understanding stacks is important for interactive debugging, because the famous "backtrace" is normally just a stack dump. Pure functional languages are still struggling to offer something comparable to a stack dump for debugging, even so they would have much more detailed information to offer (if they could represent it). –  Thomas Klimpel Sep 23 '12 at 15:54
I have to retract my former comment partially. I have seen somebody use post-fix notation to string-dump trees. It was horrible to read, but easy to code. So in very specific use cases, RP may be useful, but I am not sure whether this extends to education (in general). You can use it to show that syntax is not fixed, I guess. –  Raphael Sep 23 '12 at 16:15
show 1 more comment

In computer reverse polish notation is often used in stack-based and concatenative programming languages. It is also common in dataflow and pipeline-based systems, including Unix pipelines.
computer uses stack for evaluate the arithmetic expressions
suppose following expression
3+4
in computer stack, it pushesh the element in following order

 3
4
+


i.e. top pointer points to + symbol

  +   //top pointer points here
4
3


when computer starts to evaluate, it first pop the operator +, here computer checks, whether it is operator or any other characters, if it is operator then understand that it has to do addition operation, then it pop the two operands for arithmetic addition, i.e. these are the next elements of the stack. that is 4 & 3
here pop order is + 4 3
in short for solving complex expressions reverse polish notation is used

If there are multiple operations, the operator is given immediately after its second operand; so the expression written "3 − 4 + 5" in conventional infix notation would be written "3 4 − 5 +"in RPN: first subtract 4 from 3, then add 5 to that.

An advantage of RPN is that it obviates the need for parentheses that are required by infix. While "3 − 4 * 5" can also be written "3 − (4 * 5)", that means something quite different from "(3 − 4) * 5". In postfix, the former could be written "3 4 5 * −", which unambiguously means "3 (4 5 *) −" which reduces to "3 20 −"; the latter could be written "3 4 - 5 *" (or 5 3 4 - *, if you wish to keep similar formatting), which unambiguously means "(3 4 -) 5 *".

-

I've used RPN several times for rapid prototyping, e.g. of programs that have to read and interpret a user-supplied mathematical expression.

Whereas regular mathematical notation would require at least a recursive parser (think brackets, operator order, etc...), an RPN parser is basically a stack with a switch`-like statement. I guess it's this combination of simplicity and expressive power that led HP to use it initially.

This is, however, usually for rapid prototyping and for convenience. I would never assume that a user can, or wants to, understand RPN.

-

Just to expand the previous answers/comments: don't forget that RPN is alive and on great form ... indeed it is currently used in stack machines like the Java virtual machine.

From Wikipedia: "... a stack machine implements a stack with registers. The operands of the arithmetic logic unit (ALU) are always the top two registers of the stack and the result from the ALU is stored in the top register of the stack. 'Stack machine' commonly refers to computers which use a Last-in, First-out stack to hold short-lived temporary values while executing individual program statements. The instruction set carries out most ALU actions with postfix (Reverse Polish notation) operations that work only on the expression stack, not on data registers or main memory cells ..."

-
Hm, looking at bytecode you don't see RPN per se; of course the instructions show up in "post-fix" order but that doesn't look like RPN in arithmetics, and its reason is rather clear. Register machines have to proceed in the same way: load values, then combine. –  Raphael Sep 24 '12 at 11:21

With regards to calculators: See What is RPN?

• Benefits: RPN saves time and keystrokes. You avoid using and keeping track of parentheses while doing calculations. The process is similar to the way you learned math on paper.

• You can see the intermediary results as you perform your computations rather than just the answer at the end. This is extremely helpful for learning the logic. Math teachers are using this feature to improve student understanding of mathematics.

• An intermediate result allows the user to check the answer and correct errors more easily. It's easier to follow the stream of calculation. The user defines the priority of operators.

• RPN is logical because the user first gives the number and then tells what to do with it.

-