Shunting Yard Algorithm
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, the shunting yard algorithm is a method for parsing arithmetical or logical expressions, or a combination of both, specified in
infix notation Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—" infixed operators"—such as the plus sign in . Usage Binary relations a ...
. It can produce either a postfix notation string, also known as
Reverse Polish notation Reverse Polish notation (RPN), also known as reverse Łukasiewicz notation, Polish postfix notation or simply postfix notation, is a mathematical notation in which operators ''follow'' their operands, in contrast to Polish notation (PN), in whi ...
(RPN), or an
abstract syntax tree In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurring ...
(AST). The
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
was invented by
Edsger Dijkstra Edsger Wybe Dijkstra ( ; ; 11 May 1930 – 6 August 2002) was a Dutch computer scientist, programmer, software engineer, systems scientist, and science essayist. He received the 1972 Turing Award for fundamental contributions to developing progra ...
and named the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard. Dijkstra first described the shunting yard algorithm in the
Mathematisch Centrum The (abbr. CWI; English: "National Research Institute for Mathematics and Computer Science") is a research centre in the field of mathematics and theoretical computer science. It is part of the institutes organization of the Dutch Research Cou ...
repor
MR 34/61
Like the evaluation of RPN, the shunting yard algorithm is
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
-based. Infix expressions are the form of mathematical notation most people are used to, for instance or . For the conversion there are two text variables (
strings String or strings may refer to: *String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects Arts, entertainment, and media Films * ''Strings'' (1991 film), a Canadian anim ...
), the input and the output. There is also a
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
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 Reverse Polish notation (RPN), also known as reverse Łukasiewicz notation, Polish postfix notation or simply postfix notation, is a mathematical notation in which operators ''follow'' their operands, in contrast to Polish notation (PN), in whi ...
) and , respectively. The shunting yard algorithm will correctly parse all valid infix expressions, but does not reject all invalid expressions. For example, is not a valid infix expression, but would be parsed as . The algorithm can however reject expressions with mismatched parentheses. The shunting yard algorithm was later generalized into operator-precedence parsing.


A simple conversion

#Input: #Push 3 to the output
queue __NOTOC__ Queue () may refer to: * Queue area, or queue, a line or area where people wait for goods or services Arts, entertainment, and media *''ACM Queue'', a computer magazine * The Queue (Sorokin novel), ''The Queue'' (Sorokin novel), a 198 ...
(whenever a number is read it is pushed to the output) #
Push Push may refer to: Music * Mike Dierickx (born 1973), a Belgian producer also known as Push Albums * ''Push'' (Bros album), 1988 * ''Push'' (Gruntruck album), 1992 * ''Push'' (Jacky Terrasson album), 2010 Songs * "Push" (Enrique Iglesias s ...
+ (or its ID) onto the operator
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
#Push 4 to the output queue #After reading the expression, pop the operators off the stack and add them to the output. #:In this case there is only one, "+". #Output: This already shows a couple of rules: * All numbers are pushed to the output when they are read. * At the end of reading the expression, pop all operators off the stack and onto the output.


Graphical illustration

Graphical illustration of algorithm, using a three-way railroad junction. The input is processed one symbol at a time: if a variable or number is found, it is copied directly to the output a), c), e), h). If the symbol is an operator, it is pushed onto the operator stack b), d), f). If the operator's precedence is lower than that of the operators at the top of the stack or the precedences are equal and the operator is left associative, then that operator is popped off the stack and added to the output g). Finally, any remaining operators are popped off the stack and added to the output i).


The algorithm in detail

while there are tokens to be read: read a token if the token is: - a ''number'': put it into the output queue - a ''
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
'': push it onto the operator stack - an ''operator'' ''o''1: while ( there is an operator ''o''2 at the top of the operator stack which is not a left parenthesis, and (''o''2 has greater precedence than ''o''1 or (''o''1 and ''o''2 have the same precedence and ''o''1 is left-associative)) ): pop ''o''2 from the operator stack into the output queue push ''o''1 onto the operator stack - a ''left parenthesis'' (i.e. "("): push it onto the operator stack - a ''right parenthesis'' (i.e. ")"): while the operator at the top of the operator stack is not a left parenthesis: pop the operator from the operator stack into the output queue pop the left parenthesis from the operator stack and discard it if there is a function token at the top of the operator stack, then: pop the function from the operator stack into the output queue while there are tokens on the operator stack: pop the operator from the operator stack onto the output queue To analyze the running time complexity of this algorithm, one has only to note that each token will be read once, each number, function, or operator will be printed once, and each function, operator, or parenthesis will be pushed onto the stack and popped off the stack once—therefore, there are at most a constant number of operations executed per token, and the running time is thus O(''n'') — linear in the size of the input. The shunting yard algorithm can also be applied to produce prefix notation (also known as
Polish notation Polish notation (PN), also known as normal Polish notation (NPN), Łukasiewicz notation, Warsaw notation, Polish prefix notation or simply prefix notation, is a mathematical notation in which operators ''precede'' their operands, in contrast t ...
). To do this one would simply start from the end of a string of tokens to be parsed and work backwards, reverse the output queue (therefore making the output queue an output stack), and flip the left and right parenthesis behavior (remembering that the now-left parenthesis behavior should pop until it finds a now-right parenthesis). And changing the
associativity In mathematics, the associative property is a property of some binary operations, which means that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a valid rule of replacement f ...
condition to right.


Detailed examples

Input: : The symbol ^ represents the power operator. : Input: :


See also

*
Operator-precedence parser In computer science, an operator precedence parser is a bottom-up parser that interprets an operator-precedence grammar. For example, most calculators use operator precedence parsers to convert from the human-readable infix notation relying on ord ...
*
Stack-sortable permutation In mathematics and computer science, a stack-sortable permutation (also called a tree permutation) is a permutation whose elements may be sorted by an algorithm whose internal storage is limited to a single stack data structure. The stack-sortab ...


References


External links


Dijkstra's original description of the Shunting yard algorithmLiterate Programs implementation in CDemonstration of Shunting yard algorithm in RustJava Applet demonstrating the Shunting yard algorithmSilverlight widget demonstrating the Shunting yard algorithm and evaluation of arithmetic expressions
Theodore Norvell © 1999–2001. Access date September 14, 2006.
Matlab code, evaluation of arithmetic expressions using the shunting yard algorithm
{{Parsers Parsing algorithms Dutch inventions