An open API service indexing awesome lists of open source software.

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

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, +, ^, *]
```