https://github.com/nomemory/blog-java-shunting-yard
An academic implenentation for the Shunting Yard Algorithm in Java
https://github.com/nomemory/blog-java-shunting-yard
Last synced: 25 days ago
JSON representation
An academic implenentation for the Shunting Yard Algorithm in Java
- Host: GitHub
- URL: https://github.com/nomemory/blog-java-shunting-yard
- Owner: nomemory
- License: mit
- Created: 2020-12-29T20:38:36.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-02T20:48:37.000Z (over 5 years ago)
- Last Synced: 2025-04-04T15:17:20.726Z (over 1 year ago)
- Language: Java
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
`"Academic"` implementation for the [Shunting Yard Algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm?oldformat=true) in Java.
Usage:
```java
import java.util.Arrays;
import java.util.List;
import static net.andreinc.shunting.yard.ShuntingYard.shuntingYard;
import static org.assertj.core.api.Assertions.assertThat;
// *****
List given = Arrays.asList("( 1 + 2 ) * ( 3 / 4 ) ^ ( 5 + 6 )".split(" "));
List expected = List.of("1", "2", "+", "3", "4", "/", "5", "6", "+", "^", "*");
List computed = shuntingYard(given);
System.out.println("infix:" + given);
System.out.println("rpn (expected):" + expected);
System.out.println("rpn (computed):" + computed);
assertThat(computed).isEqualTo(expected);
```
Output:
```
infix:[(, 1, +, 2, ), *, (, 3, /, 4, ), ^, (, 5, +, 6, )]
rpn (expected):[1, 2, +, 3, 4, /, 5, 6, +, ^, *]
rpn (computed):[1, 2, +, 3, 4, /, 5, 6, +, ^, *]
```