| 1 |
|
| 2 |
|
| 3 |
from pypy.interpreter.pyparser.automata import DFA, DEFAULT |
| 4 |
|
| 5 |
class EMPTY: pass |
| 6 |
|
| 7 |
def newArcPair (states, transitionLabel): |
| 8 |
s1Index = len(states) |
| 9 |
s2Index = s1Index + 1 |
| 10 |
states.append([(transitionLabel, s2Index)]) |
| 11 |
states.append([]) |
| 12 |
return s1Index, s2Index |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
def chain (states, *stateIndexPairs): |
| 17 |
if len(stateIndexPairs) > 1: |
| 18 |
start, lastFinish = stateIndexPairs[0] |
| 19 |
for nStart, nFinish in stateIndexPairs[1:]: |
| 20 |
states[lastFinish].append((EMPTY, nStart)) |
| 21 |
lastFinish = nFinish |
| 22 |
return start, nFinish |
| 23 |
else: |
| 24 |
return stateIndexPairs[0] |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
def chainStr (states, str): |
| 30 |
return chain(states, *map(lambda x : newArcPair(states, x), str)) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
def notChainStr (states, str): |
| 35 |
"""XXX I'm not sure this is how it should be done, but I'm going to |
| 36 |
try it anyway. Note that for this case, I require only single character |
| 37 |
arcs, since I would have to basically invert all accepting states and |
| 38 |
non-accepting states of any sub-NFA's. |
| 39 |
""" |
| 40 |
assert len(str) > 0 |
| 41 |
arcs = map(lambda x : newArcPair(states, x), str) |
| 42 |
finish = len(states) |
| 43 |
states.append([]) |
| 44 |
start, lastFinish = arcs[0] |
| 45 |
states[start].append((EMPTY, finish)) |
| 46 |
for crntStart, crntFinish in arcs[1:]: |
| 47 |
states[lastFinish].append((EMPTY, crntStart)) |
| 48 |
states[crntStart].append((EMPTY, finish)) |
| 49 |
return start, finish |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
def group (states, *stateIndexPairs): |
| 54 |
if len(stateIndexPairs) > 1: |
| 55 |
start = len(states) |
| 56 |
finish = start + 1 |
| 57 |
startList = [] |
| 58 |
states.append(startList) |
| 59 |
states.append([]) |
| 60 |
for eStart, eFinish in stateIndexPairs: |
| 61 |
startList.append((EMPTY, eStart)) |
| 62 |
states[eFinish].append((EMPTY, finish)) |
| 63 |
return start, finish |
| 64 |
else: |
| 65 |
return stateIndexPairs[0] |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
def groupStr (states, str): |
| 70 |
return group(states, *map(lambda x : newArcPair(states, x), str)) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
def notGroup (states, *stateIndexPairs): |
| 75 |
"""Like group, but will add a DEFAULT transition to a new end state, |
| 76 |
causing anything in the group to not match by going to a dead state. |
| 77 |
XXX I think this is right... |
| 78 |
""" |
| 79 |
start, dead = group(states, *stateIndexPairs) |
| 80 |
finish = len(states) |
| 81 |
states.append([]) |
| 82 |
states[start].append((DEFAULT, finish)) |
| 83 |
return start, finish |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
def notGroupStr (states, str): |
| 88 |
return notGroup(states, *map(lambda x : newArcPair(states, x), str)) |
| 89 |
|
| 90 |
|
| 91 |
def any (states, *stateIndexPairs): |
| 92 |
start, finish = group(states, *stateIndexPairs) |
| 93 |
states[finish].append((EMPTY, start)) |
| 94 |
return start, start |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
def maybe (states, *stateIndexPairs): |
| 99 |
start, finish = group(states, *stateIndexPairs) |
| 100 |
states[start].append((EMPTY, finish)) |
| 101 |
return start, finish |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
def atleastonce (states, *stateIndexPairs): |
| 106 |
start, finish = group(states, *stateIndexPairs) |
| 107 |
states[finish].append((EMPTY, start)) |
| 108 |
return start, finish |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
def closure(states, start, result = frozenset()): |
| 113 |
if result is None: |
| 114 |
result = frozenset() |
| 115 |
if frozenset() == (result & {start}): |
| 116 |
result |= {start} |
| 117 |
for label, arrow in states[start]: |
| 118 |
if label == EMPTY: |
| 119 |
result |= closure(states, arrow, result) |
| 120 |
return result |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
def nfaToDfa(states, start, finish): |
| 125 |
tempStates = [] |
| 126 |
startClosure = closure(states, start) |
| 127 |
crntTempState = [startClosure, [], frozenset() != (startClosure & {finish})] |
| 128 |
tempStates.append(crntTempState) |
| 129 |
index = 0 |
| 130 |
while index < len(tempStates): |
| 131 |
crntTempState = tempStates[index] |
| 132 |
crntClosure, crntArcs, crntAccept = crntTempState |
| 133 |
for index2 in range(0, len(states)): |
| 134 |
if frozenset() != (crntClosure & {index2}): |
| 135 |
for label, nfaArrow in states[index2]: |
| 136 |
if label == EMPTY: |
| 137 |
continue |
| 138 |
foundTempArc = False |
| 139 |
for tempArc in crntArcs: |
| 140 |
if tempArc[0] == label: |
| 141 |
foundTempArc = True |
| 142 |
break |
| 143 |
if not foundTempArc: |
| 144 |
tempArc = [label, -1, frozenset()] |
| 145 |
crntArcs.append(tempArc) |
| 146 |
tempArc[2] = closure(states, nfaArrow, tempArc[2]) |
| 147 |
for arcIndex in range(0, len(crntArcs)): |
| 148 |
label, arrow, targetStates = crntArcs[arcIndex] |
| 149 |
targetFound = False |
| 150 |
arrow = 0 |
| 151 |
for destTempState in tempStates: |
| 152 |
if destTempState[0] == targetStates: |
| 153 |
targetFound = True |
| 154 |
break |
| 155 |
arrow += 1 |
| 156 |
if not targetFound: |
| 157 |
assert arrow == len(tempStates) |
| 158 |
newState = [targetStates, [], frozenset() != (targetStates & {finish})] |
| 159 |
tempStates.append(newState) |
| 160 |
crntArcs[arcIndex][1] = arrow |
| 161 |
index += 1 |
| 162 |
tempStates = simplifyTempDfa(tempStates) |
| 163 |
states = finalizeTempDfa(tempStates) |
| 164 |
return states |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
def sameState (s1, s2): |
| 169 |
"""sameState(s1, s2) |
| 170 |
Note: |
| 171 |
state := [ nfaclosure : Long, [ arc ], accept : Boolean ] |
| 172 |
arc := [ label, arrow : Int, nfaClosure : Long ] |
| 173 |
""" |
| 174 |
if (len(s1[1]) != len(s2[1])) or (s1[2] != s2[2]): |
| 175 |
return False |
| 176 |
for arcIndex in range(0, len(s1[1])): |
| 177 |
arc1 = s1[1][arcIndex] |
| 178 |
arc2 = s2[1][arcIndex] |
| 179 |
if arc1[:-1] != arc2[:-1]: |
| 180 |
return False |
| 181 |
return True |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
def simplifyTempDfa (tempStates): |
| 186 |
"""simplifyTempDfa (tempStates) |
| 187 |
""" |
| 188 |
changes = True |
| 189 |
deletedStates = [] |
| 190 |
while changes: |
| 191 |
changes = False |
| 192 |
for i in range(1, len(tempStates)): |
| 193 |
if i in deletedStates: |
| 194 |
continue |
| 195 |
for j in range(0, i): |
| 196 |
if j in deletedStates: |
| 197 |
continue |
| 198 |
if sameState(tempStates[i], tempStates[j]): |
| 199 |
deletedStates.append(i) |
| 200 |
for k in range(0, len(tempStates)): |
| 201 |
if k in deletedStates: |
| 202 |
continue |
| 203 |
for arc in tempStates[k][1]: |
| 204 |
if arc[1] == i: |
| 205 |
arc[1] = j |
| 206 |
changes = True |
| 207 |
break |
| 208 |
for stateIndex in deletedStates: |
| 209 |
tempStates[stateIndex] = None |
| 210 |
return tempStates |
| 211 |
|
| 212 |
|
| 213 |
def finalizeTempDfa (tempStates): |
| 214 |
"""finalizeTempDfa (tempStates) |
| 215 |
|
| 216 |
Input domain: |
| 217 |
tempState := [ nfaClosure : Long, [ tempArc ], accept : Boolean ] |
| 218 |
tempArc := [ label, arrow, nfaClosure ] |
| 219 |
|
| 220 |
Output domain: |
| 221 |
state := [ arcMap, accept : Boolean ] |
| 222 |
""" |
| 223 |
states = [] |
| 224 |
accepts = [] |
| 225 |
stateMap = {} |
| 226 |
tempIndex = 0 |
| 227 |
for tempIndex in range(0, len(tempStates)): |
| 228 |
tempState = tempStates[tempIndex] |
| 229 |
if None != tempState: |
| 230 |
stateMap[tempIndex] = len(states) |
| 231 |
states.append({}) |
| 232 |
accepts.append(tempState[2]) |
| 233 |
for tempIndex in stateMap.keys(): |
| 234 |
stateBitset, tempArcs, accepting = tempStates[tempIndex] |
| 235 |
newIndex = stateMap[tempIndex] |
| 236 |
arcMap = states[newIndex] |
| 237 |
for tempArc in tempArcs: |
| 238 |
arcMap[tempArc[0]] = stateMap[tempArc[1]] |
| 239 |
return states, accepts |
| 240 |
|
| 241 |
def _dot(states, final, r): |
| 242 |
for i, state in enumerate(states): |
| 243 |
shape = "circle" |
| 244 |
color = "" |
| 245 |
if final[i]: |
| 246 |
shape = "doublecircle" |
| 247 |
color = ", fillcolor=green" |
| 248 |
r.append('s%s [label="", shape="%s"%s];' % (i, shape, color)) |
| 249 |
if isinstance(state, dict): |
| 250 |
stateiter = state.iteritems() |
| 251 |
else: |
| 252 |
stateiter = state |
| 253 |
for char, target in stateiter: |
| 254 |
if char is EMPTY: |
| 255 |
char = "ε" |
| 256 |
elif char is DEFAULT: |
| 257 |
char = "default" |
| 258 |
elif type(char) is str and len(char) == 1 and ord(char) < 32: |
| 259 |
char = ord(char) |
| 260 |
elif char == "\\": |
| 261 |
char = "\\\\" |
| 262 |
elif char == '"': |
| 263 |
char = '\\"' |
| 264 |
r.append('s%s -> s%s [label="%s"];' % (i, target, char)) |
| 265 |
|
| 266 |
def view(states, final): |
| 267 |
from dotviewer import graphclient |
| 268 |
import tempfile |
| 269 |
r = ["digraph G {"] |
| 270 |
_dot(states, final, r) |
| 271 |
r.append("}") |
| 272 |
with tempfile.NamedTemporaryFile() as f: |
| 273 |
fn = f.name |
| 274 |
print fn |
| 275 |
with open(fn, "w") as f: |
| 276 |
f.write("\n".join(r)) |
| 277 |
graphclient.display_dot_file(fn) |
| 278 |
|