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
+17-13Lines changed: 17 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,14 @@ Any mathematical expression that we write is expressed in a notation known as In
4
4
5
5
For example:
6
6
7
-
A + B * C
7
+
**A + B * C**
8
8
9
9
In the above expression the operator is placed between operands hence the expression is said to be in Infix form.
10
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.
11
11
12
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
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 than parsed and evaluated the infix notation you'll soon realize that it's a tedious process. You'd have to parse the expression
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
15
multiple times to know what operation to perform first. As the number of operators increase so does the complexity.
16
16
17
17
## Postfix notations / Reverse Polish Notation
@@ -29,16 +29,13 @@ The above is the postfix representation of the example in the previous section.
29
29
30
30
A stack is used to evaluate a postfix expression. Here is the pseudocode:
31
31
32
-
1. read postfix expression token by token
33
-
2. if the token is an operand, push it
34
-
into the stack
35
-
3. if the token is a binary operator,
36
-
3.1 pop the two top most operands
37
-
from the stack
38
-
3.2 apply the binary operator with the
39
-
two operands
40
-
3.3 push the result into the stack
41
-
4. finally, the value of the whole postfix
32
+
1. Read postfix expression token by token
33
+
2. If the token is an operand, push it into the stack
34
+
3. If the token is a binary operator,
35
+
1. Pop the two top most operands from the stack
36
+
2. Apply the binary operator with thetwo operands
37
+
3. Push the result into the stack
38
+
4. Finally, the value of the whole postfix
42
39
expression remains in the stack
43
40
44
41
Using the above psuedocode the evaluation on the stack would be as follows:
@@ -105,4 +102,11 @@ The above table describes the precedence and the associativity for each operator
0 commit comments