This already shows a couple of rules:
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).
For important terms, see token (parser), function (mathematics), Operator associativity, and Order of operations.
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). 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), while making sure to change the associativity condition to right.
Input: 3 + 4 × 2 ÷ ( 1 − 5 ) ^ 2 ^ 3
The symbol ^ represents the power operator.
Input: sin ( max ( 2, 3 ) ÷ 3 × π )
Theodore Norvell (1999). "Parsing Expressions by Recursive Descent". www.engr.mun.ca. Retrieved 2020-12-28. http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm ↩
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. https://ir.cwi.nl/pub/9251 ↩