blob: b8eac35658a1cb33a2b5ad2531cf463e68f15079 (
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
60
61
|
package bjc.rgens.parser;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import bjc.utils.funcutils.ListUtils;
import bjc.utils.gen.WeightedGrammar;
/**
* App that reads a grammar from a file and generates results
*
* @author ben
*
*/
public class GrammarReaderCLI {
private static WeightedGrammar<String> grammar = null;
/**
* Main application method
*
* @param args
* CLI args
*/
public static void main(String[] args) {
if (args.length == 0) {
GrammarReaderApp.main(args);
} else {
String fName = args[0];
if (fName.equalsIgnoreCase("--help")) {
System.out.println(
"Usage: java -jar GrammarReader.jar <file-name> <init-rule> <num-res>");
System.exit(0);
}
String ruleName = args[1];
try (FileInputStream fStream = new FileInputStream(fName)) {
grammar = RBGrammarReader.fromPath(Paths.get(fName, ""));
} catch (IOException e) {
e.printStackTrace();
}
if (ruleName.equalsIgnoreCase("--list-rules")) {
grammar.getRuleNames().forEach(System.out::println);
System.exit(0);
}
int rCount = Integer.parseInt(args[2]);
for (int i = 0; i < rCount; i++) {
String ruleResult = ListUtils.collapseTokens(
grammar.generateListValues(ruleName, " "));
System.out.println(ruleResult.replaceAll("\\s+", " "));
}
}
}
}
|