From c6a2c30722377f32ea89c0c8b1f6b9efb4529702 Mon Sep 17 00:00:00 2001 From: Noah Heuser <nheuser@uni-koblenz.de> Date: Tue, 4 Jun 2024 17:10:19 +0200 Subject: [PATCH] created dedicated main class and new checkDerivation in Derivation class --- ChatGPTParsing/src/derivation/Derivation.java | 84 ++++++++++++++++ .../src/derivation/DerivationStep.java | 98 ------------------- ChatGPTParsing/src/derivation/Symbol.java | 66 ------------- ChatGPTParsing/src/main/Main.java | 40 ++++++-- 4 files changed, 115 insertions(+), 173 deletions(-) create mode 100644 ChatGPTParsing/src/derivation/Derivation.java delete mode 100644 ChatGPTParsing/src/derivation/DerivationStep.java delete mode 100644 ChatGPTParsing/src/derivation/Symbol.java diff --git a/ChatGPTParsing/src/derivation/Derivation.java b/ChatGPTParsing/src/derivation/Derivation.java new file mode 100644 index 0000000..121d812 --- /dev/null +++ b/ChatGPTParsing/src/derivation/Derivation.java @@ -0,0 +1,84 @@ +package derivation; + +import java.util.List; +import java.util.Map; + +public class Derivation { + + private String[] d; + private Map<String, List<List<String>>> rules; + private String start; + private String word; + + public Derivation(String[] d, Map<String, List<List<String>>> rules, String start, String word) { + this.d = d; + this.rules = rules; + this.start = start; + this.word = word; + } + + public boolean checkDerivation() { + if (!d[0].equals(start)) { + return false; + } + if (!d[d.length - 1].equals(word)) { + return false; + } + StringBuilder sb = new StringBuilder(d[0]); + for (int i = 0; i < d.length - 1; i++) { + int j = 0; + while (j < sb.length() && !rules.containsKey(sb.charAt(j) + "")) { + j++; + } + String v = sb.charAt(j) + ""; + List<List<String>> cs = rules.get(v); + boolean foundRule = false; + int prefixEnd = 0; + int suffixStart = 0; + for (List<String> c : cs) { + sb.deleteCharAt(j); + int ruleLength = 0; + int k = j; + for (String s : c) { + sb.insert(k++, s); + ruleLength++; + } + int end = j + ruleLength; + if (j + ruleLength > d[i + 1].length()) { + end = d[i + 1].length(); + } + if (substringEquals(sb, d[i + 1], j, end)) { + foundRule = true; + suffixStart = j + ruleLength; + break; + } + sb.replace(j, j + ruleLength, v); + } + if (!foundRule) { + return false; + } + if (!substringEquals(sb, d[i + 1], 0, j)) { + return false; + } + + if (!substringEquals(sb, d[i + 1], suffixStart, + sb.length() >= d[i + 1].length() ? sb.length() : d[i + 1].length())) { + return false; + } + } + return true; + } + + private static boolean substringEquals(StringBuilder a, String b, int begin, int end) { + for (int i = begin; i < end; i++) { + try { + if (a.charAt(i) != b.charAt(i)) { + return false; + } + } catch (IndexOutOfBoundsException e) { + return false; + } + } + return true; + } +} diff --git a/ChatGPTParsing/src/derivation/DerivationStep.java b/ChatGPTParsing/src/derivation/DerivationStep.java deleted file mode 100644 index 7da5efb..0000000 --- a/ChatGPTParsing/src/derivation/DerivationStep.java +++ /dev/null @@ -1,98 +0,0 @@ -package derivation; - -import java.util.List; -import java.util.Map; - -public class DerivationStep { - - private int length; - private Symbol first; - private Symbol prefixEnd; - private int prefixEndPos; - private Symbol suffixBegin; - private Map<String, List<List<String>>> rules; - - public DerivationStep(String startSymbol, Map<String, List<List<String>>> rules) { - this.length = 1; - this.first = new Symbol(startSymbol, false, false); - this.prefixEnd = first; - this.prefixEndPos = 0; - this.rules = rules; - } - - public void useRule(int i) { - List<String> conclusio = this.rules.get(this.prefixEnd.toString()).get(i); - if (conclusio.isEmpty()) { - this.prefixEnd.setSymbol(""); - this.prefixEnd.setEmpty(true); - this.prefixEnd.setTerminal(true); - this.length--; - } else { - this.prefixEnd.setSymbol(conclusio.get(0)); - this.prefixEnd.setEmpty(false); - this.prefixEnd.setTerminal(!rules.containsKey(conclusio.get(0))); - } - Symbol current = this.prefixEnd; - for (int j = 1; j < conclusio.size(); j++) { - Symbol next = new Symbol(conclusio.get(j), !rules.containsKey(conclusio.get(j)), false); - current.setNext(next); - current = current.getNext(); - this.length++; - } - current.setNext(suffixBegin); - } - - public void updatePointers() { - while (!rules.containsKey(this.prefixEnd.toString())) { - this.prefixEnd = this.prefixEnd.getNext(); - this.prefixEndPos++; - } - if (this.prefixEndPos != this.length) { - this.suffixBegin = this.prefixEnd.getNext(); - } - - } - - public boolean prefixEquals(String step) { - String prefix = this.getPrefix(); - int i = 0; - for (char c : prefix.toCharArray()) { - if (c != step.charAt(i)) { - return false; - } - i++; - } - return true; - } - - private String getSuffix() { - StringBuilder sb = new StringBuilder(); - for (Symbol current = suffixBegin; current != null; current = current.getNext()) { - - } - return ""; - } - - private String getPrefix() { - StringBuilder sb = new StringBuilder(); - for (Symbol current = this.first; current != this.prefixEnd; current = current.getNext()) { - sb.append(current.toString()); - } - return sb.toString(); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("["); - for (Symbol current = this.first; current != null; current = current.getNext()) { - sb.append(current.toString()); - if (current.getNext() != null) { - sb.append(", "); - } - } - sb.append("]"); - return sb.toString(); - } - -} diff --git a/ChatGPTParsing/src/derivation/Symbol.java b/ChatGPTParsing/src/derivation/Symbol.java deleted file mode 100644 index 5d414d8..0000000 --- a/ChatGPTParsing/src/derivation/Symbol.java +++ /dev/null @@ -1,66 +0,0 @@ -package derivation; - -import java.util.Objects; - -public class Symbol { - - private String symbol; - private Symbol next; - private boolean isTerminal; - private boolean isEmpty; - - public Symbol(String symbol, boolean isTerminal, boolean isEmpty) { - this.symbol = symbol; - this.isTerminal = isTerminal; - this.isEmpty = isEmpty; - } - - public void setSymbol(String symbol) { - this.symbol = symbol; - } - - public boolean isTerminal() { - return this.isTerminal; - } - - public void setTerminal(boolean isTerminal) { - this.isTerminal = isTerminal; - } - - public boolean isEmpty() { - return this.isEmpty; - } - - public void setEmpty(boolean isEmpty) { - this.isEmpty = isEmpty; - } - - public void setNext(Symbol next) { - this.next = next; - } - - public Symbol getNext() { - return next; - } - - @Override - public int hashCode() { - return Objects.hash(symbol); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!(obj instanceof Symbol)) - return false; - Symbol other = (Symbol) obj; - return Objects.equals(symbol, other.symbol); - } - - @Override - public String toString() { - return this.symbol; - } - -} diff --git a/ChatGPTParsing/src/main/Main.java b/ChatGPTParsing/src/main/Main.java index a76f028..2d93d93 100644 --- a/ChatGPTParsing/src/main/Main.java +++ b/ChatGPTParsing/src/main/Main.java @@ -1,6 +1,6 @@ package main; -import derivation.DerivationStep; +import derivation.Derivation; import grammar.Grammar; public class Main { @@ -9,18 +9,40 @@ public class Main { public static void main(String[] args) { // Reader r = new Reader(); -// String[] d = { "S", "TVaW", "aUVaW", "aVaW", "aaaW", "aaabXab", "aaabXbab", "aaabXbbab", "aaabbbab" }; + String[] d = { "S", "TVaW", "aUVaW", "aVaW", "aaaW", "aaabXab", "aaabXbab", "aaabXbbab", "aaabbbab" }; String gString = "G = (V, T, R, S)\nV = {S, T, U, V, W, X}\nT = {a, b}\nR = {S -> TVaW,\n T -> aU,\n U -> UabU | ε,\n V -> a,\n W -> Xab | bXab,\n X -> Xb | ε}"; // System.out.println(r.checkDerivation(gString, d)); // String gString = "G = (V, T, R, S)\nV = {S, A, B, C, D}\nT = {a, b, c}\nR = {S -> ε | a | cB | aB,\n A -> b | aC | aB,\n B -> bA,\n C -> bS | cD,\n D -> bB | c}"; Grammar grammar = Grammar.toGrammar(gString); - DerivationStep ds = new DerivationStep(grammar.getStart(), grammar.getRules()); - ds.useRule(0); - ds.updatePointers(); - ds.useRule(0); - ds.updatePointers(); - ds.prefixEquals("a"); - ds.useRule(1); +// DerivationStep ds = new DerivationStep(grammar.getRules(), grammar.getStart()); + Derivation dev = new Derivation(d, grammar.getRules(), grammar.getStart(), "aaabbbab"); + System.out.println(dev.checkDerivation()); +// System.out.println(ds.prefixEquals("S")); +// System.out.println(ds.suffixEquals("S")); +// System.out.println(ds.infixEquals("S")); +// ds.deleteInfix(); // Vor jeder Regel Verwendung +// ds.useRule(grammar.getRules().get("S").get(0)); // Regel korrekt +// System.out.println(ds.prefixEquals("TVaW")); +// System.out.println(ds.suffixEquals("TVaW")); +// System.out.println(ds.infixEquals("TVaW")); +// ds.updateInfixBounds(); // Folgt auf Regel korrekt +// ds.deleteInfix(); +// ds.useRule(grammar.getRules().get("T").get(0)); +// System.out.println(ds.prefixEquals("aUVaW")); +// System.out.println(ds.suffixEquals("aUVaW")); +// System.out.println(ds.infixEquals("aUVaW")); +// ds.updateInfixBounds(); +// ds.deleteInfix(); +// ds.useRule(grammar.getRules().get("U").get(0)); +// System.out.println(ds.prefixEquals("aUabUVaW")); +// System.out.println(ds.suffixEquals("aUabUVaW")); +// System.out.println(ds.infixEquals("aUabUVaW")); +// ds.deleteInfix(); +// ds.useRule(grammar.getRules().get("U").get(1)); +// System.out.println(ds.prefixEquals("aVaW")); +// System.out.println(ds.suffixEquals("aVaW")); +// System.out.println(ds.infixEquals("aVaW")); +// ds.updateInfixBounds(); // System.out.println(grammar); // Writer w = new Writer(); // String[] vars = { "S", "A", "B", "C", "D" }; -- GitLab