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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package bjc.utils.examples;
import bjc.utils.data.IPair;
import bjc.utils.data.ITree;
import bjc.utils.data.Pair;
import bjc.utils.data.Tree;
import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
import bjc.utils.funcutils.ListUtils;
import bjc.utils.funcutils.StringUtils;
import bjc.utils.parserutils.ShuntingYard;
import bjc.utils.parserutils.TreeConstructor;
import bjc.utils.parserutils.TreeConstructor.QueueFlattener;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.function.Predicate;
/**
* Test of tree constructor
*
* @author ben
*
*/
public class TreeConstructTest {
private static final class OperatorPicker implements Predicate<String> {
@Override
public boolean test(String token) {
if(StringUtils.containsOnly(token, "\\[")) return true;
else if(StringUtils.containsOnly(token, "\\]")) return true;
switch(token) {
case "+":
case "-":
case "*":
case "/":
return true;
default:
return false;
}
}
}
/**
* Main method
*
* @param args
* Unused CLI args
*/
@SuppressWarnings({ "resource", "deprecation" })
public static void main(String[] args) {
Scanner inputSource = new Scanner(System.in);
System.out.print("Enter a expression to parse: ");
String line = inputSource.nextLine();
IList<String> tokens = new FunctionalStringTokenizer(line).toList();
ShuntingYard<String> yard = new ShuntingYard<>(true);
Deque<IPair<String, String>> ops = new LinkedList<>();
ops.add(new Pair<>("+", "\\+"));
ops.add(new Pair<>("-", "-"));
ops.add(new Pair<>("*", "\\*"));
ops.add(new Pair<>("/", "/"));
ops.add(new Pair<>(":=", ":="));
ops.add(new Pair<>("=>", "=>"));
IList<String> semiExpandedTokens = ListUtils.splitTokens(tokens, ops);
ops = new LinkedList<>();
ops.add(new Pair<>("(", "\\("));
ops.add(new Pair<>(")", "\\)"));
ops.add(new Pair<>("[", "\\["));
ops.add(new Pair<>("]", "\\]"));
IList<String> fullyExpandedTokens = ListUtils.deAffixTokens(semiExpandedTokens, ops);
fullyExpandedTokens.removeIf((strang) -> strang.equals(""));
IList<String> shuntedTokens = yard.postfix(fullyExpandedTokens, (token) -> token);
System.out.println("Shunted: " + shuntedTokens.toString());
Predicate<String> specialPicker = (operator) -> {
if(StringUtils.containsOnly(operator, "\\[")) return true;
else if(StringUtils.containsOnly(operator, "\\]")) return true;
return false;
};
IMap<String, QueueFlattener<String>> operators
= new FunctionalMap<>();
operators.put("[", (queuedTrees) -> {
return null;
});
operators.put("[", (queuedTrees) -> {
Tree<String> openTree = new Tree<>("[");
queuedTrees.push(openTree);
return openTree;
});
operators.put("]", (queuedTrees) -> {
ITree<String> arrayTree = new Tree<>("[]");
while(!queuedTrees.peek().getHead().equals("[")) {
arrayTree.addChild(queuedTrees.pop());
}
queuedTrees.push(arrayTree);
return arrayTree;
});
ITree<String> constructedTree = TreeConstructor.constructTree(shuntedTokens,
new OperatorPicker(), specialPicker, operators::get);
System.out.println("AST: " + constructedTree.toString());
inputSource.close();
}
}
|