Allow one state to map to multiple states with the same transition eg. ab|aa

This commit is contained in:
2024-10-22 14:35:03 -04:00
parent 8394e7867e
commit 213da40c3b
2 changed files with 21 additions and 20 deletions

20
nfa.go
View File

@@ -3,11 +3,11 @@ package main
const EPSILON int = 0
type State struct {
content int // Contents of current state
isEmpty bool // If it is empty - Union operator states will be empty
isLast bool // If it is the last state (acept state)
output []*State // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
transitions map[int]*State // Transitions to different states (can be associated with an int, representing content of destination state)
content int // Contents of current state
isEmpty bool // If it is empty - Union operator states will be empty
isLast bool // If it is the last state (acept state)
output []*State // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these.
transitions map[int][]*State // Transitions to different states (maps a character (int representation) to a _list of states. This is useful if one character can lead multiple states eg. ab|aa)
}
type NFA struct {
@@ -21,7 +21,7 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) {
state.isLast = true
return
}
if state.transitions[state.content] == state { // Eg. a*
if len(state.transitions) == 1 && len(state.transitions[state.content]) == 1 && state.transitions[state.content][0] == state { // Eg. a*
state.isLast = true
return
}
@@ -29,9 +29,11 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) {
return
}
visited[state] = true
for k := range state.transitions {
if state.transitions[k] != state {
verifyLastStatesHelper(state.transitions[k], visited)
for _, states := range state.transitions {
for i := range states {
if states[i] != state {
verifyLastStatesHelper(states[i], visited)
}
}
}
}