You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
2 months ago
|
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)
|
||
|
}
|
||
|
|
||
|
type NFA struct {
|
||
|
start State
|
||
|
outputs []State
|
||
|
}
|
||
|
|
||
|
// verifyLastStatesHelper performs the depth-first recursion needed for verifyLastStates
|
||
|
func verifyLastStatesHelper(state *State, visited map[*State]bool) {
|
||
|
if len(state.transitions) == 0 {
|
||
|
state.isLast = true
|
||
|
return
|
||
|
}
|
||
|
if visited[state] == true {
|
||
|
return
|
||
|
}
|
||
|
visited[state] = true
|
||
|
for k := range state.transitions {
|
||
|
if state.transitions[k] != state {
|
||
|
verifyLastStatesHelper(state.transitions[k], visited)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// verifyLastStates penables the 'isLast' flag for the leaf nodes (last states)
|
||
|
func verifyLastStates(start []State) {
|
||
|
verifyLastStatesHelper(&start[0], make(map[*State]bool))
|
||
|
}
|