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

master
Aadhavan Srinivasan 2 months ago
parent 8394e7867e
commit 213da40c3b

@ -106,7 +106,7 @@ func thompson(re string) State {
for _, c := range re { for _, c := range re {
if isAlphaNum(c) { if isAlphaNum(c) {
state := State{} state := State{}
state.transitions = make(map[int]*State) state.transitions = make(map[int][]*State)
state.content = int(c) state.content = int(c)
state.output = make([]*State, 0) state.output = make([]*State, 0)
state.output = append(state.output, &state) state.output = append(state.output, &state)
@ -119,14 +119,14 @@ func thompson(re string) State {
s2 := pop(&nfa) s2 := pop(&nfa)
s1 := pop(&nfa) s1 := pop(&nfa)
for i := range s1.output { 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 s1.output = s2.output
nfa = append(nfa, s1) nfa = append(nfa, s1)
case '*': case '*':
s1 := pop(&nfa) s1 := pop(&nfa)
for i := range s1.output { 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) // Reset output to s1 (in case s1 was a union operator state, which has multiple outputs)
s1.output = nil s1.output = nil
@ -136,10 +136,10 @@ func thompson(re string) State {
s1 := pop(&nfa) s1 := pop(&nfa)
s2 := pop(&nfa) s2 := pop(&nfa)
s3 := State{} s3 := State{}
s3.transitions = make(map[int]*State) s3.transitions = make(map[int][]*State)
s3.output = append(s3.output, &s1, &s2) s3.output = append(s3.output, &s1, &s2)
s3.transitions[s1.content] = &s1 s3.transitions[s1.content] = append(s3.transitions[s1.content], &s1)
s3.transitions[s2.content] = &s2 s3.transitions[s2.content] = append(s3.transitions[s2.content], &s2)
s3.content = UNION s3.content = UNION
s3.isEmpty = true s3.isEmpty = true
@ -159,12 +159,11 @@ func thompson(re string) State {
func main() { func main() {
var re string var re string
// fmt.Scanln(&re) // fmt.Scanln(&re)
re = "a(b|c)*d" re = "abab|abbb"
re_postfix := shuntingYard(re) re_postfix := shuntingYard(re)
fmt.Println(re_postfix) fmt.Println(re_postfix)
start := thompson(re_postfix) start := thompson(re_postfix)
UNUSED(start)
assert(len(start.transitions) == 1)
assert(len(start.transitions[UNION].transitions) == 2)
} }
func UNUSED[T any](val T) {}

@ -3,11 +3,11 @@ package main
const EPSILON int = 0 const EPSILON int = 0
type State struct { type State struct {
content int // Contents of current state content int // Contents of current state
isEmpty bool // If it is empty - Union operator states will be empty isEmpty bool // If it is empty - Union operator states will be empty
isLast bool // If it is the last state (acept state) 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. 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) 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 { type NFA struct {
@ -21,7 +21,7 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) {
state.isLast = true state.isLast = true
return 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 state.isLast = true
return return
} }
@ -29,9 +29,11 @@ func verifyLastStatesHelper(state *State, visited map[*State]bool) {
return return
} }
visited[state] = true visited[state] = true
for k := range state.transitions { for _, states := range state.transitions {
if state.transitions[k] != state { for i := range states {
verifyLastStatesHelper(state.transitions[k], visited) if states[i] != state {
verifyLastStatesHelper(states[i], visited)
}
} }
} }
} }

Loading…
Cancel
Save