You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Shunting Yard/README.markdown
+27-28Lines changed: 27 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,24 @@
1
1
# Shunting Yard Algorithm
2
2
3
-
Any mathematical expression that we write is expressed in a notation known as Infix Notation.
3
+
Any mathematical expression that we write is expressed in a notation known as *infix notation*.
4
4
5
5
For example:
6
6
7
7
**A + B * C**
8
8
9
-
In the above expression the operator is placed between operands hence the expression is said to be in Infix form.
10
-
If you think about it any expression that you write on a piece of paper will always be in infix form. This is what we humans understand.
9
+
In the above expression the operator is placed between operands, hence the expression is said to be in *infix* form. If you think about it, any expression that you write on a piece of paper will always be in infix form. This is what we humans understand.
11
10
12
-
Now, think about the way the above expression is evaluated, you would first multiply B and C then add the result to A. This is because multiplication has
13
-
higher precedence than addition. We humans can easily understand the precedence of operators, but a machine needs to be given instructions about each operator. If you were to
14
-
write an algorithm that parsed and evaluated the infix notation you will realize that it's a tedious process. You'd have to parse the expression
15
-
multiple times to know what operation to perform first. As the number of operators increase so does the complexity.
11
+
When the above expression is evaluated, you would first multiply **B** and **C**, and then add the result to **A**. This is because multiplication has higher precedence than addition. We humans can easily understand the precedence of operators, but a machine needs to be given instructions about each operator.
16
12
17
-
## Postfix notations / Reverse Polish Notation
13
+
If you were to write an algorithm that parsed and evaluated the infix notation you will realize that it's a tedious process. You'd have to parse the expression multiple times to know what operation to perform first. As the number of operators increase so does the complexity.
18
14
19
-
An expression when represented in postfix form will not have any brackets and neither will you have to worry about scanning for operator precedence. This makes it easy for the computer to evaluate
20
-
expressions since the order in which the operator need to be applied is fixed.
15
+
## Postfix notations / Reverse Polish Notation
21
16
22
-
For example:
17
+
In postfix notation, also known as Reverse Polish Notation or RPN, the operators come after the corresponding operands. Here is the postfix representation of the example from the previous section:
23
18
24
19
**A B C * +**
25
20
26
-
The above is the postfix representation of the example in the previous section. The operators come after the corresponding operands.
21
+
An expression when represented in postfix form will not have any brackets and neither will you have to worry about scanning for operator precedence. This makes it easy for the computer to evaluate expressions, since the order in which the operators need to be applied is fixed.
27
22
28
23
### Evaluating a postfix expression
29
24
@@ -33,12 +28,11 @@ A stack is used to evaluate a postfix expression. Here is the pseudocode:
33
28
2. If the token is an operand, push it into the stack
34
29
3. If the token is a binary operator,
35
30
1. Pop the two top most operands from the stack
36
-
2. Apply the binary operator with thetwo operands
31
+
2. Apply the binary operator to the two operands
37
32
3. Push the result into the stack
38
-
4. Finally, the value of the whole postfix
39
-
expression remains in the stack
33
+
4. Finally, the value of the whole postfix expression remains in the stack
40
34
41
-
Using the above psuedocode the evaluation on the stack would be as follows:
35
+
Using the above pseudocode, the evaluation on the stack would be as follows:
42
36
43
37
| Expression | Stack |
44
38
| ------------- | --------|
@@ -51,34 +45,36 @@ Using the above psuedocode the evaluation on the stack would be as follows:
51
45
52
46
Where **D = B * C** and **E = A + D.**
53
47
54
-
As seen above a postfix operation is relatively easy to evaluate as the order in which the operators need to be applied is predecided.
48
+
As seen above, a postfix operation is relatively easy to evaluate as the order in which the operators need to be applied is pre-decided.
55
49
56
-
## Shunting yard algorithm
50
+
## Dijkstra's shunting yard algorithm
57
51
58
-
The Shunting yard algorithm was invented by Edsger Dijkstra to convert an infix expression to postfix. Many calculators use this algorithm to convert the expression being entered to postfix form.
52
+
The shunting yard algorithm was invented by Edsger Dijkstra to convert an infix expression to postfix. Many calculators use this algorithm to convert the expression being entered to postfix form.
59
53
60
54
Here is the psedocode of the algorithm:
61
55
62
-
1. For all the input tokens
56
+
1. For all the input tokens:
63
57
1. Read the next token
64
58
2. If token is an operator (x)
65
59
1. While there is an operator (y) at the top of the operators stack and either (x) is left-associative and its precedence is less or equal to that of (y), or (x) is right-associative and its precedence is less than (y)
66
60
1. Pop (y) from the stack
67
61
2. Add (y) output buffer
68
62
2. Push (x) on the stack
69
-
3. Else If token is left parenthesis, then push it on the stack
70
-
4. Else If token is a right parenthesis
63
+
3. Else if token is left parenthesis, then push it on the stack
64
+
4. Else if token is a right parenthesis
71
65
1. Until the top token (from the stack) is left parenthesis, pop from the stack to the output buffer
72
66
2. Also pop the left parenthesis but don’t include it in the output buffer
73
67
7. Else add token to output buffer
74
68
2. While there are still operator tokens in the stack, pop them to output
75
69
76
70
### How does it work
77
71
78
-
Let's take a small example and see how the psuedocode works.
72
+
Let's take a small example and see how the pseudocode works.
79
73
80
74
**4 + 4 * 2 / ( 1 - 5 )**
81
75
76
+
The following table describes the precedence and the associativity for each operator. The same values are used in the algorithm.
77
+
82
78
| Operator | Precedence | Associativity |
83
79
| ---------| -------------| ----------------|
84
80
| ^ | 10 | Right |
@@ -87,25 +83,28 @@ Let's take a small example and see how the psuedocode works.
87
83
| + | 0 | Left |
88
84
| - | 0 | Left |
89
85
90
-
The above table describes the precedence and the associativity for each operator. The same values are used in the algorithm.
0 commit comments