| Reverse Polish Notation |
Article Index for Reverse |
Website Links For Reverse |
Information AboutReverse Polish Notation |
| CATEGORIES ABOUT REVERSE POLISH NOTATION | |
| calculators | |
| rpn | |
| mathematical notation | |
|
Reverse Polish notation was invented by , to enable zero-address memory stores. Hamblin presented his work at a conference in June 1957, and published it in 1957 and 1962. Most of what follows is about Binary Operator s. A Unary Operator for which the Reverse Polish notation is the general convention is the Factorial . EXPLANATION In Reverse Polish notation the Operator s 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 +) −". Interpreters of Reverse Polish 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. PRACTICAL IMPLICATIONS
DISADVANTAGES
THE POSTFIX ALGORITHM The algorithm for evaluating any postfix expression is fairly straightforward:
Example
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): 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 . There are other ways of producing postfix expressions from infix notation. Most Operator-precedence Parser s 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 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 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. Existing implementations using Reverse Polish notation include:
A POSTFIX EVALUATOR IMPLEMENTED IN VISUAL BASIC 6 File: postfix.bas Dim Stack As New ByteStack Public Sub Main() Dim Answer As Double
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
Stack.POP VarPtr(C), 8
Case "/", "\" Stack.POP VarPtr(C), 8 A = C / A Case "^" Stack.POP VarPtr(C), 8 A = C ^ A Case Else A = Val(Expn(i)) Stack.PUSH VarPtr(A), 8 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
EXTERNAL LINKS
|
|
|