Shunting yard algo
-
Upload
toufiq-akbar -
Category
Technology
-
view
1.201 -
download
2
Transcript of Shunting yard algo
![Page 1: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/1.jpg)
1
ALGORITHM ANALYSIS
Course Code : CSE-305L Course Teacher: Safaet Hossain Presentation on Algorithm Analysis
Topic: Shunting-yard algorithm
Presented By: MD. Toufiq Akbar Shawon ( ID: 011103004 ) Al-Fariha Arpa (ID: 011103011 )
![Page 2: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/2.jpg)
2
SHUNTING-YARD ALGORITHM
In computer science, the shunting-yard algorithm is a method for parsing mathematical expressions specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST). The algorithm was invented by Edger Dijkstra and named the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard. Dijkstra first described the Shunting Yard Algorithm in Mathematics Centrum report.
Like the evaluation of RPN, the shunting yard algorithm is stack-based. Infix expressions are the form of mathematical notation most people are used to, for instance 3+4 or 3+4*(2−1). For the conversion there are two text variables (strings), the input and the output. There is also a stack that holds operators not yet added to the output queue. To convert, the program reads each symbol in order and does something based on that symbol.
The shunting-yard algorithm has been later generalized into operator-precedence parsing.
![Page 3: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/3.jpg)
3
GRAPHICAL REPRESENTATION
![Page 4: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/4.jpg)
4
EXPLANATION OF THE GRAPHICAL REPRESENTATION 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 direct to the output b), d), f), h). If the symbol is an operator it is pushed onto the operator stack c), e), however, if its precedence is less than that of the operator at the top of the stack or the precedence's are equal and the operator is left associative then that operator is popped off the stack and added to the output g). Finally remaining operators are popped off the stack and added to the output.
![Page 5: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/5.jpg)
5
A SIMPLE EXAMPLE
Input: 3+4 Add 3 to the output queue (whenever a number is
read it is added to the output) Push + (or its ID) onto the operator stack Add 4 to the output queue After reading the expression pop the operators off
the stack and add them to the output. In this case there is only one, "+". Output 3 4 +
This already shows a couple of rules: All numbers are added to the output when they
are read. At the end of reading the expression, pop all
operators off the stack and onto the output.
![Page 6: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/6.jpg)
6
THE ALGORITHM For all the input tokens :
Read the next token ; If token is an operator (x) :
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-associativeand its precedence is less than (y) :
Pop (y) from the stack ; Add (y) output buffer ;
Push (x) on the stack ; Else If token is left parenthesis, then push it on the stack ; Else If token is a right parenthesis :
Until the top token (from the stack) is left parenthesis, pop from the stack to the output buffer ;
Also pop the left parenthesis but don’t include it in the output buffer ;
Else add token to output buffer . While there are still operator tokens in the stack, pop them to output
Exit
![Page 7: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/7.jpg)
7
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.
RUNNING TIME COMPLEXITY
![Page 8: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/8.jpg)
8
APPLICATIONS
The Shunting-yard algorithm is Used to parsing mathematical expressions specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST).
It’s also Used in Game-Programming (mainly in the Graphics Section).
Used In calculating huge amount of numbers with efficiency.
![Page 9: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/9.jpg)
9
2 + (3 * (8 - 4)) = ?
How to evaluate this (or similar) Algorithm?
![Page 10: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/10.jpg)
10
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 11: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/11.jpg)
11
2 + (3 * (8 - 4)) = ?
The first car is a number, it goes straight through.
2 + ( 3 * ( 8 - 4 ) )
![Page 12: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/12.jpg)
12
2 + (3 * (8 - 4)) = ?
Next, the third track (the stack) is empty, we move the operator there.
2 + ( 3 * ( 8 - 4 ) )
![Page 13: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/13.jpg)
13
2 + (3 * (8 - 4)) = ?
Left parenthesis goes always down.
2
+
( 3 * ( 8 - 4 ) )
![Page 14: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/14.jpg)
14
2 + (3 * (8 - 4)) = ?
Again, there is a number. It moves always straight to the left.
2
+
(
3 * ( 8 - 4 ) )
![Page 15: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/15.jpg)
15
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 16: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/16.jpg)
16
2 + (3 * (8 - 4)) = ?
Again a left parenthesis, they go always to the stack.
2
+
(
3
*
( 8 - 4 ) )
![Page 17: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/17.jpg)
17
2 + (3 * (8 - 4)) = ?
A number, straight to the left.
2
+
(
3
*
(
8 - 4 ) )
![Page 18: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/18.jpg)
18
2 + (3 * (8 - 4)) = ?
A number, straight to the left.
2
+
(
3
*
(
8 - 4 ) )
![Page 19: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/19.jpg)
19
2 + (3 * (8 - 4)) = ?
An operator, move it down.
2
+
(
3
*
(
8 - 4 ) )
![Page 20: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/20.jpg)
20
2 + (3 * (8 - 4)) = ?
A number, to the left, as always.
2
+
(
3
*
(
8
-
4 ) )
![Page 21: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/21.jpg)
21
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 22: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/22.jpg)
22
2 + (3 * (8 - 4)) = ?
The pair of the parenthesis just disappear.
2
+
(
3
*
(
8 -4 ) )
![Page 23: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/23.jpg)
23
2 + (3 * (8 - 4)) = ?
Again, we pop out the items until there is a left parenthesis.
2
+
(
3
*
8 -4 )
![Page 24: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/24.jpg)
24
2 + (3 * (8 - 4)) = ?
A pair of parenthesis disappear.
2
+
(
3 *8 -4 )
![Page 25: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/25.jpg)
25
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 26: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/26.jpg)
26
2 + (3 * (8 - 4)) = ?
Now the transformation is done, how to evaluate it?
2 +3 *8 -4
![Page 27: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/27.jpg)
27
2 + (3 * (8 - 4)) = ?
Move the cars back to the right side.
2 +3 *8 -4
![Page 28: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/28.jpg)
28
2 + (3 * (8 - 4)) = ?
Move the cars back to the right side.
2 +3 *8 -4
![Page 29: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/29.jpg)
29
2 + (3 * (8 - 4)) = ?
Move the numbers to the down until we find an operator.
2 +3 *8 -4
![Page 30: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/30.jpg)
30
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 31: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/31.jpg)
31
2 + (3 * (8 - 4)) = ?
Do the calculation and put the result back to down.
2
+
3
*4-8
![Page 32: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/32.jpg)
32
2 + (3 * (8 - 4)) = ?
Do the calculation and put the result back to down.
2
+
3
*4
![Page 33: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/33.jpg)
33
2 + (3 * (8 - 4)) = ?
Again, operator to the middle, between the two upmost numbers.
2
+
3
*
4
![Page 34: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/34.jpg)
34
2 + (3 * (8 - 4)) = ?
Calculate the expression and put the result back to the down.
2
+3 * 4
![Page 35: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/35.jpg)
35
2 + (3 * (8 - 4)) = ?
Calculate the expression and put the result back to the down.
2
+12
![Page 36: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/36.jpg)
36
2 + (3 * (8 - 4)) = ?
And the last operator, it is handled in the same way.
2
+
12
![Page 37: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/37.jpg)
37
2 + (3 * (8 - 4)) = ?
Calculate the result and that’s it!
2 + 12
![Page 38: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/38.jpg)
38
2 + (3 * (8 - 4)) = 14
Calculate the result and that’s it!
14
![Page 39: Shunting yard algo](https://reader036.fdocuments.us/reader036/viewer/2022062300/555ecf06d8b42af67f8b5697/html5/thumbnails/39.jpg)
39
END
Thank You For listening to our presentation. For any queries ask now or Forever regret!