<i>Regexp backreference fail with a * closure</i> status still Open with 4 other changes
by
.
D 2012-02-14T11:35:59
J icomment Some\snotes\sabout\sfixing\sthe\smore\sgeneral\sissue\s...\n\nFor\scontext,\srealize\sthat\sTcl's\sregex\sengine\sis\sa\shybrid\sDFA/NFA\nimplementation.\s\sMostly\sit\srelies\son\sthe\sDFA\slogic,\swhich\shas\sfast\sand\nstable\sperformance\sbecause\sit\snever\shas\sto\sbacktrack.\s\sBut\sthere\sare\stwo\nsignificant\sregex\sfeatures\sthat\sDFA\scan't\shandle:\scapturing\sparentheses\sand\nback-references.\s\sTo\sdeal\swith\sthose\sthings,\sthe\sregex\scompilation\scode\nbuilds\sa\stree\sof\s"subRE"s,\swhich\swill\sbe\ssearched\sat\sruntime\sin\sNFA\sstyle.\nThe\sexisting\stypes\sof\ssubREs\sare:\n\n*\splain:\smatches\sany\sstring\sdescribed\sby\sa\sDFA.\n*\sbackref:\smatches\sany\sstring\sthat\sis\sequal\sto\sa\sstring\sidentified\sby\sa\nprevious\scapture\s(or\sactually,\sany\sstring\sthat\sis\sN\srepetitions\sof\ssuch\na\sstring,\sbut\sI\sdigress).\n*\scapture:\smatches\sany\sstring\sthat\smatches\sits\schild\ssubRE,\sthen\ssaves\sthe\nmatch\sfor\slater\soutput\sor\sbackref-matching.\n*\sconcatenation:\smatches\sany\sstring\sthat\smatches\sthe\sconcatenation\sof\sits\ntwo\schild\ssubREs.\n*\salternation:\smatches\sany\sstring\sthat\smatches\seither\sof\sits\stwo\schild\nsubREs.\n\nconcatenation\simplies\ssearching,\ssince\sit\shas\sto\sconsider\seach\spossible\ndivision\sof\sthe\spresented\sstring\sinto\stwo\sparts\sto\ssee\sif\sthey\scan\smatch\nits\stwo\schild\ssubREs.\s\sSimilarly,\salternation\srequires\ssearching,\salthough\nit\shas\sonly\stwo\schoices\sto\sconsider\snot\sN.\s\sTo\sreduce\sunnecessary\nsearching,\sHenry\sSpencer\smade\sthe\scompilation\scode\screate\sa\sDFA\sfor\seach\nsubRE\snode,\snot\sonly\sthe\s"plain"\snodes.\s\sThe\sDFA\sdescribes\swhat\scould\npossibly\smatch\sthe\ssubRE\sin\sas\smuch\sdetail\sas\sDFA\scan\smanage\s(which\sis\nactually\severything\sexcept\sthe\seffects\sof\sback-refs).\s\sAt\sruntime,\sthe\nsubRE\stree\ssearching\scode\sfirst\sexecutes\sthe\sassociated\sDFA,\sand\sgives\sup\nimmediately\sif\sit\sfinds\sno\smatch.\s\sOtherwise\sit\sstarts\sworking\sthrough\sthe\npossible\smatch\scandidates.\n\nNow,\sif\syou're\sa\sprogrammer,\sit\sprobably\sstruck\syou\sas\sodd\sthat\sthe\ssubRE\ntypes\slisted\sabove\sdon't\sinclude\sany\siteration\scapability.\s\sHow\sdid\sHenry\nexpect\sto\sdeal\swith\squantified\sregexes?\s\sThe\sanswer\sappears\sin\sthe\s"it's\nquantifier\stime"\sportion\sof\sparseqatom().\s\sHe\stransforms\sthe\sgeneral\scase\nx{m,n}\sas\sfollows:\n\n*\sfirst,\sturn\sx{0,...}\sinto\sx{1,...}|empty\n\n*\snext,\sknowing\swe\shave\sm>0\sand\sn>0,\sturn\sx{m,n}\sinto\sx{m-1,n-1}x,\swith\n\s\scapturing\sparens\sin\sonly\sthe\ssecond\sx\n\nThe\sreference\sto\scapturing\sparens\sis\stelling\s---\she\swasn't\sthinking\sabout\nback-refs\swhen\she\swrote\sthis.\s\sWhat\sthe\scode\sis\sactually\sdoing\sis\spushing\nall\sthe\s"messy"\sstuff\s(that\sis,\ssubREs\spossibly\sinvolving\scapture\sor\nbackref)\sinto\sthe\ssecond\schild\sof\sthe\sconcatenation\ssubRE\sit\screates\shere.\nThe\sfirst\schild\ssubRE,\sfor\s"x{m-1,n-1}",\sis\salways\simplemented\sas\sa\splain\nDFA\snode.\s\sNow,\sthat's\sfine\sfor\smessiness\sthat\sconsists\sof\scapturing\nparens;\scapturing\sparens\sdon't\saffect\smatch\sbehavior,\sso\sthere's\sno\sneed\nto\sconsider\sthem\sin\sthe\sfirst\schild\ssubRE.\s\s(What\sthis\simplementation\neffectively\ssays\sis\sthat\sif\syou\swrite\scapturing\sparens\sinside\sa\squantifier,\nsay\s"(x)+",\swhat\swill\sbe\scaptured\sis\sthe\stext\sthat\smatched\sthe\slast\niteration\sof\sthe\squantifier.\s\sNo\sobjection\sfrom\sme,\sthough\sI\sdo\snot\ssee\nthis\sbehavioral\sdetail\sdocumented\sin\sthe\sre_syntax\sman\spage\s...\sand\scome\sto\nthink\sof\sit,\sI\swonder\swhether\sthe\sPOSIX\sregex\sspec\shas\sanything\sto\ssay\nabout\sthe\ssubject.)\n\nBUT:\sthat\slogic\sfalls\sdown\scompletely\swhen\syou\sconsider\sback-refs.\nA\sback-ref\sabsolutely\sdoes\saffect\smatch\sbehavior,\sbut\swhat\sthe\sfirst\schild\nsubRE\shas\sto\swork\swith\sis\sonly\sa\scopy\sof\sthe\sDFA\sfor\sthe\sback-ref's\nreferent\sexpression.\s\sThat\sis,\sif\syou\swrite\ssomething\slike\n(\\w+)(\\s+\\1)+\n(intended\sto\smatch\s2\sor\smore\srepetitions\sof\sthe\ssame\sword),\sthe\sactual\nbehavior\syou\sget\sis\sequivalent\sto\n(\\w+)(\\s+\\w+)*(\\s+\\1)\nie,\sthe\slast\sword\sis\schecked\sto\sbe\sequal\sto\sthe\sfirst,\sbut\sany\swords\sin\sthe\nmiddle\sare\sonly\schecked\sto\sbe\swords,\sbecause\sthe\ssubRE\sfor\s"x{m-1,n-1}"\sis\nonly\sa\sDFA\sfor\s"(\\s+\\w+)*".\n\nNote\sthat\sthis\sonly\scomes\sinto\splay\swhen\sthe\sback-ref\sis\sa\spart\sof\sa\slarger\nquantified\sexpression\s---\sa\squantifier\sattached\sdirectly\sto\sa\sback-ref\suses\na\sdifferent\scode\spath,\swhich\shas\sa\sproblem\swith\s"\\n*"\sas\sI\sdocumented\nearlier,\sbut\snot\sthis\smore\sgeneral\sbug.\s\sIf\syou\swork\sthrough\sthe\sexamples\nmentioned\sso\sfar,\sI\sthink\sthey\sare\sall\sexplained\sby\sone\sissue\sor\sthe\sother.\n\nI'm\sinclined\sto\sthink\sthat\sthe\sway\sto\sattack\sthis\sis\sto\sinvent\san\n"iteration"\ssubRE\stype,\sdrop\sthe\stwo\scompilation-time\stransforms\smentioned\nabove\sin\sfavor\sof\sjust\splopping\san\siteration\ssubRE\satop\sany\smessy\nexpression\sthat\shas\sto\sbe\squantified,\sand\sdo\sthe\ssearch\swork\nstraightforwardly\sat\sruntime.\s\sHowever,\sthis\slooks\slike\sa\ssizable\samount\sof\nwork\sthat\swill\sresult\sin\sa\svery\snontrivial\spatch.\s\sSo\sbefore\sstarting,\sI'm\nsoliciting\ssome\sfeedback\sabout\swhether\sthis\sseems\slike\sa\ssane\ssolution\sthat\nwould\sget\saccepted.\n\n
J login tgl
J mimetype text/plain
J username tgl
K 1115587fffffffffffffffffffffffffffffffff
U tgl
Z faebfeaed6b367dc2b7d3a8b80dba26e