SlideShare a Scribd company logo
1 of 31
Shunting-yard algorithm

   Infix to postfix conversion




                                 Based on http://en.wikipedia.org/wiki/Shunting_yard_algorithm
2 + (3 * (8 - 4)) = ?




            How to evaluate this (or similar) formula?




                                         TODO:
                                         • rules should be visible and highlighted when a rule is applied
                                         • an example containing operator precedence rules
2 + (3 * (8 - 4)) = ?

Let’s play that the tokens are train cars and we are shunting the shunting yard.

                                                          2   +   (   3   *   (   8   -   4   )   )
2 + (3 * (8 - 4)) = ?

The first car is a number, it goes straight through.

                                                       2   +   (   3   *   (   8   -   4   )   )
2 + (3 * (8 - 4)) = ?

Next, the third track (the stack) is empty, we move the operator there.

 2                                                           +   (   3   *   (   8   -   4   )   )
2 + (3 * (8 - 4)) = ?

Left parenthesis goes always down.

 2                                       (   3   *   (   8   -   4   )   )




                                     +
2 + (3 * (8 - 4)) = ?

Again, there is a number. It moves always straight to the left.

 2                                                                3   *   (   8   -   4   )   )




                                             (

                                             +
2 + (3 * (8 - 4)) = ?

Next there is an operator, it goes down because the topmost car there is an parenthesis.

 2   3                                                               *   (   8   -   4   )   )




                                            (

                                            +
2 + (3 * (8 - 4)) = ?

Again a left parenthesis, they go always to the stack.

 2   3                                                   (   8   -   4   )   )




                                             *

                                             (

                                             +
2 + (3 * (8 - 4)) = ?

A number, straight to the left.

 2   3                                8   -   4   )   )




                                  (

                                  *

                                  (

                                  +
2 + (3 * (8 - 4)) = ?

A number, straight to the left.

 2   3   8                            -   4   )   )




                                  (

                                  *

                                  (

                                  +
2 + (3 * (8 - 4)) = ?

An operator, move it down.

 2   3   8                       -   4   )   )




                             (

                             *

                             (

                             +
2 + (3 * (8 - 4)) = ?

A number, to the left, as always.

 2   3   8                              4   )   )




                                    -

                                    (

                                    *

                                    (

                                    +
2 + (3 * (8 - 4)) = ?

A right parenthesis. Now we move the cars from the bottom until there is left
parenthesis.
 2   3   8   4                                                                  )   )




                                           -

                                           (

                                           *

                                           (

                                           +
2 + (3 * (8 - 4)) = ?

The pair of the parenthesis just disappear.

 2   3   8   4   -                                )   )




                                              (

                                              *

                                              (

                                              +
2 + (3 * (8 - 4)) = ?

Again, we pop out the items until there is a left parenthesis.

 2   3   8   4   -                                               )




                                             *

                                             (

                                             +
2 + (3 * (8 - 4)) = ?

A pair of parenthesis disappear.

 2   3   8   4   -   *                 )




                                   (

                                   +
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   -   *




                                            +
2 + (3 * (8 - 4)) = ?

Now the transformation is done, how to evaluate it?

 2   3   8   4   -   *   +
2 + (3 * (8 - 4)) = ?

Move the cars back to the right side.

 2   3   8   4   -   *   +
2 + (3 * (8 - 4)) = ?

Move the cars back to the right side.

                                        2   3   8   4   -   *   +
2 + (3 * (8 - 4)) = ?

Move the numbers to the down until we find an operator.

                                                          2   3   8   4   -   *   +
2 + (3 * (8 - 4)) = ?

When operator is found, place it to the middle so that it is between two numbers.

                                                                                    -   *   +




                                           4

                                           8

                                           3

                                           2
2 + (3 * (8 - 4)) = ?

Do the calculation and put the result back to down.

                                     8   -   4        *   +




                                             3

                                             2
2 + (3 * (8 - 4)) = ?

Do the calculation and put the result back to down.

                                        4             *   +




                                            3

                                            2
2 + (3 * (8 - 4)) = ?

Again, operator to the middle, between the two upmost numbers.

                                                                 *   +




                                         4

                                         3

                                         2
2 + (3 * (8 - 4)) = ?

Calculate the expression and put the result back to the down.

                                       3   *   4                +




                                               2
2 + (3 * (8 - 4)) = ?

Calculate the expression and put the result back to the down.

                                         12                     +




                                              2
2 + (3 * (8 - 4)) = ?

And the last operator, it is handled in the same way.

                                                        +




                                             12

                                            2
2 + (3 * (8 - 4)) = ?

Calculate the result and that’s it!

                                      2   +   12
2 + (3 * (8 - 4)) = 14

Calculate the result and that’s it!

                                      14

More Related Content

What's hot

PPt on Functions
PPt on FunctionsPPt on Functions
PPt on Functionscoolhanddav
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1Philip Schwarz
 
Different types of functions
Different types of functionsDifferent types of functions
Different types of functionsKatrina Young
 
3 2 representing functions
3 2 representing functions3 2 representing functions
3 2 representing functionslothomas
 
How to graph Functions
How to graph FunctionsHow to graph Functions
How to graph Functionscoolhanddav
 
Module 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation NotesModule 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation Notestoni dimella
 
Relations & Functions
Relations & FunctionsRelations & Functions
Relations & FunctionsBitsy Griffin
 
Logarithms
LogarithmsLogarithms
Logarithmssupoteta
 
To determine if a relation is a function
To determine if a relation is a functionTo determine if a relation is a function
To determine if a relation is a functionAnthony Abidakun
 
Matlab practice
Matlab practiceMatlab practice
Matlab practiceZunAib Ali
 
Higher Maths 1.2.1 - Sets and Functions
Higher Maths 1.2.1 - Sets and FunctionsHigher Maths 1.2.1 - Sets and Functions
Higher Maths 1.2.1 - Sets and Functionstimschmitz
 
Linear Systems - Domain & Range
Linear Systems - Domain & RangeLinear Systems - Domain & Range
Linear Systems - Domain & Rangeswartzje
 
02-04 Relations Functions
02-04 Relations Functions02-04 Relations Functions
02-04 Relations FunctionsBitsy Griffin
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)mohsinggg
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
 

What's hot (20)

PPt on Functions
PPt on FunctionsPPt on Functions
PPt on Functions
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Different types of functions
Different types of functionsDifferent types of functions
Different types of functions
 
3 2 representing functions
3 2 representing functions3 2 representing functions
3 2 representing functions
 
How to graph Functions
How to graph FunctionsHow to graph Functions
How to graph Functions
 
Functions
FunctionsFunctions
Functions
 
Programming with matlab session 6
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6
 
Module 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation NotesModule 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation Notes
 
Functions
FunctionsFunctions
Functions
 
Function : Introduction
Function : IntroductionFunction : Introduction
Function : Introduction
 
Relations & Functions
Relations & FunctionsRelations & Functions
Relations & Functions
 
Logarithms
LogarithmsLogarithms
Logarithms
 
To determine if a relation is a function
To determine if a relation is a functionTo determine if a relation is a function
To determine if a relation is a function
 
Matlab practice
Matlab practiceMatlab practice
Matlab practice
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
Higher Maths 1.2.1 - Sets and Functions
Higher Maths 1.2.1 - Sets and FunctionsHigher Maths 1.2.1 - Sets and Functions
Higher Maths 1.2.1 - Sets and Functions
 
Linear Systems - Domain & Range
Linear Systems - Domain & RangeLinear Systems - Domain & Range
Linear Systems - Domain & Range
 
02-04 Relations Functions
02-04 Relations Functions02-04 Relations Functions
02-04 Relations Functions
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
 

Viewers also liked

The software story
The software storyThe software story
The software storygrahamwell
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch developgrahamwell
 
Excel =if function
Excel =if functionExcel =if function
Excel =if functiongrahamwell
 
What is binary and why do we use it?
What is binary and why do we use it?What is binary and why do we use it?
What is binary and why do we use it?grahamwell
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch developgrahamwell
 
Excel Min max-average
Excel Min max-average Excel Min max-average
Excel Min max-average grahamwell
 

Viewers also liked (8)

The software story
The software storyThe software story
The software story
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch develop
 
Excel =if function
Excel =if functionExcel =if function
Excel =if function
 
What is binary and why do we use it?
What is binary and why do we use it?What is binary and why do we use it?
What is binary and why do we use it?
 
Introduction to touch develop
Introduction to touch developIntroduction to touch develop
Introduction to touch develop
 
Excel Min max-average
Excel Min max-average Excel Min max-average
Excel Min max-average
 
Alfredo Cuervo Barrero - Pensamientos y Poesia
Alfredo Cuervo Barrero - Pensamientos y PoesiaAlfredo Cuervo Barrero - Pensamientos y Poesia
Alfredo Cuervo Barrero - Pensamientos y Poesia
 
Pseudocode
PseudocodePseudocode
Pseudocode
 

Similar to Shunting yard

Análise institucional
Análise institucionalAnálise institucional
Análise institucionalÍtalo Nunes
 
15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdfvenkatapranaykumarGa
 
M.c.qs.- 3(functions & their graphs)
M.c.qs.- 3(functions & their graphs)M.c.qs.- 3(functions & their graphs)
M.c.qs.- 3(functions & their graphs)Nadeem Uddin
 
7 2 adding and subtracting polynomials
7 2 adding and subtracting polynomials7 2 adding and subtracting polynomials
7 2 adding and subtracting polynomialshisema01
 
Algebra 1 chapter 2 notes
Algebra 1 chapter 2 notesAlgebra 1 chapter 2 notes
Algebra 1 chapter 2 noteshhennigan
 

Similar to Shunting yard (7)

Análise institucional
Análise institucionalAnálise institucional
Análise institucional
 
15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf
 
M.c.qs.- 3(functions & their graphs)
M.c.qs.- 3(functions & their graphs)M.c.qs.- 3(functions & their graphs)
M.c.qs.- 3(functions & their graphs)
 
Matrix opt
Matrix optMatrix opt
Matrix opt
 
1150 day 5
1150 day 51150 day 5
1150 day 5
 
7 2 adding and subtracting polynomials
7 2 adding and subtracting polynomials7 2 adding and subtracting polynomials
7 2 adding and subtracting polynomials
 
Algebra 1 chapter 2 notes
Algebra 1 chapter 2 notesAlgebra 1 chapter 2 notes
Algebra 1 chapter 2 notes
 

More from grahamwell

Turtle graphics
Turtle graphicsTurtle graphics
Turtle graphicsgrahamwell
 
Database field types
Database field typesDatabase field types
Database field typesgrahamwell
 
Pascal names and types
Pascal names and typesPascal names and types
Pascal names and typesgrahamwell
 
Python part two names and types
Python part two names and typesPython part two names and types
Python part two names and typesgrahamwell
 
Abstraction - Year 9
Abstraction - Year 9Abstraction - Year 9
Abstraction - Year 9grahamwell
 
Thinking about your project
Thinking about your projectThinking about your project
Thinking about your projectgrahamwell
 
The rail fence
The rail fenceThe rail fence
The rail fencegrahamwell
 
Rsa encryption
Rsa encryptionRsa encryption
Rsa encryptiongrahamwell
 
Server side scripts
Server side scriptsServer side scripts
Server side scriptsgrahamwell
 
Revision topic 1 sensors and control
Revision topic 1 sensors and controlRevision topic 1 sensors and control
Revision topic 1 sensors and controlgrahamwell
 
Gm names n stuff
Gm   names n stuffGm   names n stuff
Gm names n stuffgrahamwell
 
Reverse polish notation
Reverse polish notationReverse polish notation
Reverse polish notationgrahamwell
 
Protocol layers
Protocol layersProtocol layers
Protocol layersgrahamwell
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsgrahamwell
 

More from grahamwell (20)

Turtle graphics
Turtle graphicsTurtle graphics
Turtle graphics
 
Database field types
Database field typesDatabase field types
Database field types
 
Databases 101
Databases 101Databases 101
Databases 101
 
Kodu controls
Kodu controlsKodu controls
Kodu controls
 
Pascal names and types
Pascal names and typesPascal names and types
Pascal names and types
 
Python part two names and types
Python part two names and typesPython part two names and types
Python part two names and types
 
Abstraction - Year 9
Abstraction - Year 9Abstraction - Year 9
Abstraction - Year 9
 
Thinking about your project
Thinking about your projectThinking about your project
Thinking about your project
 
The rail fence
The rail fenceThe rail fence
The rail fence
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
Rsa encryption
Rsa encryptionRsa encryption
Rsa encryption
 
Server side scripts
Server side scriptsServer side scripts
Server side scripts
 
Revision topic 1 sensors and control
Revision topic 1 sensors and controlRevision topic 1 sensors and control
Revision topic 1 sensors and control
 
Mtslesson
MtslessonMtslesson
Mtslesson
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Gm names n stuff
Gm   names n stuffGm   names n stuff
Gm names n stuff
 
Reverse polish notation
Reverse polish notationReverse polish notation
Reverse polish notation
 
Protocol layers
Protocol layersProtocol layers
Protocol layers
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

Shunting yard

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