| 1 |
structure DfaGen = |
| 2 |
struct |
| 3 |
datatype regex = |
| 4 |
CHAR_LITERAL of {char: char, position: int} |
| 5 |
| CONCAT of {l: regex, r: regex, leftMaxState: int, rightMaxState: int} |
| 6 |
| ALTERNATION of {l: regex, r: regex, leftMaxState: int, rightMaxState: int} |
| 7 |
| ZERO_OR_ONE of regex |
| 8 |
| ZERO_OR_MORE of regex |
| 9 |
| ONE_OR_MORE of regex |
| 10 |
| GROUP of regex |
| 11 |
| WILDCARD of int |
| 12 |
|
| 13 |
structure Set = |
| 14 |
struct |
| 15 |
datatype 'a set = BRANCH of 'a set * int * 'a * 'a set | LEAF |
| 16 |
|
| 17 |
fun insertOrReplace (newKey, newVal, tree) = |
| 18 |
case tree of |
| 19 |
BRANCH (l, curKey, curVal, r) => |
| 20 |
if newKey > curKey then |
| 21 |
let val r = insertOrReplace (newKey, newVal, r) |
| 22 |
in BRANCH (l, curKey, curVal, r) |
| 23 |
end |
| 24 |
else if newKey < curKey then |
| 25 |
let val l = insertOrReplace (newKey, newVal, l) |
| 26 |
in BRANCH (l, curKey, curVal, r) |
| 27 |
end |
| 28 |
else |
| 29 |
BRANCH (l, newKey, newVal, r) |
| 30 |
| LEAF => BRANCH (LEAF, newKey, newVal, LEAF) |
| 31 |
|
| 32 |
fun getOrDefault (findKey, tree, default) = |
| 33 |
case tree of |
| 34 |
BRANCH (l, curKey, curVal, r) => |
| 35 |
if findKey > curKey then getOrDefault (findKey, r, default) |
| 36 |
else if findKey < curKey then getOrDefault (findKey, l, default) |
| 37 |
else curVal |
| 38 |
| LEAF => default |
| 39 |
|
| 40 |
fun helpToList (tree, acc) = |
| 41 |
case tree of |
| 42 |
BRANCH (l, curKey, curVal, r) => |
| 43 |
let |
| 44 |
val acc = helpToList (r, acc) |
| 45 |
val acc = (curKey, curVal) :: acc |
| 46 |
in |
| 47 |
helpToList (l, acc) |
| 48 |
end |
| 49 |
| LEAF => acc |
| 50 |
|
| 51 |
fun toList tree = helpToList (tree, []) |
| 52 |
|
| 53 |
fun helpKeysToList (tree, acc) = |
| 54 |
case tree of |
| 55 |
BRANCH (l, curKey, _, r) => |
| 56 |
let |
| 57 |
val acc = helpKeysToList (r, acc) |
| 58 |
val acc = curKey :: acc |
| 59 |
in |
| 60 |
helpKeysToList (l, acc) |
| 61 |
end |
| 62 |
| LEAF => acc |
| 63 |
|
| 64 |
fun keysToList tree = helpKeysToList (tree, []) |
| 65 |
|
| 66 |
fun map (f, tree) = |
| 67 |
case tree of |
| 68 |
BRANCH (l, key, value, r) => |
| 69 |
let |
| 70 |
val r = map (f, r) |
| 71 |
val l = map (f, l) |
| 72 |
val value = f value |
| 73 |
in |
| 74 |
BRANCH (l, key, value, r) |
| 75 |
end |
| 76 |
| LEAF => LEAF |
| 77 |
|
| 78 |
fun foldl (f, tree, acc) = |
| 79 |
case tree of |
| 80 |
BRANCH (l, k, v, r) => |
| 81 |
let |
| 82 |
val acc = foldl (f, l, acc) |
| 83 |
val acc = f (v, acc) |
| 84 |
in |
| 85 |
foldl (f, r, acc) |
| 86 |
end |
| 87 |
| LEAF => acc |
| 88 |
|
| 89 |
fun helpToCharsAndPositionsList (tree, acc) = |
| 90 |
case tree of |
| 91 |
BRANCH (l, k, v, r) => |
| 92 |
let |
| 93 |
val acc = helpToCharsAndPositionsList (r, acc) |
| 94 |
val acc = {char = v, position = k} :: acc |
| 95 |
in |
| 96 |
helpToCharsAndPositionsList (l, acc) |
| 97 |
end |
| 98 |
| LEAF => acc |
| 99 |
|
| 100 |
fun toCharsAndPositionsList tree = helpToCharsAndPositionsList (tree, []) |
| 101 |
end |
| 102 |
|
| 103 |
structure ParseDfa = |
| 104 |
struct |
| 105 |
(* parsing through precedence climbing algorithm. *) |
| 106 |
val postfixLevel = 1 |
| 107 |
val concatLevel = 2 |
| 108 |
val altLevel = 3 |
| 109 |
|
| 110 |
local |
| 111 |
fun loop (pos, str, openParens, closeParens) = |
| 112 |
if pos = String.size str then |
| 113 |
NONE |
| 114 |
else |
| 115 |
case String.sub (str, pos) of |
| 116 |
#"(" => loop (pos + 1, str, openParens + 1, closeParens) |
| 117 |
| #")" => |
| 118 |
if closeParens + 1 = openParens then SOME pos |
| 119 |
else loop (pos + 1, str, openParens, closeParens + 1) |
| 120 |
| _ => loop (pos + 1, str, openParens, closeParens) |
| 121 |
in |
| 122 |
fun getRightParenIdx (pos, str) = loop (pos, str, 1, 0) |
| 123 |
end |
| 124 |
|
| 125 |
fun computeAtom (pos, str, stateNum) = |
| 126 |
if pos = String.size str then |
| 127 |
NONE |
| 128 |
else |
| 129 |
case String.sub (str, pos) of |
| 130 |
#"(" => |
| 131 |
(case getRightParenIdx (pos + 1, str) of |
| 132 |
SOME groupEndIdx => |
| 133 |
let |
| 134 |
val substr = String.substring |
| 135 |
(str, pos + 1, groupEndIdx - pos - 1) |
| 136 |
in |
| 137 |
case parse (substr, stateNum) of |
| 138 |
SOME (rhs, stateNum) => |
| 139 |
SOME (groupEndIdx + 1, rhs, stateNum) |
| 140 |
| NONE => NONE |
| 141 |
end |
| 142 |
| NONE => NONE) |
| 143 |
| #")" => NONE |
| 144 |
| #"?" => NONE |
| 145 |
| #"*" => NONE |
| 146 |
| #"+" => NONE |
| 147 |
| #"." => SOME (pos + 1, WILDCARD (stateNum + 1), stateNum + 1) |
| 148 |
| chr => |
| 149 |
let val chr = CHAR_LITERAL {char = chr, position = stateNum + 1} |
| 150 |
in SOME (pos + 1, chr, stateNum + 1) |
| 151 |
end |
| 152 |
|
| 153 |
and climb (pos, str, lhs, level, stateNum) : (int * regex * int) option = |
| 154 |
if pos = String.size str then |
| 155 |
SOME (pos, lhs, stateNum) |
| 156 |
else |
| 157 |
case String.sub (str, pos) of |
| 158 |
#"|" => |
| 159 |
if level < altLevel then |
| 160 |
SOME (pos, lhs, stateNum) |
| 161 |
else if pos + 1 < String.size str then |
| 162 |
let |
| 163 |
val chr = String.sub (str, pos + 1) |
| 164 |
val chr = CHAR_LITERAL {char = chr, position = stateNum + 1} |
| 165 |
in |
| 166 |
case climb (pos + 2, str, chr, altLevel, stateNum + 1) of |
| 167 |
SOME (pos, rhs, rightStateNum) => |
| 168 |
let |
| 169 |
val result = ALTERNATION |
| 170 |
{ l = lhs |
| 171 |
, r = rhs |
| 172 |
, leftMaxState = stateNum |
| 173 |
, rightMaxState = rightStateNum |
| 174 |
} |
| 175 |
in |
| 176 |
SOME (pos, result, rightStateNum) |
| 177 |
end |
| 178 |
| NONE => NONE |
| 179 |
end |
| 180 |
else |
| 181 |
NONE |
| 182 |
| #"?" => |
| 183 |
if level < postfixLevel then |
| 184 |
SOME (pos, lhs, stateNum) |
| 185 |
else |
| 186 |
let val lhs = ZERO_OR_ONE lhs |
| 187 |
in climb (pos + 1, str, lhs, postfixLevel, stateNum) |
| 188 |
end |
| 189 |
| #"*" => |
| 190 |
if level < postfixLevel then |
| 191 |
SOME (pos, lhs, stateNum) |
| 192 |
else |
| 193 |
let val lhs = ZERO_OR_MORE lhs |
| 194 |
in climb (pos + 1, str, lhs, postfixLevel, stateNum) |
| 195 |
end |
| 196 |
| #"+" => |
| 197 |
if level < postfixLevel then |
| 198 |
SOME (pos, lhs, stateNum) |
| 199 |
else |
| 200 |
let val lhs = ONE_OR_MORE lhs |
| 201 |
in climb (pos + 1, str, lhs, postfixLevel, stateNum) |
| 202 |
end |
| 203 |
| chr => |
| 204 |
if level < concatLevel then |
| 205 |
SOME (pos, lhs, stateNum) |
| 206 |
else |
| 207 |
case computeAtom (pos, str, stateNum) of |
| 208 |
SOME (nextPos, curAtom, atomStateNum) => |
| 209 |
(case climb (nextPos, str, curAtom, concatLevel, atomStateNum) of |
| 210 |
SOME (pos, rhs, rightStateNum) => |
| 211 |
let |
| 212 |
val result = CONCAT |
| 213 |
{ l = lhs |
| 214 |
, r = rhs |
| 215 |
, leftMaxState = stateNum |
| 216 |
, rightMaxState = rightStateNum |
| 217 |
} |
| 218 |
in |
| 219 |
SOME (pos, result, rightStateNum) |
| 220 |
end |
| 221 |
| NONE => NONE) |
| 222 |
| NONE => NONE |
| 223 |
|
| 224 |
and loop (pos, str, ast, stateNum) = |
| 225 |
if pos = String.size str then |
| 226 |
SOME (ast, stateNum) |
| 227 |
else |
| 228 |
case climb (pos, str, ast, altLevel, stateNum) of |
| 229 |
SOME (pos, ast, stateNum) => loop (pos, str, ast, stateNum) |
| 230 |
| NONE => NONE |
| 231 |
|
| 232 |
and parse (str, stateNum) = |
| 233 |
if String.size str > 0 then |
| 234 |
case computeAtom (0, str, stateNum) of |
| 235 |
SOME (nextPos, lhs, stateNum) => loop (nextPos, str, lhs, stateNum) |
| 236 |
| NONE => NONE |
| 237 |
else |
| 238 |
NONE |
| 239 |
end |
| 240 |
|
| 241 |
structure ToDfa = |
| 242 |
struct |
| 243 |
fun isNullable tree = |
| 244 |
case tree of |
| 245 |
CHAR_LITERAL _ => false |
| 246 |
| WILDCARD _ => false |
| 247 |
|
| 248 |
| CONCAT {l, r, ...} => isNullable l andalso isNullable r |
| 249 |
| ALTERNATION {l, r, ...} => isNullable l orelse isNullable r |
| 250 |
|
| 251 |
| ZERO_OR_ONE _ => true |
| 252 |
| ZERO_OR_MORE _ => true |
| 253 |
|
| 254 |
| ONE_OR_MORE regex => isNullable regex |
| 255 |
| GROUP regex => isNullable regex |
| 256 |
|
| 257 |
fun firstpos (tree, acc) = |
| 258 |
case tree of |
| 259 |
CHAR_LITERAL {position, ...} => position :: acc |
| 260 |
| WILDCARD i => i :: acc |
| 261 |
|
| 262 |
| CONCAT {l, r, ...} => |
| 263 |
if isNullable l then |
| 264 |
let val acc = firstpos (l, acc) |
| 265 |
in firstpos (r, acc) |
| 266 |
end |
| 267 |
else |
| 268 |
firstpos (l, acc) |
| 269 |
| ALTERNATION {l, r, ...} => |
| 270 |
let val acc = firstpos (l, acc) |
| 271 |
in firstpos (r, acc) |
| 272 |
end |
| 273 |
|
| 274 |
| ZERO_OR_ONE regex => firstpos (regex, acc) |
| 275 |
| ZERO_OR_MORE regex => firstpos (regex, acc) |
| 276 |
| ONE_OR_MORE regex => firstpos (regex, acc) |
| 277 |
| GROUP regex => firstpos (regex, acc) |
| 278 |
|
| 279 |
fun firstposWithChar (tree, acc) = |
| 280 |
case tree of |
| 281 |
CHAR_LITERAL {position, char} => |
| 282 |
{position = position, char = Char.ord char} :: acc |
| 283 |
| WILDCARD position => {position = position, char = ~1} :: acc |
| 284 |
|
| 285 |
| CONCAT {l, r, ...} => |
| 286 |
if isNullable l then |
| 287 |
let val acc = firstposWithChar (l, acc) |
| 288 |
in firstposWithChar (r, acc) |
| 289 |
end |
| 290 |
else |
| 291 |
firstposWithChar (l, acc) |
| 292 |
| ALTERNATION {l, r, ...} => |
| 293 |
let val acc = firstposWithChar (l, acc) |
| 294 |
in firstposWithChar (r, acc) |
| 295 |
end |
| 296 |
| ZERO_OR_ONE regex => firstposWithChar (regex, acc) |
| 297 |
| ZERO_OR_MORE regex => firstposWithChar (regex, acc) |
| 298 |
| ONE_OR_MORE regex => firstposWithChar (regex, acc) |
| 299 |
| GROUP regex => firstposWithChar (regex, acc) |
| 300 |
|
| 301 |
fun lastpos (tree, acc) = |
| 302 |
case tree of |
| 303 |
CHAR_LITERAL {position, ...} => position :: acc |
| 304 |
| WILDCARD i => i :: acc |
| 305 |
|
| 306 |
| CONCAT {l, r, ...} => |
| 307 |
if isNullable r then |
| 308 |
let val acc = lastpos (l, acc) |
| 309 |
in lastpos (r, acc) |
| 310 |
end |
| 311 |
else |
| 312 |
lastpos (l, acc) |
| 313 |
| ALTERNATION {l, r, ...} => |
| 314 |
let val acc = lastpos (l, acc) |
| 315 |
in lastpos (r, acc) |
| 316 |
end |
| 317 |
|
| 318 |
| ZERO_OR_ONE regex => lastpos (regex, acc) |
| 319 |
| ZERO_OR_MORE regex => lastpos (regex, acc) |
| 320 |
| ONE_OR_MORE regex => lastpos (regex, acc) |
| 321 |
| GROUP regex => lastpos (regex, acc) |
| 322 |
|
| 323 |
fun followpos (char, regex, acc) = |
| 324 |
case regex of |
| 325 |
CONCAT {r, ...} => firstposWithChar (r, acc) |
| 326 |
| ZERO_OR_MORE r => firstposWithChar (r, acc) |
| 327 |
| ZERO_OR_ONE r => firstposWithChar (r, acc) |
| 328 |
| ONE_OR_MORE r => firstposWithChar (r, acc) |
| 329 |
| _ => acc |
| 330 |
|
| 331 |
fun insertIntsFromList (lst, acc) = |
| 332 |
case lst of |
| 333 |
{position, char} :: tl => |
| 334 |
let val acc = Set.insertOrReplace (position, char, acc) |
| 335 |
in insertIntsFromList (tl, acc) |
| 336 |
end |
| 337 |
| [] => acc |
| 338 |
|
| 339 |
(* for help finding followpos of a particular node. |
| 340 |
* Get list of concat and loop nodes to pos. |
| 341 |
* Direct ancestor is at front of list, and furthest ancestor |
| 342 |
* is at end of list. |
| 343 |
* We are filtering until first concat because the concat node |
| 344 |
* represents the first transition, from which the pos can exit |
| 345 |
* its current state. |
| 346 |
* *) |
| 347 |
fun filterUntilFirstConcat (lst: regex list, acc, char: int) = |
| 348 |
case lst of |
| 349 |
hd :: tl => |
| 350 |
(case hd of |
| 351 |
CONCAT _ => |
| 352 |
let val fp = followpos (char, hd, []) |
| 353 |
in insertIntsFromList (fp, acc) |
| 354 |
end |
| 355 |
| ZERO_OR_ONE _ => |
| 356 |
let |
| 357 |
val fp = followpos (char, hd, []) |
| 358 |
val acc = insertIntsFromList (fp, acc) |
| 359 |
in |
| 360 |
filterUntilFirstConcat (tl, acc, char) |
| 361 |
end |
| 362 |
| ZERO_OR_MORE _ => |
| 363 |
let |
| 364 |
val fp = followpos (char, hd, []) |
| 365 |
val acc = insertIntsFromList (fp, acc) |
| 366 |
in |
| 367 |
filterUntilFirstConcat (tl, acc, char) |
| 368 |
end |
| 369 |
| ONE_OR_MORE _ => |
| 370 |
let |
| 371 |
val fp = followpos (char, hd, []) |
| 372 |
val acc = insertIntsFromList (fp, acc) |
| 373 |
in |
| 374 |
filterUntilFirstConcat (tl, acc, char) |
| 375 |
end |
| 376 |
| _ => |
| 377 |
raise Fail |
| 378 |
"dfa-gen.sml 310: should only have loops and concats \ |
| 379 |
\in list to filter") |
| 380 |
| [] => acc |
| 381 |
|
| 382 |
fun getConcatAndLoopsToPos (tree: regex, pos: int, acc: regex list, char) = |
| 383 |
case tree of |
| 384 |
CONCAT {l, r, leftMaxState, rightMaxState} => |
| 385 |
if pos <= leftMaxState then |
| 386 |
getConcatAndLoopsToPos (l, pos, tree :: acc, char) |
| 387 |
else |
| 388 |
getConcatAndLoopsToPos (r, pos, tree :: acc, char) |
| 389 |
| ZERO_OR_ONE r => getConcatAndLoopsToPos (r, pos, tree :: acc, char) |
| 390 |
| ZERO_OR_MORE r => getConcatAndLoopsToPos (r, pos, tree :: acc, char) |
| 391 |
| ONE_OR_MORE r => getConcatAndLoopsToPos (r, pos, tree :: acc, char) |
| 392 |
|
| 393 |
| ALTERNATION {l, r, leftMaxState, rightMaxState} => |
| 394 |
if pos <= leftMaxState then getConcatAndLoopsToPos (l, pos, acc, char) |
| 395 |
else getConcatAndLoopsToPos (r, pos, acc, char) |
| 396 |
| CHAR_LITERAL _ => acc |
| 397 |
| WILDCARD _ => acc |
| 398 |
| GROUP r => getConcatAndLoopsToPos (r, pos, acc, char) |
| 399 |
|
| 400 |
fun ifStatesInVec (pos, dstates, newStates) = |
| 401 |
if pos = Vector.length dstates then |
| 402 |
false |
| 403 |
else |
| 404 |
let |
| 405 |
val {transitions, marked = _} = Vector.sub (dstates, pos) |
| 406 |
in |
| 407 |
transitions = newStates |
| 408 |
orelse ifStatesInVec (pos + 1, dstates, newStates) |
| 409 |
end |
| 410 |
|
| 411 |
fun getUnmarkedTransitionsIfExists (pos, dstates) = |
| 412 |
if pos = Vector.length dstates then |
| 413 |
NONE |
| 414 |
else |
| 415 |
let |
| 416 |
val record = Vector.sub (dstates, pos) |
| 417 |
in |
| 418 |
if #marked record then |
| 419 |
getUnmarkedTransitionsIfExists (pos + 1, dstates) |
| 420 |
else |
| 421 |
SOME (pos, #transitions record) |
| 422 |
end |
| 423 |
|
| 424 |
fun convertLoop (regex, dstates, dtran) = |
| 425 |
case getUnmarkedTransitionsIfExists (0, dstates) of |
| 426 |
SOME (unmarkedIdx, unamarkedTransition) => |
| 427 |
let |
| 428 |
(* mark transition *) |
| 429 |
val dstates = |
| 430 |
let |
| 431 |
val newMark = {marked = true, transitions = unamarkedTransition} |
| 432 |
in |
| 433 |
Vector.update (dstates, unmarkedIdx, newMark) |
| 434 |
end |
| 435 |
|
| 436 |
(* get follow transitions *) |
| 437 |
val nodes = |
| 438 |
List.map |
| 439 |
(fn {char, position} => |
| 440 |
let |
| 441 |
val node = |
| 442 |
getConcatAndLoopsToPos (regex, position, [], char) |
| 443 |
in |
| 444 |
{node = node, char = char} |
| 445 |
end) unamarkedTransition |
| 446 |
|
| 447 |
val follows = |
| 448 |
List.foldl |
| 449 |
(fn ({node, char}, set) => |
| 450 |
let |
| 451 |
val subset = Set.getOrDefault (char, set, Set.LEAF) |
| 452 |
val subset = filterUntilFirstConcat (node, subset, char) |
| 453 |
in |
| 454 |
Set.insertOrReplace (char, subset, set) |
| 455 |
end) Set.LEAF nodes |
| 456 |
|
| 457 |
(* add any new states we find to dstates |
| 458 |
* and add mark this state as the transition *) |
| 459 |
val (dstates, dtran) = Set.foldl |
| 460 |
( fn (subtree, (dstates, dtran)) => |
| 461 |
let |
| 462 |
val subtreeStates = Set.toCharsAndPositionsList subtree |
| 463 |
in |
| 464 |
if ifStatesInVec (0, dstates, subtreeStates) then |
| 465 |
( dstates |
| 466 |
, Vector.update (dtran, unmarkedIdx, subtreeStates) |
| 467 |
) |
| 468 |
else |
| 469 |
let |
| 470 |
val dtran = Vector.concat |
| 471 |
[dtran, Vector.fromList [subtreeStates]] |
| 472 |
|
| 473 |
val record = |
| 474 |
{marked = false, transitions = subtreeStates} |
| 475 |
val dstates = Vector.concat |
| 476 |
[dstates, Vector.fromList [record]] |
| 477 |
in |
| 478 |
(dstates, dtran) |
| 479 |
end |
| 480 |
end |
| 481 |
, follows |
| 482 |
, (dstates, dtran) |
| 483 |
) |
| 484 |
in |
| 485 |
convertLoop (regex, dstates, dtran) |
| 486 |
end |
| 487 |
| NONE => (Vector.map #transitions dstates, dtran) |
| 488 |
|
| 489 |
fun convert regex = |
| 490 |
let |
| 491 |
val first = List.rev (firstposWithChar (regex, [])) |
| 492 |
val dstates = Vector.fromList [{transitions = first, marked = false}] |
| 493 |
in |
| 494 |
convertLoop (regex, dstates, Vector.fromList []) |
| 495 |
end |
| 496 |
end |
| 497 |
|
| 498 |
fun fromString str = |
| 499 |
case ParseDfa.parse (str, 0) of |
| 500 |
SOME (ast, _) => ToDfa.convert ast |
| 501 |
| NONE => (Vector.fromList [], Vector.fromList []) |
| 502 |
end |
| 503 |
|
| 504 |
val dfa = DfaGen.fromString "(a|b)*abb#" |