Allow one state to map to multiple states with the same transition eg. ab|aa
This commit is contained in:
20
nfa.go
20
nfa.go
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user