From bf3060b6729b6a53b546a781326cb4e6b9701d00 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 27 Oct 2024 12:52:59 -0400 Subject: [PATCH] Used 'unique append' to ensure that a transition can only contain a given state once --- main.go | 9 +++++++-- nfa.go | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ea12361..5e09b59 100644 --- a/main.go +++ b/main.go @@ -147,8 +147,13 @@ func thompson(re string) *State { s3.transitions = make(map[int][]*State) s3.output = append(s3.output, s1.output...) s3.output = append(s3.output, s2.output...) - s3.transitions[s1.content] = append(s3.transitions[s1.content], s1) - s3.transitions[s2.content] = append(s3.transitions[s2.content], s2) + // Unique append is used here (and elsewhere) to ensure that, + // for any given transition, a state can only be mentioned once. + // For example, given the transition 'a', the state 's1' can only be mentioned once. + // This would lead to multiple instances of the same set of match indices, since both + // 's1' states would be considered to match. + s3.transitions[s1.content] = unique_append(s3.transitions[s1.content], s1) + s3.transitions[s2.content] = unique_append(s3.transitions[s2.content], s2) s3.content = EPSILON s3.isEmpty = true diff --git a/nfa.go b/nfa.go index 05c40c0..2e5fd7c 100644 --- a/nfa.go +++ b/nfa.go @@ -55,7 +55,7 @@ func verifyLastStates(start []*State) { func concatenate(s1 *State, s2 *State) *State { for i := range s1.output { - s1.output[i].transitions[s2.content] = append(s1.output[i].transitions[s2.content], s2) + s1.output[i].transitions[s2.content] = unique_append(s1.output[i].transitions[s2.content], s2) } s1.output = s2.output return s1 @@ -69,8 +69,8 @@ func kleene(s1 State) *State { toReturn.isKleene = true toReturn.output = append(toReturn.output, toReturn) for i := range s1.output { - s1.output[i].transitions[toReturn.content] = append(s1.output[i].transitions[toReturn.content], toReturn) + s1.output[i].transitions[toReturn.content] = unique_append(s1.output[i].transitions[toReturn.content], toReturn) } - toReturn.transitions[s1.content] = append(toReturn.transitions[s1.content], &s1) + toReturn.transitions[s1.content] = unique_append(toReturn.transitions[s1.content], &s1) return toReturn }