blob: 932e2245697f071f5ef8a09c4a338f380bebfd8c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package bjc.utils.examples.parsing;
import java.util.Scanner;
import java.util.function.Predicate;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IFunctionalList;
import bjc.utils.parserutils.AST;
import bjc.utils.parserutils.ShuntingYard;
import bjc.utils.parserutils.TreeConstructor;
/**
* Test of tree constructor
*
* @author ben
*
*/
public class TreeConstructTest {
/**
* Main method
*
* @param args
* Unused CLI args
*/
public static void main(String[] args) {
Scanner inputSource = new Scanner(System.in);
System.out.print("Enter a expression to parse: ");
String line = inputSource.nextLine();
ShuntingYard<String> yard = new ShuntingYard<>();
IFunctionalList<String> shuntedTokens =
yard.postfix(new FunctionalStringTokenizer(line)
.toList((strang) -> strang), (s) -> s);
System.out.println("Shunted: " + shuntedTokens.toString());
AST<String> constructedTree = TreeConstructor
.constructTree(shuntedTokens, new Predicate<String>() {
@Override
public boolean test(String token) {
switch (token) {
case "+":
case "-":
case "*":
case "/":
return true;
default:
return false;
}
}
}, (operator) -> false, null);
System.out.println("AST: " + constructedTree.toString());
inputSource.close();
}
}
|