Computer Science    
   
Table of contents
(Prev) Reverse indexReview of United States Human ... (Next)

Reverse Polish notation

Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in contrast to Polish notation, which puts the operator in the prefix position. It is also known as postfix notation and is parenthesis-free as long as operator arities are fixed. The description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented (prefix) Polish notation in the 1920s.

The reverse Polish scheme was proposed in 1954 by Burks, Warren, and Wright[1] and was independently reinvented by F. L. Bauer and E. W. Dijkstra in the early 1960s to reduce computer memory access and utilize the stack to evaluate expressions. The algorithms and notation for this scheme were extended by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s.[2][3]

During the 1970s and 1980s, RPN was known to many calculator users, as it was used in some handheld calculators of the time designed for advanced users: for example, the HP-10C series and Sinclair Scientific calculators.

In computer science, postfix notation is often used in stack-based and concatenative programming languages. It is also common in dataflow and pipeline-based systems, including Unix pipelines.

Most of what follows is about binary operators. A unary operator for which the reverse Polish notation is the general convention is the factorial.

Contents

Explanation

In reverse Polish notation the operators follow their operands; for instance, to add 3 and 4, 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 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 *".

Despite the name, reverse Polish notation is not exactly the reverse of Polish notation, for the operands of non-commutative operations are still written in the conventional order (e.g. "/ 6 3" in Polish notation and "6 3 /" in reverse Polish both evaluating to 2, whereas "3 6 /" in reverse Polish notation would evaluate to ½).

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, most efficiently from the center outwards.
  • The automatic stack permits the automatic storage of intermediate results for use later: this key feature is what permitted RPN calculators to evaluate relatively complicated expressions.
  • Brackets are unnecessary: the user enters 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.
  • No equals key is required to force computation to occur.
  • RPN calculators do, however, require an enter key to separate two adjacent numbers.
  • Users must know the size of the stack, because practical implementations of RPN use different sizes for the stack. For example, the algebraic expression 1-1.001^{(-6.2 - 2^{3 \pi})}, if performed with a stack size of 4 and executed from left to right, might exhaust the stack.
  • In the unlikely event of writing RPN on paper, adjacent numbers need a separator between them. Using a space requires clear handwriting to prevent confusion. For example, 12 34 + could look like 123 4 +, while something like 12, 34 + is straightforward.

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 (operator here includes both operators, and functions).
      • 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 has 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):

InputOperationStackComment
5Push value5 
1Push value1
5
 
2Push value2
1
5
 
+Add3
5
Pop two values (1, 2) and push result (3)
4Push value4
3
5
 
*Multiply12
5
Pop two values (3, 4) and push result (12)
+Add17Pop two values (5, 12) and push result (17)
3Push value3
17
 
Subtract14Pop two values (17, 3) and push result (14)
 Result(14) 

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:[4]

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.

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 parsers can be modified to produce postfix expressions; in particular, once an abstract syntax tree has been constructed, the corresponding postfix expression is given by a simple post-order traversal of that tree.

Implementations

History of 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 by "Kopi" (likely Irving Copi, who was at the University of Michigan at the time) on symbolic logic[5][6] and before he was aware of Hamblin's work.

Hewlett-Packard

Friden introduced RPN to the desktop calculator market with the EC-130 in June 1963. Hewlett-Packard 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. The HP-35, the world's first handheld scientific calculator, used RPN in 1972. HP used RPN on every handheld calculator it sold, whether scientific, financial, or programmable, until it introduced the HP-10 adding machine calculator in 1977. By this time HP was the leading manufacturer of calculators for professionals, including engineers and accountants.

HP introduced an LCD-based line of calculators in the early 1980s that used RPN, such as the HP-10C, HP-11C, HP-15C, HP-16C, and the famous financial calculator, the HP-12C. When Hewlett-Packard introduced a later business calculator, the HP-19B, without RPN, feedback from financiers and others used to the 12C compelled them to release the HP-19BII[citation needed], which gave users the option of using algebraic notation or RPN. From 1990 to 2003 HP manufactured the HP-48 series of graphing RPN calculators and in 2006 introduced the HP-50g with a 131x80 LCD and a 75 MHz ARM CPU that emulates the Saturn CPU of the HP-48 series.

As of 2011, Hewlett-Packard is producing the calculator models 12C, 12C Platinum, 17BII, 20B (financial), 30B (business), 33S, 35S, 48GII and 50G (scientific) which support RPN.[7]

Prinztronic

Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores retail chain, which was later rebranded as Currys Digital stores, and became part of DSG International. A variety of calculator models was sold in the 1970s under the Prinztronic brand, all made for them by other companies.

Among these was the PROGRAM Programmable Scientific Calculator which featured RPN.

Soviet Union

Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21[8] models) used RPN for both automatic mode and programming. Modern Russian calculators MK-161[9] and MK-152,[10] designed and manufactured in Novosibirsk since 2007 and offered by Semico, are backward compatible with them. Their extended architecture is also based on reverse Polish notation.

Current implementations

Existing implementations using reverse Polish notation include:

See also

References

  1. ^ "An Analysis of a Logical Machine Using Parenthesis-Free Notation," by Arthur W. Burks, Don W. Warren and Jesse B. Wright, 1954
  2. ^ "Charles L. Hamblin and his work" by Peter McBurney
  3. ^ "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."
  4. ^ http://h20219.www2.hp.com/Hpsub/downl oads/17b2pChain.pdf
  5. ^ [1] A New Approach to the Design of a Digital Computer (1961)
  6. ^ [2] The Burroughs B5000 Conference (1985) p. 49
  7. ^ HP Calculators
  8. ^ Elektronika B3-21 page on RSkey.org
  9. ^ Elektronika MK-161 page on RSkey.org
  10. ^ MK-152: Old Russian Motive in a New Space Age.
  • Łukasiewicz, Jan (1957). Aristotle’s Syllogistic from the Standpoint of Modern Formal Logic. Oxford University Press.  Reprinted by Garland Publishing in 1987. ISBN 0-8240-6924-2

External links

(Prev) Reverse indexReview of United States Human ... (Next)