summaryrefslogtreecommitdiff
path: root/RGens/src
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-01-14 14:14:58 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-01-14 14:14:58 -0500
commit29d19f01b889e6a1882c165c75693fccc918b557 (patch)
treed8bdb06b14c1b01fe49f12b9eabfaaa6d6a0a7e6 /RGens/src
parent5100a37ff71bf5d216941f237549ea6aa6595d43 (diff)
Added grammar syntax notes and some front-end work
Diffstat (limited to 'RGens/src')
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java59
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/GrammarReaderCLI.java52
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/GrammarSyntax.txt18
3 files changed, 129 insertions, 0 deletions
diff --git a/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java b/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java
new file mode 100644
index 0000000..96f4ed7
--- /dev/null
+++ b/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java
@@ -0,0 +1,59 @@
+package bjc.RGens.parser;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+import bjc.utils.gen.WeightedGrammar;
+import bjc.utils.gui.SimpleDialogs;
+import bjc.utils.gui.awt.SimpleFileDialog;
+
+public class GrammarReaderApp {
+
+ public static void main(String[] args) {
+ try {
+ UIManager.setLookAndFeel(
+ UIManager.getSystemLookAndFeelClassName());
+ } catch (ClassNotFoundException | InstantiationException
+ | IllegalAccessException
+ | UnsupportedLookAndFeelException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ File gramFile = SimpleFileDialog.getOpenFile(null,
+ "Choose Grammar File", ".gram");
+
+ WeightedGrammar<String> wg = null;
+
+ try {
+ wg = GrammarReader.fromStream(new FileInputStream(gramFile));
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ String initRule = SimpleDialogs.getChoice(null,
+ "Pick a initial rule",
+ "Pick a initial rule to generate choices from",
+ wg.ruleNames().stream().sorted().toArray(String[]::new));
+
+ int count = SimpleDialogs.getWhole(null,
+ "Enter number of repititions",
+ "Enter the number of items to generate from the rule");
+
+ for (int i = 0; i < count; i++) {
+ String s = wg.genList(initRule, " ")
+ .reduceAux(new StringBuilder(),
+ (strang, strangBuilder) -> strangBuilder
+ .append(strang),
+ t -> t.toString())
+ .replaceAll("\\s+", " ");
+
+ System.out.println(s);
+ }
+ }
+}
diff --git a/RGens/src/main/java/bjc/RGens/parser/GrammarReaderCLI.java b/RGens/src/main/java/bjc/RGens/parser/GrammarReaderCLI.java
new file mode 100644
index 0000000..a40d622
--- /dev/null
+++ b/RGens/src/main/java/bjc/RGens/parser/GrammarReaderCLI.java
@@ -0,0 +1,52 @@
+package bjc.RGens.parser;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import bjc.utils.gen.WeightedGrammar;
+
+public class GrammarReaderCLI {
+ private static WeightedGrammar<String> wg = null;
+
+ 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 rName = args[1];
+
+ try {
+ wg = GrammarReader.fromStream(new FileInputStream(fName));
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ }
+
+ if (rName.equalsIgnoreCase("--list-rules")) {
+ for (String rn : wg.ruleNames()) {
+ System.out.println(rn);
+ }
+ System.exit(0);
+ }
+
+ int rCount = Integer.parseInt(args[2]);
+
+ for (int i = 0; i < rCount; i++) {
+ String s = wg.genList(rName, " ")
+ .reduceAux(new StringBuilder(),
+ (strang, strangBuilder) -> strangBuilder
+ .append(strang),
+ t -> t.toString())
+ .replaceAll("\\s+", " ");
+
+ System.out.println(s);
+ }
+ }
+ }
+}
diff --git a/RGens/src/main/java/bjc/RGens/parser/GrammarSyntax.txt b/RGens/src/main/java/bjc/RGens/parser/GrammarSyntax.txt
new file mode 100644
index 0000000..db9a158
--- /dev/null
+++ b/RGens/src/main/java/bjc/RGens/parser/GrammarSyntax.txt
@@ -0,0 +1,18 @@
+Each line consists of either a comment, a pragma, a rule, or a case for a previously mentioned rule.
+ * Anything after a # on a line is considered a comment
+ * A pragma is indicated by the word 'pragma' followed by the pragma name and any parameters.
+ * List of pragmas
+ * uniform: toggle uniform weighting of grammar options. The default state of this is off.
+ * subordinate: create a new grammar and set the rules of the current grammar as a subgrammar
+ of the new grammar with the name given as a parameter to the pragma
+ * promote: Pick a subgrammar with the given name, and then subordinate this grammar to that one.
+ * remove-sub-grammar: remove a designated subgrammar
+ * remove-rule: remove a designated rule from this grammar
+ * load-sub-grammar: load a designated subgrammar from a file
+ * new-sub-grammar: create a new subgrammar and start editing it
+ * edit-sub-grammar: switch to editing the designated subgrammar
+ * edit-parent: switch to editing the parent of the current grammar
+ * save-sub-grammar: save the current subgrammar under a certain name
+ * A rule is defined by saying its name, then following it with a bunch of case definitions.
+ * A case definition is defined by an optional probability if uniformity is toggled off, then a series of tokens for the rules.
+ More case definitions are indicated by a leading tab. \ No newline at end of file