| 1 |
(* lex-gen.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2005 |
| 4 |
* John Reppy (http://www.cs.uchicago.edu/~jhr) |
| 5 |
* Aaron Turon ([email protected]) |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* DFA generation using RE derivatives |
| 9 |
*) |
| 10 |
|
| 11 |
structure LexGen : |
| 12 |
sig |
| 13 |
|
| 14 |
val gen : LexSpec.spec -> LexOutputSpec.spec |
| 15 |
|
| 16 |
end = struct |
| 17 |
|
| 18 |
structure RE = RegExp |
| 19 |
structure SIS = RegExp.SymSet |
| 20 |
structure LO = LexOutputSpec |
| 21 |
|
| 22 |
structure Map = RedBlackMapFn ( |
| 23 |
struct |
| 24 |
type ord_key = RE.re Vector.vector |
| 25 |
val compare = Vector.collate RE.compare |
| 26 |
end) |
| 27 |
|
| 28 |
(* given a list of RE vectors (start states), produce a DFA recognizer |
| 29 |
* NOTE: invoked once per start state (each start state has a DFA) |
| 30 |
*) |
| 31 |
fun mkDFA startVecs = let |
| 32 |
val n = ref 0 (* next state id *) |
| 33 |
val states = ref [] |
| 34 |
(* return the state that the re vector maps to and |
| 35 |
* a flag set to true if the state is new. |
| 36 |
*) |
| 37 |
fun mkState (stateMap, res, asSS) = (case Map.find(stateMap, res) |
| 38 |
of NONE => let |
| 39 |
val id = !n |
| 40 |
fun addFinal (idx, re, finals) = |
| 41 |
if RE.nullable re |
| 42 |
then idx :: finals |
| 43 |
else finals |
| 44 |
val q = LO.State { |
| 45 |
id = id, startState = asSS, label = res, |
| 46 |
final = Vector.foldri addFinal [] res, |
| 47 |
next = ref [] |
| 48 |
} |
| 49 |
in |
| 50 |
n := id+1; |
| 51 |
states := q :: !states; |
| 52 |
(true, q, Map.insert(stateMap, res, q)) |
| 53 |
end |
| 54 |
| SOME q => (false, q, stateMap) |
| 55 |
(* end case *)) |
| 56 |
fun initIter (states, stateMap, []) = (List.rev states, stateMap) |
| 57 |
| initIter (states, stateMap, vec::vecs) = let |
| 58 |
val (_, q, stateMap') = mkState (stateMap, vec, true) |
| 59 |
in initIter (q :: states, stateMap', vecs) |
| 60 |
end |
| 61 |
val (initStates, initStatemap) = initIter ([], Map.empty, startVecs) |
| 62 |
(* val approxEdges : int ref = ref 0 |
| 63 |
val actEdges : int ref = ref 0 *) |
| 64 |
fun f (stateMap, []) = stateMap |
| 65 |
| f (stateMap, LO.State{next, label, ...}::workList) = let |
| 66 |
val firstErr = ref true |
| 67 |
fun move ((res, edge), (stateMap, workList)) = |
| 68 |
if Vector.all RE.isNone res (* if error transition *) |
| 69 |
then ( |
| 70 |
(* if !firstErr then |
| 71 |
(firstErr := false; |
| 72 |
actEdges := !actEdges + 1) |
| 73 |
else (); |
| 74 |
approxEdges := !approxEdges + 1; *) |
| 75 |
(stateMap, workList)) |
| 76 |
else let |
| 77 |
(* val _ = approxEdges := !approxEdges + 1 *) |
| 78 |
val (isNew, q, stateMap) = mkState (stateMap, res, false) |
| 79 |
fun findOrReplEdge [] = ( |
| 80 |
(* actEdges := !actEdges + 1; *) |
| 81 |
[(edge, q)]) |
| 82 |
| findOrReplEdge ((e, p)::es) = |
| 83 |
if LO.sameState (p, q) then |
| 84 |
(SIS.union (e, edge), p) :: es |
| 85 |
else (e, p) :: findOrReplEdge es |
| 86 |
in |
| 87 |
(* next := (edge, q) :: !next; *) |
| 88 |
next := findOrReplEdge (!next); |
| 89 |
if isNew |
| 90 |
then (stateMap, q::workList) |
| 91 |
else (stateMap, workList) |
| 92 |
end |
| 93 |
val edges = RE.derivatives label |
| 94 |
in |
| 95 |
f (List.foldl move (stateMap, workList) edges) |
| 96 |
end |
| 97 |
in |
| 98 |
ignore (f (initStatemap, initStates)); |
| 99 |
(* print (String.concat [ |
| 100 |
" -- Approximate edges: ", Int.toString (!approxEdges), "\n", |
| 101 |
" -- Actual edges: ", Int.toString (!actEdges), "\n"]); *) |
| 102 |
(initStates, List.rev(!states), !n) |
| 103 |
end |
| 104 |
|
| 105 |
(* clamp a machine to the right character set *) |
| 106 |
(* unused |
| 107 |
fun clamp clampTo states = let |
| 108 |
val ascii127 = SIS.interval (0w0, 0w127) |
| 109 |
fun clampTrans (edge, q) = |
| 110 |
(SIS.intersect (ascii127, edge), q) |
| 111 |
fun clampState (LO.State{next, ...}) = |
| 112 |
next := List.map clampTrans (!next) |
| 113 |
in |
| 114 |
(List.app clampState states; |
| 115 |
states) |
| 116 |
end |
| 117 |
*) |
| 118 |
|
| 119 |
fun minimize (initStates, states, numStates) = let |
| 120 |
val statesVec = Vector.fromList states |
| 121 |
val marked = Array2.array (numStates, numStates, false) |
| 122 |
fun isMarked (i, j) = if i < j then Array2.sub (marked, i, j) |
| 123 |
else Array2.sub (marked, j, i) |
| 124 |
fun mark (i, j) = if i < j then Array2.update (marked, i, j, true) |
| 125 |
else Array2.update (marked, j, i, true) |
| 126 |
fun appAll f = let |
| 127 |
fun iter i = if i < numStates then (f i; iter (i+1)) |
| 128 |
else () |
| 129 |
in iter 0 end |
| 130 |
fun appAll2 f = |
| 131 |
appAll (fn i => |
| 132 |
appAll (fn j => |
| 133 |
if i < j then f (i, j) |
| 134 |
else ())) |
| 135 |
fun smallestUnmarked j = let |
| 136 |
fun iter i = |
| 137 |
if i < j then |
| 138 |
if not (isMarked (i, j)) then i |
| 139 |
else iter (i+1) |
| 140 |
else j |
| 141 |
in iter 0 end |
| 142 |
fun markAllIf (i, f) = appAll (fn j => |
| 143 |
if f (Vector.sub (statesVec, j)) |
| 144 |
then mark (i, j) |
| 145 |
else ()) |
| 146 |
fun markAll i = markAllIf (i, fn _ => true) |
| 147 |
fun idOf (LO.State {id, ...}) = id |
| 148 |
fun stateOf id = Vector.sub (statesVec, id) |
| 149 |
fun iter() = let |
| 150 |
val changed = ref false |
| 151 |
fun diffEdge (_, []) = false |
| 152 |
| diffEdge ((set1, state1), (set2, state2)::edges) = |
| 153 |
if SIS.isEmpty (SIS.intersect (set1, set2)) |
| 154 |
then diffEdge ((set1, state1), edges) |
| 155 |
else |
| 156 |
if isMarked (idOf state1, idOf state2) |
| 157 |
then true |
| 158 |
else diffEdge ((set1, state1), edges) |
| 159 |
fun getEdgesAndLabels (i) = let |
| 160 |
val LO.State {next, ...} = Vector.sub (statesVec, i) |
| 161 |
val allLabels = foldl SIS.union SIS.empty |
| 162 |
(map (fn (set, _) => set) (!next)) |
| 163 |
in |
| 164 |
(!next, allLabels) |
| 165 |
end |
| 166 |
fun check (i, j) = |
| 167 |
if isMarked (i, j) then () |
| 168 |
else let |
| 169 |
val (edgesi, labelsi) = getEdgesAndLabels i |
| 170 |
val (edgesj, labelsj) = getEdgesAndLabels j |
| 171 |
fun tryEdge edge = diffEdge (edge, edgesj) |
| 172 |
in |
| 173 |
case SIS.compare (labelsi, labelsj) |
| 174 |
of EQUAL => |
| 175 |
if List.exists tryEdge edgesi |
| 176 |
then (mark (i, j); changed := true) |
| 177 |
else () |
| 178 |
| _ => (mark (i, j); changed := true) |
| 179 |
end |
| 180 |
in |
| 181 |
appAll2 check; |
| 182 |
if !changed then iter() else () |
| 183 |
end |
| 184 |
fun merge() = let |
| 185 |
fun move (sym, s) = (sym, (stateOf o smallestUnmarked o idOf) s) |
| 186 |
fun upd (LO.State {next, ...}) = next := (map move (!next)) |
| 187 |
fun keep s = (smallestUnmarked (idOf s) = idOf s) |
| 188 |
val states' = List.filter keep states |
| 189 |
in |
| 190 |
app upd states'; |
| 191 |
(initStates, states', List.length states') |
| 192 |
end |
| 193 |
fun isInit s1 = List.exists (fn s2 => idOf s1 = idOf s2) initStates |
| 194 |
fun isFinal (LO.State {final = [], ...}) = false |
| 195 |
| isFinal _ = true |
| 196 |
fun sameFinal (LO.State {final = f1, ...}) |
| 197 |
(LO.State {final = f2, ...}) = |
| 198 |
ListPair.allEq op= (f1, f2) |
| 199 |
handle ListPair.UnequalLengths => false |
| 200 |
in |
| 201 |
app (fn s => markAllIf (idOf s, isInit)) |
| 202 |
initStates; |
| 203 |
app (fn s => markAllIf (idOf s, not o (sameFinal s))) |
| 204 |
states; |
| 205 |
iter(); |
| 206 |
merge() |
| 207 |
end |
| 208 |
|
| 209 |
fun gen spec = let |
| 210 |
(* TODO: check for invalid start states on rules *) |
| 211 |
val LexSpec.Spec {decls, conf, rules, eofRules} = spec |
| 212 |
val LexSpec.Conf {structName, header, |
| 213 |
arg, startStates, ...} = conf |
| 214 |
val startStates' = AtomSet.add (startStates, Atom.atom "INITIAL") |
| 215 |
(* |
| 216 |
(* split out actions and associate each ruleSpec to an action ID |
| 217 |
* |
| 218 |
* Note: matchActions tries to find textually idential actions and map |
| 219 |
* them to the same entry in the action vector |
| 220 |
*) |
| 221 |
fun matchActions rules = let |
| 222 |
fun iter ((ruleSpec, action)::rules, |
| 223 |
ruleSpecs, actions, actionMap, n) = let |
| 224 |
val key = Atom.atom action |
| 225 |
val (i, actions', actionMap', n') = |
| 226 |
case AtomMap.find (actionMap, key) |
| 227 |
of NONE => (n, action::actions, |
| 228 |
AtomMap.insert (actionMap, key, n), |
| 229 |
n+1) |
| 230 |
| SOME i => (i, actions, actionMap, n) |
| 231 |
in |
| 232 |
iter (rules, (i, ruleSpec)::ruleSpecs, |
| 233 |
actions', actionMap', n') |
| 234 |
end |
| 235 |
| iter ([], ruleSpecs, actions, _, _) = |
| 236 |
(List.rev ruleSpecs, List.rev actions) |
| 237 |
in |
| 238 |
iter (rules, [], [], AtomMap.empty, 0) |
| 239 |
end |
| 240 |
val (ruleSpecs, actions) = matchActions rules |
| 241 |
*) |
| 242 |
val (ruleSpecs, actions) = ListPair.unzip rules |
| 243 |
val actionsVec = Vector.fromList actions |
| 244 |
val startStates = AtomSet.listItems startStates' |
| 245 |
fun SSVec label = let |
| 246 |
fun hasRule (NONE, re) = re |
| 247 |
| hasRule (SOME ss, re) = |
| 248 |
if AtomSet.member (ss, label) |
| 249 |
then re |
| 250 |
else RegExp.none |
| 251 |
val rules = List.map hasRule ruleSpecs |
| 252 |
in Vector.fromList rules |
| 253 |
end |
| 254 |
val (initStates, states, numStates) = |
| 255 |
mkDFA (List.map SSVec startStates) |
| 256 |
val (initStates, states, numStates) = |
| 257 |
if !Options.minimize then |
| 258 |
minimize (initStates, states, numStates) |
| 259 |
else (initStates, states, numStates) |
| 260 |
in LO.Spec { |
| 261 |
decls = decls, |
| 262 |
header = (case (header, structName) |
| 263 |
of ("", "") => "structure Mlex" |
| 264 |
| (hdr, "") => hdr |
| 265 |
| ("", name) => "structure "^name |
| 266 |
(* end case *)), |
| 267 |
arg = arg, |
| 268 |
actions = actionsVec, |
| 269 |
dfa = states, |
| 270 |
startStates = ListPair.zip |
| 271 |
(List.map Atom.toString startStates, |
| 272 |
initStates), |
| 273 |
eofRules = case eofRules |
| 274 |
of [] => [("_", "UserDeclarations.eof()")] |
| 275 |
| _ => eofRules |
| 276 |
} |
| 277 |
end |
| 278 |
|
| 279 |
end |