Shunting yard algorithm

Shunting yard algorithm
ClassParsing
Data structureStack
Worst-case performance
Worst-case space complexity

In computer science, the shunting yard algorithm is a method for parsing arithmetical or logical expressions, or a combination of both, specified in infix notation. It can produce either a postfix notation string, also known as reverse Polish notation (RPN), or an abstract syntax tree (AST).[1] The algorithm was invented by Edsger Dijkstra, first published in November 1961,[2] and named the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard.

Like the evaluation of RPN, the shunting yard algorithm is stack-based. Infix expressions are the form of mathematical notation most people are used to, for instance "3 + 4" or "3 + 4 × (2 − 1)". For the conversion there are two text variables (strings), the input and the output. There is also a stack that holds operators not yet added to the output queue. To convert, the program reads each symbol in order and does something based on that symbol. The result for the above examples would be (in reverse Polish notation) "3 4 +" and "3 4 2 1 − × +", respectively.

The shunting yard algorithm will correctly parse all valid infix expressions, but does not reject all invalid expressions. For example, "1 2 +" is not a valid infix expression, but would be parsed as "1 + 2". The algorithm can however reject expressions with mismatched parentheses.

The shunting yard algorithm was later generalized into operator-precedence parsing.

  1. ^ Theodore Norvell (1999). "Parsing Expressions by Recursive Descent". www.engr.mun.ca. Retrieved 2020-12-28.
  2. ^ Dijkstra, Edsger (1961-11-01). "Algol 60 translation : An Algol 60 translator for the X1 and making a translator for Algol 60". Stichting Mathematisch Centrum.