Added functions for concatenation and kleene star creation, removed relevant code from main
This commit is contained in:
26
main.go
26
main.go
@@ -129,32 +129,16 @@ func thompson(re string) *State {
|
|||||||
case CONCAT:
|
case CONCAT:
|
||||||
s2 := mustPop(&nfa)
|
s2 := mustPop(&nfa)
|
||||||
s1 := mustPop(&nfa)
|
s1 := mustPop(&nfa)
|
||||||
for i := range s1.output {
|
s1 = concatenate(s1, s2)
|
||||||
s1.output[i].transitions[s2.content] = append(s1.output[i].transitions[s2.content], s2)
|
|
||||||
}
|
|
||||||
s1.output = s2.output
|
|
||||||
nfa = append(nfa, s1)
|
nfa = append(nfa, s1)
|
||||||
case '*': // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state
|
case '*': // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state
|
||||||
s1 := mustPop(&nfa)
|
s1 := mustPop(&nfa)
|
||||||
stateToAdd := &State{}
|
stateToAdd := kleene(*s1)
|
||||||
stateToAdd.transitions = make(map[int][]*State)
|
|
||||||
stateToAdd.content = EPSILON
|
|
||||||
stateToAdd.isEmpty = true
|
|
||||||
stateToAdd.isKleene = true
|
|
||||||
stateToAdd.output = append(stateToAdd.output, stateToAdd)
|
|
||||||
for i := range s1.output {
|
|
||||||
s1.output[i].transitions[stateToAdd.content] = append(s1.output[i].transitions[stateToAdd.content], stateToAdd)
|
|
||||||
}
|
|
||||||
stateToAdd.transitions[s1.content] = append(stateToAdd.transitions[s1.content], s1)
|
|
||||||
nfa = append(nfa, stateToAdd)
|
nfa = append(nfa, stateToAdd)
|
||||||
case '+':
|
case '+': // a+ is equivalent to aa*
|
||||||
s1 := mustPop(&nfa)
|
s1 := mustPop(&nfa)
|
||||||
for i := range s1.output {
|
s2 := kleene(*s1)
|
||||||
s1.output[i].transitions[s1.content] = append(s1.output[i].transitions[s1.content], s1)
|
s1 = concatenate(s1, s2)
|
||||||
}
|
|
||||||
// Reset output to s1 (in case s1 was a union operator state, which has multiple outputs)
|
|
||||||
s1.output = nil
|
|
||||||
s1.output = append(s1.output, s1)
|
|
||||||
nfa = append(nfa, s1)
|
nfa = append(nfa, s1)
|
||||||
case '|':
|
case '|':
|
||||||
s1 := mustPop(&nfa)
|
s1 := mustPop(&nfa)
|
||||||
|
Reference in New Issue
Block a user