Allow one state to map to multiple states with the same transition eg. ab|aa

This commit is contained in:
2024-10-22 14:35:03 -04:00
parent 8394e7867e
commit 213da40c3b
2 changed files with 21 additions and 20 deletions

21
main.go
View File

@@ -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) {}