NDFA to DFA start
This commit is contained in:
@@ -99,9 +99,7 @@ public class Main {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void Thompson()
|
||||
{
|
||||
public static void Thompson() {
|
||||
RegExp expr1 = new RegExp("a");
|
||||
RegExp expr2 = new RegExp("b");
|
||||
RegExp expr3 = new RegExp("c");
|
||||
@@ -116,6 +114,29 @@ public class Main {
|
||||
test.printTransitions();
|
||||
}
|
||||
|
||||
public static void Practicum4() {
|
||||
Character [] characters = {'a', 'b'};
|
||||
NDFA<String> myAutomata = new NDFA<String>(characters);
|
||||
|
||||
myAutomata.addTransition(new Transition<String>("0", 'a', "1"));
|
||||
myAutomata.addTransition(new Transition<String>("0", 'a', "2"));
|
||||
myAutomata.addTransition(new Transition<String>("1", 'a', "1"));
|
||||
myAutomata.addTransition(new Transition<String>("1", 'a', "2"));
|
||||
myAutomata.addTransition(new Transition<String>("2", 'b', "1"));
|
||||
myAutomata.addTransition(new Transition<String>("2", 'b', "3"));
|
||||
myAutomata.addTransition(new Transition<String>("3", 'a', "2"));
|
||||
myAutomata.addTransition(new Transition<String>("3", 'a', "1"));
|
||||
|
||||
myAutomata.defineAsStartState("0");
|
||||
myAutomata.defineAsEndState("1");
|
||||
System.out.println("De NDFA is ");
|
||||
myAutomata.printTransitions();
|
||||
|
||||
System.out.println("DFA is dan ");
|
||||
myAutomata.toDFA().printTransitions();
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// System.out.println("Here my code be");
|
||||
// com.imegumii.codevandocent.Automata<String> t1 = TestAutomata.getExampleSlide8Lesson2();
|
||||
@@ -131,6 +152,6 @@ public class Main {
|
||||
|
||||
// Practicum2();
|
||||
|
||||
Thompson();
|
||||
Practicum4();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.imegumii;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by imegumii on 01/05/2017.
|
||||
@@ -13,4 +13,68 @@ public class NDFA<T extends Comparable> extends Automata<T> {
|
||||
public NDFA(SortedSet<Character> symbols) {
|
||||
super(symbols);
|
||||
}
|
||||
|
||||
public SortedSet<T> statesBereikbaarVanaf(T vanaf, char s) {
|
||||
SortedSet<T> bereikbaar = new TreeSet<T>();
|
||||
Iterator<Transition<T>> it = this.transistions.iterator();
|
||||
|
||||
while(it.hasNext()) {
|
||||
Transition<T> t = it.next();
|
||||
|
||||
boolean isGelijk = false;
|
||||
isGelijk = s == t.symbol;
|
||||
|
||||
if (isGelijk && !bereikbaar.contains(t.naarState)) {
|
||||
bereikbaar.add(t.naarState);
|
||||
bereikbaar.addAll(statesBereikbaarVanaf(t.naarState, Transition.EPSILON));
|
||||
}
|
||||
}
|
||||
|
||||
return bereikbaar;
|
||||
}
|
||||
|
||||
public SortedSet<T> statesBereikbaarVanafSymbool(char s) {
|
||||
SortedSet<T> bereikbareStates = new TreeSet<T>();
|
||||
Iterator<T> it = this.states.iterator();
|
||||
while (it.hasNext()) {
|
||||
T state = it.next();
|
||||
bereikbareStates.addAll(statesBereikbaarVanaf(state, s));
|
||||
}
|
||||
|
||||
return bereikbareStates;
|
||||
}
|
||||
|
||||
public SortedSet<T> transitie (char c) {
|
||||
SortedSet<T> bereikbareStates = statesBereikbaarVanafSymbool(c);
|
||||
if (bereikbareStates.equals(this.states)) {
|
||||
return this.states;
|
||||
} else {
|
||||
return bereikbareStates;
|
||||
}
|
||||
}
|
||||
|
||||
public DFA<T> toDFA () {
|
||||
DFA<T> eindDFA = new DFA<T>(this.symbols);
|
||||
|
||||
SortedSet<T> startSet = new TreeSet<T>();
|
||||
T firstState = this.beginStates.first();
|
||||
startSet.add(firstState);
|
||||
startSet.addAll(statesBereikbaarVanaf(firstState, Transition.EPSILON));
|
||||
System.out.println("Begin states zijn: ");
|
||||
startSet.forEach(System.out::println);
|
||||
|
||||
for (T t : startSet) {
|
||||
eindDFA.defineAsStartState(t);
|
||||
}
|
||||
|
||||
Queue<SortedSet<T>> kue = new LinkedList<>();
|
||||
kue.offer(startSet);
|
||||
|
||||
while (!kue.isEmpty()) {
|
||||
SortedSet<T> set = kue.poll();
|
||||
|
||||
}
|
||||
|
||||
return eindDFA;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package com.imegumii;
|
||||
*/
|
||||
public class Transition <T extends Comparable> implements Comparable<Transition<T>> {
|
||||
public static final char ENDCHAR = '$';
|
||||
|
||||
public static final char EPSILON = 'ε';
|
||||
|
||||
public T vanState;
|
||||
|
||||
Reference in New Issue
Block a user