| 1 |
/* |
| 2 |
* NFA utilities. |
| 3 |
* This file is #included by regcomp.c. |
| 4 |
* |
| 5 |
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. |
| 6 |
* |
| 7 |
* Development of this software was funded, in part, by Cray Research Inc., |
| 8 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics |
| 9 |
* Corporation, none of whom are responsible for the results. The author |
| 10 |
* thanks all of them. |
| 11 |
* |
| 12 |
* Redistribution and use in source and binary forms -- with or without |
| 13 |
* modification -- are permitted for any purpose, provided that |
| 14 |
* redistributions in source form retain this entire copyright notice and |
| 15 |
* indicate the origin and nature of any modifications. |
| 16 |
* |
| 17 |
* I'd appreciate being given credit for this package in the documentation of |
| 18 |
* software which uses it, but that is not a requirement. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 21 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 22 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 26 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 29 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
* One or two things that technically ought to be in here are actually in |
| 32 |
* color.c, thanks to some incestuous relationships in the color chains. |
| 33 |
*/ |
| 34 |
|
| 35 |
#define NISERR() VISERR(nfa->v) |
| 36 |
#define NERR(e) VERR(nfa->v, (e)) |
| 37 |
#define STACK_TOO_DEEP(x) (0) |
| 38 |
#define CANCEL_REQUESTED(x) (0) |
| 39 |
#define REG_CANCEL 777 |
| 40 |
|
| 41 |
/* |
| 42 |
- newnfa - set up an NFA |
| 43 |
^ static struct nfa *newnfa(struct vars *, struct colormap *, struct nfa *); |
| 44 |
*/ |
| 45 |
static struct nfa * /* the NFA, or NULL */ |
| 46 |
newnfa( |
| 47 |
struct vars *v, |
| 48 |
struct colormap *cm, |
| 49 |
struct nfa *parent) /* NULL if primary NFA */ |
| 50 |
{ |
| 51 |
struct nfa *nfa; |
| 52 |
|
| 53 |
nfa = (struct nfa *) MALLOC(sizeof(struct nfa)); |
| 54 |
if (nfa == NULL) { |
| 55 |
ERR(REG_ESPACE); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
nfa->states = NULL; |
| 60 |
nfa->slast = NULL; |
| 61 |
nfa->free = NULL; |
| 62 |
nfa->nstates = 0; |
| 63 |
nfa->cm = cm; |
| 64 |
nfa->v = v; |
| 65 |
nfa->bos[0] = nfa->bos[1] = COLORLESS; |
| 66 |
nfa->eos[0] = nfa->eos[1] = COLORLESS; |
| 67 |
nfa->parent = parent; /* Precedes newfstate so parent is valid. */ |
| 68 |
nfa->post = newfstate(nfa, '@'); /* number 0 */ |
| 69 |
nfa->pre = newfstate(nfa, '>'); /* number 1 */ |
| 70 |
|
| 71 |
nfa->init = newstate(nfa); /* May become invalid later. */ |
| 72 |
nfa->final = newstate(nfa); |
| 73 |
if (ISERR()) { |
| 74 |
freenfa(nfa); |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
rainbow(nfa, nfa->cm, PLAIN, COLORLESS, nfa->pre, nfa->init); |
| 78 |
newarc(nfa, '^', 1, nfa->pre, nfa->init); |
| 79 |
newarc(nfa, '^', 0, nfa->pre, nfa->init); |
| 80 |
rainbow(nfa, nfa->cm, PLAIN, COLORLESS, nfa->final, nfa->post); |
| 81 |
newarc(nfa, '$', 1, nfa->final, nfa->post); |
| 82 |
newarc(nfa, '$', 0, nfa->final, nfa->post); |
| 83 |
|
| 84 |
if (ISERR()) { |
| 85 |
freenfa(nfa); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
return nfa; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
- freenfa - free an entire NFA |
| 93 |
^ static void freenfa(struct nfa *); |
| 94 |
*/ |
| 95 |
static void |
| 96 |
freenfa( |
| 97 |
struct nfa *nfa) |
| 98 |
{ |
| 99 |
struct state *s; |
| 100 |
|
| 101 |
while ((s = nfa->states) != NULL) { |
| 102 |
s->nins = s->nouts = 0; /* don't worry about arcs */ |
| 103 |
freestate(nfa, s); |
| 104 |
} |
| 105 |
while ((s = nfa->free) != NULL) { |
| 106 |
nfa->free = s->next; |
| 107 |
destroystate(nfa, s); |
| 108 |
} |
| 109 |
|
| 110 |
nfa->slast = NULL; |
| 111 |
nfa->nstates = -1; |
| 112 |
nfa->pre = NULL; |
| 113 |
nfa->post = NULL; |
| 114 |
FREE(nfa); |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
- newstate - allocate an NFA state, with zero flag value |
| 119 |
^ static struct state *newstate(struct nfa *); |
| 120 |
*/ |
| 121 |
static struct state * /* NULL on error */ |
| 122 |
newstate( |
| 123 |
struct nfa *nfa) |
| 124 |
{ |
| 125 |
struct state *s; |
| 126 |
|
| 127 |
if (nfa->free != NULL) { |
| 128 |
s = nfa->free; |
| 129 |
nfa->free = s->next; |
| 130 |
} else { |
| 131 |
if (nfa->v->spaceused >= REG_MAX_COMPILE_SPACE) { |
| 132 |
NERR(REG_ETOOBIG); |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
s = (struct state *) MALLOC(sizeof(struct state)); |
| 136 |
if (s == NULL) { |
| 137 |
NERR(REG_ESPACE); |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
nfa->v->spaceused += sizeof(struct state); |
| 141 |
s->oas.next = NULL; |
| 142 |
s->free = NULL; |
| 143 |
s->noas = 0; |
| 144 |
} |
| 145 |
|
| 146 |
assert(nfa->nstates >= 0); |
| 147 |
s->no = nfa->nstates++; |
| 148 |
s->flag = 0; |
| 149 |
if (nfa->states == NULL) { |
| 150 |
nfa->states = s; |
| 151 |
} |
| 152 |
s->nins = 0; |
| 153 |
s->ins = NULL; |
| 154 |
s->nouts = 0; |
| 155 |
s->outs = NULL; |
| 156 |
s->tmp = NULL; |
| 157 |
s->next = NULL; |
| 158 |
if (nfa->slast != NULL) { |
| 159 |
assert(nfa->slast->next == NULL); |
| 160 |
nfa->slast->next = s; |
| 161 |
} |
| 162 |
s->prev = nfa->slast; |
| 163 |
nfa->slast = s; |
| 164 |
return s; |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
- newfstate - allocate an NFA state with a specified flag value |
| 169 |
^ static struct state *newfstate(struct nfa *, int flag); |
| 170 |
*/ |
| 171 |
static struct state * /* NULL on error */ |
| 172 |
newfstate( |
| 173 |
struct nfa *nfa, |
| 174 |
int flag) |
| 175 |
{ |
| 176 |
struct state *s; |
| 177 |
|
| 178 |
s = newstate(nfa); |
| 179 |
if (s != NULL) { |
| 180 |
s->flag = (char) flag; |
| 181 |
} |
| 182 |
return s; |
| 183 |
} |
| 184 |
|
| 185 |
/* |
| 186 |
- dropstate - delete a state's inarcs and outarcs and free it |
| 187 |
^ static void dropstate(struct nfa *, struct state *); |
| 188 |
*/ |
| 189 |
static void |
| 190 |
dropstate( |
| 191 |
struct nfa *nfa, |
| 192 |
struct state *s) |
| 193 |
{ |
| 194 |
struct arc *a; |
| 195 |
|
| 196 |
while ((a = s->ins) != NULL) { |
| 197 |
freearc(nfa, a); |
| 198 |
} |
| 199 |
while ((a = s->outs) != NULL) { |
| 200 |
freearc(nfa, a); |
| 201 |
} |
| 202 |
freestate(nfa, s); |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
- freestate - free a state, which has no in-arcs or out-arcs |
| 207 |
^ static void freestate(struct nfa *, struct state *); |
| 208 |
*/ |
| 209 |
static void |
| 210 |
freestate( |
| 211 |
struct nfa *nfa, |
| 212 |
struct state *s) |
| 213 |
{ |
| 214 |
assert(s != NULL); |
| 215 |
assert(s->nins == 0 && s->nouts == 0); |
| 216 |
|
| 217 |
s->no = FREESTATE; |
| 218 |
s->flag = 0; |
| 219 |
if (s->next != NULL) { |
| 220 |
s->next->prev = s->prev; |
| 221 |
} else { |
| 222 |
assert(s == nfa->slast); |
| 223 |
nfa->slast = s->prev; |
| 224 |
} |
| 225 |
if (s->prev != NULL) { |
| 226 |
s->prev->next = s->next; |
| 227 |
} else { |
| 228 |
assert(s == nfa->states); |
| 229 |
nfa->states = s->next; |
| 230 |
} |
| 231 |
s->prev = NULL; |
| 232 |
s->next = nfa->free; /* don't delete it, put it on the free list */ |
| 233 |
nfa->free = s; |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
- destroystate - really get rid of an already-freed state |
| 238 |
^ static void destroystate(struct nfa *, struct state *); |
| 239 |
*/ |
| 240 |
static void |
| 241 |
destroystate( |
| 242 |
struct nfa *nfa, |
| 243 |
struct state *s) |
| 244 |
{ |
| 245 |
struct arcbatch *ab; |
| 246 |
struct arcbatch *abnext; |
| 247 |
|
| 248 |
assert(s->no == FREESTATE); |
| 249 |
for (ab=s->oas.next ; ab!=NULL ; ab=abnext) { |
| 250 |
abnext = ab->next; |
| 251 |
FREE(ab); |
| 252 |
nfa->v->spaceused -= sizeof(struct arcbatch); |
| 253 |
} |
| 254 |
s->ins = NULL; |
| 255 |
s->outs = NULL; |
| 256 |
s->next = NULL; |
| 257 |
FREE(s); |
| 258 |
nfa->v->spaceused -= sizeof(struct state); |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
- newarc - set up a new arc within an NFA |
| 263 |
^ static void newarc(struct nfa *, int, pcolor, struct state *, |
| 264 |
^ struct state *); |
| 265 |
*/ |
| 266 |
/* |
| 267 |
* This function checks to make sure that no duplicate arcs are created. |
| 268 |
* In general we never want duplicates. |
| 269 |
*/ |
| 270 |
static void |
| 271 |
newarc( |
| 272 |
struct nfa *nfa, |
| 273 |
int t, |
| 274 |
pcolor co, |
| 275 |
struct state *from, |
| 276 |
struct state *to) |
| 277 |
{ |
| 278 |
struct arc *a; |
| 279 |
|
| 280 |
assert(from != NULL && to != NULL); |
| 281 |
|
| 282 |
/* check for duplicate arc, using whichever chain is shorter */ |
| 283 |
if (from->nouts <= to->nins) { |
| 284 |
for (a = from->outs; a != NULL; a = a->outchain) { |
| 285 |
if (a->to == to && a->co == co && a->type == t) { |
| 286 |
return; |
| 287 |
} |
| 288 |
} |
| 289 |
} else { |
| 290 |
for (a = to->ins; a != NULL; a = a->inchain) { |
| 291 |
if (a->from == from && a->co == co && a->type == t) { |
| 292 |
return; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* no dup, so create the arc */ |
| 298 |
createarc(nfa, t, co, from, to); |
| 299 |
} |
| 300 |
|
| 301 |
/* |
| 302 |
* createarc - create a new arc within an NFA |
| 303 |
* |
| 304 |
* This function must *only* be used after verifying that there is no existing |
| 305 |
* identical arc (same type/color/from/to). |
| 306 |
*/ |
| 307 |
static void |
| 308 |
createarc( |
| 309 |
struct nfa * nfa, |
| 310 |
int t, |
| 311 |
pcolor co, |
| 312 |
struct state * from, |
| 313 |
struct state * to) |
| 314 |
{ |
| 315 |
struct arc *a; |
| 316 |
|
| 317 |
/* the arc is physically allocated within its from-state */ |
| 318 |
a = allocarc(nfa, from); |
| 319 |
if (NISERR()) { |
| 320 |
return; |
| 321 |
} |
| 322 |
assert(a != NULL); |
| 323 |
|
| 324 |
a->type = t; |
| 325 |
a->co = (color) co; |
| 326 |
a->to = to; |
| 327 |
a->from = from; |
| 328 |
|
| 329 |
/* |
| 330 |
* Put the new arc on the beginning, not the end, of the chains; it's |
| 331 |
* simpler here, and freearc() is the same cost either way. See also the |
| 332 |
* logic in moveins() and its cohorts, as well as fixempties(). |
| 333 |
*/ |
| 334 |
a->inchain = to->ins; |
| 335 |
a->inchainRev = NULL; |
| 336 |
if (to->ins) { |
| 337 |
to->ins->inchainRev = a; |
| 338 |
} |
| 339 |
to->ins = a; |
| 340 |
a->outchain = from->outs; |
| 341 |
a->outchainRev = NULL; |
| 342 |
if (from->outs) { |
| 343 |
from->outs->outchainRev = a; |
| 344 |
} |
| 345 |
from->outs = a; |
| 346 |
|
| 347 |
from->nouts++; |
| 348 |
to->nins++; |
| 349 |
|
| 350 |
if (COLORED(a) && nfa->parent == NULL) { |
| 351 |
colorchain(nfa->cm, a); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
- allocarc - allocate a new out-arc within a state |
| 357 |
^ static struct arc *allocarc(struct nfa *, struct state *); |
| 358 |
*/ |
| 359 |
static struct arc * /* NULL for failure */ |
| 360 |
allocarc( |
| 361 |
struct nfa *nfa, |
| 362 |
struct state *s) |
| 363 |
{ |
| 364 |
struct arc *a; |
| 365 |
|
| 366 |
/* |
| 367 |
* Shortcut |
| 368 |
*/ |
| 369 |
|
| 370 |
if (s->free == NULL && s->noas < ABSIZE) { |
| 371 |
a = &s->oas.a[s->noas]; |
| 372 |
s->noas++; |
| 373 |
return a; |
| 374 |
} |
| 375 |
|
| 376 |
/* |
| 377 |
* if none at hand, get more |
| 378 |
*/ |
| 379 |
|
| 380 |
if (s->free == NULL) { |
| 381 |
struct arcbatch *newAb; |
| 382 |
int i; |
| 383 |
|
| 384 |
if (nfa->v->spaceused >= REG_MAX_COMPILE_SPACE) { |
| 385 |
NERR(REG_ETOOBIG); |
| 386 |
return NULL; |
| 387 |
} |
| 388 |
newAb = (struct arcbatch *) MALLOC(sizeof(struct arcbatch)); |
| 389 |
if (newAb == NULL) { |
| 390 |
NERR(REG_ESPACE); |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
nfa->v->spaceused += sizeof(struct arcbatch); |
| 394 |
newAb->next = s->oas.next; |
| 395 |
s->oas.next = newAb; |
| 396 |
|
| 397 |
for (i=0 ; i<ABSIZE ; i++) { |
| 398 |
newAb->a[i].type = 0; |
| 399 |
newAb->a[i].freechain = &newAb->a[i+1]; |
| 400 |
} |
| 401 |
newAb->a[ABSIZE-1].freechain = NULL; |
| 402 |
s->free = &newAb->a[0]; |
| 403 |
} |
| 404 |
assert(s->free != NULL); |
| 405 |
|
| 406 |
a = s->free; |
| 407 |
s->free = a->freechain; |
| 408 |
return a; |
| 409 |
} |
| 410 |
|
| 411 |
/* |
| 412 |
- freearc - free an arc |
| 413 |
^ static void freearc(struct nfa *, struct arc *); |
| 414 |
*/ |
| 415 |
static void |
| 416 |
freearc( |
| 417 |
struct nfa *nfa, |
| 418 |
struct arc *victim) |
| 419 |
{ |
| 420 |
struct state *from = victim->from; |
| 421 |
struct state *to = victim->to; |
| 422 |
struct arc *predecessor; |
| 423 |
|
| 424 |
assert(victim->type != 0); |
| 425 |
|
| 426 |
/* |
| 427 |
* Take it off color chain if necessary. |
| 428 |
*/ |
| 429 |
|
| 430 |
if (COLORED(victim) && nfa->parent == NULL) { |
| 431 |
uncolorchain(nfa->cm, victim); |
| 432 |
} |
| 433 |
|
| 434 |
/* |
| 435 |
* Take it off source's out-chain. |
| 436 |
*/ |
| 437 |
|
| 438 |
assert(from != NULL); |
| 439 |
predecessor = victim->outchainRev; |
| 440 |
if (predecessor == NULL) { |
| 441 |
assert(from->outs == victim); |
| 442 |
from->outs = victim->outchain; |
| 443 |
} else { |
| 444 |
assert(predecessor->outchain == victim); |
| 445 |
predecessor->outchain = victim->outchain; |
| 446 |
} |
| 447 |
if (victim->outchain != NULL) { |
| 448 |
assert(victim->outchain->outchainRev == victim); |
| 449 |
victim->outchain->outchainRev = predecessor; |
| 450 |
} |
| 451 |
from->nouts--; |
| 452 |
|
| 453 |
/* |
| 454 |
* Take it off target's in-chain. |
| 455 |
*/ |
| 456 |
|
| 457 |
assert(to != NULL); |
| 458 |
predecessor = victim->inchainRev; |
| 459 |
if (predecessor == NULL) { |
| 460 |
assert(to->ins == victim); |
| 461 |
to->ins = victim->inchain; |
| 462 |
} else { |
| 463 |
assert(predecessor->inchain == victim); |
| 464 |
predecessor->inchain = victim->inchain; |
| 465 |
} |
| 466 |
if (victim->inchain != NULL) { |
| 467 |
assert(victim->inchain->inchainRev == victim); |
| 468 |
victim->inchain->inchainRev = predecessor; |
| 469 |
} |
| 470 |
to->nins--; |
| 471 |
|
| 472 |
/* |
| 473 |
* Clean up and place on from-state's free list. |
| 474 |
*/ |
| 475 |
|
| 476 |
victim->type = 0; |
| 477 |
victim->from = NULL; /* precautions... */ |
| 478 |
victim->to = NULL; |
| 479 |
victim->inchain = NULL; |
| 480 |
victim->inchainRev = NULL; |
| 481 |
victim->outchain = NULL; |
| 482 |
victim->outchainRev = NULL; |
| 483 |
victim->freechain = from->free; |
| 484 |
from->free = victim; |
| 485 |
} |
| 486 |
|
| 487 |
/* |
| 488 |
* changearctarget - flip an arc to have a different to state |
| 489 |
* |
| 490 |
* Caller must have verified that there is no preexisting duplicate arc. |
| 491 |
* |
| 492 |
* Note that because we store arcs in their from state, we can't easily have |
| 493 |
* a similar changearcsource function. |
| 494 |
*/ |
| 495 |
static void |
| 496 |
changearctarget(struct arc * a, struct state * newto) |
| 497 |
{ |
| 498 |
struct state *oldto = a->to; |
| 499 |
struct arc *predecessor; |
| 500 |
|
| 501 |
assert(oldto != newto); |
| 502 |
|
| 503 |
/* take it off old target's in-chain */ |
| 504 |
assert(oldto != NULL); |
| 505 |
predecessor = a->inchainRev; |
| 506 |
if (predecessor == NULL) { |
| 507 |
assert(oldto->ins == a); |
| 508 |
oldto->ins = a->inchain; |
| 509 |
} else { |
| 510 |
assert(predecessor->inchain == a); |
| 511 |
predecessor->inchain = a->inchain; |
| 512 |
} |
| 513 |
if (a->inchain != NULL) { |
| 514 |
assert(a->inchain->inchainRev == a); |
| 515 |
a->inchain->inchainRev = predecessor; |
| 516 |
} |
| 517 |
oldto->nins--; |
| 518 |
|
| 519 |
a->to = newto; |
| 520 |
|
| 521 |
/* prepend it to new target's in-chain */ |
| 522 |
a->inchain = newto->ins; |
| 523 |
a->inchainRev = NULL; |
| 524 |
if (newto->ins) { |
| 525 |
newto->ins->inchainRev = a; |
| 526 |
} |
| 527 |
newto->ins = a; |
| 528 |
newto->nins++; |
| 529 |
} |
| 530 |
|
| 531 |
/* |
| 532 |
- hasnonemptyout - Does state have a non-EMPTY out arc? |
| 533 |
^ static int hasnonemptyout(struct state *); |
| 534 |
*/ |
| 535 |
static int |
| 536 |
hasnonemptyout( |
| 537 |
struct state *s) |
| 538 |
{ |
| 539 |
struct arc *a; |
| 540 |
|
| 541 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 542 |
if (a->type != EMPTY) { |
| 543 |
return 1; |
| 544 |
} |
| 545 |
} |
| 546 |
return 0; |
| 547 |
} |
| 548 |
|
| 549 |
/* |
| 550 |
- findarc - find arc, if any, from given source with given type and color |
| 551 |
* If there is more than one such arc, the result is random. |
| 552 |
^ static struct arc *findarc(struct state *, int, pcolor); |
| 553 |
*/ |
| 554 |
static struct arc * |
| 555 |
findarc( |
| 556 |
struct state *s, |
| 557 |
int type, |
| 558 |
pcolor co) |
| 559 |
{ |
| 560 |
struct arc *a; |
| 561 |
|
| 562 |
for (a=s->outs ; a!=NULL ; a=a->outchain) { |
| 563 |
if (a->type == type && a->co == co) { |
| 564 |
return a; |
| 565 |
} |
| 566 |
} |
| 567 |
return NULL; |
| 568 |
} |
| 569 |
|
| 570 |
/* |
| 571 |
- cparc - allocate a new arc within an NFA, copying details from old one |
| 572 |
^ static void cparc(struct nfa *, struct arc *, struct state *, |
| 573 |
^ struct state *); |
| 574 |
*/ |
| 575 |
static void |
| 576 |
cparc( |
| 577 |
struct nfa *nfa, |
| 578 |
struct arc *oa, |
| 579 |
struct state *from, |
| 580 |
struct state *to) |
| 581 |
{ |
| 582 |
newarc(nfa, oa->type, oa->co, from, to); |
| 583 |
} |
| 584 |
|
| 585 |
/* |
| 586 |
* sortins - sort the in arcs of a state by from/color/type |
| 587 |
*/ |
| 588 |
static void |
| 589 |
sortins( |
| 590 |
struct nfa * nfa, |
| 591 |
struct state * s) |
| 592 |
{ |
| 593 |
struct arc **sortarray; |
| 594 |
struct arc *a; |
| 595 |
int n = s->nins; |
| 596 |
int i; |
| 597 |
|
| 598 |
if (n <= 1) { |
| 599 |
return; /* nothing to do */ |
| 600 |
} |
| 601 |
/* make an array of arc pointers ... */ |
| 602 |
sortarray = (struct arc **) MALLOC(n * sizeof(struct arc *)); |
| 603 |
if (sortarray == NULL) { |
| 604 |
NERR(REG_ESPACE); |
| 605 |
return; |
| 606 |
} |
| 607 |
i = 0; |
| 608 |
for (a = s->ins; a != NULL; a = a->inchain) { |
| 609 |
sortarray[i++] = a; |
| 610 |
} |
| 611 |
assert(i == n); |
| 612 |
/* ... sort the array */ |
| 613 |
qsort(sortarray, n, sizeof(struct arc *), sortins_cmp); |
| 614 |
/* ... and rebuild arc list in order */ |
| 615 |
/* it seems worth special-casing first and last items to simplify loop */ |
| 616 |
a = sortarray[0]; |
| 617 |
s->ins = a; |
| 618 |
a->inchain = sortarray[1]; |
| 619 |
a->inchainRev = NULL; |
| 620 |
for (i = 1; i < n - 1; i++) { |
| 621 |
a = sortarray[i]; |
| 622 |
a->inchain = sortarray[i + 1]; |
| 623 |
a->inchainRev = sortarray[i - 1]; |
| 624 |
} |
| 625 |
a = sortarray[i]; |
| 626 |
a->inchain = NULL; |
| 627 |
a->inchainRev = sortarray[i - 1]; |
| 628 |
FREE(sortarray); |
| 629 |
} |
| 630 |
|
| 631 |
static int |
| 632 |
sortins_cmp( |
| 633 |
const void *a, |
| 634 |
const void *b) |
| 635 |
{ |
| 636 |
const struct arc *aa = *((const struct arc * const *) a); |
| 637 |
const struct arc *bb = *((const struct arc * const *) b); |
| 638 |
|
| 639 |
/* we check the fields in the order they are most likely to be different */ |
| 640 |
if (aa->from->no < bb->from->no) { |
| 641 |
return -1; |
| 642 |
} |
| 643 |
if (aa->from->no > bb->from->no) { |
| 644 |
return 1; |
| 645 |
} |
| 646 |
if (aa->co < bb->co) { |
| 647 |
return -1; |
| 648 |
} |
| 649 |
if (aa->co > bb->co) { |
| 650 |
return 1; |
| 651 |
} |
| 652 |
if (aa->type < bb->type) { |
| 653 |
return -1; |
| 654 |
} |
| 655 |
if (aa->type > bb->type) { |
| 656 |
return 1; |
| 657 |
} |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
/* |
| 662 |
* sortouts - sort the out arcs of a state by to/color/type |
| 663 |
*/ |
| 664 |
static void |
| 665 |
sortouts( |
| 666 |
struct nfa * nfa, |
| 667 |
struct state * s) |
| 668 |
{ |
| 669 |
struct arc **sortarray; |
| 670 |
struct arc *a; |
| 671 |
int n = s->nouts; |
| 672 |
int i; |
| 673 |
|
| 674 |
if (n <= 1) { |
| 675 |
return; /* nothing to do */ |
| 676 |
} |
| 677 |
/* make an array of arc pointers ... */ |
| 678 |
sortarray = (struct arc **) MALLOC(n * sizeof(struct arc *)); |
| 679 |
if (sortarray == NULL) { |
| 680 |
NERR(REG_ESPACE); |
| 681 |
return; |
| 682 |
} |
| 683 |
i = 0; |
| 684 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 685 |
sortarray[i++] = a; |
| 686 |
} |
| 687 |
assert(i == n); |
| 688 |
/* ... sort the array */ |
| 689 |
qsort(sortarray, n, sizeof(struct arc *), sortouts_cmp); |
| 690 |
/* ... and rebuild arc list in order */ |
| 691 |
/* it seems worth special-casing first and last items to simplify loop */ |
| 692 |
a = sortarray[0]; |
| 693 |
s->outs = a; |
| 694 |
a->outchain = sortarray[1]; |
| 695 |
a->outchainRev = NULL; |
| 696 |
for (i = 1; i < n - 1; i++) { |
| 697 |
a = sortarray[i]; |
| 698 |
a->outchain = sortarray[i + 1]; |
| 699 |
a->outchainRev = sortarray[i - 1]; |
| 700 |
} |
| 701 |
a = sortarray[i]; |
| 702 |
a->outchain = NULL; |
| 703 |
a->outchainRev = sortarray[i - 1]; |
| 704 |
FREE(sortarray); |
| 705 |
} |
| 706 |
|
| 707 |
static int |
| 708 |
sortouts_cmp( |
| 709 |
const void *a, |
| 710 |
const void *b) |
| 711 |
{ |
| 712 |
const struct arc *aa = *((const struct arc * const *) a); |
| 713 |
const struct arc *bb = *((const struct arc * const *) b); |
| 714 |
|
| 715 |
/* we check the fields in the order they are most likely to be different */ |
| 716 |
if (aa->to->no < bb->to->no) { |
| 717 |
return -1; |
| 718 |
} |
| 719 |
if (aa->to->no > bb->to->no) { |
| 720 |
return 1; |
| 721 |
} |
| 722 |
if (aa->co < bb->co) { |
| 723 |
return -1; |
| 724 |
} |
| 725 |
if (aa->co > bb->co) { |
| 726 |
return 1; |
| 727 |
} |
| 728 |
if (aa->type < bb->type) { |
| 729 |
return -1; |
| 730 |
} |
| 731 |
if (aa->type > bb->type) { |
| 732 |
return 1; |
| 733 |
} |
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
/* |
| 738 |
* Common decision logic about whether to use arc-by-arc operations or |
| 739 |
* sort/merge. If there's just a few source arcs we cannot recoup the |
| 740 |
* cost of sorting the destination arc list, no matter how large it is. |
| 741 |
* Otherwise, limit the number of arc-by-arc comparisons to about 1000 |
| 742 |
* (a somewhat arbitrary choice, but the breakeven point would probably |
| 743 |
* be machine dependent anyway). |
| 744 |
*/ |
| 745 |
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs) \ |
| 746 |
((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32)) |
| 747 |
|
| 748 |
/* |
| 749 |
- moveins - move all in arcs of a state to another state |
| 750 |
* You might think this could be done better by just updating the |
| 751 |
* existing arcs, and you would be right if it weren't for the need |
| 752 |
* for duplicate suppression, which makes it easier to just make new |
| 753 |
* ones to exploit the suppression built into newarc. |
| 754 |
* |
| 755 |
* However, if we have a whole lot of arcs to deal with, retail duplicate |
| 756 |
* checks become too slow. In that case we proceed by sorting and merging |
| 757 |
* the arc lists, and then we can indeed just update the arcs in-place. |
| 758 |
* |
| 759 |
^ static void moveins(struct nfa *, struct state *, struct state *); |
| 760 |
*/ |
| 761 |
static void |
| 762 |
moveins( |
| 763 |
struct nfa *nfa, |
| 764 |
struct state *oldState, |
| 765 |
struct state *newState) |
| 766 |
{ |
| 767 |
assert(oldState != newState); |
| 768 |
|
| 769 |
if (!BULK_ARC_OP_USE_SORT(oldState->nins, newState->nins)) { |
| 770 |
/* With not too many arcs, just do them one at a time */ |
| 771 |
struct arc *a; |
| 772 |
|
| 773 |
while ((a = oldState->ins) != NULL) { |
| 774 |
cparc(nfa, a, a->from, newState); |
| 775 |
freearc(nfa, a); |
| 776 |
} |
| 777 |
} else { |
| 778 |
/* |
| 779 |
* With many arcs, use a sort-merge approach. Note changearctarget() |
| 780 |
* will put the arc onto the front of newState's chain, so it does not |
| 781 |
* break our walk through the sorted part of the chain. |
| 782 |
*/ |
| 783 |
struct arc *oa; |
| 784 |
struct arc *na; |
| 785 |
|
| 786 |
/* |
| 787 |
* Because we bypass newarc() in this code path, we'd better include a |
| 788 |
* cancel check. |
| 789 |
*/ |
| 790 |
if (CANCEL_REQUESTED(nfa->v->re)) { |
| 791 |
NERR(REG_CANCEL); |
| 792 |
return; |
| 793 |
} |
| 794 |
|
| 795 |
sortins(nfa, oldState); |
| 796 |
sortins(nfa, newState); |
| 797 |
if (NISERR()) { |
| 798 |
return; /* might have failed to sort */ |
| 799 |
} |
| 800 |
oa = oldState->ins; |
| 801 |
na = newState->ins; |
| 802 |
while (oa != NULL && na != NULL) { |
| 803 |
struct arc *a = oa; |
| 804 |
|
| 805 |
switch (sortins_cmp(&oa, &na)) { |
| 806 |
case -1: |
| 807 |
/* newState does not have anything matching oa */ |
| 808 |
oa = oa->inchain; |
| 809 |
|
| 810 |
/* |
| 811 |
* Rather than doing createarc+freearc, we can just unlink |
| 812 |
* and relink the existing arc struct. |
| 813 |
*/ |
| 814 |
changearctarget(a, newState); |
| 815 |
break; |
| 816 |
case 0: |
| 817 |
/* match, advance in both lists */ |
| 818 |
oa = oa->inchain; |
| 819 |
na = na->inchain; |
| 820 |
/* ... and drop duplicate arc from oldState */ |
| 821 |
freearc(nfa, a); |
| 822 |
break; |
| 823 |
case +1: |
| 824 |
/* advance only na; oa might have a match later */ |
| 825 |
na = na->inchain; |
| 826 |
break; |
| 827 |
default: |
| 828 |
assert(NOTREACHED); |
| 829 |
} |
| 830 |
} |
| 831 |
while (oa != NULL) { |
| 832 |
/* newState does not have anything matching oa */ |
| 833 |
struct arc *a = oa; |
| 834 |
|
| 835 |
oa = oa->inchain; |
| 836 |
changearctarget(a, newState); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
assert(oldState->nins == 0); |
| 841 |
assert(oldState->ins == NULL); |
| 842 |
} |
| 843 |
|
| 844 |
/* |
| 845 |
- copyins - copy in arcs of a state to another state |
| 846 |
^ static VOID copyins(struct nfa *, struct state *, struct state *, int); |
| 847 |
*/ |
| 848 |
static void |
| 849 |
copyins( |
| 850 |
struct nfa *nfa, |
| 851 |
struct state *oldState, |
| 852 |
struct state *newState) |
| 853 |
{ |
| 854 |
assert(oldState != newState); |
| 855 |
|
| 856 |
if (!BULK_ARC_OP_USE_SORT(oldState->nins, newState->nins)) { |
| 857 |
/* With not too many arcs, just do them one at a time */ |
| 858 |
struct arc *a; |
| 859 |
|
| 860 |
for (a = oldState->ins; a != NULL; a = a->inchain) { |
| 861 |
cparc(nfa, a, a->from, newState); |
| 862 |
} |
| 863 |
} else { |
| 864 |
/* |
| 865 |
* With many arcs, use a sort-merge approach. Note that createarc() |
| 866 |
* will put new arcs onto the front of newState's chain, so it does |
| 867 |
* not break our walk through the sorted part of the chain. |
| 868 |
*/ |
| 869 |
struct arc *oa; |
| 870 |
struct arc *na; |
| 871 |
|
| 872 |
/* |
| 873 |
* Because we bypass newarc() in this code path, we'd better include a |
| 874 |
* cancel check. |
| 875 |
*/ |
| 876 |
if (CANCEL_REQUESTED(nfa->v->re)) { |
| 877 |
NERR(REG_CANCEL); |
| 878 |
return; |
| 879 |
} |
| 880 |
|
| 881 |
sortins(nfa, oldState); |
| 882 |
sortins(nfa, newState); |
| 883 |
if (NISERR()) { |
| 884 |
return; /* might have failed to sort */ |
| 885 |
} |
| 886 |
oa = oldState->ins; |
| 887 |
na = newState->ins; |
| 888 |
while (oa != NULL && na != NULL) { |
| 889 |
struct arc *a = oa; |
| 890 |
|
| 891 |
switch (sortins_cmp(&oa, &na)) { |
| 892 |
case -1: |
| 893 |
/* newState does not have anything matching oa */ |
| 894 |
oa = oa->inchain; |
| 895 |
createarc(nfa, a->type, a->co, a->from, newState); |
| 896 |
break; |
| 897 |
case 0: |
| 898 |
/* match, advance in both lists */ |
| 899 |
oa = oa->inchain; |
| 900 |
na = na->inchain; |
| 901 |
break; |
| 902 |
case +1: |
| 903 |
/* advance only na; oa might have a match later */ |
| 904 |
na = na->inchain; |
| 905 |
break; |
| 906 |
default: |
| 907 |
assert(NOTREACHED); |
| 908 |
} |
| 909 |
} |
| 910 |
while (oa != NULL) { |
| 911 |
/* newState does not have anything matching oa */ |
| 912 |
struct arc *a = oa; |
| 913 |
|
| 914 |
oa = oa->inchain; |
| 915 |
createarc(nfa, a->type, a->co, a->from, newState); |
| 916 |
} |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
/* |
| 921 |
* mergeins - merge a list of inarcs into a state |
| 922 |
* |
| 923 |
* This is much like copyins, but the source arcs are listed in an array, |
| 924 |
* and are not guaranteed unique. It's okay to clobber the array contents. |
| 925 |
*/ |
| 926 |
static void |
| 927 |
mergeins( |
| 928 |
struct nfa * nfa, |
| 929 |
struct state * s, |
| 930 |
struct arc ** arcarray, |
| 931 |
int arccount) |
| 932 |
{ |
| 933 |
struct arc *na; |
| 934 |
int i; |
| 935 |
int j; |
| 936 |
|
| 937 |
if (arccount <= 0) { |
| 938 |
return; |
| 939 |
} |
| 940 |
|
| 941 |
/* |
| 942 |
* Because we bypass newarc() in this code path, we'd better include a |
| 943 |
* cancel check. |
| 944 |
*/ |
| 945 |
if (CANCEL_REQUESTED(nfa->v->re)) { |
| 946 |
NERR(REG_CANCEL); |
| 947 |
return; |
| 948 |
} |
| 949 |
|
| 950 |
/* Sort existing inarcs as well as proposed new ones */ |
| 951 |
sortins(nfa, s); |
| 952 |
if (NISERR()) { |
| 953 |
return; /* might have failed to sort */ |
| 954 |
} |
| 955 |
|
| 956 |
qsort(arcarray, arccount, sizeof(struct arc *), sortins_cmp); |
| 957 |
|
| 958 |
/* |
| 959 |
* arcarray very likely includes dups, so we must eliminate them. (This |
| 960 |
* could be folded into the next loop, but it's not worth the trouble.) |
| 961 |
*/ |
| 962 |
j = 0; |
| 963 |
for (i = 1; i < arccount; i++) { |
| 964 |
switch (sortins_cmp(&arcarray[j], &arcarray[i])) { |
| 965 |
case -1: |
| 966 |
/* non-dup */ |
| 967 |
arcarray[++j] = arcarray[i]; |
| 968 |
break; |
| 969 |
case 0: |
| 970 |
/* dup */ |
| 971 |
break; |
| 972 |
default: |
| 973 |
/* trouble */ |
| 974 |
assert(NOTREACHED); |
| 975 |
} |
| 976 |
} |
| 977 |
arccount = j + 1; |
| 978 |
|
| 979 |
/* |
| 980 |
* Now merge into s' inchain. Note that createarc() will put new arcs |
| 981 |
* onto the front of s's chain, so it does not break our walk through the |
| 982 |
* sorted part of the chain. |
| 983 |
*/ |
| 984 |
i = 0; |
| 985 |
na = s->ins; |
| 986 |
while (i < arccount && na != NULL) { |
| 987 |
struct arc *a = arcarray[i]; |
| 988 |
|
| 989 |
switch (sortins_cmp(&a, &na)) { |
| 990 |
case -1: |
| 991 |
/* s does not have anything matching a */ |
| 992 |
createarc(nfa, a->type, a->co, a->from, s); |
| 993 |
i++; |
| 994 |
break; |
| 995 |
case 0: |
| 996 |
/* match, advance in both lists */ |
| 997 |
i++; |
| 998 |
na = na->inchain; |
| 999 |
break; |
| 1000 |
case +1: |
| 1001 |
/* advance only na; array might have a match later */ |
| 1002 |
na = na->inchain; |
| 1003 |
break; |
| 1004 |
default: |
| 1005 |
assert(NOTREACHED); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
while (i < arccount) { |
| 1009 |
/* s does not have anything matching a */ |
| 1010 |
struct arc *a = arcarray[i]; |
| 1011 |
|
| 1012 |
createarc(nfa, a->type, a->co, a->from, s); |
| 1013 |
i++; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* |
| 1018 |
- moveouts - move all out arcs of a state to another state |
| 1019 |
^ static void moveouts(struct nfa *, struct state *, struct state *); |
| 1020 |
*/ |
| 1021 |
static void |
| 1022 |
moveouts( |
| 1023 |
struct nfa *nfa, |
| 1024 |
struct state *oldState, |
| 1025 |
struct state *newState) |
| 1026 |
{ |
| 1027 |
assert(oldState != newState); |
| 1028 |
|
| 1029 |
if (!BULK_ARC_OP_USE_SORT(oldState->nouts, newState->nouts)) { |
| 1030 |
/* With not too many arcs, just do them one at a time */ |
| 1031 |
struct arc *a; |
| 1032 |
|
| 1033 |
while ((a = oldState->outs) != NULL) { |
| 1034 |
cparc(nfa, a, newState, a->to); |
| 1035 |
freearc(nfa, a); |
| 1036 |
} |
| 1037 |
} else { |
| 1038 |
/* |
| 1039 |
* With many arcs, use a sort-merge approach. Note that createarc() |
| 1040 |
* will put new arcs onto the front of newState's chain, so it does |
| 1041 |
* not break our walk through the sorted part of the chain. |
| 1042 |
*/ |
| 1043 |
struct arc *oa; |
| 1044 |
struct arc *na; |
| 1045 |
|
| 1046 |
/* |
| 1047 |
* Because we bypass newarc() in this code path, we'd better include a |
| 1048 |
* cancel check. |
| 1049 |
*/ |
| 1050 |
if (CANCEL_REQUESTED(nfa->v->re)) { |
| 1051 |
NERR(REG_CANCEL); |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
|
| 1055 |
sortouts(nfa, oldState); |
| 1056 |
sortouts(nfa, newState); |
| 1057 |
if (NISERR()) { |
| 1058 |
return; /* might have failed to sort */ |
| 1059 |
} |
| 1060 |
oa = oldState->outs; |
| 1061 |
na = newState->outs; |
| 1062 |
while (oa != NULL && na != NULL) { |
| 1063 |
struct arc *a = oa; |
| 1064 |
|
| 1065 |
switch (sortouts_cmp(&oa, &na)) { |
| 1066 |
case -1: |
| 1067 |
/* newState does not have anything matching oa */ |
| 1068 |
oa = oa->outchain; |
| 1069 |
createarc(nfa, a->type, a->co, newState, a->to); |
| 1070 |
freearc(nfa, a); |
| 1071 |
break; |
| 1072 |
case 0: |
| 1073 |
/* match, advance in both lists */ |
| 1074 |
oa = oa->outchain; |
| 1075 |
na = na->outchain; |
| 1076 |
/* ... and drop duplicate arc from oldState */ |
| 1077 |
freearc(nfa, a); |
| 1078 |
break; |
| 1079 |
case +1: |
| 1080 |
/* advance only na; oa might have a match later */ |
| 1081 |
na = na->outchain; |
| 1082 |
break; |
| 1083 |
default: |
| 1084 |
assert(NOTREACHED); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
while (oa != NULL) { |
| 1088 |
/* newState does not have anything matching oa */ |
| 1089 |
struct arc *a = oa; |
| 1090 |
|
| 1091 |
oa = oa->outchain; |
| 1092 |
createarc(nfa, a->type, a->co, newState, a->to); |
| 1093 |
freearc(nfa, a); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
assert(oldState->nouts == 0); |
| 1098 |
assert(oldState->outs == NULL); |
| 1099 |
} |
| 1100 |
|
| 1101 |
/* |
| 1102 |
- copyouts - copy out arcs of a state to another state |
| 1103 |
^ static VOID copyouts(struct nfa *, struct state *, struct state *, int); |
| 1104 |
*/ |
| 1105 |
static void |
| 1106 |
copyouts( |
| 1107 |
struct nfa *nfa, |
| 1108 |
struct state *oldState, |
| 1109 |
struct state *newState) |
| 1110 |
{ |
| 1111 |
assert(oldState != newState); |
| 1112 |
|
| 1113 |
if (!BULK_ARC_OP_USE_SORT(oldState->nouts, newState->nouts)) { |
| 1114 |
/* With not too many arcs, just do them one at a time */ |
| 1115 |
struct arc *a; |
| 1116 |
|
| 1117 |
for (a = oldState->outs; a != NULL; a = a->outchain) { |
| 1118 |
cparc(nfa, a, newState, a->to); |
| 1119 |
} |
| 1120 |
} else { |
| 1121 |
/* |
| 1122 |
* With many arcs, use a sort-merge approach. Note that createarc() |
| 1123 |
* will put new arcs onto the front of newState's chain, so it does |
| 1124 |
* not break our walk through the sorted part of the chain. |
| 1125 |
*/ |
| 1126 |
struct arc *oa; |
| 1127 |
struct arc *na; |
| 1128 |
|
| 1129 |
/* |
| 1130 |
* Because we bypass newarc() in this code path, we'd better include a |
| 1131 |
* cancel check. |
| 1132 |
*/ |
| 1133 |
if (CANCEL_REQUESTED(nfa->v->re)) { |
| 1134 |
NERR(REG_CANCEL); |
| 1135 |
return; |
| 1136 |
} |
| 1137 |
|
| 1138 |
sortouts(nfa, oldState); |
| 1139 |
sortouts(nfa, newState); |
| 1140 |
if (NISERR()) { |
| 1141 |
return; /* might have failed to sort */ |
| 1142 |
} |
| 1143 |
oa = oldState->outs; |
| 1144 |
na = newState->outs; |
| 1145 |
while (oa != NULL && na != NULL) { |
| 1146 |
struct arc *a = oa; |
| 1147 |
|
| 1148 |
switch (sortouts_cmp(&oa, &na)) { |
| 1149 |
case -1: |
| 1150 |
/* newState does not have anything matching oa */ |
| 1151 |
oa = oa->outchain; |
| 1152 |
createarc(nfa, a->type, a->co, newState, a->to); |
| 1153 |
break; |
| 1154 |
case 0: |
| 1155 |
/* match, advance in both lists */ |
| 1156 |
oa = oa->outchain; |
| 1157 |
na = na->outchain; |
| 1158 |
break; |
| 1159 |
case +1: |
| 1160 |
/* advance only na; oa might have a match later */ |
| 1161 |
na = na->outchain; |
| 1162 |
break; |
| 1163 |
default: |
| 1164 |
assert(NOTREACHED); |
| 1165 |
} |
| 1166 |
} |
| 1167 |
while (oa != NULL) { |
| 1168 |
/* newState does not have anything matching oa */ |
| 1169 |
struct arc *a = oa; |
| 1170 |
|
| 1171 |
oa = oa->outchain; |
| 1172 |
createarc(nfa, a->type, a->co, newState, a->to); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* |
| 1178 |
- cloneouts - copy out arcs of a state to another state pair, modifying type |
| 1179 |
^ static void cloneouts(struct nfa *, struct state *, struct state *, |
| 1180 |
^ struct state *, int); |
| 1181 |
*/ |
| 1182 |
static void |
| 1183 |
cloneouts( |
| 1184 |
struct nfa *nfa, |
| 1185 |
struct state *old, |
| 1186 |
struct state *from, |
| 1187 |
struct state *to, |
| 1188 |
int type) |
| 1189 |
{ |
| 1190 |
struct arc *a; |
| 1191 |
|
| 1192 |
assert(old != from); |
| 1193 |
|
| 1194 |
for (a=old->outs ; a!=NULL ; a=a->outchain) { |
| 1195 |
newarc(nfa, type, a->co, from, to); |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* |
| 1200 |
- delsub - delete a sub-NFA, updating subre pointers if necessary |
| 1201 |
* This uses a recursive traversal of the sub-NFA, marking already-seen |
| 1202 |
* states using their tmp pointer. |
| 1203 |
^ static void delsub(struct nfa *, struct state *, struct state *); |
| 1204 |
*/ |
| 1205 |
static void |
| 1206 |
delsub( |
| 1207 |
struct nfa *nfa, |
| 1208 |
struct state *lp, /* the sub-NFA goes from here... */ |
| 1209 |
struct state *rp) /* ...to here, *not* inclusive */ |
| 1210 |
{ |
| 1211 |
assert(lp != rp); |
| 1212 |
|
| 1213 |
rp->tmp = rp; /* mark end */ |
| 1214 |
|
| 1215 |
deltraverse(nfa, lp, lp); |
| 1216 |
assert(lp->nouts == 0 && rp->nins == 0); /* did the job */ |
| 1217 |
assert(lp->no != FREESTATE && rp->no != FREESTATE); /* no more */ |
| 1218 |
|
| 1219 |
rp->tmp = NULL; /* unmark end */ |
| 1220 |
lp->tmp = NULL; /* and begin, marked by deltraverse */ |
| 1221 |
} |
| 1222 |
|
| 1223 |
/* |
| 1224 |
- deltraverse - the recursive heart of delsub |
| 1225 |
* This routine's basic job is to destroy all out-arcs of the state. |
| 1226 |
^ static void deltraverse(struct nfa *, struct state *, struct state *); |
| 1227 |
*/ |
| 1228 |
static void |
| 1229 |
deltraverse( |
| 1230 |
struct nfa *nfa, |
| 1231 |
struct state *leftend, |
| 1232 |
struct state *s) |
| 1233 |
{ |
| 1234 |
struct arc *a; |
| 1235 |
struct state *to; |
| 1236 |
|
| 1237 |
if (s->nouts == 0) { |
| 1238 |
return; /* nothing to do */ |
| 1239 |
} |
| 1240 |
if (s->tmp != NULL) { |
| 1241 |
return; /* already in progress */ |
| 1242 |
} |
| 1243 |
|
| 1244 |
s->tmp = s; /* mark as in progress */ |
| 1245 |
|
| 1246 |
while ((a = s->outs) != NULL) { |
| 1247 |
to = a->to; |
| 1248 |
deltraverse(nfa, leftend, to); |
| 1249 |
assert(to->nouts == 0 || to->tmp != NULL); |
| 1250 |
freearc(nfa, a); |
| 1251 |
if (to->nins == 0 && to->tmp == NULL) { |
| 1252 |
assert(to->nouts == 0); |
| 1253 |
freestate(nfa, to); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
assert(s->no != FREESTATE); /* we're still here */ |
| 1258 |
assert(s == leftend || s->nins != 0); /* and still reachable */ |
| 1259 |
assert(s->nouts == 0); /* but have no outarcs */ |
| 1260 |
|
| 1261 |
s->tmp = NULL; /* we're done here */ |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* |
| 1265 |
- dupnfa - duplicate sub-NFA |
| 1266 |
* Another recursive traversal, this time using tmp to point to duplicates as |
| 1267 |
* well as mark already-seen states. (You knew there was a reason why it's a |
| 1268 |
* state pointer, didn't you? :-)) |
| 1269 |
^ static void dupnfa(struct nfa *, struct state *, struct state *, |
| 1270 |
^ struct state *, struct state *); |
| 1271 |
*/ |
| 1272 |
static void |
| 1273 |
dupnfa( |
| 1274 |
struct nfa *nfa, |
| 1275 |
struct state *start, /* duplicate of subNFA starting here */ |
| 1276 |
struct state *stop, /* and stopping here */ |
| 1277 |
struct state *from, /* stringing duplicate from here */ |
| 1278 |
struct state *to) /* to here */ |
| 1279 |
{ |
| 1280 |
if (start == stop) { |
| 1281 |
newarc(nfa, EMPTY, 0, from, to); |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
|
| 1285 |
stop->tmp = to; |
| 1286 |
duptraverse(nfa, start, from, 0); |
| 1287 |
/* done, except for clearing out the tmp pointers */ |
| 1288 |
|
| 1289 |
stop->tmp = NULL; |
| 1290 |
cleartraverse(nfa, start); |
| 1291 |
} |
| 1292 |
|
| 1293 |
/* |
| 1294 |
- duptraverse - recursive heart of dupnfa |
| 1295 |
^ static void duptraverse(struct nfa *, struct state *, struct state *); |
| 1296 |
*/ |
| 1297 |
static void |
| 1298 |
duptraverse( |
| 1299 |
struct nfa *nfa, |
| 1300 |
struct state *s, |
| 1301 |
struct state *stmp, /* s's duplicate, or NULL */ |
| 1302 |
int depth) |
| 1303 |
{ |
| 1304 |
struct arc *a; |
| 1305 |
|
| 1306 |
if (s->tmp != NULL) { |
| 1307 |
return; /* already done */ |
| 1308 |
} |
| 1309 |
|
| 1310 |
s->tmp = (stmp == NULL) ? newstate(nfa) : stmp; |
| 1311 |
if (s->tmp == NULL) { |
| 1312 |
assert(NISERR()); |
| 1313 |
return; |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* |
| 1317 |
* Arbitrary depth limit. Needs tuning, but this value is sufficient to |
| 1318 |
* make all normal tests (not reg-33.14) pass. |
| 1319 |
*/ |
| 1320 |
#ifndef DUPTRAVERSE_MAX_DEPTH |
| 1321 |
#define DUPTRAVERSE_MAX_DEPTH 15000 |
| 1322 |
#endif |
| 1323 |
|
| 1324 |
if (depth++ > DUPTRAVERSE_MAX_DEPTH) { |
| 1325 |
NERR(REG_ESPACE); |
| 1326 |
} |
| 1327 |
|
| 1328 |
for (a=s->outs ; a!=NULL && !NISERR() ; a=a->outchain) { |
| 1329 |
duptraverse(nfa, a->to, NULL, depth); |
| 1330 |
if (NISERR()) { |
| 1331 |
break; |
| 1332 |
} |
| 1333 |
assert(a->to->tmp != NULL); |
| 1334 |
cparc(nfa, a, s->tmp, a->to->tmp); |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
/* |
| 1339 |
- cleartraverse - recursive cleanup for algorithms that leave tmp ptrs set |
| 1340 |
^ static void cleartraverse(struct nfa *, struct state *); |
| 1341 |
*/ |
| 1342 |
static void |
| 1343 |
cleartraverse( |
| 1344 |
struct nfa *nfa, |
| 1345 |
struct state *s) |
| 1346 |
{ |
| 1347 |
struct arc *a; |
| 1348 |
|
| 1349 |
if (s->tmp == NULL) { |
| 1350 |
return; |
| 1351 |
} |
| 1352 |
s->tmp = NULL; |
| 1353 |
|
| 1354 |
for (a=s->outs ; a!=NULL ; a=a->outchain) { |
| 1355 |
cleartraverse(nfa, a->to); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
/* |
| 1360 |
- specialcolors - fill in special colors for an NFA |
| 1361 |
^ static void specialcolors(struct nfa *); |
| 1362 |
*/ |
| 1363 |
static void |
| 1364 |
specialcolors( |
| 1365 |
struct nfa *nfa) |
| 1366 |
{ |
| 1367 |
/* |
| 1368 |
* False colors for BOS, BOL, EOS, EOL |
| 1369 |
*/ |
| 1370 |
|
| 1371 |
if (nfa->parent == NULL) { |
| 1372 |
nfa->bos[0] = pseudocolor(nfa->cm); |
| 1373 |
nfa->bos[1] = pseudocolor(nfa->cm); |
| 1374 |
nfa->eos[0] = pseudocolor(nfa->cm); |
| 1375 |
nfa->eos[1] = pseudocolor(nfa->cm); |
| 1376 |
} else { |
| 1377 |
assert(nfa->parent->bos[0] != COLORLESS); |
| 1378 |
nfa->bos[0] = nfa->parent->bos[0]; |
| 1379 |
assert(nfa->parent->bos[1] != COLORLESS); |
| 1380 |
nfa->bos[1] = nfa->parent->bos[1]; |
| 1381 |
assert(nfa->parent->eos[0] != COLORLESS); |
| 1382 |
nfa->eos[0] = nfa->parent->eos[0]; |
| 1383 |
assert(nfa->parent->eos[1] != COLORLESS); |
| 1384 |
nfa->eos[1] = nfa->parent->eos[1]; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
/* |
| 1389 |
- optimize - optimize an NFA |
| 1390 |
^ static long optimize(struct nfa *, FILE *); |
| 1391 |
*/ |
| 1392 |
|
| 1393 |
/* |
| 1394 |
* The main goal of this function is not so much "optimization" (though it |
| 1395 |
* does try to get rid of useless NFA states) as reducing the NFA to a form |
| 1396 |
* the regex executor can handle. The executor, and indeed the cNFA format |
| 1397 |
* that is its input, can only handle PLAIN and LACON arcs. The output of |
| 1398 |
* the regex parser also includes EMPTY (do-nothing) arcs, as well as |
| 1399 |
* ^, $, AHEAD, and BEHIND constraint arcs, which we must get rid of here. |
| 1400 |
* We first get rid of EMPTY arcs and then deal with the constraint arcs. |
| 1401 |
* The hardest part of either job is to get rid of circular loops of the |
| 1402 |
* target arc type. We would have to do that in any case, though, as such a |
| 1403 |
* loop would otherwise allow the executor to cycle through the loop endlessly |
| 1404 |
* without making any progress in the input string. |
| 1405 |
*/ |
| 1406 |
static long /* re_info bits */ |
| 1407 |
optimize( |
| 1408 |
struct nfa *nfa, |
| 1409 |
FILE *f) /* for debug output; NULL none */ |
| 1410 |
{ |
| 1411 |
int verbose = (f != NULL) ? 1 : 0; |
| 1412 |
|
| 1413 |
if (verbose) { |
| 1414 |
fprintf(f, "\ninitial cleanup:\n"); |
| 1415 |
} |
| 1416 |
cleanup(nfa); /* may simplify situation */ |
| 1417 |
if (verbose) { |
| 1418 |
dumpnfa(nfa, f); |
| 1419 |
} |
| 1420 |
if (verbose) { |
| 1421 |
fprintf(f, "\nempties:\n"); |
| 1422 |
} |
| 1423 |
fixempties(nfa, f); /* get rid of EMPTY arcs */ |
| 1424 |
if (verbose) { |
| 1425 |
fprintf(f, "\nconstraints:\n"); |
| 1426 |
} |
| 1427 |
fixconstraintloops(nfa, f); /* get rid of constraint loops */ |
| 1428 |
pullback(nfa, f); /* pull back constraints backward */ |
| 1429 |
pushfwd(nfa, f); /* push fwd constraints forward */ |
| 1430 |
if (verbose) { |
| 1431 |
fprintf(f, "\nfinal cleanup:\n"); |
| 1432 |
} |
| 1433 |
cleanup(nfa); /* final tidying */ |
| 1434 |
#ifdef REG_DEBUG |
| 1435 |
if (verbose) { |
| 1436 |
dumpnfa(nfa, f); |
| 1437 |
} |
| 1438 |
#endif |
| 1439 |
return analyze(nfa); /* and analysis */ |
| 1440 |
} |
| 1441 |
|
| 1442 |
/* |
| 1443 |
- pullback - pull back constraints backward to eliminate them |
| 1444 |
^ static void pullback(struct nfa *, FILE *); |
| 1445 |
*/ |
| 1446 |
static void |
| 1447 |
pullback( |
| 1448 |
struct nfa *nfa, |
| 1449 |
FILE *f) /* for debug output; NULL none */ |
| 1450 |
{ |
| 1451 |
struct state *s; |
| 1452 |
struct state *nexts; |
| 1453 |
struct arc *a; |
| 1454 |
struct arc *nexta; |
| 1455 |
struct state *intermediates; |
| 1456 |
int progress; |
| 1457 |
|
| 1458 |
/* |
| 1459 |
* Find and pull until there are no more. |
| 1460 |
*/ |
| 1461 |
|
| 1462 |
do { |
| 1463 |
progress = 0; |
| 1464 |
for (s=nfa->states ; s!=NULL && !NISERR() ; s=nexts) { |
| 1465 |
nexts = s->next; |
| 1466 |
intermediates = NULL; |
| 1467 |
for (a=s->outs ; a!=NULL && !NISERR() ; a=nexta) { |
| 1468 |
nexta = a->outchain; |
| 1469 |
if (a->type == '^' || a->type == BEHIND) { |
| 1470 |
if (pull(nfa, a, &intermediates)) { |
| 1471 |
progress = 1; |
| 1472 |
} |
| 1473 |
} |
| 1474 |
assert(nexta == NULL || s->no != FREESTATE); |
| 1475 |
} |
| 1476 |
/* clear tmp fields of intermediate states created here */ |
| 1477 |
while (intermediates != NULL) { |
| 1478 |
struct state *ns = intermediates->tmp; |
| 1479 |
|
| 1480 |
intermediates->tmp = NULL; |
| 1481 |
intermediates = ns; |
| 1482 |
} |
| 1483 |
/* if s is now useless, get rid of it */ |
| 1484 |
if ((s->nins == 0 || s->nouts == 0) && !s->flag) { |
| 1485 |
dropstate(nfa, s); |
| 1486 |
} |
| 1487 |
} |
| 1488 |
if (progress && f != NULL) { |
| 1489 |
dumpnfa(nfa, f); |
| 1490 |
} |
| 1491 |
} while (progress && !NISERR()); |
| 1492 |
if (NISERR()) { |
| 1493 |
return; |
| 1494 |
} |
| 1495 |
|
| 1496 |
/* |
| 1497 |
* Any ^ constraints we were able to pull to the start state can now be |
| 1498 |
* replaced by PLAIN arcs referencing the BOS or BOL colors. There should |
| 1499 |
* be no other ^ or BEHIND arcs left in the NFA, though we do not check |
| 1500 |
* that here (compact() will fail if so). |
| 1501 |
*/ |
| 1502 |
for (a=nfa->pre->outs ; a!=NULL ; a=nexta) { |
| 1503 |
nexta = a->outchain; |
| 1504 |
if (a->type == '^') { |
| 1505 |
assert(a->co == 0 || a->co == 1); |
| 1506 |
newarc(nfa, PLAIN, nfa->bos[a->co], a->from, a->to); |
| 1507 |
freearc(nfa, a); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
/* |
| 1513 |
- pull - pull a back constraint backward past its source state |
| 1514 |
* |
| 1515 |
* Returns 1 if successful (which it always is unless the source is the |
| 1516 |
* start state or we have an internal error), 0 if nothing happened. |
| 1517 |
* |
| 1518 |
* A significant property of this function is that it deletes no preexisting |
| 1519 |
* states, and no outarcs of the constraint's from state other than the given |
| 1520 |
* constraint arc. This makes the loops in pullback() safe, at the cost that |
| 1521 |
* we may leave useless states behind. Therefore, we leave it to pullback() |
| 1522 |
* to delete such states. |
| 1523 |
* |
| 1524 |
* If the from state has multiple back-constraint outarcs, and/or multiple |
| 1525 |
* compatible constraint inarcs, we only need to create one new intermediate |
| 1526 |
* state per combination of predecessor and successor states. *intermediates |
| 1527 |
* points to a list of such intermediate states for this from state (chained |
| 1528 |
* through their tmp fields). |
| 1529 |
^ static int pull(struct nfa *, struct arc *); |
| 1530 |
*/ |
| 1531 |
static int |
| 1532 |
pull( |
| 1533 |
struct nfa *nfa, |
| 1534 |
struct arc *con, |
| 1535 |
struct state **intermediates) |
| 1536 |
{ |
| 1537 |
struct state *from = con->from; |
| 1538 |
struct state *to = con->to; |
| 1539 |
struct arc *a; |
| 1540 |
struct arc *nexta; |
| 1541 |
struct state *s; |
| 1542 |
|
| 1543 |
assert(from != to); /* should have gotten rid of this earlier */ |
| 1544 |
if (from->flag) { /* can't pull back beyond start */ |
| 1545 |
return 0; |
| 1546 |
} |
| 1547 |
if (from->nins == 0) { /* unreachable */ |
| 1548 |
freearc(nfa, con); |
| 1549 |
return 1; |
| 1550 |
} |
| 1551 |
|
| 1552 |
/* |
| 1553 |
* First, clone from state if necessary to avoid other outarcs. This may |
| 1554 |
* seem wasteful, but it simplifies the logic, and we'll get rid of the |
| 1555 |
* clone state again at the bottom. |
| 1556 |
*/ |
| 1557 |
|
| 1558 |
if (from->nouts > 1) { |
| 1559 |
s = newstate(nfa); |
| 1560 |
if (NISERR()) { |
| 1561 |
return 0; |
| 1562 |
} |
| 1563 |
copyins(nfa, from, s); /* duplicate inarcs */ |
| 1564 |
cparc(nfa, con, s, to); /* move constraint arc */ |
| 1565 |
freearc(nfa, con); |
| 1566 |
if (NISERR()) { |
| 1567 |
return 0; |
| 1568 |
} |
| 1569 |
from = s; |
| 1570 |
con = from->outs; |
| 1571 |
} |
| 1572 |
assert(from->nouts == 1); |
| 1573 |
|
| 1574 |
/* |
| 1575 |
* Propagate the constraint into the from state's inarcs. |
| 1576 |
*/ |
| 1577 |
|
| 1578 |
for (a=from->ins ; a!=NULL && !NISERR(); a=nexta) { |
| 1579 |
nexta = a->inchain; |
| 1580 |
switch (combine(con, a)) { |
| 1581 |
case INCOMPATIBLE: /* destroy the arc */ |
| 1582 |
freearc(nfa, a); |
| 1583 |
break; |
| 1584 |
case SATISFIED: /* no action needed */ |
| 1585 |
break; |
| 1586 |
case COMPATIBLE: /* swap the two arcs, more or less */ |
| 1587 |
/* need an intermediate state, but might have one already */ |
| 1588 |
for (s = *intermediates; s != NULL; s = s->tmp) { |
| 1589 |
assert(s->nins > 0 && s->nouts > 0); |
| 1590 |
if (s->ins->from == a->from && s->outs->to == to) { |
| 1591 |
break; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
if (s == NULL) { |
| 1595 |
s = newstate(nfa); |
| 1596 |
if (NISERR()) { |
| 1597 |
return 0; |
| 1598 |
} |
| 1599 |
s->tmp = *intermediates; |
| 1600 |
*intermediates = s; |
| 1601 |
} |
| 1602 |
cparc(nfa, con, a->from, s); |
| 1603 |
cparc(nfa, a, s, to); |
| 1604 |
freearc(nfa, a); |
| 1605 |
break; |
| 1606 |
default: |
| 1607 |
assert(NOTREACHED); |
| 1608 |
break; |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
/* |
| 1613 |
* Remaining inarcs, if any, incorporate the constraint. |
| 1614 |
*/ |
| 1615 |
|
| 1616 |
moveins(nfa, from, to); |
| 1617 |
freearc(nfa, con); |
| 1618 |
/* from state is now useless, but we leave it to pullback() to clean up */ |
| 1619 |
return 1; |
| 1620 |
} |
| 1621 |
|
| 1622 |
/* |
| 1623 |
- pushfwd - push forward constraints forward to eliminate them |
| 1624 |
^ static void pushfwd(struct nfa *, FILE *); |
| 1625 |
*/ |
| 1626 |
static void |
| 1627 |
pushfwd( |
| 1628 |
struct nfa *nfa, |
| 1629 |
FILE *f) /* for debug output; NULL none */ |
| 1630 |
{ |
| 1631 |
struct state *s; |
| 1632 |
struct state *nexts; |
| 1633 |
struct arc *a; |
| 1634 |
struct arc *nexta; |
| 1635 |
struct state *intermediates; |
| 1636 |
int progress; |
| 1637 |
|
| 1638 |
/* |
| 1639 |
* Find and push until there are no more. |
| 1640 |
*/ |
| 1641 |
|
| 1642 |
do { |
| 1643 |
progress = 0; |
| 1644 |
for (s=nfa->states ; s!=NULL && !NISERR() ; s=nexts) { |
| 1645 |
nexts = s->next; |
| 1646 |
intermediates = NULL; |
| 1647 |
for (a = s->ins; a != NULL && !NISERR(); a = nexta) { |
| 1648 |
nexta = a->inchain; |
| 1649 |
if (a->type == '$' || a->type == AHEAD) { |
| 1650 |
if (push(nfa, a, &intermediates)) { |
| 1651 |
progress = 1; |
| 1652 |
} |
| 1653 |
} |
| 1654 |
} |
| 1655 |
/* clear tmp fields of intermediate states created here */ |
| 1656 |
while (intermediates != NULL) { |
| 1657 |
struct state *ns = intermediates->tmp; |
| 1658 |
|
| 1659 |
intermediates->tmp = NULL; |
| 1660 |
intermediates = ns; |
| 1661 |
} |
| 1662 |
/* if s is now useless, get rid of it */ |
| 1663 |
if ((s->nins == 0 || s->nouts == 0) && !s->flag) { |
| 1664 |
dropstate(nfa, s); |
| 1665 |
} |
| 1666 |
} |
| 1667 |
if (progress && f != NULL) { |
| 1668 |
dumpnfa(nfa, f); |
| 1669 |
} |
| 1670 |
} while (progress && !NISERR()); |
| 1671 |
if (NISERR()) { |
| 1672 |
return; |
| 1673 |
} |
| 1674 |
|
| 1675 |
/* |
| 1676 |
* Any $ constraints we were able to push to the post state can now be |
| 1677 |
* replaced by PLAIN arcs referencing the EOS or EOL colors. There should |
| 1678 |
* be no other $ or AHEAD arcs left in the NFA, though we do not check |
| 1679 |
* that here (compact() will fail if so). |
| 1680 |
*/ |
| 1681 |
for (a = nfa->post->ins; a != NULL; a = nexta) { |
| 1682 |
nexta = a->inchain; |
| 1683 |
if (a->type == '$') { |
| 1684 |
assert(a->co == 0 || a->co == 1); |
| 1685 |
newarc(nfa, PLAIN, nfa->eos[a->co], a->from, a->to); |
| 1686 |
freearc(nfa, a); |
| 1687 |
} |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
/* |
| 1692 |
- push - push a forward constraint forward past its destination state |
| 1693 |
* |
| 1694 |
* Returns 1 if successful (which it always is unless the destination is the |
| 1695 |
* post state or we have an internal error), 0 if nothing happened. |
| 1696 |
* |
| 1697 |
* A significant property of this function is that it deletes no preexisting |
| 1698 |
* states, and no inarcs of the constraint's to state other than the given |
| 1699 |
* constraint arc. This makes the loops in pushfwd() safe, at the cost that |
| 1700 |
* we may leave useless states behind. Therefore, we leave it to pushfwd() |
| 1701 |
* to delete such states. |
| 1702 |
* |
| 1703 |
* If the to state has multiple forward-constraint inarcs, and/or multiple |
| 1704 |
* compatible constraint outarcs, we only need to create one new intermediate |
| 1705 |
* state per combination of predecessor and successor states. *intermediates |
| 1706 |
* points to a list of such intermediate states for this to state (chained |
| 1707 |
* through their tmp fields). |
| 1708 |
^ static int push(struct nfa *, struct arc *); |
| 1709 |
*/ |
| 1710 |
static int |
| 1711 |
push( |
| 1712 |
struct nfa *nfa, |
| 1713 |
struct arc *con, |
| 1714 |
struct state **intermediates) |
| 1715 |
{ |
| 1716 |
struct state *from = con->from; |
| 1717 |
struct state *to = con->to; |
| 1718 |
struct arc *a; |
| 1719 |
struct arc *nexta; |
| 1720 |
struct state *s; |
| 1721 |
|
| 1722 |
assert(to != from); /* should have gotten rid of this earlier */ |
| 1723 |
if (to->flag) { /* can't push forward beyond end */ |
| 1724 |
return 0; |
| 1725 |
} |
| 1726 |
if (to->nouts == 0) { /* dead end */ |
| 1727 |
freearc(nfa, con); |
| 1728 |
return 1; |
| 1729 |
} |
| 1730 |
|
| 1731 |
/* |
| 1732 |
* First, clone to state if necessary to avoid other inarcs. This may |
| 1733 |
* seem wasteful, but it simplifies the logic, and we'll get rid of the |
| 1734 |
* clone state again at the bottom. |
| 1735 |
*/ |
| 1736 |
|
| 1737 |
if (to->nins > 1) { |
| 1738 |
s = newstate(nfa); |
| 1739 |
if (NISERR()) { |
| 1740 |
return 0; |
| 1741 |
} |
| 1742 |
copyouts(nfa, to, s); /* duplicate outarcs */ |
| 1743 |
cparc(nfa, con, from, s); /* move constraint arc */ |
| 1744 |
freearc(nfa, con); |
| 1745 |
if (NISERR()) { |
| 1746 |
return 0; |
| 1747 |
} |
| 1748 |
to = s; |
| 1749 |
con = to->ins; |
| 1750 |
} |
| 1751 |
assert(to->nins == 1); |
| 1752 |
|
| 1753 |
/* |
| 1754 |
* Propagate the constraint into the to state's outarcs. |
| 1755 |
*/ |
| 1756 |
|
| 1757 |
for (a = to->outs; a != NULL && !NISERR(); a = nexta) { |
| 1758 |
nexta = a->outchain; |
| 1759 |
switch (combine(con, a)) { |
| 1760 |
case INCOMPATIBLE: /* destroy the arc */ |
| 1761 |
freearc(nfa, a); |
| 1762 |
break; |
| 1763 |
case SATISFIED: /* no action needed */ |
| 1764 |
break; |
| 1765 |
case COMPATIBLE: /* swap the two arcs, more or less */ |
| 1766 |
/* need an intermediate state, but might have one already */ |
| 1767 |
for (s = *intermediates; s != NULL; s = s->tmp) { |
| 1768 |
assert(s->nins > 0 && s->nouts > 0); |
| 1769 |
if (s->ins->from == from && s->outs->to == a->to) { |
| 1770 |
break; |
| 1771 |
} |
| 1772 |
} |
| 1773 |
if (s == NULL) { |
| 1774 |
s = newstate(nfa); |
| 1775 |
if (NISERR()) { |
| 1776 |
return 0; |
| 1777 |
} |
| 1778 |
s->tmp = *intermediates; |
| 1779 |
*intermediates = s; |
| 1780 |
} |
| 1781 |
cparc(nfa, con, s, a->to); |
| 1782 |
cparc(nfa, a, from, s); |
| 1783 |
freearc(nfa, a); |
| 1784 |
break; |
| 1785 |
default: |
| 1786 |
assert(NOTREACHED); |
| 1787 |
break; |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
/* |
| 1792 |
* Remaining outarcs, if any, incorporate the constraint. |
| 1793 |
*/ |
| 1794 |
|
| 1795 |
moveouts(nfa, to, from); |
| 1796 |
freearc(nfa, con); |
| 1797 |
/* to state is now useless, but we leave it to pushfwd() to clean up */ |
| 1798 |
return 1; |
| 1799 |
} |
| 1800 |
|
| 1801 |
/* |
| 1802 |
- combine - constraint lands on an arc, what happens? |
| 1803 |
^ #def INCOMPATIBLE 1 // destroys arc |
| 1804 |
^ #def SATISFIED 2 // constraint satisfied |
| 1805 |
^ #def COMPATIBLE 3 // compatible but not satisfied yet |
| 1806 |
^ static int combine(struct arc *, struct arc *); |
| 1807 |
*/ |
| 1808 |
static int |
| 1809 |
combine( |
| 1810 |
struct arc *con, |
| 1811 |
struct arc *a) |
| 1812 |
{ |
| 1813 |
#define CA(ct,at) (((ct)<<CHAR_BIT) | (at)) |
| 1814 |
|
| 1815 |
switch (CA(con->type, a->type)) { |
| 1816 |
case CA('^', PLAIN): /* newlines are handled separately */ |
| 1817 |
case CA('$', PLAIN): |
| 1818 |
return INCOMPATIBLE; |
| 1819 |
break; |
| 1820 |
case CA(AHEAD, PLAIN): /* color constraints meet colors */ |
| 1821 |
case CA(BEHIND, PLAIN): |
| 1822 |
if (con->co == a->co) { |
| 1823 |
return SATISFIED; |
| 1824 |
} |
| 1825 |
return INCOMPATIBLE; |
| 1826 |
break; |
| 1827 |
case CA('^', '^'): /* collision, similar constraints */ |
| 1828 |
case CA('$', '$'): |
| 1829 |
case CA(AHEAD, AHEAD): |
| 1830 |
case CA(BEHIND, BEHIND): |
| 1831 |
if (con->co == a->co) { /* true duplication */ |
| 1832 |
return SATISFIED; |
| 1833 |
} |
| 1834 |
return INCOMPATIBLE; |
| 1835 |
break; |
| 1836 |
case CA('^', BEHIND): /* collision, dissimilar constraints */ |
| 1837 |
case CA(BEHIND, '^'): |
| 1838 |
case CA('$', AHEAD): |
| 1839 |
case CA(AHEAD, '$'): |
| 1840 |
return INCOMPATIBLE; |
| 1841 |
break; |
| 1842 |
case CA('^', '$'): /* constraints passing each other */ |
| 1843 |
case CA('^', AHEAD): |
| 1844 |
case CA(BEHIND, '$'): |
| 1845 |
case CA(BEHIND, AHEAD): |
| 1846 |
case CA('$', '^'): |
| 1847 |
case CA('$', BEHIND): |
| 1848 |
case CA(AHEAD, '^'): |
| 1849 |
case CA(AHEAD, BEHIND): |
| 1850 |
case CA('^', LACON): |
| 1851 |
case CA(BEHIND, LACON): |
| 1852 |
case CA('$', LACON): |
| 1853 |
case CA(AHEAD, LACON): |
| 1854 |
return COMPATIBLE; |
| 1855 |
break; |
| 1856 |
} |
| 1857 |
assert(NOTREACHED); |
| 1858 |
return INCOMPATIBLE; /* for benefit of blind compilers */ |
| 1859 |
} |
| 1860 |
|
| 1861 |
/* |
| 1862 |
- fixempties - get rid of EMPTY arcs |
| 1863 |
^ static void fixempties(struct nfa *, FILE *); |
| 1864 |
*/ |
| 1865 |
static void |
| 1866 |
fixempties( |
| 1867 |
struct nfa *nfa, |
| 1868 |
FILE *f) /* for debug output; NULL none */ |
| 1869 |
{ |
| 1870 |
struct state *s; |
| 1871 |
struct state *s2; |
| 1872 |
struct state *nexts; |
| 1873 |
struct arc *a; |
| 1874 |
struct arc *nexta; |
| 1875 |
int totalinarcs; |
| 1876 |
struct arc **inarcsorig; |
| 1877 |
struct arc **arcarray; |
| 1878 |
int arccount; |
| 1879 |
int prevnins; |
| 1880 |
int nskip; |
| 1881 |
|
| 1882 |
/* |
| 1883 |
* First, get rid of any states whose sole out-arc is an EMPTY, |
| 1884 |
* since they're basically just aliases for their successor. The |
| 1885 |
* parsing algorithm creates enough of these that it's worth |
| 1886 |
* special-casing this. |
| 1887 |
*/ |
| 1888 |
for (s = nfa->states; s != NULL && !NISERR(); s = nexts) { |
| 1889 |
nexts = s->next; |
| 1890 |
if (s->flag || s->nouts != 1) { |
| 1891 |
continue; |
| 1892 |
} |
| 1893 |
a = s->outs; |
| 1894 |
assert(a != NULL && a->outchain == NULL); |
| 1895 |
if (a->type != EMPTY) { |
| 1896 |
continue; |
| 1897 |
} |
| 1898 |
if (s != a->to) { |
| 1899 |
moveins(nfa, s, a->to); |
| 1900 |
} |
| 1901 |
dropstate(nfa, s); |
| 1902 |
} |
| 1903 |
|
| 1904 |
/* |
| 1905 |
* Similarly, get rid of any state with a single EMPTY in-arc, by |
| 1906 |
* folding it into its predecessor. |
| 1907 |
*/ |
| 1908 |
for (s = nfa->states; s != NULL && !NISERR(); s = nexts) { |
| 1909 |
nexts = s->next; |
| 1910 |
/* Ensure tmp fields are clear for next step */ |
| 1911 |
assert(s->tmp == NULL); |
| 1912 |
if (s->flag || s->nins != 1) { |
| 1913 |
continue; |
| 1914 |
} |
| 1915 |
a = s->ins; |
| 1916 |
assert(a != NULL && a->inchain == NULL); |
| 1917 |
if (a->type != EMPTY) { |
| 1918 |
continue; |
| 1919 |
} |
| 1920 |
if (s != a->from) { |
| 1921 |
moveouts(nfa, s, a->from); |
| 1922 |
} |
| 1923 |
dropstate(nfa, s); |
| 1924 |
} |
| 1925 |
|
| 1926 |
if (NISERR()) { |
| 1927 |
return; |
| 1928 |
} |
| 1929 |
|
| 1930 |
/* |
| 1931 |
* For each remaining NFA state, find all other states from which it is |
| 1932 |
* reachable by a chain of one or more EMPTY arcs. Then generate new arcs |
| 1933 |
* that eliminate the need for each such chain. |
| 1934 |
* |
| 1935 |
* We could replace a chain of EMPTY arcs that leads from a "from" state |
| 1936 |
* to a "to" state either by pushing non-EMPTY arcs forward (linking |
| 1937 |
* directly from "from"'s predecessors to "to") or by pulling them back |
| 1938 |
* (linking directly from "from" to "to"'s successors). We choose to |
| 1939 |
* always do the former; this choice is somewhat arbitrary, but the |
| 1940 |
* approach below requires that we uniformly do one or the other. |
| 1941 |
* |
| 1942 |
* Suppose we have a chain of N successive EMPTY arcs (where N can easily |
| 1943 |
* approach the size of the NFA). All of the intermediate states must |
| 1944 |
* have additional inarcs and outarcs, else they'd have been removed by |
| 1945 |
* the steps above. Assuming their inarcs are mostly not empties, we will |
| 1946 |
* add O(N^2) arcs to the NFA, since a non-EMPTY inarc leading to any one |
| 1947 |
* state in the chain must be duplicated to lead to all its successor |
| 1948 |
* states as well. So there is no hope of doing less than O(N^2) work; |
| 1949 |
* however, we should endeavor to keep the big-O cost from being even |
| 1950 |
* worse than that, which it can easily become without care. In |
| 1951 |
* particular, suppose we were to copy all S1's inarcs forward to S2, and |
| 1952 |
* then also to S3, and then later we consider pushing S2's inarcs forward |
| 1953 |
* to S3. If we include the arcs already copied from S1 in that, we'd be |
| 1954 |
* doing O(N^3) work. (The duplicate-arc elimination built into newarc() |
| 1955 |
* and its cohorts would get rid of the extra arcs, but not without cost.) |
| 1956 |
* |
| 1957 |
* We can avoid this cost by treating only arcs that existed at the start |
| 1958 |
* of this phase as candidates to be pushed forward. To identify those, |
| 1959 |
* we remember the first inarc each state had to start with. We rely on |
| 1960 |
* the fact that newarc() and friends put new arcs on the front of their |
| 1961 |
* to-states' inchains, and that this phase never deletes arcs, so that |
| 1962 |
* the original arcs must be the last arcs in their to-states' inchains. |
| 1963 |
* |
| 1964 |
* So the process here is that, for each state in the NFA, we gather up |
| 1965 |
* all non-EMPTY inarcs of states that can reach the target state via |
| 1966 |
* EMPTY arcs. We then sort, de-duplicate, and merge these arcs into the |
| 1967 |
* target state's inchain. (We can safely use sort-merge for this as long |
| 1968 |
* as we update each state's original-arcs pointer after we add arcs to |
| 1969 |
* it; the sort step of mergeins probably changed the order of the old |
| 1970 |
* arcs.) |
| 1971 |
* |
| 1972 |
* Another refinement worth making is that, because we only add non-EMPTY |
| 1973 |
* arcs during this phase, and all added arcs have the same from-state as |
| 1974 |
* the non-EMPTY arc they were cloned from, we know ahead of time that any |
| 1975 |
* states having only EMPTY outarcs will be useless for lack of outarcs |
| 1976 |
* after we drop the EMPTY arcs. (They cannot gain non-EMPTY outarcs if |
| 1977 |
* they had none to start with.) So we need not bother to update the |
| 1978 |
* inchains of such states at all. |
| 1979 |
*/ |
| 1980 |
|
| 1981 |
/* Remember the states' first original inarcs */ |
| 1982 |
/* ... and while at it, count how many old inarcs there are altogether */ |
| 1983 |
inarcsorig = (struct arc **) MALLOC(nfa->nstates * sizeof(struct arc *)); |
| 1984 |
if (inarcsorig == NULL) { |
| 1985 |
NERR(REG_ESPACE); |
| 1986 |
return; |
| 1987 |
} |
| 1988 |
totalinarcs = 0; |
| 1989 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 1990 |
inarcsorig[s->no] = s->ins; |
| 1991 |
totalinarcs += s->nins; |
| 1992 |
} |
| 1993 |
|
| 1994 |
/* |
| 1995 |
* Create a workspace for accumulating the inarcs to be added to the |
| 1996 |
* current target state. totalinarcs is probably a considerable |
| 1997 |
* overestimate of the space needed, but the NFA is unlikely to be large |
| 1998 |
* enough at this point to make it worth being smarter. |
| 1999 |
*/ |
| 2000 |
arcarray = (struct arc **) MALLOC(totalinarcs * sizeof(struct arc *)); |
| 2001 |
if (arcarray == NULL) { |
| 2002 |
NERR(REG_ESPACE); |
| 2003 |
FREE(inarcsorig); |
| 2004 |
return; |
| 2005 |
} |
| 2006 |
|
| 2007 |
/* And iterate over the target states */ |
| 2008 |
for (s = nfa->states; s != NULL && !NISERR(); s = s->next) { |
| 2009 |
/* Ignore target states without non-EMPTY outarcs, per note above */ |
| 2010 |
if (!s->flag && !hasnonemptyout(s)) { |
| 2011 |
continue; |
| 2012 |
} |
| 2013 |
|
| 2014 |
/* Find predecessor states and accumulate their original inarcs */ |
| 2015 |
arccount = 0; |
| 2016 |
for (s2 = emptyreachable(nfa, s, s, inarcsorig); s2 != s; s2 = nexts) { |
| 2017 |
/* Add s2's original inarcs to arcarray[], but ignore empties */ |
| 2018 |
for (a = inarcsorig[s2->no]; a != NULL; a = a->inchain) { |
| 2019 |
if (a->type != EMPTY) { |
| 2020 |
arcarray[arccount++] = a; |
| 2021 |
} |
| 2022 |
} |
| 2023 |
|
| 2024 |
/* Reset the tmp fields as we walk back */ |
| 2025 |
nexts = s2->tmp; |
| 2026 |
s2->tmp = NULL; |
| 2027 |
} |
| 2028 |
s->tmp = NULL; |
| 2029 |
assert(arccount <= totalinarcs); |
| 2030 |
|
| 2031 |
/* Remember how many original inarcs this state has */ |
| 2032 |
prevnins = s->nins; |
| 2033 |
|
| 2034 |
/* Add non-duplicate inarcs to target state */ |
| 2035 |
mergeins(nfa, s, arcarray, arccount); |
| 2036 |
|
| 2037 |
/* Now we must update the state's inarcsorig pointer */ |
| 2038 |
nskip = s->nins - prevnins; |
| 2039 |
a = s->ins; |
| 2040 |
while (nskip-- > 0) { |
| 2041 |
a = a->inchain; |
| 2042 |
} |
| 2043 |
inarcsorig[s->no] = a; |
| 2044 |
} |
| 2045 |
|
| 2046 |
FREE(arcarray); |
| 2047 |
FREE(inarcsorig); |
| 2048 |
|
| 2049 |
if (NISERR()) { |
| 2050 |
return; |
| 2051 |
} |
| 2052 |
|
| 2053 |
/* |
| 2054 |
* Remove all the EMPTY arcs, since we don't need them anymore. |
| 2055 |
*/ |
| 2056 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 2057 |
for (a = s->outs; a != NULL; a = nexta) { |
| 2058 |
nexta = a->outchain; |
| 2059 |
if (a->type == EMPTY) { |
| 2060 |
freearc(nfa, a); |
| 2061 |
} |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
/* |
| 2066 |
* And remove any states that have become useless. (This cleanup is |
| 2067 |
* not very thorough, and would be even less so if we tried to |
| 2068 |
* combine it with the previous step; but cleanup() will take care |
| 2069 |
* of anything we miss.) |
| 2070 |
*/ |
| 2071 |
for (s = nfa->states; s != NULL; s = nexts) { |
| 2072 |
nexts = s->next; |
| 2073 |
if ((s->nins == 0 || s->nouts == 0) && !s->flag) { |
| 2074 |
dropstate(nfa, s); |
| 2075 |
} |
| 2076 |
} |
| 2077 |
|
| 2078 |
if (f != NULL) { |
| 2079 |
dumpnfa(nfa, f); |
| 2080 |
} |
| 2081 |
} |
| 2082 |
|
| 2083 |
/* |
| 2084 |
- emptyreachable - recursively find all states that can reach s by EMPTY arcs |
| 2085 |
* The return value is the last such state found. Its tmp field links back |
| 2086 |
* to the next-to-last such state, and so on back to s, so that all these |
| 2087 |
* states can be located without searching the whole NFA. |
| 2088 |
* |
| 2089 |
* Since this is only used in fixempties(), we pass in the inarcsorig[] array |
| 2090 |
* maintained by that function. This lets us skip over all new inarcs, which |
| 2091 |
* are certainly not EMPTY arcs. |
| 2092 |
* |
| 2093 |
* The maximum recursion depth here is equal to the length of the longest |
| 2094 |
* loop-free chain of EMPTY arcs, which is surely no more than the size of |
| 2095 |
* the NFA, and in practice will be less than that. |
| 2096 |
^ static struct state *emptyreachable(struct state *, struct state *); |
| 2097 |
*/ |
| 2098 |
static struct state * |
| 2099 |
emptyreachable( |
| 2100 |
struct nfa *nfa, |
| 2101 |
struct state *s, |
| 2102 |
struct state *lastfound, |
| 2103 |
struct arc **inarcsorig) |
| 2104 |
{ |
| 2105 |
struct arc *a; |
| 2106 |
|
| 2107 |
s->tmp = lastfound; |
| 2108 |
lastfound = s; |
| 2109 |
for (a = inarcsorig[s->no]; a != NULL; a = a->inchain) { |
| 2110 |
if (a->type == EMPTY && a->from->tmp == NULL) { |
| 2111 |
lastfound = emptyreachable(nfa, a->from, lastfound, inarcsorig); |
| 2112 |
} |
| 2113 |
} |
| 2114 |
return lastfound; |
| 2115 |
} |
| 2116 |
|
| 2117 |
/* |
| 2118 |
* isconstraintarc - detect whether an arc is of a constraint type |
| 2119 |
*/ |
| 2120 |
static inline int |
| 2121 |
isconstraintarc(struct arc * a) |
| 2122 |
{ |
| 2123 |
switch (a->type) |
| 2124 |
{ |
| 2125 |
case '^': |
| 2126 |
case '$': |
| 2127 |
case BEHIND: |
| 2128 |
case AHEAD: |
| 2129 |
case LACON: |
| 2130 |
return 1; |
| 2131 |
} |
| 2132 |
return 0; |
| 2133 |
} |
| 2134 |
|
| 2135 |
/* |
| 2136 |
* hasconstraintout - does state have a constraint out arc? |
| 2137 |
*/ |
| 2138 |
static int |
| 2139 |
hasconstraintout(struct state * s) |
| 2140 |
{ |
| 2141 |
struct arc *a; |
| 2142 |
|
| 2143 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 2144 |
if (isconstraintarc(a)) { |
| 2145 |
return 1; |
| 2146 |
} |
| 2147 |
} |
| 2148 |
return 0; |
| 2149 |
} |
| 2150 |
|
| 2151 |
/* |
| 2152 |
* fixconstraintloops - get rid of loops containing only constraint arcs |
| 2153 |
* |
| 2154 |
* A loop of states that contains only constraint arcs is useless, since |
| 2155 |
* passing around the loop represents no forward progress. Moreover, it |
| 2156 |
* would cause infinite looping in pullback/pushfwd, so we need to get rid |
| 2157 |
* of such loops before doing that. |
| 2158 |
*/ |
| 2159 |
static void |
| 2160 |
fixconstraintloops( |
| 2161 |
struct nfa * nfa, |
| 2162 |
FILE *f) /* for debug output; NULL none */ |
| 2163 |
{ |
| 2164 |
struct state *s; |
| 2165 |
struct state *nexts; |
| 2166 |
struct arc *a; |
| 2167 |
struct arc *nexta; |
| 2168 |
int hasconstraints; |
| 2169 |
|
| 2170 |
/* |
| 2171 |
* In the trivial case of a state that loops to itself, we can just drop |
| 2172 |
* the constraint arc altogether. This is worth special-casing because |
| 2173 |
* such loops are far more common than loops containing multiple states. |
| 2174 |
* While we're at it, note whether any constraint arcs survive. |
| 2175 |
*/ |
| 2176 |
hasconstraints = 0; |
| 2177 |
for (s = nfa->states; s != NULL && !NISERR(); s = nexts) { |
| 2178 |
nexts = s->next; |
| 2179 |
/* while we're at it, ensure tmp fields are clear for next step */ |
| 2180 |
assert(s->tmp == NULL); |
| 2181 |
for (a = s->outs; a != NULL && !NISERR(); a = nexta) { |
| 2182 |
nexta = a->outchain; |
| 2183 |
if (isconstraintarc(a)) { |
| 2184 |
if (a->to == s) { |
| 2185 |
freearc(nfa, a); |
| 2186 |
} else { |
| 2187 |
hasconstraints = 1; |
| 2188 |
} |
| 2189 |
} |
| 2190 |
} |
| 2191 |
/* If we removed all the outarcs, the state is useless. */ |
| 2192 |
if (s->nouts == 0 && !s->flag) { |
| 2193 |
dropstate(nfa, s); |
| 2194 |
} |
| 2195 |
} |
| 2196 |
|
| 2197 |
/* Nothing to do if no remaining constraint arcs */ |
| 2198 |
if (NISERR() || !hasconstraints) { |
| 2199 |
return; |
| 2200 |
} |
| 2201 |
|
| 2202 |
/* |
| 2203 |
* Starting from each remaining NFA state, search outwards for a |
| 2204 |
* constraint loop. If we find a loop, break the loop, then start the |
| 2205 |
* search over. (We could possibly retain some state from the first scan, |
| 2206 |
* but it would complicate things greatly, and multi-state constraint |
| 2207 |
* loops are rare enough that it's not worth optimizing the case.) |
| 2208 |
*/ |
| 2209 |
restart: |
| 2210 |
for (s = nfa->states; s != NULL && !NISERR(); s = s->next) { |
| 2211 |
if (findconstraintloop(nfa, s)) { |
| 2212 |
goto restart; |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
if (NISERR()) { |
| 2217 |
return; |
| 2218 |
} |
| 2219 |
|
| 2220 |
/* |
| 2221 |
* Now remove any states that have become useless. (This cleanup is not |
| 2222 |
* very thorough, and would be even less so if we tried to combine it with |
| 2223 |
* the previous step; but cleanup() will take care of anything we miss.) |
| 2224 |
* |
| 2225 |
* Because findconstraintloop intentionally doesn't reset all tmp fields, |
| 2226 |
* we have to clear them after it's done. This is a convenient place to |
| 2227 |
* do that, too. |
| 2228 |
*/ |
| 2229 |
for (s = nfa->states; s != NULL; s = nexts) { |
| 2230 |
nexts = s->next; |
| 2231 |
s->tmp = NULL; |
| 2232 |
if ((s->nins == 0 || s->nouts == 0) && !s->flag) { |
| 2233 |
dropstate(nfa, s); |
| 2234 |
} |
| 2235 |
} |
| 2236 |
|
| 2237 |
if (f != NULL) { |
| 2238 |
dumpnfa(nfa, f); |
| 2239 |
} |
| 2240 |
} |
| 2241 |
|
| 2242 |
/* |
| 2243 |
* findconstraintloop - recursively find a loop of constraint arcs |
| 2244 |
* |
| 2245 |
* If we find a loop, break it by calling breakconstraintloop(), then |
| 2246 |
* return 1; otherwise return 0. |
| 2247 |
* |
| 2248 |
* State tmp fields are guaranteed all NULL on a success return, because |
| 2249 |
* breakconstraintloop does that. After a failure return, any state that |
| 2250 |
* is known not to be part of a loop is marked with s->tmp == s; this allows |
| 2251 |
* us not to have to re-prove that fact on later calls. (This convention is |
| 2252 |
* workable because we already eliminated single-state loops.) |
| 2253 |
* |
| 2254 |
* Note that the found loop doesn't necessarily include the first state we |
| 2255 |
* are called on. Any loop reachable from that state will do. |
| 2256 |
* |
| 2257 |
* The maximum recursion depth here is one more than the length of the longest |
| 2258 |
* loop-free chain of constraint arcs, which is surely no more than the size |
| 2259 |
* of the NFA, and in practice will be a lot less than that. |
| 2260 |
*/ |
| 2261 |
static int |
| 2262 |
findconstraintloop(struct nfa * nfa, struct state * s) |
| 2263 |
{ |
| 2264 |
struct arc *a; |
| 2265 |
|
| 2266 |
/* Since this is recursive, it could be driven to stack overflow */ |
| 2267 |
if (STACK_TOO_DEEP(nfa->v->re)) { |
| 2268 |
NERR(REG_ETOOBIG); |
| 2269 |
return 1; /* to exit as quickly as possible */ |
| 2270 |
} |
| 2271 |
|
| 2272 |
if (s->tmp != NULL) { |
| 2273 |
/* Already proven uninteresting? */ |
| 2274 |
if (s->tmp == s) { |
| 2275 |
return 0; |
| 2276 |
} |
| 2277 |
/* Found a loop involving s */ |
| 2278 |
breakconstraintloop(nfa, s); |
| 2279 |
/* The tmp fields have been cleaned up by breakconstraintloop */ |
| 2280 |
return 1; |
| 2281 |
} |
| 2282 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 2283 |
if (isconstraintarc(a)) { |
| 2284 |
struct state *sto = a->to; |
| 2285 |
|
| 2286 |
assert(sto != s); |
| 2287 |
s->tmp = sto; |
| 2288 |
if (findconstraintloop(nfa, sto)) { |
| 2289 |
return 1; |
| 2290 |
} |
| 2291 |
} |
| 2292 |
} |
| 2293 |
|
| 2294 |
/* |
| 2295 |
* If we get here, no constraint loop exists leading out from s. Mark it |
| 2296 |
* with s->tmp == s so we need not rediscover that fact again later. |
| 2297 |
*/ |
| 2298 |
s->tmp = s; |
| 2299 |
return 0; |
| 2300 |
} |
| 2301 |
|
| 2302 |
/* |
| 2303 |
* breakconstraintloop - break a loop of constraint arcs |
| 2304 |
* |
| 2305 |
* sinitial is any one member state of the loop. Each loop member's tmp |
| 2306 |
* field links to its successor within the loop. (Note that this function |
| 2307 |
* will reset all the tmp fields to NULL.) |
| 2308 |
* |
| 2309 |
* We can break the loop by, for any one state S1 in the loop, cloning its |
| 2310 |
* loop successor state S2 (and possibly following states), and then moving |
| 2311 |
* all S1->S2 constraint arcs to point to the cloned S2. The cloned S2 should |
| 2312 |
* copy any non-constraint outarcs of S2. Constraint outarcs should be |
| 2313 |
* dropped if they point back to S1, else they need to be copied as arcs to |
| 2314 |
* similarly cloned states S3, S4, etc. In general, each cloned state copies |
| 2315 |
* non-constraint outarcs, drops constraint outarcs that would lead to itself |
| 2316 |
* or any earlier cloned state, and sends other constraint outarcs to newly |
| 2317 |
* cloned states. No cloned state will have any inarcs that aren't constraint |
| 2318 |
* arcs or do not lead from S1 or earlier-cloned states. It's okay to drop |
| 2319 |
* constraint back-arcs since they would not take us to any state we've not |
| 2320 |
* already been in; therefore, no new constraint loop is created. In this way |
| 2321 |
* we generate a modified NFA that can still represent every useful state |
| 2322 |
* sequence, but not sequences that represent state loops with no consumption |
| 2323 |
* of input data. Note that the set of cloned states will certainly include |
| 2324 |
* all of the loop member states other than S1, and it may also include |
| 2325 |
* non-loop states that are reachable from S2 via constraint arcs. This is |
| 2326 |
* important because there is no guarantee that findconstraintloop found a |
| 2327 |
* maximal loop (and searching for one would be NP-hard, so don't try). |
| 2328 |
* Frequently the "non-loop states" are actually part of a larger loop that |
| 2329 |
* we didn't notice, and indeed there may be several overlapping loops. |
| 2330 |
* This technique ensures convergence in such cases, while considering only |
| 2331 |
* the originally-found loop does not. |
| 2332 |
* |
| 2333 |
* If there is only one S1->S2 constraint arc, then that constraint is |
| 2334 |
* certainly satisfied when we enter any of the clone states. This means that |
| 2335 |
* in the common case where many of the constraint arcs are identically |
| 2336 |
* labeled, we can merge together clone states linked by a similarly-labeled |
| 2337 |
* constraint: if we can get to the first one we can certainly get to the |
| 2338 |
* second, so there's no need to distinguish. This greatly reduces the number |
| 2339 |
* of new states needed, so we preferentially break the given loop at a state |
| 2340 |
* pair where this is true. |
| 2341 |
* |
| 2342 |
* Furthermore, it's fairly common to find that a cloned successor state has |
| 2343 |
* no outarcs, especially if we're a bit aggressive about removing unnecessary |
| 2344 |
* outarcs. If that happens, then there is simply not any interesting state |
| 2345 |
* that can be reached through the predecessor's loop arcs, which means we can |
| 2346 |
* break the loop just by removing those loop arcs, with no new states added. |
| 2347 |
*/ |
| 2348 |
static void |
| 2349 |
breakconstraintloop(struct nfa * nfa, struct state * sinitial) |
| 2350 |
{ |
| 2351 |
struct state *s; |
| 2352 |
struct state *shead; |
| 2353 |
struct state *stail; |
| 2354 |
struct state *sclone; |
| 2355 |
struct state *nexts; |
| 2356 |
struct arc *refarc; |
| 2357 |
struct arc *a; |
| 2358 |
struct arc *nexta; |
| 2359 |
|
| 2360 |
/* |
| 2361 |
* Start by identifying which loop step we want to break at. |
| 2362 |
* Preferentially this is one with only one constraint arc. (XXX are |
| 2363 |
* there any other secondary heuristics we want to use here?) Set refarc |
| 2364 |
* to point to the selected lone constraint arc, if there is one. |
| 2365 |
*/ |
| 2366 |
refarc = NULL; |
| 2367 |
s = sinitial; |
| 2368 |
do { |
| 2369 |
nexts = s->tmp; |
| 2370 |
assert(nexts != s); /* should not see any one-element loops */ |
| 2371 |
if (refarc == NULL) { |
| 2372 |
int narcs = 0; |
| 2373 |
|
| 2374 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 2375 |
if (a->to == nexts && isconstraintarc(a)) { |
| 2376 |
refarc = a; |
| 2377 |
narcs++; |
| 2378 |
} |
| 2379 |
} |
| 2380 |
assert(narcs > 0); |
| 2381 |
if (narcs > 1) { |
| 2382 |
refarc = NULL; /* multiple constraint arcs here, no good */ |
| 2383 |
} |
| 2384 |
} |
| 2385 |
s = nexts; |
| 2386 |
} while (s != sinitial); |
| 2387 |
|
| 2388 |
if (refarc) { |
| 2389 |
/* break at the refarc */ |
| 2390 |
shead = refarc->from; |
| 2391 |
stail = refarc->to; |
| 2392 |
assert(stail == shead->tmp); |
| 2393 |
} else { |
| 2394 |
/* for lack of a better idea, break after sinitial */ |
| 2395 |
shead = sinitial; |
| 2396 |
stail = sinitial->tmp; |
| 2397 |
} |
| 2398 |
|
| 2399 |
/* |
| 2400 |
* Reset the tmp fields so that we can use them for local storage in |
| 2401 |
* clonesuccessorstates. (findconstraintloop won't mind, since it's just |
| 2402 |
* going to abandon its search anyway.) |
| 2403 |
*/ |
| 2404 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 2405 |
s->tmp = NULL; |
| 2406 |
} |
| 2407 |
|
| 2408 |
/* |
| 2409 |
* Recursively build clone state(s) as needed. |
| 2410 |
*/ |
| 2411 |
sclone = newstate(nfa); |
| 2412 |
if (sclone == NULL) { |
| 2413 |
assert(NISERR()); |
| 2414 |
return; |
| 2415 |
} |
| 2416 |
|
| 2417 |
clonesuccessorstates(nfa, stail, sclone, shead, refarc, |
| 2418 |
NULL, NULL, nfa->nstates); |
| 2419 |
|
| 2420 |
if (NISERR()) { |
| 2421 |
return; |
| 2422 |
} |
| 2423 |
|
| 2424 |
/* |
| 2425 |
* It's possible that sclone has no outarcs at all, in which case it's |
| 2426 |
* useless. (We don't try extremely hard to get rid of useless states |
| 2427 |
* here, but this is an easy and fairly common case.) |
| 2428 |
*/ |
| 2429 |
if (sclone->nouts == 0) { |
| 2430 |
freestate(nfa, sclone); |
| 2431 |
sclone = NULL; |
| 2432 |
} |
| 2433 |
|
| 2434 |
/* |
| 2435 |
* Move shead's constraint-loop arcs to point to sclone, or just drop them |
| 2436 |
* if we discovered we don't need sclone. |
| 2437 |
*/ |
| 2438 |
for (a = shead->outs; a != NULL; a = nexta) { |
| 2439 |
nexta = a->outchain; |
| 2440 |
if (a->to == stail && isconstraintarc(a)) { |
| 2441 |
if (sclone) { |
| 2442 |
cparc(nfa, a, shead, sclone); |
| 2443 |
} |
| 2444 |
freearc(nfa, a); |
| 2445 |
if (NISERR()) { |
| 2446 |
break; |
| 2447 |
} |
| 2448 |
} |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
/* |
| 2453 |
* clonesuccessorstates - create a tree of constraint-arc successor states |
| 2454 |
* |
| 2455 |
* ssource is the state to be cloned, and sclone is the state to copy its |
| 2456 |
* outarcs into. sclone's inarcs, if any, should already be set up. |
| 2457 |
* |
| 2458 |
* spredecessor is the original predecessor state that we are trying to build |
| 2459 |
* successors for (it may not be the immediate predecessor of ssource). |
| 2460 |
* refarc, if not NULL, is the original constraint arc that is known to have |
| 2461 |
* been traversed out of spredecessor to reach the successor(s). |
| 2462 |
* |
| 2463 |
* For each cloned successor state, we transiently create a "donemap" that is |
| 2464 |
* a boolean array showing which source states we've already visited for this |
| 2465 |
* clone state. This prevents infinite recursion as well as useless repeat |
| 2466 |
* visits to the same state subtree (which can add up fast, since typical NFAs |
| 2467 |
* have multiple redundant arc pathways). Each donemap is a char array |
| 2468 |
* indexed by state number. The donemaps are all of the same size "nstates", |
| 2469 |
* which is nfa->nstates as of the start of the recursion. This is enough to |
| 2470 |
* have entries for all preexisting states, but *not* entries for clone |
| 2471 |
* states created during the recursion. That's okay since we have no need to |
| 2472 |
* mark those. |
| 2473 |
* |
| 2474 |
* curdonemap is NULL when recursing to a new sclone state, or sclone's |
| 2475 |
* donemap when we are recursing without having created a new state (which we |
| 2476 |
* do when we decide we can merge a successor state into the current clone |
| 2477 |
* state). outerdonemap is NULL at the top level and otherwise the parent |
| 2478 |
* clone state's donemap. |
| 2479 |
* |
| 2480 |
* The successor states we create and fill here form a strict tree structure, |
| 2481 |
* with each state having exactly one predecessor, except that the toplevel |
| 2482 |
* state has no inarcs as yet (breakconstraintloop will add its inarcs from |
| 2483 |
* spredecessor after we're done). Thus, we can examine sclone's inarcs back |
| 2484 |
* to the root, plus refarc if any, to identify the set of constraints already |
| 2485 |
* known valid at the current point. This allows us to avoid generating extra |
| 2486 |
* successor states. |
| 2487 |
*/ |
| 2488 |
static void |
| 2489 |
clonesuccessorstates( |
| 2490 |
struct nfa * nfa, |
| 2491 |
struct state * ssource, |
| 2492 |
struct state * sclone, |
| 2493 |
struct state * spredecessor, |
| 2494 |
struct arc * refarc, |
| 2495 |
char *curdonemap, |
| 2496 |
char *outerdonemap, |
| 2497 |
int nstates) |
| 2498 |
{ |
| 2499 |
char *donemap; |
| 2500 |
struct arc *a; |
| 2501 |
|
| 2502 |
/* Since this is recursive, it could be driven to stack overflow */ |
| 2503 |
if (STACK_TOO_DEEP(nfa->v->re)) { |
| 2504 |
NERR(REG_ETOOBIG); |
| 2505 |
return; |
| 2506 |
} |
| 2507 |
|
| 2508 |
/* If this state hasn't already got a donemap, create one */ |
| 2509 |
donemap = curdonemap; |
| 2510 |
if (donemap == NULL) { |
| 2511 |
donemap = (char *) MALLOC(nstates * sizeof(char)); |
| 2512 |
if (donemap == NULL) { |
| 2513 |
NERR(REG_ESPACE); |
| 2514 |
return; |
| 2515 |
} |
| 2516 |
|
| 2517 |
if (outerdonemap != NULL) { |
| 2518 |
/* |
| 2519 |
* Not at outermost recursion level, so copy the outer level's |
| 2520 |
* donemap; this ensures that we see states in process of being |
| 2521 |
* visited at outer levels, or already merged into predecessor |
| 2522 |
* states, as ones we shouldn't traverse back to. |
| 2523 |
*/ |
| 2524 |
memcpy(donemap, outerdonemap, nstates * sizeof(char)); |
| 2525 |
} else { |
| 2526 |
/* At outermost level, only spredecessor is off-limits */ |
| 2527 |
memset(donemap, 0, nstates * sizeof(char)); |
| 2528 |
assert(spredecessor->no < nstates); |
| 2529 |
donemap[spredecessor->no] = 1; |
| 2530 |
} |
| 2531 |
} |
| 2532 |
|
| 2533 |
/* Mark ssource as visited in the donemap */ |
| 2534 |
assert(ssource->no < nstates); |
| 2535 |
assert(donemap[ssource->no] == 0); |
| 2536 |
donemap[ssource->no] = 1; |
| 2537 |
|
| 2538 |
/* |
| 2539 |
* We proceed by first cloning all of ssource's outarcs, creating new |
| 2540 |
* clone states as needed but not doing more with them than that. Then in |
| 2541 |
* a second pass, recurse to process the child clone states. This allows |
| 2542 |
* us to have only one child clone state per reachable source state, even |
| 2543 |
* when there are multiple outarcs leading to the same state. Also, when |
| 2544 |
* we do visit a child state, its set of inarcs is known exactly, which |
| 2545 |
* makes it safe to apply the constraint-is-already-checked optimization. |
| 2546 |
* Also, this ensures that we've merged all the states we can into the |
| 2547 |
* current clone before we recurse to any children, thus possibly saving |
| 2548 |
* them from making extra images of those states. |
| 2549 |
* |
| 2550 |
* While this function runs, child clone states of the current state are |
| 2551 |
* marked by setting their tmp fields to point to the original state they |
| 2552 |
* were cloned from. This makes it possible to detect multiple outarcs |
| 2553 |
* leading to the same state, and also makes it easy to distinguish clone |
| 2554 |
* states from original states (which will have tmp == NULL). |
| 2555 |
*/ |
| 2556 |
for (a = ssource->outs; a != NULL && !NISERR(); a = a->outchain) { |
| 2557 |
struct state *sto = a->to; |
| 2558 |
|
| 2559 |
/* |
| 2560 |
* We do not consider cloning successor states that have no constraint |
| 2561 |
* outarcs; just link to them as-is. They cannot be part of a |
| 2562 |
* constraint loop so there is no need to make copies. In particular, |
| 2563 |
* this rule keeps us from trying to clone the post state, which would |
| 2564 |
* be a bad idea. |
| 2565 |
*/ |
| 2566 |
if (isconstraintarc(a) && hasconstraintout(sto)) { |
| 2567 |
struct state *prevclone; |
| 2568 |
int canmerge; |
| 2569 |
struct arc *a2; |
| 2570 |
|
| 2571 |
/* |
| 2572 |
* Back-link constraint arcs must not be followed. Nor is there a |
| 2573 |
* need to revisit states previously merged into this clone. |
| 2574 |
*/ |
| 2575 |
assert(sto->no < nstates); |
| 2576 |
if (donemap[sto->no] != 0) { |
| 2577 |
continue; |
| 2578 |
} |
| 2579 |
|
| 2580 |
/* |
| 2581 |
* Check whether we already have a child clone state for this |
| 2582 |
* source state. |
| 2583 |
*/ |
| 2584 |
prevclone = NULL; |
| 2585 |
for (a2 = sclone->outs; a2 != NULL; a2 = a2->outchain) { |
| 2586 |
if (a2->to->tmp == sto) { |
| 2587 |
prevclone = a2->to; |
| 2588 |
break; |
| 2589 |
} |
| 2590 |
} |
| 2591 |
|
| 2592 |
/* |
| 2593 |
* If this arc is labeled the same as refarc, or the same as any |
| 2594 |
* arc we must have traversed to get to sclone, then no additional |
| 2595 |
* constraints need to be met to get to sto, so we should just |
| 2596 |
* merge its outarcs into sclone. |
| 2597 |
*/ |
| 2598 |
if (refarc && a->type == refarc->type && a->co == refarc->co) { |
| 2599 |
canmerge = 1; |
| 2600 |
} else { |
| 2601 |
struct state *s; |
| 2602 |
|
| 2603 |
canmerge = 0; |
| 2604 |
for (s = sclone; s->ins; s = s->ins->from) { |
| 2605 |
if (s->nins == 1 && |
| 2606 |
a->type == s->ins->type && a->co == s->ins->co) { |
| 2607 |
canmerge = 1; |
| 2608 |
break; |
| 2609 |
} |
| 2610 |
} |
| 2611 |
} |
| 2612 |
|
| 2613 |
if (canmerge) { |
| 2614 |
/* |
| 2615 |
* We can merge into sclone. If we previously made a child |
| 2616 |
* clone state, drop it; there's no need to visit it. (This |
| 2617 |
* can happen if ssource has multiple pathways to sto, and we |
| 2618 |
* only just now found one that is provably a no-op.) |
| 2619 |
*/ |
| 2620 |
if (prevclone) { |
| 2621 |
dropstate(nfa, prevclone); /* kills our outarc, too */ |
| 2622 |
} |
| 2623 |
|
| 2624 |
/* Recurse to merge sto's outarcs into sclone */ |
| 2625 |
clonesuccessorstates(nfa, sto, sclone, spredecessor, refarc, |
| 2626 |
donemap, outerdonemap, nstates); |
| 2627 |
/* sto should now be marked as previously visited */ |
| 2628 |
assert(NISERR() || donemap[sto->no] == 1); |
| 2629 |
} else if (prevclone) { |
| 2630 |
/* |
| 2631 |
* We already have a clone state for this successor, so just |
| 2632 |
* make another arc to it. |
| 2633 |
*/ |
| 2634 |
cparc(nfa, a, sclone, prevclone); |
| 2635 |
} else { |
| 2636 |
/* |
| 2637 |
* We need to create a new successor clone state. |
| 2638 |
*/ |
| 2639 |
struct state *stoclone; |
| 2640 |
|
| 2641 |
stoclone = newstate(nfa); |
| 2642 |
if (stoclone == NULL) { |
| 2643 |
assert(NISERR()); |
| 2644 |
break; |
| 2645 |
} |
| 2646 |
/* Mark it as to what it's a clone of */ |
| 2647 |
stoclone->tmp = sto; |
| 2648 |
/* ... and add the outarc leading to it */ |
| 2649 |
cparc(nfa, a, sclone, stoclone); |
| 2650 |
} |
| 2651 |
} else { |
| 2652 |
/* |
| 2653 |
* Non-constraint outarcs just get copied to sclone, as do outarcs |
| 2654 |
* leading to states with no constraint outarc. |
| 2655 |
*/ |
| 2656 |
cparc(nfa, a, sclone, sto); |
| 2657 |
} |
| 2658 |
} |
| 2659 |
|
| 2660 |
/* |
| 2661 |
* If we are at outer level for this clone state, recurse to all its child |
| 2662 |
* clone states, clearing their tmp fields as we go. (If we're not |
| 2663 |
* outermost for sclone, leave this to be done by the outer call level.) |
| 2664 |
* Note that if we have multiple outarcs leading to the same clone state, |
| 2665 |
* it will only be recursed-to once. |
| 2666 |
*/ |
| 2667 |
if (curdonemap == NULL) { |
| 2668 |
for (a = sclone->outs; a != NULL && !NISERR(); a = a->outchain) { |
| 2669 |
struct state *stoclone = a->to; |
| 2670 |
struct state *sto = stoclone->tmp; |
| 2671 |
|
| 2672 |
if (sto != NULL) { |
| 2673 |
stoclone->tmp = NULL; |
| 2674 |
clonesuccessorstates(nfa, sto, stoclone, spredecessor, refarc, |
| 2675 |
NULL, donemap, nstates); |
| 2676 |
} |
| 2677 |
} |
| 2678 |
|
| 2679 |
/* Don't forget to free sclone's donemap when done with it */ |
| 2680 |
FREE(donemap); |
| 2681 |
} |
| 2682 |
} |
| 2683 |
|
| 2684 |
/* |
| 2685 |
- cleanup - clean up NFA after optimizations |
| 2686 |
^ static void cleanup(struct nfa *); |
| 2687 |
*/ |
| 2688 |
static void |
| 2689 |
cleanup( |
| 2690 |
struct nfa *nfa) |
| 2691 |
{ |
| 2692 |
struct state *s; |
| 2693 |
struct state *nexts; |
| 2694 |
int n; |
| 2695 |
|
| 2696 |
/* |
| 2697 |
* Clear out unreachable or dead-end states. Use pre to mark reachable, |
| 2698 |
* then post to mark can-reach-post. |
| 2699 |
*/ |
| 2700 |
|
| 2701 |
markreachable(nfa, nfa->pre, NULL, nfa->pre); |
| 2702 |
markcanreach(nfa, nfa->post, nfa->pre, nfa->post); |
| 2703 |
for (s = nfa->states; s != NULL; s = nexts) { |
| 2704 |
nexts = s->next; |
| 2705 |
if (s->tmp != nfa->post && !s->flag) { |
| 2706 |
dropstate(nfa, s); |
| 2707 |
} |
| 2708 |
} |
| 2709 |
assert(nfa->post->nins == 0 || nfa->post->tmp == nfa->post); |
| 2710 |
cleartraverse(nfa, nfa->pre); |
| 2711 |
assert(nfa->post->nins == 0 || nfa->post->tmp == NULL); |
| 2712 |
/* the nins==0 (final unreachable) case will be caught later */ |
| 2713 |
|
| 2714 |
/* |
| 2715 |
* Renumber surviving states. |
| 2716 |
*/ |
| 2717 |
|
| 2718 |
n = 0; |
| 2719 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 2720 |
s->no = n++; |
| 2721 |
} |
| 2722 |
nfa->nstates = n; |
| 2723 |
} |
| 2724 |
|
| 2725 |
/* |
| 2726 |
- markreachable - recursive marking of reachable states |
| 2727 |
^ static void markreachable(struct nfa *, struct state *, struct state *, |
| 2728 |
^ struct state *); |
| 2729 |
*/ |
| 2730 |
static void |
| 2731 |
markreachable( |
| 2732 |
struct nfa *nfa, |
| 2733 |
struct state *s, |
| 2734 |
struct state *okay, /* consider only states with this mark */ |
| 2735 |
struct state *mark) /* the value to mark with */ |
| 2736 |
{ |
| 2737 |
struct arc *a; |
| 2738 |
|
| 2739 |
if (s->tmp != okay) { |
| 2740 |
return; |
| 2741 |
} |
| 2742 |
s->tmp = mark; |
| 2743 |
|
| 2744 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 2745 |
markreachable(nfa, a->to, okay, mark); |
| 2746 |
} |
| 2747 |
} |
| 2748 |
|
| 2749 |
/* |
| 2750 |
- markcanreach - recursive marking of states which can reach here |
| 2751 |
^ static void markcanreach(struct nfa *, struct state *, struct state *, |
| 2752 |
^ struct state *); |
| 2753 |
*/ |
| 2754 |
static void |
| 2755 |
markcanreach( |
| 2756 |
struct nfa *nfa, |
| 2757 |
struct state *s, |
| 2758 |
struct state *okay, /* consider only states with this mark */ |
| 2759 |
struct state *mark) /* the value to mark with */ |
| 2760 |
{ |
| 2761 |
struct arc *a; |
| 2762 |
|
| 2763 |
if (s->tmp != okay) { |
| 2764 |
return; |
| 2765 |
} |
| 2766 |
s->tmp = mark; |
| 2767 |
|
| 2768 |
for (a = s->ins; a != NULL; a = a->inchain) { |
| 2769 |
markcanreach(nfa, a->from, okay, mark); |
| 2770 |
} |
| 2771 |
} |
| 2772 |
|
| 2773 |
/* |
| 2774 |
- analyze - ascertain potentially-useful facts about an optimized NFA |
| 2775 |
^ static long analyze(struct nfa *); |
| 2776 |
*/ |
| 2777 |
static long /* re_info bits to be OR'ed in */ |
| 2778 |
analyze( |
| 2779 |
struct nfa *nfa) |
| 2780 |
{ |
| 2781 |
struct arc *a; |
| 2782 |
struct arc *aa; |
| 2783 |
|
| 2784 |
if (nfa->pre->outs == NULL) { |
| 2785 |
return REG_UIMPOSSIBLE; |
| 2786 |
} |
| 2787 |
for (a = nfa->pre->outs; a != NULL; a = a->outchain) { |
| 2788 |
for (aa = a->to->outs; aa != NULL; aa = aa->outchain) { |
| 2789 |
if (aa->to == nfa->post) { |
| 2790 |
return REG_UEMPTYMATCH; |
| 2791 |
} |
| 2792 |
} |
| 2793 |
} |
| 2794 |
return 0; |
| 2795 |
} |
| 2796 |
|
| 2797 |
/* |
| 2798 |
- compact - construct the compact representation of an NFA |
| 2799 |
^ static void compact(struct nfa *, struct cnfa *); |
| 2800 |
*/ |
| 2801 |
static void |
| 2802 |
compact( |
| 2803 |
struct nfa *nfa, |
| 2804 |
struct cnfa *cnfa) |
| 2805 |
{ |
| 2806 |
struct state *s; |
| 2807 |
struct arc *a; |
| 2808 |
size_t nstates; |
| 2809 |
size_t narcs; |
| 2810 |
struct carc *ca; |
| 2811 |
struct carc *first; |
| 2812 |
|
| 2813 |
assert(!NISERR()); |
| 2814 |
|
| 2815 |
nstates = 0; |
| 2816 |
narcs = 0; |
| 2817 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 2818 |
nstates++; |
| 2819 |
narcs += s->nouts + 1; /* need one extra for endmarker */ |
| 2820 |
} |
| 2821 |
|
| 2822 |
cnfa->stflags = (char *) MALLOC(nstates * sizeof(char)); |
| 2823 |
cnfa->states = (struct carc **) MALLOC(nstates * sizeof(struct carc *)); |
| 2824 |
cnfa->arcs = (struct carc *) MALLOC(narcs * sizeof(struct carc)); |
| 2825 |
if (cnfa->stflags == NULL || cnfa->states == NULL || cnfa->arcs == NULL) { |
| 2826 |
if (cnfa->stflags != NULL) { |
| 2827 |
FREE(cnfa->stflags); |
| 2828 |
} |
| 2829 |
if (cnfa->states != NULL) { |
| 2830 |
FREE(cnfa->states); |
| 2831 |
} |
| 2832 |
if (cnfa->arcs != NULL) { |
| 2833 |
FREE(cnfa->arcs); |
| 2834 |
} |
| 2835 |
NERR(REG_ESPACE); |
| 2836 |
return; |
| 2837 |
} |
| 2838 |
cnfa->nstates = nstates; |
| 2839 |
cnfa->pre = nfa->pre->no; |
| 2840 |
cnfa->post = nfa->post->no; |
| 2841 |
cnfa->bos[0] = nfa->bos[0]; |
| 2842 |
cnfa->bos[1] = nfa->bos[1]; |
| 2843 |
cnfa->eos[0] = nfa->eos[0]; |
| 2844 |
cnfa->eos[1] = nfa->eos[1]; |
| 2845 |
cnfa->ncolors = maxcolor(nfa->cm) + 1; |
| 2846 |
cnfa->flags = 0; |
| 2847 |
|
| 2848 |
ca = cnfa->arcs; |
| 2849 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 2850 |
assert((size_t) s->no < nstates); |
| 2851 |
cnfa->stflags[s->no] = 0; |
| 2852 |
cnfa->states[s->no] = ca; |
| 2853 |
first = ca; |
| 2854 |
for (a = s->outs; a != NULL; a = a->outchain) { |
| 2855 |
switch (a->type) { |
| 2856 |
case PLAIN: |
| 2857 |
ca->co = a->co; |
| 2858 |
ca->to = a->to->no; |
| 2859 |
ca++; |
| 2860 |
break; |
| 2861 |
case LACON: |
| 2862 |
assert(s->no != cnfa->pre); |
| 2863 |
ca->co = (color) (cnfa->ncolors + a->co); |
| 2864 |
ca->to = a->to->no; |
| 2865 |
ca++; |
| 2866 |
cnfa->flags |= HASLACONS; |
| 2867 |
break; |
| 2868 |
default: |
| 2869 |
NERR(REG_ASSERT); |
| 2870 |
break; |
| 2871 |
} |
| 2872 |
} |
| 2873 |
carcsort(first, ca - first); |
| 2874 |
ca->co = COLORLESS; |
| 2875 |
ca->to = 0; |
| 2876 |
ca++; |
| 2877 |
} |
| 2878 |
assert(ca == &cnfa->arcs[narcs]); |
| 2879 |
assert(cnfa->nstates != 0); |
| 2880 |
|
| 2881 |
/* |
| 2882 |
* Mark no-progress states. |
| 2883 |
*/ |
| 2884 |
|
| 2885 |
for (a = nfa->pre->outs; a != NULL; a = a->outchain) { |
| 2886 |
cnfa->stflags[a->to->no] = CNFA_NOPROGRESS; |
| 2887 |
} |
| 2888 |
cnfa->stflags[nfa->pre->no] = CNFA_NOPROGRESS; |
| 2889 |
} |
| 2890 |
|
| 2891 |
/* |
| 2892 |
- carcsort - sort compacted-NFA arcs by color |
| 2893 |
^ static void carcsort(struct carc *, struct carc *); |
| 2894 |
*/ |
| 2895 |
static void |
| 2896 |
carcsort( |
| 2897 |
struct carc *first, |
| 2898 |
size_t n) |
| 2899 |
{ |
| 2900 |
if (n > 1) { |
| 2901 |
qsort(first, n, sizeof(struct carc), carc_cmp); |
| 2902 |
} |
| 2903 |
} |
| 2904 |
|
| 2905 |
static int |
| 2906 |
carc_cmp( |
| 2907 |
const void *a, |
| 2908 |
const void *b) |
| 2909 |
{ |
| 2910 |
const struct carc *aa = (const struct carc *) a; |
| 2911 |
const struct carc *bb = (const struct carc *) b; |
| 2912 |
|
| 2913 |
if (aa->co < bb->co) { |
| 2914 |
return -1; |
| 2915 |
} |
| 2916 |
if (aa->co > bb->co) { |
| 2917 |
return +1; |
| 2918 |
} |
| 2919 |
if (aa->to < bb->to) { |
| 2920 |
return -1; |
| 2921 |
} |
| 2922 |
if (aa->to > bb->to) { |
| 2923 |
return +1; |
| 2924 |
} |
| 2925 |
return 0; |
| 2926 |
} |
| 2927 |
|
| 2928 |
/* |
| 2929 |
- freecnfa - free a compacted NFA |
| 2930 |
^ static void freecnfa(struct cnfa *); |
| 2931 |
*/ |
| 2932 |
static void |
| 2933 |
freecnfa( |
| 2934 |
struct cnfa *cnfa) |
| 2935 |
{ |
| 2936 |
assert(cnfa->nstates != 0); /* not empty already */ |
| 2937 |
cnfa->nstates = 0; |
| 2938 |
FREE(cnfa->stflags); |
| 2939 |
FREE(cnfa->states); |
| 2940 |
FREE(cnfa->arcs); |
| 2941 |
} |
| 2942 |
|
| 2943 |
/* |
| 2944 |
- dumpnfa - dump an NFA in human-readable form |
| 2945 |
^ static void dumpnfa(struct nfa *, FILE *); |
| 2946 |
*/ |
| 2947 |
static void |
| 2948 |
dumpnfa( |
| 2949 |
struct nfa *nfa, |
| 2950 |
FILE *f) |
| 2951 |
{ |
| 2952 |
#ifdef REG_DEBUG |
| 2953 |
struct state *s; |
| 2954 |
int nstates = 0; |
| 2955 |
int narcs = 0; |
| 2956 |
|
| 2957 |
fprintf(f, "pre %d, post %d", nfa->pre->no, nfa->post->no); |
| 2958 |
if (nfa->bos[0] != COLORLESS) { |
| 2959 |
fprintf(f, ", bos [%ld]", (long) nfa->bos[0]); |
| 2960 |
} |
| 2961 |
if (nfa->bos[1] != COLORLESS) { |
| 2962 |
fprintf(f, ", bol [%ld]", (long) nfa->bos[1]); |
| 2963 |
} |
| 2964 |
if (nfa->eos[0] != COLORLESS) { |
| 2965 |
fprintf(f, ", eos [%ld]", (long) nfa->eos[0]); |
| 2966 |
} |
| 2967 |
if (nfa->eos[1] != COLORLESS) { |
| 2968 |
fprintf(f, ", eol [%ld]", (long) nfa->eos[1]); |
| 2969 |
} |
| 2970 |
fprintf(f, "\n"); |
| 2971 |
for (s = nfa->states; s != NULL; s = s->next) { |
| 2972 |
dumpstate(s, f); |
| 2973 |
nstates++; |
| 2974 |
narcs += s->nouts; |
| 2975 |
} |
| 2976 |
fprintf(f, "total of %d states, %d arcs\n", nstates, narcs); |
| 2977 |
if (nfa->parent == NULL) { |
| 2978 |
dumpcolors(nfa->cm, f); |
| 2979 |
} |
| 2980 |
fflush(f); |
| 2981 |
#else |
| 2982 |
(void)nfa; |
| 2983 |
(void)f; |
| 2984 |
#endif |
| 2985 |
} |
| 2986 |
|
| 2987 |
#ifdef REG_DEBUG /* subordinates of dumpnfa */ |
| 2988 |
/* |
| 2989 |
^ #ifdef REG_DEBUG |
| 2990 |
*/ |
| 2991 |
|
| 2992 |
/* |
| 2993 |
- dumpstate - dump an NFA state in human-readable form |
| 2994 |
^ static void dumpstate(struct state *, FILE *); |
| 2995 |
*/ |
| 2996 |
static void |
| 2997 |
dumpstate( |
| 2998 |
struct state *s, |
| 2999 |
FILE *f) |
| 3000 |
{ |
| 3001 |
struct arc *a; |
| 3002 |
|
| 3003 |
fprintf(f, "%d%s%c", s->no, (s->tmp != NULL) ? "T" : "", |
| 3004 |
(s->flag) ? s->flag : '.'); |
| 3005 |
if (s->prev != NULL && s->prev->next != s) { |
| 3006 |
fprintf(f, "\tstate chain bad\n"); |
| 3007 |
} |
| 3008 |
if (s->nouts == 0) { |
| 3009 |
fprintf(f, "\tno out arcs\n"); |
| 3010 |
} else { |
| 3011 |
dumparcs(s, f); |
| 3012 |
} |
| 3013 |
fflush(f); |
| 3014 |
for (a = s->ins; a != NULL; a = a->inchain) { |
| 3015 |
if (a->to != s) { |
| 3016 |
fprintf(f, "\tlink from %d to %d on %d's in-chain\n", |
| 3017 |
a->from->no, a->to->no, s->no); |
| 3018 |
} |
| 3019 |
} |
| 3020 |
} |
| 3021 |
|
| 3022 |
/* |
| 3023 |
- dumparcs - dump out-arcs in human-readable form |
| 3024 |
^ static void dumparcs(struct state *, FILE *); |
| 3025 |
*/ |
| 3026 |
static void |
| 3027 |
dumparcs( |
| 3028 |
struct state *s, |
| 3029 |
FILE *f) |
| 3030 |
{ |
| 3031 |
int pos; |
| 3032 |
struct arc *a; |
| 3033 |
|
| 3034 |
/* printing oldest arcs first is usually clearer */ |
| 3035 |
a = s->outs; |
| 3036 |
assert(a != NULL); |
| 3037 |
while (a->outchain != NULL) { |
| 3038 |
a = a->outchain; |
| 3039 |
} |
| 3040 |
pos = 1; |
| 3041 |
do { |
| 3042 |
dumparc(a, s, f); |
| 3043 |
if (pos == 5) { |
| 3044 |
fprintf(f, "\n"); |
| 3045 |
pos = 1; |
| 3046 |
} else { |
| 3047 |
pos++; |
| 3048 |
} |
| 3049 |
a = a->outchainRev; |
| 3050 |
} while (a != NULL); |
| 3051 |
if (pos != 1) { |
| 3052 |
fprintf(f, "\n"); |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
/* |
| 3057 |
- dumparc - dump one outarc in readable form, including prefixing tab |
| 3058 |
^ static void dumparc(struct arc *, struct state *, FILE *); |
| 3059 |
*/ |
| 3060 |
static void |
| 3061 |
dumparc( |
| 3062 |
struct arc *a, |
| 3063 |
struct state *s, |
| 3064 |
FILE *f) |
| 3065 |
{ |
| 3066 |
struct arc *aa; |
| 3067 |
struct arcbatch *ab; |
| 3068 |
|
| 3069 |
fprintf(f, "\t"); |
| 3070 |
switch (a->type) { |
| 3071 |
case PLAIN: |
| 3072 |
fprintf(f, "[%ld]", (long) a->co); |
| 3073 |
break; |
| 3074 |
case AHEAD: |
| 3075 |
fprintf(f, ">%ld>", (long) a->co); |
| 3076 |
break; |
| 3077 |
case BEHIND: |
| 3078 |
fprintf(f, "<%ld<", (long) a->co); |
| 3079 |
break; |
| 3080 |
case LACON: |
| 3081 |
fprintf(f, ":%ld:", (long) a->co); |
| 3082 |
break; |
| 3083 |
case '^': |
| 3084 |
case '$': |
| 3085 |
fprintf(f, "%c%d", a->type, (int) a->co); |
| 3086 |
break; |
| 3087 |
case EMPTY: |
| 3088 |
break; |
| 3089 |
default: |
| 3090 |
fprintf(f, "0x%x/0%lo", a->type, (long) a->co); |
| 3091 |
break; |
| 3092 |
} |
| 3093 |
if (a->from != s) { |
| 3094 |
fprintf(f, "?%d?", a->from->no); |
| 3095 |
} |
| 3096 |
for (ab = &a->from->oas; ab != NULL; ab = ab->next) { |
| 3097 |
for (aa = &ab->a[0]; aa < &ab->a[ABSIZE]; aa++) { |
| 3098 |
if (aa == a) { |
| 3099 |
break; /* NOTE BREAK OUT */ |
| 3100 |
} |
| 3101 |
} |
| 3102 |
if (aa < &ab->a[ABSIZE]) { /* propagate break */ |
| 3103 |
break; /* NOTE BREAK OUT */ |
| 3104 |
} |
| 3105 |
} |
| 3106 |
if (ab == NULL) { |
| 3107 |
fprintf(f, "?!?"); /* not in allocated space */ |
| 3108 |
} |
| 3109 |
fprintf(f, "->"); |
| 3110 |
if (a->to == NULL) { |
| 3111 |
fprintf(f, "NULL"); |
| 3112 |
return; |
| 3113 |
} |
| 3114 |
fprintf(f, "%d", a->to->no); |
| 3115 |
for (aa = a->to->ins; aa != NULL; aa = aa->inchain) { |
| 3116 |
if (aa == a) { |
| 3117 |
break; /* NOTE BREAK OUT */ |
| 3118 |
} |
| 3119 |
} |
| 3120 |
if (aa == NULL) { |
| 3121 |
fprintf(f, "?!?"); /* missing from in-chain */ |
| 3122 |
} |
| 3123 |
} |
| 3124 |
|
| 3125 |
/* |
| 3126 |
^ #endif |
| 3127 |
*/ |
| 3128 |
#endif /* ifdef REG_DEBUG */ |
| 3129 |
|
| 3130 |
/* |
| 3131 |
- dumpcnfa - dump a compacted NFA in human-readable form |
| 3132 |
^ static void dumpcnfa(struct cnfa *, FILE *); |
| 3133 |
*/ |
| 3134 |
static void |
| 3135 |
dumpcnfa( |
| 3136 |
struct cnfa *cnfa, |
| 3137 |
FILE *f) |
| 3138 |
{ |
| 3139 |
#ifdef REG_DEBUG |
| 3140 |
int st; |
| 3141 |
|
| 3142 |
fprintf(f, "pre %d, post %d", cnfa->pre, cnfa->post); |
| 3143 |
if (cnfa->bos[0] != COLORLESS) { |
| 3144 |
fprintf(f, ", bos [%ld]", (long) cnfa->bos[0]); |
| 3145 |
} |
| 3146 |
if (cnfa->bos[1] != COLORLESS) { |
| 3147 |
fprintf(f, ", bol [%ld]", (long) cnfa->bos[1]); |
| 3148 |
} |
| 3149 |
if (cnfa->eos[0] != COLORLESS) { |
| 3150 |
fprintf(f, ", eos [%ld]", (long) cnfa->eos[0]); |
| 3151 |
} |
| 3152 |
if (cnfa->eos[1] != COLORLESS) { |
| 3153 |
fprintf(f, ", eol [%ld]", (long) cnfa->eos[1]); |
| 3154 |
} |
| 3155 |
if (cnfa->flags&HASLACONS) { |
| 3156 |
fprintf(f, ", haslacons"); |
| 3157 |
} |
| 3158 |
fprintf(f, "\n"); |
| 3159 |
for (st = 0; st < cnfa->nstates; st++) { |
| 3160 |
dumpcstate(st, cnfa, f); |
| 3161 |
} |
| 3162 |
fflush(f); |
| 3163 |
#else |
| 3164 |
(void)cnfa; |
| 3165 |
(void)f; |
| 3166 |
#endif |
| 3167 |
} |
| 3168 |
|
| 3169 |
#ifdef REG_DEBUG /* subordinates of dumpcnfa */ |
| 3170 |
/* |
| 3171 |
^ #ifdef REG_DEBUG |
| 3172 |
*/ |
| 3173 |
|
| 3174 |
/* |
| 3175 |
- dumpcstate - dump a compacted-NFA state in human-readable form |
| 3176 |
^ static void dumpcstate(int, struct cnfa *, FILE *); |
| 3177 |
*/ |
| 3178 |
static void |
| 3179 |
dumpcstate( |
| 3180 |
int st, |
| 3181 |
struct cnfa *cnfa, |
| 3182 |
FILE *f) |
| 3183 |
{ |
| 3184 |
struct carc *ca; |
| 3185 |
int pos; |
| 3186 |
|
| 3187 |
fprintf(f, "%d%s", st, (cnfa->stflags[st] & CNFA_NOPROGRESS) ? ":" : "."); |
| 3188 |
pos = 1; |
| 3189 |
for (ca = cnfa->states[st]; ca->co != COLORLESS; ca++) { |
| 3190 |
if (ca->co < cnfa->ncolors) { |
| 3191 |
fprintf(f, "\t[%ld]->%d", (long) ca->co, ca->to); |
| 3192 |
} else { |
| 3193 |
fprintf(f, "\t:%ld:->%d", (long) (ca->co - cnfa->ncolors), ca->to); |
| 3194 |
} |
| 3195 |
if (pos == 5) { |
| 3196 |
fprintf(f, "\n"); |
| 3197 |
pos = 1; |
| 3198 |
} else { |
| 3199 |
pos++; |
| 3200 |
} |
| 3201 |
} |
| 3202 |
if (ca == cnfa->states[st] || pos != 1) { |
| 3203 |
fprintf(f, "\n"); |
| 3204 |
} |
| 3205 |
fflush(f); |
| 3206 |
} |
| 3207 |
|
| 3208 |
/* |
| 3209 |
^ #endif |
| 3210 |
*/ |
| 3211 |
#endif /* ifdef REG_DEBUG */ |
| 3212 |
|
| 3213 |
/* |
| 3214 |
* Local Variables: |
| 3215 |
* mode: c |
| 3216 |
* c-basic-offset: 4 |
| 3217 |
* fill-column: 78 |
| 3218 |
* End: |
| 3219 |
*/ |