Shunting-yard algorithm

The shunting yard algorithm ( German: , Rangierbahnhof' algorithm) is a method to convert mathematical expression of the infix notation in the reversed Polish notation or in an abstract syntax tree. The algorithm was invented by Edsger W. Dijkstra and with " shunting yard ": named because he remembers in his way of working to a marshalling yard ( German " marshalling yard ").

Operation

The shunting yard algorithm requires both an input and an output variable, and a stack that is required for the intermediate storage of the operators for the conversion. The input string is read character by character, where all numbers are written directly and in the same order in the output variable. If the character is a pending operation signs, it is placed on the stack of operators. If an operator is already on the stack, it is decided on the basis of operator precedence and the Operatorassoziativität whether the new operator is put directly on the stack or if the stack is emptied first in the output.

Opening parentheses are also placed on the operator stack, but they are not written in the removal into the output stream. In closing parentheses, the stack is emptied until encountering an opening parenthesis; should be no opening parenthesis is found, the input string is incomplete, although the error handling is not defined by the algorithm.

In detail

It follows German pseudo - code that can be used in most programming languages ​​simply translated and adapted.

  • Create Stack with LIFO and output queue.
  • UNTIL tokens are available:
  • Token read.
  • WHEN IS token number:
  • TO token output.
  • END IF
  • IF token IS function:
  • TO token stack.
  • END IF
  • WHEN IS token argument delimiter:
  • UP stack tip IS opening bracket:
  • Stack - peak output TO.
  • ERROR WHEN stack IS - EMPTY:
  • BASIC ( 1) A misplaced argument delimiter.
  • BASIC ( 2) The closing brace is not open advance.
  • END ERROR
  • ENDEBIS
  • END IF
  • IF token IS operator
  • WHILE stack IS NOT BLANK AND stack - tip IS AND operator
  • Stack - peak output TO.
  • END OLANGE
  • TO token stack.
  • END IF
  • IF token IS opening bracket:
  • TO token stack.
  • END IF
  • IF token IS closing brace:
  • UP stack tip IS opening bracket:
  • ERROR WHEN stack IS - EMPTY:
  • BASIC ( 1) The closing brace is not open advance.
  • END ERROR
  • Stack - peak output TO.
  • ENDEBIS
  • Remove stack - tip (opening brace )
  • IF stack - tip IS function:
  • Stack - peak output TO.
  • END IF
  • END IF
  • END OLANGE
  • UP stack IS NOT BLANK:
  • ERROR WHEN IS stack tip opening bracket:
  • BASIC ( 1) There are more open than closing parentheses.
  • END ERROR
  • Stack - peak output TO.
  • ENDEBIS

This algorithm assumes that all tokens are correctly recognized and valid. In particular the superposition of the characters " " and "-", this algorithm does not take over. A conflict of right - and left-associative operators of equal precedence is not caught.

The " read " access on the stack (eg "stack - tip IS " ) and the " participating " (for "stack - tip TO " ) is required.

Required functions are:

  • Recognizing numbers
  • Detecting Functions
  • Detecting argument separator
  • Recognition of Operators
  • Determine the Operatorassoziativität
  • Determining the operator precedence ( here means higher precedence a stronger bond ), the precedence of a function is a maximum.

Examples

The following given in infix notation invoices should be reshaped. It is documented that happens.

Precedents: ( , - ) <( x, / ) <( ^ ) < Features

(3 4 ) ( 5 - 6 )

Here, since each token is unique, is the indication of where it is located, unnecessary.

1 2 - 3.4 5 ^ 6 ^ 7.8 - 9

Explicitly clamped ( [( 1 2) - ( 3.4) ] {[ 5 ^ (6 ^ 7)] · 8} ) - 9 it may be easier to understand.

Cos ( 1 sin ( ln ( 5) - exp (8)) ^ 2)

728292
de