FACTOID # 46: Almost the entire Cook Islands are covered by forest.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

Encyclopedia > Reverse Polish notation
Prefix notation
Infix notation
Postfix notation

Postfix notation is a mathematical notation wherein every operator follows all of its operands. It is also known as 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. Image File history File links Postfix-dia. ... Polish notation, also known as prefix notation was created by Jan Łukasiewicz. ... Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on (e. ... It has been suggested that this article or section be merged with Reverse Polish notation. ... Year 1920 (MCMXX) was a leap year starting on Thursday. ... // Jan Łukasiewicz (21 December 1878 - 13 February 1956) was a Polish mathematician born in Lemberg, Galicia, Austria-Hungary (now Lviv, Ukraine). ...


Postfix notation was invented by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s, to enable zero-address memory stores. Hamblin presented his work at a conference in June 1957, and published it in 1957 and 1962. // Recovering from World War I and its aftermath, the economic miracle emerged in West Germany and Italy. ...


Most of what follows is about binary operators. A unary operator for which the postfix notation is the general convention is the factorial. In mathematics, a binary operation, or binary operator, is a calculation involving two input quantities and one kind of a specific operation. ... In mathematics, a unary operation is an operation with only one operand. ... For factorial rings in mathematics, see unique factorisation domain. ...

Contents

Explanation

In postfix 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)", and only the parentheses disambiguate the two meanings. In postfix, the latter would be written "3 4 5 + −", which unambiguously means "3 (4 5 +) −". In mathematics, an operator is a function that performs some sort of operation on a number, variable, or function. ... In mathematics, an operand is one of the inputs of an operator. ...


Interpreters of postfix notation are 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. Simple representation of a stack In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). ...


Practical implications

  • Reading from left to right, calculations can occur as soon as an operator is read; processing can always begin before the entire expression is read in.
  • Brackets and parentheses are unnecessary.
  • 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 state is always just a stack of values awaiting operation; it is never the case that an operator has already been entered and is still awaiting an operand.

Disadvantages

  • The widespread use of standard ordered equations (infix) in educational systems (and therefore infix electronic calculators being the norm in classrooms) can make RPN impractical, hard, and hindering at times. (However, it is easy for a computer to convert infix notation to postfix, most notably Dijkstra's Shunting yard algorithm)
  • Adjacent numbers have to have a space between them, which requires precise handwriting to prevent confusion (for instance, 12 34 + could look a lot like 123 4 +)
  • Most RPN electronic calculators have programmable functions and multiple memory registers. In formal examinations (such as licensing examinations) calculators with such extended functions are often banned, but using a simple infix calculator is allowed.

Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on (e. ... Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on (e. ... The Shunting yard algorithm is a method for parsing mathematical equations specified in infix notation. ...

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 a function. (Operators, like +, are simply functions taking two arguments.)
      • It is known that the function takes n arguments.
      • So, pop the top n values from the stack.
        • If there are fewer than n values on the stack
          • (Error) The user has not input sufficient values in the expression.
      • Evaluate the function, 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.

3 + 2 = 5 with apples, a popular choice in textbooks[1] Addition is the mathematical operation of combining or adding two numbers to obtain an equal simple amount or total. ...

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):

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.


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. The Shunting yard algorithm is a method for parsing mathematical equations specified in infix notation. ... Edsger Dijkstra Edsger Wybe Dijkstra (Rotterdam, May 11, 1930 – Nuenen, August 6, 2002; IPA: ) was a Dutch computer scientist. ... Chicago and North Western Railways Proviso Yard in Chicago, Illinois, December 1942. ...


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. An operator precedence parser is a computer program that interprets an operator-precedence grammar. ... In computer science, an abstract syntax tree (AST) is a finite, labeled, directed tree, where the internal nodes are labeled by operators, and the leaf nodes represent the operands of the node operators. ... In computer science, tree traversal is the process of visiting each node in a tree data structure. ...


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, R. 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. The Burroughs large systems were the largest of three series of Burroughs Corporation mainframe computers. ...


Friden introduced RPN to the desktop calculator market with the EC-130 in June of 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 handheld scientific calculator brought RPN to the first scientific pocket calculator in 1972. The HP-10C series of calculators, including the famous financial calculator the HP-12C, all used RPN. When Hewlett-Packard introduced a later business calculator, the HP-19B, without RPN, feedback from financiers and others used to using the 12-C compelled them to release the HP-19BII, which gave users the option of using algebraic notation or RPN. Friden Calculating Machine Company (Friden, Inc. ... The Hewlett-Packard Company (NYSE: HPQ), commonly known as HP, is a very large, global company headquartered in Palo Alto, California, United States. ... An HP-35 calculator The HP-35 was Hewlett-Packards first pocket calculator and the worlds first scientific pocket calculator (a calculator with trigonometric and exponential functions). ... A calculator is a device for performing calculations. ... 1972 (MCMLXXII) was a leap year starting on Saturday. ... The HP-10C is the last and lowest featured calculator in this line even though its number would indicate its lower and earlier origin. ...


Existing implementations using postfix notation include:

Calculator is a basic calculator application made by Apple Computer and bundled with Mac OS X. It has three modes: basic, scientific, and programmer. ... Forth is a programming language and programming environment, initially developed by Charles H. Moore at the US National Radio Astronomy Observatory in the early 1970s. ... Factor is a concatenative programming language designed and implemented by Slava Pestov. ... The Hewlett-Packard Company (NYSE: HPQ), commonly known as HP, is a very large, global company headquartered in Palo Alto, California, United States. ... A calculator is a device for performing calculations. ... PostScript (PS) is a page description language and programming language used primarily in the electronic and desktop publishing areas. ... The TI-89 and the TI-89 Titanium are graphing calculators developed by Texas Instruments. ... Filiation of Unix and Unix-like systems Unix (officially trademarked as UNIX®) is a computer operating system originally developed in the 1960s and 1970s by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie and Douglas McIlroy. ... From the GNU Project dc man page: Dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but... An interpreter is a computer program that executes other programs. ... JavaScript is the name of Netscape Communications Corporations and now the Mozilla Foundations implementation of the ECMAScript standard, a scripting language based on the concept of prototype-based programming. ... Ada is a structured, statically typed imperative computer programming language designed by a team led by Jean Ichbiah of CII Honeywell Bull during 1977–1983. ... Emacs is a class of text editors, possessing an extensive set of features, that are popular with computer programmers and other technically proficient computer users. ... GTK+, or the GIMP Toolkit, is one of the two most popular widget toolkits for the X Window System for creating graphical user interfaces. ...

A Postfix Evaluator Implemented In Visual Basic 6

File: postfix.bas

 Dim Stack As New ByteStack 
 Public Sub Main() Dim Answer As Double Answer = EvaluatePostfix("3 2 + 5 *") MsgBox "Answer is " & CStr(Answer), vbInformation, "Reverse Polish Notation (Postfix) Evaluator" End End Sub 
 Public Function EvaluatePostfix(Expression As String) As Double Dim i As Integer Dim A As Double Dim C As Double Dim Expn() As String Stack.Initialize 128 Expression = Replace(Expression, " ", " ") Expn() = Split(Expression, " ") For i = 0 To UBound(Expn()) Select Case Expn(i) Case "+" Stack.POP VarPtr(C), 8 A = C + A Case "-" Stack.POP VarPtr(C), 8 A = C - A Case "*" Stack.POP VarPtr(C), 8 A = C * A Case "/", "" Stack.POP VarPtr(C), 8 A = C / A Case "^" Stack.POP VarPtr(C), 8 A = C ^ A Case Else Stack.PUSH VarPtr(A), 8 A = Val(Expn(i)) End Select Next i EvaluatePostfix = A End Function 

File: ByteStack.cls

 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, Source As Any, ByVal Length As Long) Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, ByVal Source As Long, ByVal Length As Long) Dim Stack() As Byte Dim TOS As Long 
 Public Sub Initialize(Bytes As Long) ReDim Stack(Bytes - 1) TOS = 0 End Sub 
 Public Sub PUSH(ptrDATA As Long, Length As Long) If (Length + TOS - 1) > UBound(Stack()) Then ReDim Preserve Stack(UBound(Stack()) + Length) End If RtlMoveMemory Stack(TOS), ptrDATA, Length TOS = TOS + Length End Sub 
 Public Function POP(ptrDATA As Long, Length As Long) As Boolean If TOS - Length > -1 Then TOS = TOS - Length CopyMemory ptrDATA, Stack(TOS), Length POP = True End If End Function 

See also

Forth is a programming language and programming environment, initially developed by Charles H. Moore at the US National Radio Astronomy Observatory in the early 1970s. ... HP Calculators refer to various calculators manufactured by the Hewlett-Packard company over the years. ... In a stack, the topmost item, which is added last, is taken out first. ... In computer science, a stack machine is a model of computation in which the computers memory takes the form of a stack. ... In linguistic typology, Subject Object Verb (SOV) is the type of languages in which the subject, object, and verb of a sentence appear (usually) in that order. ... Object Subject Verb (OSV) is one of the permutations of expression used in Linguistic typology. ... Polish notation, also known as prefix notation was created by Jan Łukasiewicz. ... The Joy programming language is a purely functional programming language that was produced by Manfred von Thun of Latrobe University in Melbourne, Australia. ...

External links


  Results from FactBites:
 
PlanetMath: reverse Polish notation (327 words)
Invented by Australian philosopher Charles Hamblin, reverse Polish notation requires that the number of operands of a given operator be defined in advance.
Reverse Polish notation is particularly advantageous for stack-based programming languages like Forth and Adobe PostScript.
This is version 3 of reverse Polish notation, born on 2006-08-14, modified 2006-11-10.
Reverse Polish Notation (1173 words)
Polish notation was described in the 1920s by Polish mathematician Jan Lukasiewicz as a logical system for the specification of mathematical equations without parentheses.
Equation with parenthesis (1 + 2) * 3 Prefix notation * 3 + 1 2 or * + 1 2 3 Postfix notation 1 2 + 3 * or 3 1 2 + *
The basic reverse Polish calculator algorithm is to key in a number.
  More results at FactBites »

 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your location
Your comments
Please enter the 5-letter protection code


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.