From 761a5ad052fee93c61b329df59032460d91b7df5 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 27 Oct 2024 11:18:00 -0400 Subject: [PATCH] Added functions for concatenation and kleene star creation, removed relevant code from main --- main.go | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index a2920d8..ea12361 100644 --- a/main.go +++ b/main.go @@ -129,32 +129,16 @@ func thompson(re string) *State { case CONCAT: s2 := mustPop(&nfa) s1 := mustPop(&nfa) - for i := range s1.output { - s1.output[i].transitions[s2.content] = append(s1.output[i].transitions[s2.content], s2) - } - s1.output = s2.output + s1 = concatenate(s1, s2) nfa = append(nfa, s1) case '*': // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state s1 := mustPop(&nfa) - stateToAdd := &State{} - stateToAdd.transitions = make(map[int][]*State) - stateToAdd.content = EPSILON - stateToAdd.isEmpty = true - stateToAdd.isKleene = true - stateToAdd.output = append(stateToAdd.output, stateToAdd) - for i := range s1.output { - s1.output[i].transitions[stateToAdd.content] = append(s1.output[i].transitions[stateToAdd.content], stateToAdd) - } - stateToAdd.transitions[s1.content] = append(stateToAdd.transitions[s1.content], s1) + stateToAdd := kleene(*s1) nfa = append(nfa, stateToAdd) - case '+': + case '+': // a+ is equivalent to aa* s1 := mustPop(&nfa) - for i := range s1.output { - s1.output[i].transitions[s1.content] = append(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) + s2 := kleene(*s1) + s1 = concatenate(s1, s2) nfa = append(nfa, s1) case '|': s1 := mustPop(&nfa)