Reverse Polish notation

From Academic Kids

(Redirected from Postfix 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", "*".)

Implementations of RPN are stack-based; that is, operands are popped from a stack, and calculation results are pushed back onto it. Although this concept may seem obscure at first, RPN has the advantage of being extremely easy, and therefore fast, for a computer to analyze.

Contents

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:

1 2 + 4 * 3 +

The expression is evaluated in the following way (the Stack is displayed after Operation has taken place):

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

The final result, 15, lies on the top of the stack at the end of the calculation.

An alternate way of viewing the stack during the above operation is shown below (as seen on HP48S calculator).

+---------------+
|               |
|               |
|             1 |  1 enter
+---------------+

+---------------+
|               |
|             1 |
|             2 |  2 [enter]
+---------------+

+---------------+
|               |
|               |
|             3 |  + 
+---------------+

+---------------+
|               |
|             3 |
|             4 |  4 [enter]
+---------------+

+---------------+
|               |
|               |
|            12 |  * 
+---------------+

+---------------+
|               |
|            12 |
|             3 |  3 [enter]
+---------------+

+---------------+
|               |
|               |
|            15 |  + 
+---------------+

The enters are in brackets because they are optional when followed by an operator press. An enter is only needed to clear the insertion mark from the line. Thus, RPN allows the expression to be entered and evaluated in eight rather than eleven or twelve steps.

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:
Read a token.
  • If the token is a number, then add it to the output queue.
  • If the token is an operator, o1, then:
1) while there is an operator, o2, at the top of the stack, and either
o1 is left-associative and its precedence is less than or equal to that of o2, or
o1 is right-associative and its precedence is less than that of o2,
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
Read "3"
 Add "3" to the output
  Output: 3
Read "+"
 Push "+" onto the stack
  Output: 3
  Stack: +
Read "4"
 Add "4" to the output
  Output: 3 4
  Stack: +
Read "*"
 Push "*" onto the stack
  Output: 3 4
  Stack: + *
Read "2"
 Add "2" to the output
  Output: 3 4 2
  Stack: + *
Read "/"
 Pop "*" off stack and add it to output, push "/" onto the stack
  Output: 3 4 2 *
  Stack: + /
Read "("
 Push "(" onto the stack
  Output: 3 4 2 *
  Stack: + / (
Read "1"
 Add "1" to output
  Output: 3 4 2 * 1
  Stack: + / (
Read "-"
 Push "-" onto the stack
  Output: 3 4 2 * 1
  Stack: + / ( -
Read "5"
 Add "5" to output
  Output: 3 4 2 * 1 5
  Stack: + / ( -
Read ")"
 Pop "-" off stack and add it to the output, pop (
  Output: 3 4 2 * 1 5 -
  Stack: + /
Read "^"
 Push "^" onto stack
  Output: 3 4 2 * 1 5 -
  Stack: + / ^
Read "2"
 Add "2" to output
  Output: 3 4 2 * 1 5 - 2
  Stack: + / ^
End of Expression
 Pop stack to output
  Output: 3 4 2 * 1 5 - 2 ^ / +

If you were writing an interpreter, this output would be tokenized and written to a compiled file to be later interpreted. Conversion from Infix to RPN can also allow for easier computer simplification of expressions. To do this, act like you are solving the RPN expression, however, whenever you come to a variable its value is null, and whenever an operator has a null value, it and its parameters are written to the output (this is a simplification, problems arise when the parameters are operators). When an operator has no null parameters its value can simply be written to the output. This method obviously doesn't include all the simplifications possible.

Real-world RPN use

See also

External links

de:Umgekehrte Polnische Notation es:Notacin polaca inversa fr:Notation polonaise inverse ko:역폴란드 표기법 ja:逆ポーランド記法 pl:Odwrotna notacja polska ru:Обратная польская запись sl:Obrnjen poljski zapis sv:Omvnd polsk notation

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools