-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenfa.go
184 lines (156 loc) · 4.09 KB
/
enfa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package enfa
import "fmt"
type transitionInput struct {
srcState int
input string
}
type destState map[int]bool
type ENFA struct {
initState int
currentState map[int]bool
totalStates []int
finalStates []int
transition map[transitionInput]destState
inputMap map[string]bool
}
//New a new NFA
func NewENFA(initState int, isFinal bool) *ENFA {
retNFA := &ENFA{
transition: make(map[transitionInput]destState),
inputMap: make(map[string]bool),
initState: initState}
retNFA.currentState = make(map[int]bool)
retNFA.currentState[initState] = true
retNFA.AddState(initState, isFinal)
return retNFA
}
//Add new state in this NFA
func (d *ENFA) AddState(state int, isFinal bool) {
if state == -1 {
fmt.Println("Cannot add state as -1, it is dead state")
return
}
d.totalStates = append(d.totalStates, state)
if isFinal {
d.finalStates = append(d.finalStates, state)
}
}
//Add new transition function into NFA
func (d *ENFA) AddTransition(srcState int, input string, dstStateList ...int) {
find := false
//find input if exist in NFA input List
if _, ok := d.inputMap[input]; !ok {
//not exist, new input in this NFA
d.inputMap[input] = true
}
for _, v := range d.totalStates {
if v == srcState {
find = true
}
}
if !find {
fmt.Println("No such state:", srcState, " in current NFA")
return
}
dstMap := make(map[int]bool)
for _, destState := range dstStateList {
dstMap[destState] = true
}
targetTrans := transitionInput{srcState: srcState, input: input}
d.transition[targetTrans] = dstMap
}
func (d *ENFA) CheckPathExist(src int, input string, dst int) bool {
retMap, _ := d.transition[transitionInput{srcState: src, input: input}]
if _, ok := retMap[dst]; ok {
return true
}
return false
}
func (d *ENFA) Input(testInput string) []int {
updateCurrentState := make(map[int]bool)
for current, _ := range d.currentState {
intputTrans := transitionInput{srcState: current, input: testInput}
valMap, ok := d.transition[intputTrans]
if ok {
for dst, _ := range valMap {
updateCurrentState[dst] = true
//Update epsilon input way... if exist
epsilonTrans := transitionInput{srcState: dst}
if eMap, ok := d.transition[epsilonTrans]; ok {
for eDst, _ := range eMap {
updateCurrentState[eDst] = true
}
}
}
} else {
//dead state, remove in current state
//do nothing.
}
}
//update curret state
d.currentState = updateCurrentState
//return result
var ret []int
for state, _ := range updateCurrentState {
ret = append(ret, state)
}
return ret
}
//To verify current state if it is final state
func (d *ENFA) Verify() bool {
for _, val := range d.finalStates {
for cState, _ := range d.currentState {
if val == cState {
return true
}
}
}
return false
}
//Reset NFA state to initilize state, but all state and transition function will remain
func (d *ENFA) Reset() {
initState := make(map[int]bool)
initState[d.initState] = true
d.currentState = initState
}
//Verify if list of input could be accept by NFA or not
func (d *ENFA) VerifyInputs(inputs []string) bool {
for _, v := range inputs {
d.Input(v)
}
return d.Verify()
}
//To print detail transition table contain of current NFA
func (d *ENFA) PrintTransitionTable() {
fmt.Println("===================================================")
//list all inputs
var inputList []string
for key, _ := range d.inputMap {
if key == "" {
fmt.Printf("\tε|")
} else {
fmt.Printf("\t%s|", key)
}
inputList = append(inputList, key)
}
fmt.Printf("\n")
fmt.Println("---------------------------------------------------")
for _, state := range d.totalStates {
fmt.Printf("%d |", state)
for _, key := range inputList {
checkInput := transitionInput{srcState: state, input: key}
if dstState, ok := d.transition[checkInput]; ok {
fmt.Printf("\t")
for val, _ := range dstState {
fmt.Printf("%d,", val)
}
fmt.Printf("|")
} else {
fmt.Printf("\tNA|")
}
}
fmt.Printf("\n")
}
fmt.Println("---------------------------------------------------")
fmt.Println("===================================================")
}