concat GEHT

This commit is contained in:
Walcher 2024-01-08 23:18:52 +01:00
parent 271588221f
commit 3571f542ff
2 changed files with 94 additions and 30 deletions

View file

@ -16,6 +16,8 @@ public class NFAImpl implements NFA {
private Set<Character> alphabet;
private Set<Character> completeAlphabet;
private boolean isFinalized;
public NFAImpl(String startState) {
@ -25,6 +27,10 @@ public class NFAImpl implements NFA {
this.transitions = new HashSet<>();
this.acceptingStates = new HashSet<>();
this.alphabet = new HashSet<>();
this.completeAlphabet = new HashSet<>();
for (char ch = 'a'; ch <= 'z'; ch++) {
this.completeAlphabet.add(ch);
}
this.isFinalized = false;
}
@ -211,28 +217,55 @@ public class NFAImpl implements NFA {
return intersectionNFA;
}
// #TODO
@Override
public NFA concatenation(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) {
throw new FinalizedStateException();
}
NFAImpl concatenationNFA = new NFAImpl(this.initialState);
// Add states from 'this'
concatenationNFA.states.addAll(this.states);
concatenationNFA.states.addAll(other.getStates());
// Add states from 'other', with renaming if necessary
Map<String, String> renamedStates = new HashMap<>();
for (String state : other.getStates()) {
String newState = changeIfNecessary(state, concatenationNFA.states);
renamedStates.put(state, newState); // Old state name -> New state name
concatenationNFA.states.add(newState);
}
// Add transitions from 'this'
concatenationNFA.transitions.addAll(this.transitions);
concatenationNFA.transitions.addAll(other.getTransitions());
for (String accceptingState : this.acceptingStates) {
Transition epsilon = new Transition(accceptingState, null, other.getInitialState());
// Add transitions from 'other', updating state names
for (Transition transition : other.getTransitions()) {
String newFromState = renamedStates.getOrDefault(transition.fromState(), transition.fromState());
String newToState = renamedStates.getOrDefault(transition.toState(), transition.toState());
Transition newTransition = new Transition(newFromState, transition.readSymbol(), newToState);
concatenationNFA.transitions.add(newTransition);
}
// Connect accepting states of 'this' to the initial state of 'other' with epsilon transitions
String newOtherInitialState = renamedStates.getOrDefault(other.getInitialState(), other.getInitialState());
for (String acceptingState : this.acceptingStates) {
Transition epsilon = new Transition(acceptingState, null, newOtherInitialState);
concatenationNFA.transitions.add(epsilon);
}
// Set accepting states of the concatenated NFA to be the accepting states of 'other'
concatenationNFA.acceptingStates.clear();
for (String acceptingState : other.getAcceptingStates()) {
concatenationNFA.acceptingStates.add(renamedStates.getOrDefault(acceptingState, acceptingState));
}
concatenationNFA.finalizeAutomaton();
return concatenationNFA;
}
@Override
public NFA kleeneStar() throws FinalizedStateException {
if (!this.isFinalized) {
@ -256,6 +289,7 @@ public class NFAImpl implements NFA {
nfa.transitions.add(loopBackTransition);
}
nfa.finalizeAutomaton();
return nfa;
}
@ -293,6 +327,8 @@ public class NFAImpl implements NFA {
}
*/
nfa.finalizeAutomaton();
return nfa;
}
@ -388,6 +424,8 @@ public class NFAImpl implements NFA {
currentStates = epsilonClosure(currentStates, this);
}
return isAcceptingState(currentStates, this);
}