- Reverse Polish notation
**Reverse Polish notation**(or just**RPN**) by analogy with the relatedPolish notation , a prefix notation introduced in 1920 by the Polish mathematicianJan Łukasiewicz , is a mathematical notation wherein every operator follows all of its operands. It is also known as**Postfix notation**and is parenthesis-free.The Reverse Polish scheme was proposed by

F. L. Bauer andE. W. Dijkstra in the early 1960s to reduce computer memory access and utilize the stack to evaluate expressions. The notation and algorithms for this scheme were enriched byAustralia n philosopher and computer scientist Charles Hamblin in the mid-1960s. [*[*] [*http://www.csc.liv.ac.uk/~peter/hamblin.html "Charles L. Hamblin and his work"*] by Peter McBurney*[*]*http://www.csc.liv.ac.uk/~peter/this-month/this-month-3-030303.html "Charles L. Hamblin: Computer Pioneer"*] by Peter McBurney, July 27, 2008. "Hamblin soon became aware of the problems of (a) computing mathematical formulae containing brackets, and (b) the memory overhead in having dealing with memory stores each of which had its own name. One solution to the first problem was Jan Lukasiewicz's Polish notation, which enables a writer of mathematical notation to instruct a reader the order in which to execute the operations (e.g. addition, multiplication, etc) without using brackets. Polish notation achieves this by having an operator (+, *, etc) precede the operands to which it applies, e.g., +ab, instead of the usual, a+b. Hamblin, with his training in formal logic, knew of Lukasiewicz's work."Most of what follows is about

binary operator s. Aunary operator for which the Reverse Polish notation is the general convention is thefactorial .**Explanation**In Reverse Polish notation the

operator s follow theiroperands ; for instance, to add three and four, one would write "3 4 +" rather than "3 + 4". 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", and only the parentheses disambiguate the two meanings. In postfix, the former would be written "3 4 5 * −", which unambiguously means "3 (4 5 *) −".Interpreters of Reverse Polish notation are often stack-based; that is, operands are pushed onto a stack, and when an operation is performed, its operands are popped from a stack and its result pushed back on. Stacks, and therefore RPN, have the advantage of being easy to implement and very fast.

Note that, despite the name, reverse Polish notation is not exactly the reverse of Polish notation, as the operands of non-

commutative operations are still written in the conventional order (e.g. "6 3 /" in reverse Polish corresponds to "/ 6 3" in Polish notation, these both evaluating to 2). Numbers are also written with the digits in the conventional order.**Practical implications*** Calculations occur as soon as an operator is specified. Thus, expressions are not entered wholesale from right to left but calculated one piece at a time from the centre outwards. This results in fewer operator errors when performing complex calculations.Fact|date=June 2008

* The automatic stack permits the automatic storage of intermediate results for use later: this key feature is what permits RPN calculators easily to evaluate expressions of arbitrary complexity: they do not have limits on the complexity of expression they can calculate, unlike typical scientific calculators.

* Brackets and parentheses are unnecessary: the user simply performs calculations in the order that is required, letting the automatic stack store intermediate results on the fly for later use. Likewise, there is no requirement for the precedence rules required in infix notation.

* In RPN calculators, no "equals" key is required to force computation to occur.

* RPN calculators do, however, require an "enter" key to separate two adjacent numeric operands.

* The machine state is always a stack of values awaiting operation; it is impossible to enter an operator onto the stack. This makes use conceptually easy compared to more complex entry methods.

* Educationally, RPN calculators have the advantage that the user must understand the expression being calculated: it is not possible to simply copy the expression from paper into the machine and read off the answer without understanding. One must calculate from the middle of the expression, which makes life easier but only if the user understands what they are doing.

* Reverse Polish notation also reflects the way calculations are done on pen and paper. One first writes the numbers down and then performs the calculation. Thus the concept is easy to teach.

* The widespread use of infix electronic calculators using (infix) in educational systems can make RPN impractical at times due to rigid teaching methods; but once learned, most users of RPN find that it is faster and easier to calculate expressions,Fact|date=June 2008Who|date=June 2008 particularly the more complex ones, than with a conventional "scientific" calculator. It is also easy for a computer to convertinfix notation to postfix, most notably via Dijkstra's shunting yard algorithm - see converting from infix notation below.

*Users must know the size of the stack, since practical implementations of RPN use different sizes for the stack. For example, thealgebraic expression**1-1.001^(-6.2-2^3π)**, if performed with a stack size of 4 and executed from left to right, would exhaust the stack. The answer might be given as animaginary number instead of approximately 0.5 as areal number . To the novice user, calculations performed without regard to the limits of the stack would be inexplicably wrong.Fact|date=September 2008

* When writing RPN on paper, a job that is very rarely needed, adjacent numbers have to have a space between them. This requires clear handwriting to prevent confusion (for instance, 12 34 + could look a lot like 123 4 +).**The postfix algorithm**The algorithm for evaluating any postfix expression is fairly straightforward:

* While there are input tokens left

** Read the next token from input.

** If the token is a value

*** Push it onto the stack.

** Otherwise, the token is an operator.

*** It is known "a priori" that the operator takes**n**arguments.

*** If there are fewer than**n**values on the stack

******(Error)**The user has not input sufficient values in the expression.

*** Else, Pop the top**n**values from the stack.

*** Evaluate the operator, with the values as arguments.

*** Push the returned results, if any, back onto the stack.

* If there is only one value in the stack

** That value is the result of the calculation.

* If there are more values in the stack

****(Error)**The user input too many values.**Example**The infix expression "5 + ((1 + 2) * 4) − 3" can be written down like this in RPN::5 1 2 + 4 * + 3 −

The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the "Stack" is the list of values the algorithm is "keeping track of" after the "Operation" given in the middle column has taken place):When a computation is finished, its result remains as the top (and only) value in the stack; in this case, 14.

The above example could be rewritten by following the "chain calculation" method described by

HP for their series of RPN calculators:"As was demonstrated in the Algebraic mode, it is usually easier (fewer keystrokes) in working a problem like this to begin with the arithmetic operations inside the parentheses first." [

*http://h20219.www2.hp.com/Hpsub/downloads/17b2pChain.pdf*]:1 2 + 4 * 5 + 3 −

**Converting from infix notation**Edsger Dijkstra invented the "shunting yard" algorithm to convert infix expressions to postfix (RPN), so named because its operation resembles that of a railroad shunting yard.There are other ways of producing postfix expressions from infix notation. Most

Operator-precedence parser s can be modified to produce postfix expressions; in particular, once anabstract syntax tree has been constructed, the corresponding postfix expression is given by a simplepost-order traversal of that tree.**Implementations**The first computers to implement architectures enabling RPN were the English Electric Company's KDF9 machine, which was announced in 1960 and delivered (i.e. made available commercially) in 1963, and the American Burroughs B5000, announced in 1961 and also delivered in 1963. One of the designers of the B5000, Robert S. Barton, later wrote that he developed RPN independently of Hamblin, sometime in 1958 while reading a textbook on symbolic logic, and before he was aware of Hamblin's work.

Friden introduced RPN to the desktop calculator market with the EC-130 in June 1963.

Hewlett-Packard (HP) engineers designed the 9100A Desktop Calculator in 1968 with RPN. This calculator popularized RPN among the scientific and engineering communities, even though early advertisements for the 9100A failed to mention RPN. TheHP-35 , the world's first handheld scientificcalculator , used RPN in 1972, as did theHP-10C series of calculators, including the famous financial calculator, the HP-12C. When Hewlett-Packard introduced a later business calculator, theHP-19B , without RPN, feedback from financiers and others used to the 12-C compelled them to release theHP-19BII , which gave users the option of using algebraic notation or RPN.Existing implementations using Reverse Polish notation include:

* AnyStack-oriented programming language , such as:

** Forth

** Factor

**PostScript page description language

* [*http://www.farsightsoft.com RPN calculator*] for Windows

* [*http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx Microsoft PowerToy calculator*] for Windows XP

* [*http://midp-calc.sourceforge.net RPN calculator for cellular phones*] , in open source Java

* [*http://www.nthlab.com/software/rpn RPN calculator*] for Palm PDAs

* Mac OS X Calculator

* MostHewlett-Packard science/engineering and business/financecalculator s

*TI-89 andTI-92 series via third party software [*http://www.paxm.org/symbulator/download/rpn.html implementation*]

* Unix system calculator program dc

* [*http://web.newsguy.com/marcio/iphone/macalc/ MAcalc*] for theiPhone

* InteractiveJavaScript [*http://main.linuxfocus.org/~guido/javascript/rpnjcalc.html RPN calculator*]

* JavaScript [*http://dse.webonastick.com/rpncalc/ RPN calculator with keyboard-based user interface*] , more like HP calculators

* Mouseless online [*http://www.rpn-calculator.com RPN calculator*]

* [*http://www.lowth.com/rope/LanguageReference Linux IpTables "Rope" programming language*]

* (RPN calculator implemented in Ada)

* Emacs lisp library package: calc

* [*http://www.vintagecalculators.com/html/sinclair___the_pocket_calculat.html Sinclair calculators*]

* Open sourceGTK+ based [*http://galculator.sourceforge.net/ galculator*]

* Infix to Postfix Conversion / Postfix Evaluator / Postfix to Infix Conversion [*http://www.java2s.com/Code/JavaScript/Development/Postfix-Infix.htm*]

* Simple [*http://www.randomwalking.com/misc/xul_rpn/rpn.xul XUL RPN Calculator*]**A Postfix evaluator implemented in Python****Notes****See also**

*Forth (programming language)

*PostScript

*HP calculators

*LIFO

*Stack machine

*Subject Object Verb

*Object Subject Verb

*Prefix notation (Polish notation)

*Joy Programming Language

*Factor programming language **External links*** [

*http://rpnsolve.sourceforge.net RPNSolve*] - Free commandline-based RPN solver for Linux/Unix , Open Source (Gnu GPL). By Adam N. Ward

* [*http://midp-calc.sourceforge.net/Calc.html MIDP calculator*] A powerful RPN calculator for cells: statistics, matrix, plot,...(GPL) - By Roar Lauritzsen

* [*http://www.xnumber.com/xnumber/rpn_or_adl.htm "RPN or DAL? A brief analysis of Reverse Polish Notation against Direct Algebraic Logic"*] – By James Redin

* [*http://www.spsu.edu/cs/faculty/bbrown/web_lectures/postfix/ Postfix Notation Mini-Lecture*] – By Bob Brown

* [*http://www.langmaker.com/shallowfith.htm Fith: An Alien Conlang With A LIFO Grammar*] – By Jeffrey Henning

* [*http://qalculate.sourceforge.net/ Qalculate!*] An open-source calculator with an RPN mode

* [*http://www.tordivel.no/xcalc/ XCALC: A Windows freeware RPN Calculator*] – By Bernt Ribbum

* [*http://lashwhip.com/grpn.html GRPN: A Graphical RPN Calculator*] for Unix systems (GPL) – By Paul Wilkins

* [*http://mtvoid.com/calcium/ Calcium: A freeware RPN Calculator for S60 smartphones*] – By mtvoid

* [*http://www.objecttechnology.com/yarpncalc/ YaRPNcalc: A freeware RPN Calculator for PocketPC*] – By Philipp Tschannen

* [*http://www.nthlab.com/software/rpn/ RPN: An advanced, programmable postfix calculator with solving and graphing for Palm PDAs*] - By Russell Y. Webb

* [*http://www.phpclasses.org/browse/package/4078.html Simple RPN Interpreter written in PHP*] - by Arturo González-Mata

* [*http://www.speech.kth.se/calculator/ Calculator.NET*] - Enhanced Calculator for Windows with full RPN support

* [*http://web.newsguy.com/marcio/iphone/macalc/ MACalc*] - HP16C based calculator for iPhone

* [*http://www.benhaim.net/mecalc/mecalc.html Meculcalator*] - An on-line Javascript RPN calculator with advanced features

* [*http://www.cs.inf.ethz.ch/~wirth/Articles/GoodIdeas_origFig.pdf*] - Good Ideas remembered by Nick Wurth

* [*http://www.cs.inf.ethz.ch/~wirth/Articles/GoodIdeas_origFig.pdf*] - Good Ideas remembered by Nick Wurth

* [*http://home.scarlet.be/kpm/magenta2008/mg.html#rpn Magenta2008 RPN*] - RPN calculator written in JavaScript with support of complex numbers and optional expression evaluation

* [*http://free42.sourceforge.net/ Free42 -- An HP-42S Calculator Simulator*] - Free42 is a complete and free multi-platform re-implementation of the HP-42S calculator and the HP-82240 printer

*Wikimedia Foundation.
2010.*