Wrote function to clone the NFA starting at a given state, and a function to find question mark operator (a? == (a|))

master
Aadhavan Srinivasan 2 months ago
parent b602295bee
commit 21142e6e13

@ -25,6 +25,52 @@ type State struct {
zeroMatchFound bool // Whether or not the state has been used for a zero-length match - only relevant for zero states zeroMatchFound bool // Whether or not the state has been used for a zero-length match - only relevant for zero states
} }
// Clones the NFA starting from the given state.
func cloneState(start *State) *State {
return cloneStateHelper(start, make(map[*State]*State))
}
// Helper function for clone. The map is used to keep track of which states have
// already been copied, and which ones haven't.
// This function was created using output from Llama3.1:405B.
func cloneStateHelper(state *State, cloneMap map[*State]*State) *State {
// Base case - if the clone exists in our map, return it.
if clone, exists := cloneMap[state]; exists {
return clone
}
// Recursive case - if the clone doesn't exist, create it, add it to the map,
// and recursively call for each of the transition states.
clone := &State{
content: append([]int{}, state.content...),
isEmpty: state.isEmpty,
isLast: state.isLast,
output: make([]*State, len(state.output)),
transitions: make(map[int][]*State),
isKleene: state.isKleene,
assert: state.assert,
zeroMatchFound: state.zeroMatchFound,
}
cloneMap[state] = clone
for i, s := range state.output {
if s == state {
clone.output[i] = clone
} else {
clone.output[i] = cloneStateHelper(s, cloneMap)
}
}
for k, v := range state.transitions {
clone.transitions[k] = make([]*State, len(v))
for i, s := range v {
if s == state {
clone.transitions[k][i] = clone
} else {
clone.transitions[k][i] = cloneStateHelper(s, cloneMap)
}
}
}
return clone
}
// Checks if the given state's assertion is true. Returns true if the given // Checks if the given state's assertion is true. Returns true if the given
// state doesn't have an assertion. // state doesn't have an assertion.
func (s State) checkAssertion(str string, idx int) bool { func (s State) checkAssertion(str string, idx int) bool {
@ -123,9 +169,13 @@ func verifyLastStates(start []*State) {
verifyLastStatesHelper(start[0], make(map[*State]bool)) verifyLastStatesHelper(start[0], make(map[*State]bool))
} }
// Concatenates s1 and s2, returns the start of the concatenation.
func concatenate(s1 *State, s2 *State) *State { func concatenate(s1 *State, s2 *State) *State {
if s1 == nil {
return s2
}
for i := range s1.output { for i := range s1.output {
for _, c := range s2.content { // Create transitions for every element in s2's content to s2' for _, c := range s2.content { // Create transitions for every element in s1's content to s2'
s1.output[i].transitions[c], _ = unique_append(s1.output[i].transitions[c], s2) s1.output[i].transitions[c], _ = unique_append(s1.output[i].transitions[c], s2)
} }
} }
@ -172,3 +222,13 @@ func alternate(s1 *State, s2 *State) *State {
return toReturn return toReturn
} }
func question(s1 *State) *State { // Use the fact that ab? == a(b|)
s2 := &State{}
s2.transitions = make(map[int][]*State)
s2.content = newContents(EPSILON)
s2.output = append(s2.output, s2)
s2.isEmpty = true
s3 := alternate(s1, s2)
return s3
}

Loading…
Cancel
Save