|
|
1.1 ! root 1: #include "re.h" ! 2: #include "lre.h" ! 3: #include "hdr.h" ! 4: ! 5: #define DEBUG ! 6: ! 7: static Expr **proot; ! 8: ! 9: #define PURE(e) (!(e)->backref && !(e)->parens) ! 10: #define PROC(kid) if(ee = proc(kid)){ ee->parent = (kid)->parent; free((char *)kid); kid = ee; } ! 11: ! 12: static Expr * ! 13: proc(Expr *e) ! 14: { ! 15: Expr *ee; ! 16: ! 17: if(e->type == Cat){ ! 18: if(PURE(e->l)){ ! 19: if(proot){ ! 20: *proot = eg_newexpr(Cat, 0, *proot, e->l); ! 21: return((ee = proc(e->r))? ee:e->r); ! 22: } else ! 23: proot = &e->l; ! 24: } else { ! 25: PROC(e->l) ! 26: } ! 27: if(PURE(e->r)){ ! 28: if(proot){ ! 29: *proot = eg_newexpr(Cat, 0, *proot, e->r); ! 30: return(e->l); ! 31: } else ! 32: proot = &e->r; ! 33: } else { ! 34: PROC(e->r) ! 35: } ! 36: return(0); ! 37: } ! 38: proot = 0; ! 39: switch(e->type) ! 40: { ! 41: case Alternate: ! 42: PROC(e->l) ! 43: proot = 0; ! 44: PROC(e->r) ! 45: break; ! 46: case Star: ! 47: case Plus: ! 48: case Quest: ! 49: case EOP: ! 50: case Group: ! 51: PROC(e->l) ! 52: break; ! 53: } ! 54: proot = 0; ! 55: return(0); ! 56: } ! 57: ! 58: void ! 59: egcanon(Expr *e) ! 60: { ! 61: #ifdef DEBUG ! 62: char before[EPRINTSIZE], after[EPRINTSIZE]; ! 63: #endif ! 64: ! 65: #ifdef DEBUG ! 66: eg_epr(e, before, 0); ! 67: if(TRACE(3)){ ! 68: PR "egcanon(%s):\n", before); ! 69: } ! 70: #endif ! 71: proot = 0; ! 72: if(!PURE(e)) ! 73: proc(e); ! 74: #ifdef DEBUG ! 75: eg_epr(e, after, 0); ! 76: if(TRACE(3)){ ! 77: PR "egcanon returns %s\n", after); ! 78: } ! 79: if(strcmp(before, after)){ ! 80: EPR "URK! egcanon did not preserve!\nbefore=%s\n after=%s\n", before, after); ! 81: exit(1); ! 82: } ! 83: #endif ! 84: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.