begin attempt to compute followpos properly

This commit is contained in:
2025-10-10 04:44:18 +01:00
parent bdfca17b5a
commit 244d0ce26d

View File

@@ -814,112 +814,49 @@ struct
Vector.tabulate (256, fn i => Set.getOrDefault (i, set, ~1))) Vector.tabulate (256, fn i => Set.getOrDefault (i, set, ~1)))
dtran dtran
fun addFollowsToPos (tree, pos, addList) = fun addKeysToFollowSet (lst, addSet, followSet) =
case lst of
hd :: tl =>
let
val currentFollows = Set.getOrDefault (hd, followSet, [])
val updatedFollows = Set.addFromList (currentFollows, addSet)
val updatedFollows = Set.keysToList updatedFollows
val followSet = Set.insertOrReplace (hd, updatedFollows, followSet)
in
addKeysToFollowSet (tl, addSet, followSet)
end
| [] => followSet
fun addToFollowSet (tree, followSet) =
case tree of case tree of
CHAR_LITERAL {char, position, follows} => WILDCARD _ => followSet
| CHAR_LITERAL _ => followSet
| IS_ANY_CHARACTER _ => followSet
| NOT_ANY_CHARACTER _ => followSet
| CONCAT {l, r, ...} =>
let let
val set = Set.addFromList (Set.LEAF, follows) val followSet = addToFollowSet (l, followSet)
val set = Set.addFromList (set, addList) val followSet = addToFollowSet (r, followSet)
val newList = Set.keysToList set
val lp = lastpos (l, [])
val fp = firstpos (r, [])
val fp = Set.addFromList (fp, Set.LEAF)
in in
CHAR_LITERAL {char = char, position = position, follows = newList} addKeysToFollowSet (lp, fp, followSet)
end end
| WILDCARD {position, follows} => | ALTERNATION {l, r, ...} =>
let let val followSet = addToFollowSet (l, followSet)
val set = Set.addFromList (Set.LEAF, follows) in addToFollowSet (r, followSet)
val set = Set.addFromList (set, addList)
val newList = Set.keysToList set
in
WILDCARD {position = position, follows = newList}
end
| IS_ANY_CHARACTER {chars, position, follows} =>
let
val set = Set.addFromList (Set.LEAF, follows)
val set = Set.addFromList (set, addList)
val newList = Set.keysToList set
in
IS_ANY_CHARACTER
{chars = chars, position = position, follows = newList}
end
| NOT_ANY_CHARACTER {chars, position, follows} =>
let
val set = Set.addFromList (Set.LEAF, follows)
val set = Set.addFromList (set, addList)
val newList = Set.keysToList set
in
NOT_ANY_CHARACTER
{chars = chars, position = position, follows = newList}
end
| CONCAT {l, r, leftMaxState, rightMaxState, firstpos, lastpos} =>
if pos > leftMaxState then
let
val r = addFollowsToPos (r, pos, addList)
in
CONCAT
{ l = l
, r = r
, leftMaxState = leftMaxState
, rightMaxState = rightMaxState
, firstpos = firstpos
, lastpos = lastpos
}
end
else
let
val l = addFollowsToPos (l, pos, addList)
in
CONCAT
{ l = l
, r = r
, leftMaxState = leftMaxState
, rightMaxState = rightMaxState
, firstpos = firstpos
, lastpos = lastpos
}
end
| ALTERNATION {l, r, leftMaxState, rightMaxState, firstpos, lastpos} =>
if pos > leftMaxState then
let
val r = addFollowsToPos (r, pos, addList)
in
ALTERNATION
{ l = l
, r = r
, leftMaxState = leftMaxState
, rightMaxState = rightMaxState
, firstpos = firstpos
, lastpos = lastpos
}
end
else
let
val l = addFollowsToPos (l, pos, addList)
in
ALTERNATION
{ l = l
, r = r
, leftMaxState = leftMaxState
, rightMaxState = rightMaxState
, firstpos = firstpos
, lastpos = lastpos
}
end
| ZERO_OR_ONE child =>
let val child = addFollowsToPos (child, pos, addList)
in ZERO_OR_ONE child
end end
| ZERO_OR_MORE child => | ZERO_OR_MORE child =>
let val child = addFollowsToPos (child, pos, addList) let
in ZERO_OR_MORE child val lp = lastpos (child, [])
end val fp = firstpos (child, [])
| ONE_OR_MORE child => val fp = Set.addToFollowSet (fp, Set.LEAF)
let val child = addFollowsToPos (child, pos, addList) in
in ONE_OR_MORE child addToFollowSet (lp, fp, followSet)
end
| GROUP child =>
let val child = addFollowsToPos (child, pos, addList)
in GROUP child
end end
| _ => raise Fail "dfa-gen.sml 816: unimplemented"
fun convert (regex, numStates) = fun convert (regex, numStates) =
let let