Fixed kleene star behavior, which used to behave like a '+'

This commit is contained in:
2024-10-23 08:51:40 -04:00
parent 2cd43bf2a1
commit 9d3bc2b804
3 changed files with 34 additions and 12 deletions

12
nfa.go
View File

@@ -4,10 +4,11 @@ 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
isEmpty bool // If it is empty - Union operator and Kleene star 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)
isKleene bool // Identifies whether current node is a 0-state representing Kleene star
}
type NFA struct {
@@ -25,6 +26,15 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) {
state.isLast = true
return
}
if len(state.transitions) == 1 && state.isKleene { // A State representing a Kleene Star has a transition going out, which loops back to it. If that is the only transition (and it contains only one state), then it must be a last-state
for _, v := range state.transitions { // Should only loop once
if len(v) == 1 {
state.isLast = true
return
}
}
}
if visited[state] == true {
return
}