-
Notifications
You must be signed in to change notification settings - Fork 10
/
Regex.kt
259 lines (231 loc) · 7.6 KB
/
Regex.kt
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package ai.hypergraph.kaliningraph.automata
import ai.hypergraph.kaliningraph.parsing.Σᐩ
import kotlin.random.Random
// TODO: rewrite in matrix form
class KRegex(val regex: Σᐩ) {
val postfixRegex = PostFix.toPostfix(regex)
val symbols = mutableListOf<Char>()
val transitions = mutableListOf<Transition>()
val finalStates = mutableListOf<State>()
val initialStates = mutableListOf<State>()
val states = mutableListOf<Σᐩ>()
private var saveFinal: State? = null
private val stackInitial = Stack<State>()
private val stackFinal = Stack<State>()
init {
updateSymbols()
compile()
updateStates()
updateInitialState()
}
fun check(input: Σᐩ): Σᐩ {
var currentStates = initialStates
var tmpStateList = mutableListOf<State>()
for (element in input) {
if (tmpStateList.size > 0) currentStates = tmpStateList
for (j in currentStates.indices) {
val currentChar = element.toString()
val currentState = currentStates[j]
var k = 0
while (k < transitions.size) {
if (transitions[k].from === currentState &&
transitions[k].sym == currentChar
) {
val nextState = transitions[k].to
tmpStateList = eClosure(nextState)
tmpStateList.add(nextState)
k = transitions.size
} else if (transitions[k].from === currentState &&
transitions[k].sym == "ε"
) {
val nextState = transitions[k].to
tmpStateList = eClosure(nextState, currentStates)
tmpStateList.add(nextState)
}
k++
}
}
}
val result = if (finalStates[0] in currentStates) {
"Σᐩ was accepted (states = ${currentStates.toSet()})"
} else {
"Σᐩ was rejected (states = ${currentStates.toSet()})"
}
return result
}
private fun eClosure(initialState: State, closure: MutableList<State> = mutableListOf()): MutableList<State> {
for (i in transitions.indices) {
if (transitions[i].from === initialState) {
if (transitions[i].sym == "ε") {
if (transitions[i].to !in closure)
closure.add(transitions[i].to)
if (initialState !in closure)
eClosure(transitions[i].to, closure)
}
}
}
return closure
}
private fun updateSymbols() {
for (i in postfixRegex.indices) {
if (postfixRegex[i] !in PostFix.precedenceMap) {
if (postfixRegex[i] !in symbols) {
symbols.add(postfixRegex[i])
symbols.sort()
}
}
}
}
fun updateStates() {
for (i in transitions.indices) {
if (transitions[i].from.toString() !in states)
states.add(transitions[i].from.toString())
if (transitions[i].to.toString() !in states)
states.add(transitions[i].to.toString())
}
}
fun updateInitialState() {
val initial = stackInitial.pop()
initial.initial = true
stackInitial.push(initial)
initialStates.add(stackInitial.pop())
}
private fun compile() {
for (i in postfixRegex.indices) {
when {
postfixRegex[i] in symbols -> {
val tr1 = Transition(postfixRegex[i].toString())
transitions += tr1
val initialState = tr1.from
val finalState = tr1.to
stackInitial.push(initialState)
stackFinal.push(finalState)
}
postfixRegex[i] == '|' -> {
val lowerInitial = stackInitial.pop()
val lowerFinal = stackFinal.pop()
val upperInitial = stackInitial.pop()
val upperFinal = stackFinal.pop()
union(upperInitial, upperFinal, lowerInitial, lowerFinal)
}
postfixRegex[i] == '*' -> {
val initialState = stackInitial.pop()
val finalState = stackFinal.pop()
kleene(initialState, finalState)
}
postfixRegex[i] == '.' -> {
saveFinal = stackFinal.pop()
val finalState = stackFinal.pop()
val initialState = stackInitial.pop()
concat(finalState, initialState)
}
}
if (i == postfixRegex.length - 1) {
val finalState = stackFinal.pop()
finalState.final = true
stackFinal.push(finalState)
finalStates += stackFinal.pop()
if ('ε' in symbols) symbols.remove('ε')
}
}
}
private fun union(
upperInitialState: State,
upperFinalState: State,
lowerInitialState: State,
lowerFinalState: State
) {
val inState = State()
val outState = State()
val tr1 = Transition("ε", inState, upperInitialState)
val tr2 = Transition("ε", inState, lowerInitialState)
val tr3 = Transition("ε", upperFinalState, outState)
val tr4 = Transition("ε", lowerFinalState, outState)
transitions += listOf(tr1, tr2, tr3, tr4)
stackInitial.push(inState)
stackFinal.push(outState)
}
private fun concat(initialState: State, finalState: State) {
val tr1 = Transition("ε", initialState, finalState)
transitions += tr1
stackFinal.push(saveFinal!!)
saveFinal = null
}
private fun kleene(initial: State, final: State) {
val inState = State()
val outState = State()
val tr1 = Transition("ε", final, initial)
val tr2 = Transition("ε", inState, outState)
val tr3 = Transition("ε", inState, initial)
val tr4 = Transition("ε", final, outState)
transitions += listOf(tr1, tr2, tr3, tr4)
stackInitial.push(inState)
stackFinal.push(outState)
}
class State(var stateId: Int = Random.Default.nextInt(9999)) {
var previousStates: MutableList<State> = mutableListOf()
var nextStates: MutableList<State> = mutableListOf()
var initial = false
var final = false
override fun toString() = stateId.toString()
}
class Transition(val sym: Σᐩ, val from: State, val to: State) {
constructor(transitionSymbol: Σᐩ) : this(transitionSymbol, State(), State())
init {
from.nextStates.add(to)
to.previousStates.add(from)
}
override fun toString() = "$from - $sym - $to"
}
object PostFix {
var precedenceMap: Map<Char, Int> =
mapOf( '(' to 1, '|' to 2, '.' to 3, '*' to 4, '^' to 5)
private fun getPrecedence(c: Char) = precedenceMap[c] ?: 6
private fun format(regex: Σᐩ): Σᐩ {
var res = ""
val allOperators = listOf('|', '*', '^')
val binaryOperators = listOf('^', '|')
for (i in regex.indices) {
val c1 = regex[i]
if (i + 1 < regex.length) {
val c2 = regex[i + 1]
res += c1
if (c1 != '(' && c2 != ')' && c2 !in allOperators && c1 !in binaryOperators)
res += '.'
}
}
res += regex[regex.length - 1]
return res
}
fun toPostfix(infixRegex: Σᐩ): Σᐩ {
var postfix = ""
val stack = Stack<Char>()
val formattedRegEx = format(infixRegex)
for (c in formattedRegEx.toCharArray()) {
when (c) {
'(' -> stack.push(c)
')' -> {
while (stack.peek() != '(') postfix += stack.pop()
stack.pop()
}
else -> {
while (stack.size > 0) {
postfix += if (getPrecedence(stack.peek()) >= getPrecedence(c)) {
stack.pop()
} else {
break
}
}
stack.push(c)
}
}
}
while (stack.size > 0) postfix += stack.pop()
return postfix
}
}
}
typealias Stack<T> = ArrayDeque<T>
inline fun <T> Stack<T>.push(element: T) = addLast(element) // returns Unit
inline fun <T> Stack<T>.pop() = removeLastOrNull()!! // returns T?
fun <T> Stack<T>.peek(): T = this[lastIndex]!!