(computer science) The version of Polish notation, used in some calculators, in which operators follow the operators with which they are associated. Abbreviated RPN. Also known as postfix notation; suffix notation.
| Sci-Tech Dictionary: reverse Polish notation |
(computer science) The version of Polish notation, used in some calculators, in which operators follow the operators with which they are associated. Abbreviated RPN. Also known as postfix notation; suffix notation.
| 5min Related Video: Reverse Polish notation |
| Computer Desktop Encyclopedia: reverse Polish notation |
| WordNet: reverse Polish notation |
The noun has one meaning:
Meaning #1:
a parenthesis-free notation for forming mathematical expressions in which each operator follows its operands
Synonyms: postfix notation, suffix notation
| Wikipedia: Reverse Polish notation |
| Prefix notation |
| Infix notation |
| Postfix notation |
Reverse Polish notation (or just RPN) by analogy with the related Polish notation, a prefix notation introduced in 1920 by the Polish mathematician Jan Łukasiewicz, is a mathematical notation wherein every operator follows all of its operands. It is also known as Postfix notation and is parenthesis-free as long as operator arities are fixed.
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 notation and algorithms 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 had some currency even among the general public, as it was widely used in desktop calculators of the time – 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 |
In Reverse Polish notation the operators follow their operands; 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". In postfix, the former would be written "3 4 5 * −", which unambiguously means "3 (4 5 *) −" which of course reduces to "3 20 -".
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 Polish notation corresponds to "6 3 /" in reverse Polish, both evaluating to 2, whereas "3 6 /" would evaluate to 0.5).
|
|
This section is in a list format that may be better presented using prose. You can help by converting this section to prose, if appropriate. Editing help is available. (May 2009) |
, if performed with a stack size of 4 and executed from left to right, would exhaust the stack. The answer might be given as an erroneous imaginary number instead of approximately 0.5 as a real number.The algorithm for evaluating any postfix expression is fairly straightforward:
The infix expression "5 + ((1 + 2) * 4) − 3" can be written down like this in RPN:
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):
| Input | Operation | Stack | Comment |
|---|---|---|---|
| 5 | Push operand | 5 | |
| 1 | Push operand | 5, 1 | |
| 2 | Push operand | 5, 1, 2 | |
| + | Add | 5, 3 | Pop two values (1, 2) and push result (3) |
| 4 | Push operand | 5, 3, 4 | |
| * | Multiply | 5, 12 | Pop two values (3, 4) and push result (12) |
| + | Add | 17 | Pop two values (5, 12) and push result (17) |
| 3 | Push operand | 17, 3 | |
| − | Subtract | 14 | Pop two values (17, 3) and push 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.
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.
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. 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 an adding machine-style calculator, the HP-10A. 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 12-C compelled them to release the HP-19BII, 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.
Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21[5] models) used RPN for both automatic mode and programming. Modern Russian calculators MK-161[6] and MK-152[7], designed and manufactured in Novosibirsk since 2007, are backward compatible with them. Their extended architecture is also based on Reverse Polish notation.
Existing implementations using Reverse Polish notation include:
import operator OPERATORS = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv, '^': operator.pow, '%': operator.mod, } def evaluate_rpn(expression, stack): try: for token in expression.split(): try: result = float(token) except ValueError: result = OPERATORS[token](stack.pop(-2), stack.pop()) stack.append(result) except KeyError: print "Invalid input", repr(token) except IndexError: print "The stack is too small to apply the operator", repr(token) if __name__ == "__main__": print "RPN.py - Press Control-D to exit." stack = [] try: while True: evaluate_rpn(raw_input('%s> ' % stack), stack) except (KeyboardInterrupt, EOFError): print
|
This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)
| Best of the Web: Reverse Polish notation |
Some good "Reverse Polish notation" pages on the web:
Math mathworld.wolfram.com |
| Shopping: Reverse Polish notation |
| infix notation (technology) | |
| Polish Notation (business term) | |
| Polish notation (technology) |
| How can you explain Polish notation in simple words? Read answer... | |
| Convert from infix to reverse Polish A plus B plus C plus D plus E? Read answer... | |
| What does polish do? Read answer... |
| What are three reverse Polish notation formulas that cannot be converted to infix? | |
| Use of stack in evaluating an expression in Polish notation? | |
| Reverse polish to infix of ab plus c plus d? |
Copyrights:
![]() | Sci-Tech Dictionary. McGraw-Hill Dictionary of Scientific and Technical Terms. Copyright © 2003, 1994, 1989, 1984, 1978, 1976, 1974 by McGraw-Hill Companies, Inc. All rights reserved. Read more | |
![]() | Computer Desktop Encyclopedia. THIS COPYRIGHTED DEFINITION IS FOR PERSONAL USE ONLY. All other reproduction is strictly prohibited without permission from the publisher. © 1981-2009 Computer Language Company Inc. All rights reserved. Read more | |
![]() | WordNet. WordNet 1.7.1 Copyright © 2001 by Princeton University. All rights reserved. Read more | |
![]() | Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Reverse Polish notation". Read more |
Mentioned in