From 8394e7867e77e3d7f591d5131fbe40ee6698a980 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 21 Oct 2024 23:17:10 -0400 Subject: [PATCH] Fixed bug with last state detection --- main.go | 3 +++ nfa.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/main.go b/main.go index 7a2f5ac..a04a100 100644 --- a/main.go +++ b/main.go @@ -128,6 +128,9 @@ func thompson(re string) State { for i := range s1.output { s1.output[i].transitions[s1.content] = &s1 } + // Reset output to s1 (in case s1 was a union operator state, which has multiple outputs) + s1.output = nil + s1.output = append(s1.output, &s1) nfa = append(nfa, s1) case '|': s1 := pop(&nfa) diff --git a/nfa.go b/nfa.go index 0244b91..70bd855 100644 --- a/nfa.go +++ b/nfa.go @@ -21,6 +21,10 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) { state.isLast = true return } + if state.transitions[state.content] == state { // Eg. a* + state.isLast = true + return + } if visited[state] == true { return }