2025-09-28 22:01:44 +01:00
|
|
|
structure Nfa =
|
|
|
|
|
struct
|
|
|
|
|
datatype regex =
|
2025-10-01 11:23:41 +01:00
|
|
|
CHAR_LITERAL of {char: char, position: int}
|
2025-10-02 04:34:16 +01:00
|
|
|
| CONCAT of {l: regex, r: regex, leftMaxState: int, rightMaxState: int}
|
|
|
|
|
| ALTERNATION of {l: regex, r: regex, leftMaxState: int, rightMaxState: int}
|
2025-10-01 10:48:45 +01:00
|
|
|
| ZERO_OR_ONE of regex
|
|
|
|
|
| ZERO_OR_MORE of regex
|
|
|
|
|
| ONE_OR_MORE of regex
|
|
|
|
|
| GROUP of regex
|
2025-10-01 11:23:41 +01:00
|
|
|
| WILDCARD of int
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-10-02 04:34:16 +01:00
|
|
|
structure Set =
|
|
|
|
|
struct
|
2025-10-02 13:54:59 +01:00
|
|
|
datatype 'a set = BRANCH of 'a set * int * 'a * 'a set | LEAF
|
2025-10-02 04:34:16 +01:00
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun insertOrReplace (newKey, newVal, tree) =
|
2025-10-02 04:34:16 +01:00
|
|
|
case tree of
|
2025-10-02 13:54:59 +01:00
|
|
|
BRANCH (l, curKey, curVal, r) =>
|
2025-10-02 04:34:16 +01:00
|
|
|
if newKey > curKey then
|
2025-10-02 13:54:59 +01:00
|
|
|
let val r = insertOrReplace (newKey, newVal, r)
|
|
|
|
|
in BRANCH (l, curKey, curVal, r)
|
2025-10-02 04:34:16 +01:00
|
|
|
end
|
|
|
|
|
else if newKey < curKey then
|
2025-10-02 13:54:59 +01:00
|
|
|
let val l = insertOrReplace (newKey, newVal, l)
|
|
|
|
|
in BRANCH (l, curKey, curVal, r)
|
2025-10-02 04:34:16 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-10-02 13:54:59 +01:00
|
|
|
BRANCH (l, newKey, newVal, r)
|
|
|
|
|
| LEAF => BRANCH (LEAF, newKey, newVal, LEAF)
|
2025-10-02 04:34:16 +01:00
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun getOrDefault (findKey, tree, default) =
|
2025-10-02 04:34:16 +01:00
|
|
|
case tree of
|
2025-10-02 13:54:59 +01:00
|
|
|
BRANCH (l, curKey, curVal, r) =>
|
|
|
|
|
if findKey > curKey then getOrDefault (findKey, r, default)
|
|
|
|
|
else if findKey < curKey then getOrDefault (findKey, l, default)
|
|
|
|
|
else curVal
|
|
|
|
|
| LEAF => default
|
|
|
|
|
|
|
|
|
|
fun helpToList (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, curKey, curVal, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = helpToList (r, acc)
|
|
|
|
|
val acc = (curKey, curVal) :: acc
|
|
|
|
|
in
|
|
|
|
|
helpToList (l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun toList tree = helpToList (tree, [])
|
|
|
|
|
|
|
|
|
|
fun helpKeysToList (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, curKey, _, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = helpKeysToList (r, acc)
|
|
|
|
|
val acc = curKey :: acc
|
|
|
|
|
in
|
|
|
|
|
helpKeysToList (l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun keysToList tree = helpKeysToList (tree, [])
|
|
|
|
|
|
|
|
|
|
fun map (f, tree) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, key, value, r) =>
|
|
|
|
|
let
|
|
|
|
|
val r = map (f, r)
|
|
|
|
|
val l = map (f, l)
|
|
|
|
|
val value = f value
|
|
|
|
|
in
|
|
|
|
|
BRANCH (l, key, value, r)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => LEAF
|
2025-10-03 05:17:13 +01:00
|
|
|
|
|
|
|
|
fun foldl (f, tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, k, v, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = foldl (f, l, acc)
|
|
|
|
|
val acc = f (v, acc)
|
|
|
|
|
in
|
|
|
|
|
foldl (f, r, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun helpToCharsAndPositionsList (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
BRANCH (l, k, v, r) =>
|
|
|
|
|
let
|
|
|
|
|
val acc = helpToCharsAndPositionsList (r, acc)
|
|
|
|
|
val acc = {char = v, position = k} :: acc
|
|
|
|
|
in
|
|
|
|
|
helpToCharsAndPositionsList (l, acc)
|
|
|
|
|
end
|
|
|
|
|
| LEAF => acc
|
|
|
|
|
|
|
|
|
|
fun toCharsAndPositionsList tree = helpToCharsAndPositionsList (tree, [])
|
2025-10-02 04:34:16 +01:00
|
|
|
end
|
|
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
structure ParseNfa =
|
|
|
|
|
struct
|
2025-09-29 13:34:55 +01:00
|
|
|
(* parsing through precedence climbing algorithm. *)
|
2025-10-01 13:48:18 +01:00
|
|
|
val postfixLevel = 1
|
|
|
|
|
val concatLevel = 2
|
|
|
|
|
val altLevel = 3
|
2025-09-29 01:06:15 +01:00
|
|
|
|
2025-09-29 08:33:10 +01:00
|
|
|
local
|
|
|
|
|
fun loop (pos, str, openParens, closeParens) =
|
|
|
|
|
if pos = String.size str then
|
2025-09-29 13:34:55 +01:00
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
|
|
|
|
case String.sub (str, pos) of
|
|
|
|
|
#"(" => loop (pos + 1, str, openParens + 1, closeParens)
|
|
|
|
|
| #")" =>
|
2025-09-29 13:34:55 +01:00
|
|
|
if closeParens + 1 = openParens then SOME pos
|
2025-09-29 08:33:10 +01:00
|
|
|
else loop (pos + 1, str, openParens, closeParens + 1)
|
|
|
|
|
| _ => loop (pos + 1, str, openParens, closeParens)
|
|
|
|
|
in
|
|
|
|
|
fun getRightParenIdx (pos, str) = loop (pos, str, 1, 0)
|
|
|
|
|
end
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-10-01 13:48:18 +01:00
|
|
|
fun computeAtom (pos, str, stateNum) =
|
|
|
|
|
if pos = String.size str then
|
|
|
|
|
NONE
|
|
|
|
|
else
|
|
|
|
|
case String.sub (str, pos) of
|
|
|
|
|
#"(" =>
|
|
|
|
|
(case getRightParenIdx (pos + 1, str) of
|
|
|
|
|
SOME groupEndIdx =>
|
|
|
|
|
let
|
|
|
|
|
val substr = String.substring
|
|
|
|
|
(str, pos + 1, groupEndIdx - pos - 1)
|
|
|
|
|
in
|
|
|
|
|
case parse (substr, stateNum) of
|
|
|
|
|
SOME (rhs, stateNum) =>
|
|
|
|
|
SOME (groupEndIdx + 1, rhs, stateNum)
|
|
|
|
|
| NONE => NONE
|
|
|
|
|
end
|
|
|
|
|
| NONE => NONE)
|
|
|
|
|
| #")" => NONE
|
|
|
|
|
| #"?" => NONE
|
|
|
|
|
| #"*" => NONE
|
|
|
|
|
| #"+" => NONE
|
|
|
|
|
| #"." => SOME (pos + 1, WILDCARD (stateNum + 1), stateNum + 1)
|
|
|
|
|
| chr =>
|
|
|
|
|
let val chr = CHAR_LITERAL {char = chr, position = stateNum + 1}
|
|
|
|
|
in SOME (pos + 1, chr, stateNum + 1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
and climb (pos, str, lhs, level, stateNum) : (int * regex * int) option =
|
2025-09-29 00:46:05 +01:00
|
|
|
if pos = String.size str then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 00:46:05 +01:00
|
|
|
else
|
2025-09-29 08:33:10 +01:00
|
|
|
case String.sub (str, pos) of
|
2025-10-01 13:48:18 +01:00
|
|
|
#"|" =>
|
2025-09-29 08:33:10 +01:00
|
|
|
if level < altLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 13:34:55 +01:00
|
|
|
else if pos + 1 < String.size str then
|
2025-09-29 08:33:10 +01:00
|
|
|
let
|
|
|
|
|
val chr = String.sub (str, pos + 1)
|
2025-10-01 11:23:41 +01:00
|
|
|
val chr = CHAR_LITERAL {char = chr, position = stateNum + 1}
|
2025-09-29 08:33:10 +01:00
|
|
|
in
|
2025-10-01 11:23:41 +01:00
|
|
|
case climb (pos + 2, str, chr, altLevel, stateNum + 1) of
|
2025-10-02 04:34:16 +01:00
|
|
|
SOME (pos, rhs, rightStateNum) =>
|
|
|
|
|
let
|
|
|
|
|
val result = ALTERNATION
|
|
|
|
|
{ l = lhs
|
|
|
|
|
, r = rhs
|
|
|
|
|
, leftMaxState = stateNum
|
|
|
|
|
, rightMaxState = rightStateNum
|
|
|
|
|
}
|
|
|
|
|
in
|
|
|
|
|
SOME (pos, result, rightStateNum)
|
2025-09-29 13:34:55 +01:00
|
|
|
end
|
|
|
|
|
| NONE => NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-29 13:34:55 +01:00
|
|
|
else
|
|
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
| #"?" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 10:48:45 +01:00
|
|
|
let val lhs = ZERO_OR_ONE lhs
|
2025-10-01 11:23:41 +01:00
|
|
|
in climb (pos + 1, str, lhs, postfixLevel, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
|
|
|
|
| #"*" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 10:48:45 +01:00
|
|
|
let val lhs = ZERO_OR_MORE lhs
|
2025-10-01 11:23:41 +01:00
|
|
|
in climb (pos + 1, str, lhs, postfixLevel, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
|
|
|
|
| #"+" =>
|
|
|
|
|
if level < postfixLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 10:48:45 +01:00
|
|
|
let val lhs = ONE_OR_MORE lhs
|
2025-10-01 11:23:41 +01:00
|
|
|
in climb (pos + 1, str, lhs, postfixLevel, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
|
|
|
|
| chr =>
|
|
|
|
|
if level < concatLevel then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (pos, lhs, stateNum)
|
2025-09-29 08:33:10 +01:00
|
|
|
else
|
2025-10-01 13:48:18 +01:00
|
|
|
case computeAtom (pos, str, stateNum) of
|
2025-10-02 04:34:16 +01:00
|
|
|
SOME (nextPos, curAtom, atomStateNum) =>
|
|
|
|
|
(case climb (nextPos, str, curAtom, concatLevel, atomStateNum) of
|
|
|
|
|
SOME (pos, rhs, rightStateNum) =>
|
|
|
|
|
let
|
|
|
|
|
val result = CONCAT
|
|
|
|
|
{ l = lhs
|
|
|
|
|
, r = rhs
|
|
|
|
|
, leftMaxState = stateNum
|
|
|
|
|
, rightMaxState = rightStateNum
|
|
|
|
|
}
|
|
|
|
|
in
|
|
|
|
|
SOME (pos, result, rightStateNum)
|
2025-10-01 13:48:18 +01:00
|
|
|
end
|
|
|
|
|
| NONE => NONE)
|
|
|
|
|
| NONE => NONE
|
2025-09-29 00:46:05 +01:00
|
|
|
|
2025-10-01 11:23:41 +01:00
|
|
|
and loop (pos, str, ast, stateNum) =
|
2025-09-28 22:01:44 +01:00
|
|
|
if pos = String.size str then
|
2025-10-01 11:23:41 +01:00
|
|
|
SOME (ast, stateNum)
|
2025-09-28 22:01:44 +01:00
|
|
|
else
|
2025-10-01 11:23:41 +01:00
|
|
|
case climb (pos, str, ast, altLevel, stateNum) of
|
|
|
|
|
SOME (pos, ast, stateNum) => loop (pos, str, ast, stateNum)
|
2025-09-29 13:34:55 +01:00
|
|
|
| NONE => NONE
|
2025-09-28 22:01:44 +01:00
|
|
|
|
2025-10-01 11:23:41 +01:00
|
|
|
and parse (str, stateNum) =
|
2025-09-29 13:34:55 +01:00
|
|
|
if String.size str > 0 then
|
2025-10-01 13:48:18 +01:00
|
|
|
case computeAtom (0, str, stateNum) of
|
|
|
|
|
SOME (nextPos, lhs, stateNum) => loop (nextPos, str, lhs, stateNum)
|
|
|
|
|
| NONE => NONE
|
2025-09-29 13:34:55 +01:00
|
|
|
else
|
|
|
|
|
NONE
|
2025-09-29 08:33:10 +01:00
|
|
|
end
|
2025-09-28 22:01:44 +01:00
|
|
|
|
2025-10-01 11:52:45 +01:00
|
|
|
structure ToDfa =
|
|
|
|
|
struct
|
|
|
|
|
fun isNullable tree =
|
|
|
|
|
case tree of
|
2025-10-01 12:36:26 +01:00
|
|
|
CHAR_LITERAL _ => false
|
|
|
|
|
| WILDCARD _ => false
|
2025-10-01 11:52:45 +01:00
|
|
|
|
2025-10-02 04:34:16 +01:00
|
|
|
| CONCAT {l, r, ...} => isNullable l andalso isNullable r
|
|
|
|
|
| ALTERNATION {l, r, ...} => isNullable l orelse isNullable r
|
2025-10-01 11:52:45 +01:00
|
|
|
|
2025-10-01 12:36:26 +01:00
|
|
|
| ZERO_OR_ONE _ => true
|
|
|
|
|
| ZERO_OR_MORE _ => true
|
2025-10-01 11:52:45 +01:00
|
|
|
|
2025-10-01 12:36:26 +01:00
|
|
|
| ONE_OR_MORE regex => isNullable regex
|
|
|
|
|
| GROUP regex => isNullable regex
|
2025-10-01 11:52:45 +01:00
|
|
|
|
2025-10-01 12:36:26 +01:00
|
|
|
fun firstpos (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
CHAR_LITERAL {position, ...} => position :: acc
|
|
|
|
|
| WILDCARD i => i :: acc
|
|
|
|
|
|
2025-10-02 04:34:16 +01:00
|
|
|
| CONCAT {l, r, ...} =>
|
|
|
|
|
if isNullable l then
|
|
|
|
|
let val acc = firstpos (l, acc)
|
|
|
|
|
in firstpos (r, acc)
|
2025-10-01 12:36:26 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-10-02 04:34:16 +01:00
|
|
|
firstpos (l, acc)
|
|
|
|
|
| ALTERNATION {l, r, ...} =>
|
|
|
|
|
let val acc = firstpos (l, acc)
|
|
|
|
|
in firstpos (r, acc)
|
2025-10-01 12:36:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
| ZERO_OR_ONE regex => firstpos (regex, acc)
|
|
|
|
|
| ZERO_OR_MORE regex => firstpos (regex, acc)
|
|
|
|
|
| ONE_OR_MORE regex => firstpos (regex, acc)
|
|
|
|
|
| GROUP regex => firstpos (regex, acc)
|
|
|
|
|
|
2025-10-02 05:00:23 +01:00
|
|
|
fun firstposWithChar (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
CHAR_LITERAL {position, char} =>
|
2025-10-03 05:17:13 +01:00
|
|
|
{position = position, char = Char.ord char} :: acc
|
|
|
|
|
| WILDCARD position => {position = position, char = ~1} :: acc
|
2025-10-02 05:00:23 +01:00
|
|
|
|
|
|
|
|
| CONCAT {l, r, ...} =>
|
|
|
|
|
if isNullable l then
|
|
|
|
|
let val acc = firstposWithChar (l, acc)
|
|
|
|
|
in firstposWithChar (r, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
firstposWithChar (l, acc)
|
|
|
|
|
| ALTERNATION {l, r, ...} =>
|
|
|
|
|
let val acc = firstposWithChar (l, acc)
|
|
|
|
|
in firstposWithChar (r, acc)
|
|
|
|
|
end
|
|
|
|
|
| ZERO_OR_ONE regex => firstposWithChar (regex, acc)
|
|
|
|
|
| ZERO_OR_MORE regex => firstposWithChar (regex, acc)
|
|
|
|
|
| ONE_OR_MORE regex => firstposWithChar (regex, acc)
|
|
|
|
|
| GROUP regex => firstposWithChar (regex, acc)
|
|
|
|
|
|
2025-10-01 12:36:26 +01:00
|
|
|
fun lastpos (tree, acc) =
|
|
|
|
|
case tree of
|
|
|
|
|
CHAR_LITERAL {position, ...} => position :: acc
|
|
|
|
|
| WILDCARD i => i :: acc
|
|
|
|
|
|
2025-10-02 04:34:16 +01:00
|
|
|
| CONCAT {l, r, ...} =>
|
|
|
|
|
if isNullable r then
|
|
|
|
|
let val acc = lastpos (l, acc)
|
|
|
|
|
in lastpos (r, acc)
|
2025-10-01 12:36:26 +01:00
|
|
|
end
|
|
|
|
|
else
|
2025-10-02 04:34:16 +01:00
|
|
|
lastpos (l, acc)
|
|
|
|
|
| ALTERNATION {l, r, ...} =>
|
|
|
|
|
let val acc = lastpos (l, acc)
|
|
|
|
|
in lastpos (r, acc)
|
2025-10-01 12:36:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
| ZERO_OR_ONE regex => lastpos (regex, acc)
|
|
|
|
|
| ZERO_OR_MORE regex => lastpos (regex, acc)
|
|
|
|
|
| ONE_OR_MORE regex => lastpos (regex, acc)
|
|
|
|
|
| GROUP regex => lastpos (regex, acc)
|
2025-10-01 14:10:40 +01:00
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun followpos (char, regex, acc) =
|
|
|
|
|
case regex of
|
2025-10-03 05:17:13 +01:00
|
|
|
CONCAT {r, ...} => firstposWithChar (r, acc)
|
|
|
|
|
| ZERO_OR_MORE r => firstposWithChar (r, acc)
|
|
|
|
|
| ZERO_OR_ONE r => firstposWithChar (r, acc)
|
|
|
|
|
| ONE_OR_MORE r => firstposWithChar (r, acc)
|
2025-10-02 13:54:59 +01:00
|
|
|
| _ => acc
|
|
|
|
|
|
2025-10-03 05:17:13 +01:00
|
|
|
fun insertIntsFromList (lst, acc) =
|
2025-10-02 13:54:59 +01:00
|
|
|
case lst of
|
2025-10-03 05:17:13 +01:00
|
|
|
{position, char} :: tl =>
|
|
|
|
|
let val acc = Set.insertOrReplace (position, char, acc)
|
2025-10-02 13:54:59 +01:00
|
|
|
in insertIntsFromList (tl, acc)
|
|
|
|
|
end
|
|
|
|
|
| [] => acc
|
2025-10-02 05:00:23 +01:00
|
|
|
|
|
|
|
|
(* for help finding followpos of a particular node.
|
|
|
|
|
* Get list of concat and loop nodes to pos.
|
|
|
|
|
* Direct ancestor is at front of list, and furthest ancestor
|
2025-10-02 13:54:59 +01:00
|
|
|
* is at end of list.
|
|
|
|
|
* We are filtering until first concat because the concat node
|
|
|
|
|
* represents the first transition, from which the pos can exit
|
|
|
|
|
* its current state.
|
|
|
|
|
* *)
|
2025-10-03 05:17:13 +01:00
|
|
|
fun filterUntilFirstConcat (lst: regex list, acc, char: int) =
|
2025-10-02 13:54:59 +01:00
|
|
|
case lst of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
(case hd of
|
|
|
|
|
CONCAT _ =>
|
2025-10-03 05:17:13 +01:00
|
|
|
let val fp = followpos (char, hd, [])
|
2025-10-02 13:54:59 +01:00
|
|
|
in insertIntsFromList (fp, acc)
|
|
|
|
|
end
|
|
|
|
|
| ZERO_OR_ONE _ =>
|
|
|
|
|
let
|
|
|
|
|
val fp = followpos (char, hd, [])
|
|
|
|
|
val acc = insertIntsFromList (fp, acc)
|
|
|
|
|
in
|
|
|
|
|
filterUntilFirstConcat (tl, acc, char)
|
|
|
|
|
end
|
|
|
|
|
| ZERO_OR_MORE _ =>
|
|
|
|
|
let
|
|
|
|
|
val fp = followpos (char, hd, [])
|
|
|
|
|
val acc = insertIntsFromList (fp, acc)
|
|
|
|
|
in
|
|
|
|
|
filterUntilFirstConcat (tl, acc, char)
|
|
|
|
|
end
|
|
|
|
|
| ONE_OR_MORE _ =>
|
|
|
|
|
let
|
|
|
|
|
val fp = followpos (char, hd, [])
|
|
|
|
|
val acc = insertIntsFromList (fp, acc)
|
|
|
|
|
in
|
|
|
|
|
filterUntilFirstConcat (tl, acc, char)
|
|
|
|
|
end
|
|
|
|
|
| _ =>
|
|
|
|
|
raise Fail
|
|
|
|
|
"nfa.sml 310: should only have loops and concats \
|
|
|
|
|
\in list to filter")
|
|
|
|
|
| [] => acc
|
|
|
|
|
|
|
|
|
|
fun getConcatAndLoopsToPos (tree: regex, pos: int, acc: regex list, char) =
|
2025-10-02 05:00:23 +01:00
|
|
|
case tree of
|
|
|
|
|
CONCAT {l, r, leftMaxState, rightMaxState} =>
|
|
|
|
|
if pos <= leftMaxState then
|
2025-10-02 13:54:59 +01:00
|
|
|
getConcatAndLoopsToPos (l, pos, tree :: acc, char)
|
2025-10-02 05:00:23 +01:00
|
|
|
else
|
2025-10-02 13:54:59 +01:00
|
|
|
getConcatAndLoopsToPos (r, pos, tree :: acc, char)
|
|
|
|
|
| ZERO_OR_ONE r => getConcatAndLoopsToPos (r, pos, tree :: acc, char)
|
|
|
|
|
| ZERO_OR_MORE r => getConcatAndLoopsToPos (r, pos, tree :: acc, char)
|
|
|
|
|
| ONE_OR_MORE r => getConcatAndLoopsToPos (r, pos, tree :: acc, char)
|
2025-10-02 05:00:23 +01:00
|
|
|
|
|
|
|
|
| ALTERNATION {l, r, leftMaxState, rightMaxState} =>
|
2025-10-02 13:54:59 +01:00
|
|
|
if pos <= leftMaxState then getConcatAndLoopsToPos (l, pos, acc, char)
|
|
|
|
|
else getConcatAndLoopsToPos (r, pos, acc, char)
|
2025-10-02 05:00:23 +01:00
|
|
|
| CHAR_LITERAL _ => acc
|
|
|
|
|
| WILDCARD _ => acc
|
2025-10-02 13:54:59 +01:00
|
|
|
| GROUP r => getConcatAndLoopsToPos (r, pos, acc, char)
|
|
|
|
|
|
2025-10-03 05:17:13 +01:00
|
|
|
fun statesInList (lst, newStates) =
|
|
|
|
|
case lst of
|
|
|
|
|
{marked = _, transitions} :: tl =>
|
|
|
|
|
newStates = transitions orelse statesInList (tl, newStates)
|
|
|
|
|
| [] => false
|
|
|
|
|
|
|
|
|
|
fun getUnmarkedTransitionsIfExists lst =
|
|
|
|
|
case lst of
|
|
|
|
|
{marked, transitions} :: tl =>
|
|
|
|
|
if marked then getUnmarkedTransitionsIfExists tl else SOME transitions
|
|
|
|
|
| [] => NONE
|
|
|
|
|
|
|
|
|
|
fun addListToAcc (lst, acc) =
|
|
|
|
|
case lst of
|
|
|
|
|
hd :: tl => addListToAcc (tl, hd :: acc)
|
|
|
|
|
| [] => acc
|
|
|
|
|
|
|
|
|
|
fun markTransition (lst, transitionToMark, acc) =
|
|
|
|
|
case lst of
|
|
|
|
|
(hd as {marked, transitions}) :: tl =>
|
|
|
|
|
if transitions = transitionToMark then
|
|
|
|
|
let val acc = {marked = true, transitions = transitionToMark} :: acc
|
|
|
|
|
in addListToAcc (tl, acc)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
markTransition (tl, transitionToMark, hd :: acc)
|
|
|
|
|
| [] => {marked = true, transitions = transitionToMark} :: acc
|
|
|
|
|
|
|
|
|
|
fun convertLoop (regex, dstates) =
|
|
|
|
|
case getUnmarkedTransitionsIfExists dstates of
|
|
|
|
|
SOME unamarkedTransition =>
|
|
|
|
|
let
|
|
|
|
|
val dstates = markTransition (dstates, unamarkedTransition, [])
|
|
|
|
|
|
|
|
|
|
(* get follow transitions *)
|
|
|
|
|
val nodes =
|
|
|
|
|
List.map
|
|
|
|
|
(fn {char, position} =>
|
|
|
|
|
let
|
|
|
|
|
val node =
|
|
|
|
|
getConcatAndLoopsToPos (regex, position, [], char)
|
|
|
|
|
in
|
|
|
|
|
{node = node, char = char}
|
|
|
|
|
end) unamarkedTransition
|
|
|
|
|
|
|
|
|
|
val follows =
|
|
|
|
|
List.foldl
|
|
|
|
|
(fn ({node, char}, set) =>
|
|
|
|
|
let
|
|
|
|
|
val subset = Set.getOrDefault (char, set, Set.LEAF)
|
|
|
|
|
val subset = filterUntilFirstConcat (node, subset, char)
|
|
|
|
|
in
|
|
|
|
|
Set.insertOrReplace (char, subset, set)
|
|
|
|
|
end) Set.LEAF nodes
|
|
|
|
|
|
|
|
|
|
(* add any new transitions we find *)
|
|
|
|
|
val newdstates = Set.foldl
|
|
|
|
|
( fn (subtree, acc) =>
|
|
|
|
|
let
|
|
|
|
|
val subtreeStates = Set.toCharsAndPositionsList subtree
|
|
|
|
|
in
|
|
|
|
|
if statesInList (acc, subtreeStates) then acc
|
|
|
|
|
else {marked = false, transitions = subtreeStates} :: acc
|
|
|
|
|
end
|
|
|
|
|
, follows
|
|
|
|
|
, dstates
|
|
|
|
|
)
|
|
|
|
|
in
|
|
|
|
|
convertLoop (regex, newdstates)
|
|
|
|
|
end
|
|
|
|
|
| NONE => dstates
|
|
|
|
|
|
|
|
|
|
fun convert regex =
|
2025-10-02 13:54:59 +01:00
|
|
|
let
|
2025-10-03 05:17:13 +01:00
|
|
|
val first = List.rev (firstposWithChar (regex, []))
|
|
|
|
|
val dstates = [{transitions = first, marked = false}]
|
2025-10-02 13:54:59 +01:00
|
|
|
in
|
2025-10-03 05:17:13 +01:00
|
|
|
convertLoop (regex, dstates)
|
2025-10-02 13:54:59 +01:00
|
|
|
end
|
2025-10-01 11:52:45 +01:00
|
|
|
end
|
|
|
|
|
|
2025-10-01 11:23:41 +01:00
|
|
|
fun parse str =
|
|
|
|
|
case ParseNfa.parse (str, 0) of
|
|
|
|
|
SOME (ast, _) => SOME ast
|
2025-10-01 12:36:26 +01:00
|
|
|
| NONE => NONE
|
2025-10-01 14:06:41 +01:00
|
|
|
|
2025-10-02 13:54:59 +01:00
|
|
|
fun firstposWithChar regex = ToDfa.firstposWithChar (regex, [])
|
2025-10-02 04:34:16 +01:00
|
|
|
|
2025-10-01 14:06:41 +01:00
|
|
|
fun lastpos regex = ToDfa.lastpos (regex, [])
|
2025-10-03 05:17:13 +01:00
|
|
|
val test = ToDfa.convert
|
2025-09-28 22:01:44 +01:00
|
|
|
end
|
2025-10-02 13:54:59 +01:00
|
|
|
|
|
|
|
|
val SOME nfa = Nfa.parse "(a|b)*abb"
|