Holy shit regex works

This commit is contained in:
Yorick Rommers
2017-06-12 23:31:08 +02:00
parent 71337f2207
commit e2c916620d
8 changed files with 222 additions and 49 deletions
+6
View File
@@ -0,0 +1,6 @@
DFA
B0
E1
0>a>1
1>a>2
2>a>2
+6
View File
@@ -0,0 +1,6 @@
DFA
B0
E1
0>b>1
1>b>2
2>b>2
+8
View File
@@ -0,0 +1,8 @@
NDFA
Bq1
Eq6
q1>ε>q2
q2>ε>q3
q3>ε>q4
q4>ε>q5
q5>a>q6
+29 -8
View File
@@ -1,9 +1,6 @@
package com.imegumii;
import java.util.Arrays;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.*;
/**
* Created by imegumii on 18/04/2017.
@@ -54,13 +51,37 @@ public class Automata <T extends Comparable> extends Importable {
public String getTransitions()
{
String s = "";
for(Transition t : transistions)
StringBuilder s = new StringBuilder();
s.append(type).append("\n");
s.append('B');
boolean first = true;
for (T beginState : this.beginStates) {
if (first) {
first = false;
} else {
s.append(',');
}
s.append(beginState.toString());
}
s.append('\n');
first = true;
s.append('E');
for (T eindState : this.eindStates) {
if (first) {
first = false;
} else {
s.append(',');
}
s.append(eindState.toString());
}
s.append('\n');
for(Transition<T> t : transistions)
{
s += t.toString() + "\n";
s.append(t.toString()).append("\n");
}
return s;
return s.toString();
}
public void print() {
+97
View File
@@ -0,0 +1,97 @@
package com.imegumii;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Created by imegumii on 12/06/2017.
*/
public class GrammarConverter {
private SortedSet<String> getGrammar (NDFA<String> automata, String state) {
SortedSet<String> canGoTo = new TreeSet<>();
for (Character symbol : automata.symbols) {
SortedSet<String> bereikbaarVanafViaEpsilonBereikbareState = automata.statesBereikbaarVanaf(state, symbol);
for (String s1 : bereikbaarVanafViaEpsilonBereikbareState) {
if (automata.eindStates.contains(s1)) {
canGoTo.add("" + symbol);
} else {
canGoTo.add("" + symbol + s1);
}
}
}
return canGoTo;
}
public Grammar toGrammar(NDFA<String> automata) {
ArrayList<Grammar> grammar = new ArrayList<>();
Grammar retval = null;
for (String state : automata.states) {
SortedSet<String> canGoTo = new TreeSet<>();
// Check where we can go from here
for (Character symbol : automata.symbols) {
// canGoTo.add("" + symbol + state);
// follow epsilon
canGoTo.addAll(getGrammar(automata, state));
SortedSet<String> bereikbaarVanafEpsilon = automata.statesBereikbaarVanaf(state, Transition.EPSILON);
for (String s : bereikbaarVanafEpsilon) {
SortedSet<String> bereikbaarVanafEpsilonViaSymbool = automata.statesBereikbaarVanaf(s, symbol);
for (String s1 : bereikbaarVanafEpsilonViaSymbool) {
canGoTo.add("" + symbol + s1);
}
}
// for (String s : bereikbaarVanafEpsilon) {
// canGoTo.addAll(getGrammar(automata, s));
// }
}
grammar.add(new Grammar(state, canGoTo, null));
}
// System.out.println(grammar);
// grammar.forEach(System.out::println);
for (Grammar grammar1 : grammar) {
if (grammar.size() > 1) {
if (retval == null) {
retval = grammar1;
continue;
} else {
retval = retval.link(grammar1);
}
} else {
retval = grammar1;
}
}
return grammar.get(0);
}
public class Grammar {
Grammar below;
String state;
SortedSet<String> options;
public Grammar(String state, SortedSet<String> options, Grammar below) {
this.state = state;
this.options = options;
this.below = below;
}
@Override
public String toString() {
return state + " -> " + options;
}
public Grammar link(Grammar other) {
this.below = other;
return other;
}
public void print() {
System.out.println(state + " -> " + options);
if (below != null) {
below.print();
}
}
}
}
+9 -10
View File
@@ -9,26 +9,25 @@ public class Main {
public static void main(String[] args) {
// Test.test();
// Hopcroft();
// TestUitOpdrachtBeschrijving();
String s = "a*(aa+ |ba*b ) * (abba|baab|bbbb)+";
String s2 = "a(a+ | b*((a|b)+))";
String s3 = "(a|b)+a";
String s4 = "a+((ab)*b|ab|(b)*bb)+(abba|baab)+";
String s5 = "a(ab)*b(a|b)|ab|(b)*bb";
String s6 = "(fuck)+";
String s7 = "(a|b)";
String s7 = "a((a|b)+(baab|abba))";
RegExp reg = new RegExp();
// NDFA<String> ndfa = (NDFA<String>) FileParser.read("ndfa1.dot");
// ndfa.print();
// new GrammarConverter().toGrammar(ndfa).print();
// RegExp r = new RegExp();
// String todo = s;
// System.out.println(todo);
// System.out.println(r.naarRegExp(todo).getTaal(4, 10));
new Frame();
// new PopupFrame("Regex", reg.naarRegExp(s6));
}
}
+66 -30
View File
@@ -42,46 +42,82 @@ public class RegExp extends Importable{
rechts = null;
}
public RegExp loopRecursiefDoorString(String s, RegExp r) {
Deque<Integer> start = new ArrayDeque<>();
Deque<Integer> end = new ArrayDeque<>();
public RegExp loopRecursiefDoorString(String startString, RegExp r) {
int count = 0;
boolean moetRecursief = false;
ArrayList<Tuple<Integer, Integer>> segments = new ArrayList<>();
Deque<RegExp> processedParantheses = new ArrayDeque<>();
String toProcessAfter = s;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
for (int i = 0; i < startString.length(); i++) {
char c = startString.charAt(i);
if (c == '(') {
start.addLast(i);
count++;
}
if (c == ')') {
end.addFirst(i);
count--;
}
if (!start.isEmpty() && !end.isEmpty()) {
int startPos = start.pollLast();
int endPos = end.pollFirst();
String sub = s.substring(startPos + 1, endPos);
// System.out.println("Sub is " + sub);
StringBuilder toReplace = new StringBuilder(s.substring(startPos, endPos + 1));
StringBuilder replaceWith = new StringBuilder("" + Transition.ENDCHAR);
// System.out.println("Toreplace is " + toReplace.toString());
toProcessAfter = toProcessAfter.replace(toReplace, replaceWith);
processedParantheses.add(stringNaarRegExp(sub, r, null));
if (count > 1) {
moetRecursief = true;
}
}
RegExp reg = stringNaarRegExp(toProcessAfter, r, processedParantheses);
// System.out.println(reg.getTaal(10));
Queue<String> queue = new LinkedList<>();
queue.add(startString);
Deque<RegExp> processedParantheses = new ArrayDeque<>();
RegExp retval = new RegExp();
while (!queue.isEmpty()) {
String s = queue.poll();
// System.out.println("Working on " + s);
Deque<Integer> start = new ArrayDeque<>();
Deque<Integer> end = new ArrayDeque<>();
String toProcessAfter = s;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
start.addLast(i);
}
if (c == ')') {
end.addFirst(i);
}
if (!start.isEmpty() && !end.isEmpty()) {
int startPos = start.pollLast();
int endPos = end.pollFirst();
String sub = s.substring(startPos + 1, endPos);
// System.out.println("Sub is " + sub);
StringBuilder toReplace = new StringBuilder(s.substring(startPos, endPos + 1));
StringBuilder replaceWith = new StringBuilder("" + Transition.ENDCHAR);
// System.out.println("Toreplace is " + toReplace.toString());
toProcessAfter = toProcessAfter.replace(toReplace, replaceWith);
// System.out.println("Next string is " + toProcessAfter);
queue.add(toProcessAfter);
if (sub.contains("" + Transition.ENDCHAR)) {
// We have simplified the parantheses here, therefore before we continue we must first turn this into a regexp
RegExp reg = stringNaarRegExp(sub, r, processedParantheses);
processedParantheses.add(reg);
// System.out.println(reg.getTaal(10, 100));
} else {
processedParantheses.add(stringNaarRegExp(sub, r, null));
}
// System.out.println("----");
break;
}
}
if (!s.contains("(")) {
retval = stringNaarRegExp(toProcessAfter, r, processedParantheses);
}
}
// System.out.println(retval.getTaal(10, 100));
// System.out.println("----------");
return reg;
return retval;
}
public RegExp naarRegExp(String s) {
+1 -1
View File
@@ -44,7 +44,7 @@ public class Transition <T extends Comparable> implements Comparable<Transition<
@Override
public String toString() {
return "(" + this.vanState + ", " + this.symbol + ")-->" + this.naarState;
return "" + this.vanState + ">" + this.symbol + ">" + this.naarState;
}
}