# Shunting yard algorithm

The shunting yard algorithm is mainly used for parsing mathematical expressions specified in infix notation. It produces the postfix notation. That means this algorithm is used to convert the infix notation to RPN. The postfix notation is also known as the reverse polish notation (RPN).

Edsger Dijkstra developed this algorithm. The algorithm was named "shunting yard" because its activity resembles a railroad shunting yard.

It is a stack-based algorithm. This algorithm was later generalized to operator-precedence parsing. The input of this algorithm is divided into two parts: the output queue and the operator stack, as shown in the examples below.

The form of infix notation expressions is "7 + 5" or 7 + 5 * (8 - 5) and the form of the reverse polish notation expressions is "7 5 +" or "7 5 8 5 - * +".

### Precedence Table

Symbol Precedence Value
^ High 3
/ Less than ^ 2
* Equal of / 2
+ Low 1
- Equal of + 1

### Example 1: Simple conversion of the infix notation to postfix notation "5 + 8".

Step 1: Input "5 + 8"

Step 2: Push 5 to the output queue

Step 3: Push " + " to the operator stack, because + is a operator.

Step 4: Push 8 to the output queue

Step 5: After reading the input expression, the output queue and operator stack pop the expression and then add them to the output.

Step 6: Output "5 8 +"

### Example 2: Convert infix notation to reverser polish notation.

Input: A + B - C * D + (E ^ F) * G / H + I

Symbols Action Stack Output Note
A "A" add token to output A
+ Push token to stack + A
B "B" add token to output + A B
- Pop stack to output A B + + and - have same precedence
Now, - push token to stack - A B +
C "C" add token to output - A B + C
* Push token to stack -* A B + C * has higher precedence than -
D "D" add token to output -* A B + C D
+ Pop stack to output - A B + C D * + has lesser precedence than *
Pop stack to output A B + C D * - + and - have same precedence
Now, + push token to stack + A B + C D * -
( Push token to stack +( A B + C D * -
E "E" add token to output +( A B + C D * - E
^ Push token to stack +(^ A B + C D * - E
F "F" add token to output +(^ A B + C D * - E F
) Pop stack to output + A B + C D * - E F ^ Discard bracket
* Push token to stack +* A B + C D * - E F ^ * has higher precedence than +
G "G" add token to output +* A B + C D * - E F ^ G
/ Pop stack to output + A B + C D * - E F ^ G * / and * have same precedence
Now, push token to stack +/ A B + C D * - E F ^ G *
H "H" add token to output +/ A B + C D * - E F ^ G * H
+ Pop stack to output + A B + C D * - E F ^ G * H / + has lesser precedence than /
Pop stack to output A B + C D * - E F ^ G * H / + Same precedence
Now, Push token to stack + A B + C D * - E F ^ G * H / +
I "I" add token to output + A B + C D * - E F ^ G * H / + I
End Pop whole stack A B + C D * - E F ^ G * H / + I +

Output: A B + C D * - E F ^ G * H / + I +

### Example 3: Convert infix notation to reverser polish notation.

Input: 4 + 2 / (9 - 8) ^ 4 ^ 2

Symbols Action Stack Output Note
4 "4" add token to output 4
+ Push token to stack + 4
2 "2" add token to output + 4 2
/ Push token to stack + / 4 2 / has higher precedence than +
( Push token to stack + / ( 4 2
9 "9" add token to output + / ( 4 2 9
- Push token to stack + / ( - 4 2 9
8 "8" add token to output + / ( - 4 2 9 8
) Pop stack to output + / 4 2 9 8 - Discard bracket
^ Push token to stack + / ^ 4 2 9 8 - ^ has higher precedence than /
4 "4" add token to output + / ^ 4 2 9 8 - 4
^ Push token to stack + / ^ ^ 4 2 9 8 - 4 ^ is evaluated right to left
2 "2" add token to output + / ^ ^ 4 2 9 8 - 4 2
End Pop whole stack 4 2 9 8 - 4 2 ^ ^ / +

Output: 4 2 9 8 - 4 2 ^ ^ / +

### Implementation of shunting yard algorithm in Java

Next TopicJava Destructor

### Feedback   