From 213da40c3ba75a270b09abab5da6d8ea41d67da0 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 22 Oct 2024 14:35:03 -0400 Subject: [PATCH] Allow one state to map to multiple states with the same transition eg. ab|aa --- main.go | 21 ++++++++++----------- nfa.go | 20 +++++++++++--------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index a04a100..cc62206 100644 --- a/main.go +++ b/main.go @@ -106,7 +106,7 @@ func thompson(re string) State { for _, c := range re { if isAlphaNum(c) { state := State{} - state.transitions = make(map[int]*State) + state.transitions = make(map[int][]*State) state.content = int(c) state.output = make([]*State, 0) state.output = append(state.output, &state) @@ -119,14 +119,14 @@ func thompson(re string) State { s2 := pop(&nfa) s1 := pop(&nfa) for i := range s1.output { - s1.output[i].transitions[s2.content] = &s2 + s1.output[i].transitions[s2.content] = append(s1.output[i].transitions[s2.content], &s2) } s1.output = s2.output nfa = append(nfa, s1) case '*': s1 := pop(&nfa) for i := range s1.output { - s1.output[i].transitions[s1.content] = &s1 + 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 @@ -136,10 +136,10 @@ func thompson(re string) State { s1 := pop(&nfa) s2 := pop(&nfa) s3 := State{} - s3.transitions = make(map[int]*State) + s3.transitions = make(map[int][]*State) s3.output = append(s3.output, &s1, &s2) - s3.transitions[s1.content] = &s1 - s3.transitions[s2.content] = &s2 + s3.transitions[s1.content] = append(s3.transitions[s1.content], &s1) + s3.transitions[s2.content] = append(s3.transitions[s2.content], &s2) s3.content = UNION s3.isEmpty = true @@ -159,12 +159,11 @@ func thompson(re string) State { func main() { var re string // fmt.Scanln(&re) - re = "a(b|c)*d" + re = "abab|abbb" re_postfix := shuntingYard(re) fmt.Println(re_postfix) start := thompson(re_postfix) - - assert(len(start.transitions) == 1) - assert(len(start.transitions[UNION].transitions) == 2) - + UNUSED(start) } + +func UNUSED[T any](val T) {} diff --git a/nfa.go b/nfa.go index 70bd855..030d21c 100644 --- a/nfa.go +++ b/nfa.go @@ -3,11 +3,11 @@ package main const EPSILON int = 0 type State struct { - content int // Contents of current state - isEmpty bool // If it is empty - Union operator states will be empty - isLast bool // If it is the last state (acept state) - output []*State // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these. - transitions map[int]*State // Transitions to different states (can be associated with an int, representing content of destination state) + content int // Contents of current state + isEmpty bool // If it is empty - Union operator states will be empty + isLast bool // If it is the last state (acept state) + output []*State // The outputs of the current state ie. the 'outward arrows'. A union operator state will have more than one of these. + transitions map[int][]*State // Transitions to different states (maps a character (int representation) to a _list of states. This is useful if one character can lead multiple states eg. ab|aa) } type NFA struct { @@ -21,7 +21,7 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) { state.isLast = true return } - if state.transitions[state.content] == state { // Eg. a* + if len(state.transitions) == 1 && len(state.transitions[state.content]) == 1 && state.transitions[state.content][0] == state { // Eg. a* state.isLast = true return } @@ -29,9 +29,11 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) { return } visited[state] = true - for k := range state.transitions { - if state.transitions[k] != state { - verifyLastStatesHelper(state.transitions[k], visited) + for _, states := range state.transitions { + for i := range states { + if states[i] != state { + verifyLastStatesHelper(states[i], visited) + } } } }