Skip to content
Snippets Groups Projects
Commit 7726abb0 authored by Noah Heuser's avatar Noah Heuser
Browse files

added class Main, removed other main methods, added path variable to

writer, fixed word generator bug
parent d2e51828
No related branches found
No related tags found
No related merge requests found
......@@ -145,10 +145,4 @@ public abstract class Grammar {
sb.append("}");
return sb.toString();
}
public static void main(String[] args) {
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 = toGrammar(gString);
System.out.println(grammar);
}
}
package main;
import grammar.Grammar;
import type3.FiniteStateMachine;
import type3.Type3Parser;
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 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);
// System.out.println(grammar);
// Writer w = new Writer();
// String[] vars = { "S", "A", "B", "C", "D" };
// int alphaSize = 3;
// int maxConclusios = 3;
// int maxConclusioSize = 3;
// String start = "S";
// int mode = 3;
// Type2Grammar g = new Type2Grammar(vars, alphaSize, start, maxConclusios, maxConclusioSize, mode);
// System.out.println(g.getRules());
// w.runType3Grammar(vars, alphaSize, maxConclusios, start, mode, 3);
Type3Parser t3p = new Type3Parser();
String gString = "G = (V, T, R, S)\nV = {S, A, B, C, D}\nT = {a, b, c}\nR = {S -> bB | aC,\n A -> c | cD,\n B -> cA | cB,\n C -> bA | aA | c,\n D -> c | aA | cS}";
Grammar g = Grammar.toGrammar(gString);
FiniteStateMachine fsm = t3p.grammarToFSM(g.getRules(), g.getStart());
String word = "abc";
fsm.switchStates("a");
fsm.switchStates("b");
fsm.switchStates("c");
while (true) {
if (fsm.canStop()) {
fsm.resetStateMachine();
System.out.println(word);
break;
}
fsm.resetStateMachine();
}
fsm.switchStates("c");
while (true) {
word = "c";
if (fsm.canStop()) {
// fsm.resetStateMachine();
System.out.println(word);
break;
}
fsm.resetStateMachine();
}
}
}
......@@ -66,13 +66,4 @@ public class Reader {
return sb.toString();
}
// ε
public static void main(String[] args) {
Reader r = new Reader();
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));
}
}
......@@ -57,6 +57,7 @@ public class WordGenerator {
while (true) {
word = wordGeneratorOverType3GrammarTry(random.nextInt(maxLength - 1) + 1, fsm, alphaSize);
if (fsm.canStop()) {
fsm.resetStateMachine();
break;
}
fsm.resetStateMachine();
......
......@@ -20,6 +20,9 @@ import word_generator.WordGenerator;
public class Writer {
private String defaultPath = "//wsl.localhost/Ubuntu/home/noah/gptgrammarparsing/PythonAPI";
private String path;
private static final int DISTINCT_WORDS = 20;
private static final int MAX_LENGTH = 20;
......@@ -27,6 +30,14 @@ public class Writer {
private Type2Parser t2p = new Type2Parser();
private Type3Parser t3p = new Type3Parser();
public Writer() {
path = defaultPath;
}
public Writer(String path) {
this.path = path;
}
private String createGrammarWordsPair(String grammar, List<String> words) {
JSONObject grammarWordsPair = new JSONObject();
JSONArray wordArray = new JSONArray();
......@@ -39,8 +50,7 @@ public class Writer {
}
private void writePairsToFile(List<String> pairs) {
try (FileWriter fw = new FileWriter(
"//wsl.localhost/Ubuntu/home/noah/gptgrammarparsing/PythonAPI/grammarWordsPairs.txt")) {
try (FileWriter fw = new FileWriter(path + "/grammarWordsPairs.txt")) {
fw.write(pairs.toString());
} catch (IOException e) {
e.printStackTrace();
......@@ -49,8 +59,7 @@ public class Writer {
private void writeResultToFileType2(List<Grammar> grammars, List<Map<String, List<List<String>>>> normalizedRuleSet,
List<String> words) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(
"//wsl.localhost/Ubuntu/home/noah/gptgrammarparsing/PythonAPI/resultsOfMembershipTest.txt"))) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + "/resultsOfMembershipTest.txt"))) {
int i = 0;
int k = 0;
for (Grammar g : grammars) {
......@@ -68,8 +77,7 @@ public class Writer {
}
private void writeResultToFileType3(List<FiniteStateMachine> fsms, List<String> words) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(
"//wsl.localhost/Ubuntu/home/noah/gptgrammarparsing/PythonAPI/resultsOfMembershipTest.txt"))) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + "/resultsOfMembershipTest.txt"))) {
int i = 0;
for (FiniteStateMachine fsm : fsms) {
for (int j = 0; j < DISTINCT_WORDS; j++) {
......@@ -150,17 +158,4 @@ public class Writer {
writePairsToFile(grammarWordsPairs);
}
public static void main(String[] args) {
Writer w = new Writer();
String[] vars = { "S", "A", "B", "C", "D" };
int alphaSize = 3;
int maxConclusios = 3;
int maxConclusioSize = 3;
String start = "S";
int mode = 3;
// Type2Grammar g = new Type2Grammar(vars, alphaSize, start, maxConclusios, maxConclusioSize, mode);
// System.out.println(g.getRules());
w.runType3Grammar(vars, alphaSize, maxConclusios, start, mode, 3);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment