Reverse Polish notation
'''Reverse Polish notation (RPN)''' , also known as postfix notation, is an arithmetic formula notation, derived from the Polish notation introduced in 1920 by the Polish mathematician Jan Łukasiewicz. RPN was invented by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s, to enable zero-address memory stores. As a user interface for calculation the notation was first used in Hewlett-Packard's desktop calculators from the late 1960s and then in the HP-35 handheld scientific calculator launched in 1972. In RPN the operands precede the operator, thus dispensing with the need for parentheses. For example, the expression 3
- ( 4 + 7) would be written as 3 4 7 + *, and done on an RPN calculator as "3", "Enter", "4", "Enter", "7", "+", "*". (Alternatively, and more-compactly, it could also be re-ordered and written as 4 7 + 3 *, and done on an RPN calculator as "4", "Enter", "7", "+", "3", "*".)
Practical implications
- Calculations proceed from left to right
- There are no brackets or parentheses, as they are unnecessary.
- Operands precede operator. They are removed as the operation is evaluated.
- When an operation is made, the result becomes an operand itself (for later operators)
- There is no hidden state. No need to wonder if you hit an operator or not.
Example
The calculation: ((1 + 2)- 4) + 3 can be written down like this in RPN:
- 3 +
| Input | Stack | Operation |
|---|---|---|
| 1 | 1 | Push operand |
| 2 | 1, 2 | Push operand |
| + | 3 | Addition |
| 4 | 3, 4 | Push operand |
|
| 12 | Multiplication |
| 3 | 12, 3 | Push operand |
| + | 15 | Addition |
Converting from infix notation
Like the evaluation of RPN, conversion from infix notation to RPN is stack-based. Infix expressions are the form of math most people are used to, for instance 3+4 or 3+4- (2-1). For the conversion there are 2 text variables (strings), the input and the output. There is also a stack holding operators not yet added to the output stack. To convert, the program reads each letter in order and does something based on that letter.
A simple conversion
Input: 3+4 #Add 3 to the output queue (whenever a number is read it is added to the output) #Add 4 to the output queue #Push + (or its ID) onto the operator stack #After reading expression pop the operators off the stack and add them to the output. # In this case there is only one, "+". #Output 3 4 + This already shows a couple of rules:- All numbers are added to the output when they are read.
- At the end of reading the expression, pop all operators off the stack and onto the output.
The algorithm in detail
- While there are tokens to be read:
- If the token is a number, then add it to the output queue.
- If the token is an operator, o1, then:
:::: pop o2 off the stack, onto the output queue; :::2) push o1 onto the operator stack. ::
- If the token is a left parenthesis, then push it onto the stack.
- If the token is a right parenthesis, then pop operators off the stack, onto the output queue, until the token at the top of the stack is a left parenthesis, at which point it is popped off the stack but not added to the output queue. If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
- When there are no more tokens to read, pop all the tokens, if any, off the stack, add each to the output as it is popped out and exit. (These must only be operators; if a left parenthesis is popped, then there are mismatched parentheses.)
Complex example
Input 3+4- 2/(1-5)^2
- "
- " onto the stack
- " off stack and add it to output, push "/" onto the stack
- 1
- 1
- 1 5
- 1 5 -
- 1 5 -
- 1 5 - 2
- 1 5 - 2 ^ / +
Real-world RPN use
- Forth programming language
- Hewlett-Packard science/engineering calculators
- PostScript page description language
- TI-68k (TI-89) implementation
- Unix system calculator program dc
- Writing an Interpreter
- Interactive JavaScript calculator with RPN
- Linux IpTables "Rope" programming language
See also
External links
- RPN or DAL? A brief analysis of Reverse Polish Notation against Direct Algebraic Logic – By James Redin
- Postfix Notation Mini-Lecture – By Bob Brown
everse Polish notation
Rverse Polish notation
Reerse Polish notation
Revrse Polish notation
Revese Polish notation
Revere Polish notation
Revers Polish notation
ReversePolish notation
Reverse olish notation
Reverse Plish notation
Reverse Poish notation
Reverse Polsh notation
Reverse Polih notation
Reverse Polis notation
Reverse Polishnotation
Reverse Polish otation
Reverse Polish ntation
Reverse Polish noation
Reverse Polish nottion
Reverse Polish notaion
Reverse Polish notaton
Reverse Polish notatin
Reverse Polish notatio
eRverse Polish notation
Rveerse Polish notation
Reevrse Polish notation
Revrese Polish notation
Revesre Polish notation
Reveres Polish notation
Revers ePolish notation
ReverseP olish notation
Reverse oPlish notation
Reverse Ploish notation
Reverse Poilsh notation
Reverse Polsih notation
Reverse Polihs notation
Reverse Polis hnotation
Reverse Polishn otation
Reverse Polish ontation
Reverse Polish ntoation
Reverse Polish noattion
Reverse Polish nottaion
Reverse Polish notaiton
Reverse Polish notatoin
Reverse Polish notatino
Reverse Polish notatio
RReverse Polish notation
Reeverse Polish notation
Revverse Polish notation
Reveerse Polish notation
Reverrse Polish notation
Reversse Polish notation
Reversee Polish notation
Reverse Polish notation
Reverse PPolish notation
Reverse Poolish notation
Reverse Pollish notation
Reverse Poliish notation
Reverse Polissh notation
Reverse Polishh notation
Reverse Polish notation
Reverse Polish nnotation
Reverse Polish nootation
Reverse Polish nottation
Reverse Polish notaation
Reverse Polish notattion
Reverse Polish notatiion
Reverse Polish notatioon
Reverse Polish notationn
everse polish notation
rverse polish notation
reerse polish notation
revrse polish notation
revese polish notation
revere polish notation
revers polish notation
reversepolish notation
reverse olish notation
reverse plish notation
reverse poish notation
reverse polsh notation
reverse polih notation
reverse polis notation
reverse polishnotation
reverse polish otation
reverse polish ntation
reverse polish noation
reverse polish nottion
reverse polish notaion
reverse polish notaton
reverse polish notatin
reverse polish notatio
erverse polish notation
rveerse polish notation
reevrse polish notation
revrese polish notation
revesre polish notation
reveres polish notation
revers epolish notation
reversep olish notation
reverse oplish notation
reverse ploish notation
reverse poilsh notation
reverse polsih notation
reverse polihs notation
reverse polis hnotation
reverse polishn otation
reverse polish ontation
reverse polish ntoation
reverse polish noattion
reverse polish nottaion
reverse polish notaiton
reverse polish notatoin
reverse polish notatino
reverse polish notatio
rreverse polish notation
reeverse polish notation
revverse polish notation
reveerse polish notation
reverrse polish notation
reversse polish notation
reversee polish notation
reverse polish notation
reverse ppolish notation
reverse poolish notation
reverse pollish notation
reverse poliish notation
reverse polissh notation
reverse polishh notation
reverse polish notation
reverse polish nnotation
reverse polish nootation
reverse polish nottation
reverse polish notaation
reverse polish notattion
reverse polish notatiion
reverse polish notatioon
reverse polish notationn