Shunting-yard algorithm Infix to postfix conversion Based on .
-
Upload
camryn-towery -
Category
Documents
-
view
245 -
download
3
Transcript of Shunting-yard algorithm Infix to postfix conversion Based on .
![Page 1: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/1.jpg)
Shunting-yard algorithm
Infix to postfix conversion
Based on http://en.wikipedia.org/wiki/Shunting_yard_algorithm
![Page 2: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/2.jpg)
2 + (3 * (8 - 4)) = ?
TODO:• rules should be visible and highlighted when a rule is applied• an example containing operator precedence rules
How to evaluate this (or similar) formula?
![Page 3: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/3.jpg)
2 + (3 * (8 - 4)) = ?
Let’s play that the tokens are train cars and we are shunting the shunting yard.
2 + ( 3 * ( 8 - 4 ) )
![Page 4: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/4.jpg)
2 + (3 * (8 - 4)) = ?
The first car is a number, it goes straight through.
2 + ( 3 * ( 8 - 4 ) )
![Page 5: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/5.jpg)
2 + (3 * (8 - 4)) = ?
Next, the third track (the stack) is empty, we move the operator there.
2 + ( 3 * ( 8 - 4 ) )
![Page 6: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/6.jpg)
2 + (3 * (8 - 4)) = ?
Left parenthesis goes always down.
2
+
( 3 * ( 8 - 4 ) )
![Page 7: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/7.jpg)
2 + (3 * (8 - 4)) = ?
Again, there is a number. It moves always straight to the left.
2
+
(
3 * ( 8 - 4 ) )
![Page 8: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/8.jpg)
2 + (3 * (8 - 4)) = ?
Next there is an operator, it goes down because the topmost car there is an parenthesis.
2
+
(
3 * ( 8 - 4 ) )
![Page 9: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/9.jpg)
2 + (3 * (8 - 4)) = ?
Again a left parenthesis, they go always to the stack.
2
+
(
3
*
( 8 - 4 ) )
![Page 10: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/10.jpg)
2 + (3 * (8 - 4)) = ?
A number, straight to the left.
2
+
(
3
*
(
8 - 4 ) )
![Page 11: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/11.jpg)
2 + (3 * (8 - 4)) = ?
A number, straight to the left.
2
+
(
3
*
(
8 - 4 ) )
![Page 12: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/12.jpg)
2 + (3 * (8 - 4)) = ?
An operator, move it down.
2
+
(
3
*
(
8 - 4 ) )
![Page 13: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/13.jpg)
2 + (3 * (8 - 4)) = ?
A number, to the left, as always.
2
+
(
3
*
(
8
-
4 ) )
![Page 14: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/14.jpg)
2 + (3 * (8 - 4)) = ?
A right parenthesis. Now we move the cars from the bottom until there is left parenthesis.
2
+
(
3
*
(
8
-
4 ) )
![Page 15: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/15.jpg)
2 + (3 * (8 - 4)) = ?
The pair of the parenthesis just disappear.
2
+
(
3
*
(
8 -4 ) )
![Page 16: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/16.jpg)
2 + (3 * (8 - 4)) = ?
Again, we pop out the items until there is a left parenthesis.
2
+
(
3
*
8 -4 )
![Page 17: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/17.jpg)
2 + (3 * (8 - 4)) = ?
A pair of parenthesis disappear.
2
+
(
3 *8 -4 )
![Page 18: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/18.jpg)
2 + (3 * (8 - 4)) = ?
No more cars on the right side, so we move the cars from the bottom to the left.
2
+
3 *8 -4
![Page 19: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/19.jpg)
2 + (3 * (8 - 4)) = ?
Now the transformation is done, how to evaluate it?
2 +3 *8 -4
![Page 20: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/20.jpg)
2 + (3 * (8 - 4)) = ?
Move the cars back to the right side.
2 +3 *8 -4
![Page 21: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/21.jpg)
2 + (3 * (8 - 4)) = ?
Move the cars back to the right side.
2 +3 *8 -4
![Page 22: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/22.jpg)
2 + (3 * (8 - 4)) = ?
Move the numbers to the down until we find an operator.
2 +3 *8 -4
![Page 23: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/23.jpg)
2 + (3 * (8 - 4)) = ?
When operator is found, place it to the middle so that it is between two numbers.
2
+
3
*
8
-
4
![Page 24: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/24.jpg)
2 + (3 * (8 - 4)) = ?
Do the calculation and put the result back to down.
2
+
3
*4-8
![Page 25: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/25.jpg)
2 + (3 * (8 - 4)) = ?
Do the calculation and put the result back to down.
2
+
3
*4
![Page 26: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/26.jpg)
2 + (3 * (8 - 4)) = ?
Again, operator to the middle, between the two upmost numbers.
2
+
3
*
4
![Page 27: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/27.jpg)
2 + (3 * (8 - 4)) = ?
Calculate the expression and put the result back to the down.
2
+3 * 4
![Page 28: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/28.jpg)
2 + (3 * (8 - 4)) = ?
Calculate the expression and put the result back to the down.
2
+12
![Page 29: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/29.jpg)
2 + (3 * (8 - 4)) = ?
And the last operator, it is handled in the same way.
2
+
12
![Page 30: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/30.jpg)
2 + (3 * (8 - 4)) = ?
Calculate the result and that’s it!
2 + 12
![Page 31: Shunting-yard algorithm Infix to postfix conversion Based on .](https://reader035.fdocuments.in/reader035/viewer/2022081420/56649c8f5503460f94948470/html5/thumbnails/31.jpg)
2 + (3 * (8 - 4)) = 14
Calculate the result and that’s it!
14