From b327143fa2660a7ac572d0d5a4972aaaa756c6c5 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 27 Oct 2024 11:19:06 -0400 Subject: [PATCH] Added function for concatenation and kleene star --- nfa.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/nfa.go b/nfa.go index cc1a9c2..05c40c0 100644 --- a/nfa.go +++ b/nfa.go @@ -48,7 +48,29 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) { } } -// verifyLastStates penables the 'isLast' flag for the leaf nodes (last states) +// verifyLastStates enables the 'isLast' flag for the leaf nodes (last states) func verifyLastStates(start []*State) { verifyLastStatesHelper(start[0], make(map[*State]bool)) } + +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 = s2.output + return s1 +} + +func kleene(s1 State) *State { + toReturn := &State{} + toReturn.transitions = make(map[int][]*State) + toReturn.content = EPSILON + toReturn.isEmpty = true + 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) + } + toReturn.transitions[s1.content] = append(toReturn.transitions[s1.content], &s1) + return toReturn +}