Shunting Yard Algorithm - Intro and Reverse Polish Notation

Creel · 33K views · 2013 · 18:41

Open on YouTube

Algorithms & data structuresCompilers, interpreters & program analysistutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about evaluating infix expressions, building calculators, or handling operator precedence and nested parentheses.

Recommended by 3 people across 3 different months, 2013 to 2015.

What it is

A tutorial by Creel introducing Dijkstra's Shunting Yard algorithm and Reverse Polish notation as a way to parse mathematical expressions. It explains the train shunting yard analogy and leads into a follow-up C# implementation.

What people said

Recommends Reshurum · 2013-09 · Stack Overflow · on “Java calculator that takes in expressions”

I think what you are looking for is Reverse Polish Notation which allows you to evaluate expressions. There is a video here that explains how it works: [this video]

Recommends Paul Vargas · 2014-03 · Stack Overflow · on “How to read text between multi-level parenthetical expressions”

You can do that with the Shunting-yard algorithm [LINK] . If you only validate an expression, you do not need to implement all the algorithm. Your expression is valid when you have all the required operators and operands. In YouTube, you can see Shunting Yard Algorithm - Intro and Reverse Polish Notation [this video] .

Recommends Richard · 2015-04 · Stack Overflow · on “Calculator method”

With regards to your question I think the best approach would be to write an algorithm to convert the input that you get from the user (Infix Notation) to something like Reverse Polish notation using Dijkstra's Shunting Yard Algorithm. This will allow you to take operator precedence into account. Here is a video explaining it, it has a follow up video in C++ I think, however converting that to Java will be trivial. [this video] Here is the Wiki page: [LINK]