summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-11 21:58:22 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-11 21:58:22 -0400
commit24e05ffb2b55a9c2d487a6400cd5b5077c4e0bda (patch)
tree11da455a5541a446a9d96af6a7895b937869b188
parent3f74e1e25fd572adab34e53eb90edcf49404fbe5 (diff)
Start on regex grammar
-rw-r--r--JPratt/src/examples/java/bjc/pratt/examples/regex/Destringer.java29
-rw-r--r--JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java62
2 files changed, 91 insertions, 0 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/regex/Destringer.java b/JPratt/src/examples/java/bjc/pratt/examples/regex/Destringer.java
new file mode 100644
index 0000000..dc9f1ba
--- /dev/null
+++ b/JPratt/src/examples/java/bjc/pratt/examples/regex/Destringer.java
@@ -0,0 +1,29 @@
+package bjc.pratt.examples.regex;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.function.UnaryOperator;
+
+final class Destringer implements UnaryOperator<String> {
+ private final Iterator<Integer> numbers;
+ private final Map<String, String> stringLiterals;
+
+ public Destringer(final Iterator<Integer> nmbers, final Map<String, String> literals) {
+ numbers = nmbers;
+ stringLiterals = literals;
+ }
+
+ @Override
+ public String apply(final String token) {
+ if (token.startsWith("\"") && token.endsWith("\"")) {
+ final String symName = "stringLiteral" + Integer.toString(numbers.next());
+
+ final String dequotedString = token.substring(1, token.length() - 1);
+ stringLiterals.put(symName, dequotedString);
+
+ return symName;
+ }
+
+ return token;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java b/JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java
new file mode 100644
index 0000000..c684f32
--- /dev/null
+++ b/JPratt/src/examples/java/bjc/pratt/examples/regex/RegexGrammar.java
@@ -0,0 +1,62 @@
+package bjc.pratt.examples.regex;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Scanner;
+
+import bjc.utils.data.GeneratingIterator;
+import bjc.utils.funcdata.IList;
+import bjc.utils.parserutils.TokenUtils;
+import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter;
+import bjc.utils.parserutils.splitter.TokenSplitter;
+import bjc.utils.parserutils.splitter.TransformTokenSplitter;
+
+/**
+ * Grammar test for regular expressions.
+ *
+ * @author bjculkin
+ *
+ */
+public class RegexGrammar {
+ /**
+ * Main method.
+ *
+ * @param args
+ * Unused CLI arguments.
+ */
+ public static void main(final String[] args) {
+ final Scanner scn = new Scanner(System.in);
+
+ System.out.print("Enter text to parse (blank line to exit): ");
+ String ln = scn.nextLine().trim();
+
+ final Iterator<Integer> numbers = new GeneratingIterator<>(0, (num) -> num + 1, (val) -> true);
+
+ final Map<String, String> stringLiterals = new HashMap<>();
+ final Destringer destringer = new Destringer(numbers, stringLiterals);
+
+ final TokenSplitter dquoteSplitter = new TokenUtils.StringTokenSplitter();
+ final TokenSplitter dquoteRemover = new TransformTokenSplitter(dquoteSplitter, destringer);
+
+ final ConfigurableTokenSplitter regexSplitter = new ConfigurableTokenSplitter(true);
+ regexSplitter.addSimpleDelimiters("+", "|");
+
+ while (!ln.equals("")) {
+ final IList<String> quotelessTokens = dquoteRemover.split(ln);
+
+ System.out.println("\nTokens without quoted strings: " + quotelessTokens);
+
+ System.out.print("\nEnter text to parse (blank line to exit): ");
+ ln = scn.nextLine().trim();
+ }
+
+ System.out.println("\nString table: ");
+ for (final Entry<String, String> entry : stringLiterals.entrySet()) {
+ System.out.printf("\t%s\t'%s'\n", entry.getKey(), entry.getValue());
+ }
+
+ scn.close();
+ }
+}