28 lines
15 KiB
HTML
28 lines
15 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>9.8 Backtracking</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Guide</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="intro.html" class="tocviewlink" data-pltdoc="x">Welcome to Racket</a></td></tr><tr><td align="right">2 </td><td><a href="to-scheme.html" class="tocviewlink" data-pltdoc="x">Racket Essentials</a></td></tr><tr><td align="right">3 </td><td><a href="datatypes.html" class="tocviewlink" data-pltdoc="x">Built-<wbr></wbr>In Datatypes</a></td></tr><tr><td align="right">4 </td><td><a href="scheme-forms.html" class="tocviewlink" data-pltdoc="x">Expressions and Definitions</a></td></tr><tr><td align="right">5 </td><td><a href="define-struct.html" class="tocviewlink" data-pltdoc="x">Programmer-<wbr></wbr>Defined Datatypes</a></td></tr><tr><td align="right">6 </td><td><a href="modules.html" class="tocviewlink" data-pltdoc="x">Modules</a></td></tr><tr><td align="right">7 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">8 </td><td><a href="i_o.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">9 </td><td><a href="regexp.html" class="tocviewselflink" data-pltdoc="x">Regular Expressions</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Exceptions and Control</a></td></tr><tr><td align="right">11 </td><td><a href="for.html" class="tocviewlink" data-pltdoc="x">Iterations and Comprehensions</a></td></tr><tr><td align="right">12 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">13 </td><td><a href="classes.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">14 </td><td><a href="units.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">15 </td><td><a href="reflection.html" class="tocviewlink" data-pltdoc="x">Reflection and Dynamic Evaluation</a></td></tr><tr><td align="right">16 </td><td><a href="macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">17 </td><td><a href="languages.html" class="tocviewlink" data-pltdoc="x">Creating Languages</a></td></tr><tr><td align="right">18 </td><td><a href="concurrency.html" class="tocviewlink"
|
||
|
times, but the overriding priority is that the overall match succeed.
|
||
|
Consider</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=regexp.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._regexp-match%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">regexp-match</a></span><span class="hspace"> </span><span class="RktVal">#rx"a*a"</span><span class="hspace"> </span><span class="RktVal">"aaaa"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">'("aaaa")</span></p></td></tr></table></blockquote><p>The regexp consists of two subregexps: <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a*</span><span class="hspace"></span></span> followed by
|
||
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>. The subregexp <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a*</span><span class="hspace"></span></span> cannot be allowed to match
|
||
|
all four <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>’s in the text string <span class="RktSym">aaaa</span>, even though
|
||
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">*</span><span class="hspace"></span></span> is a greedy quantifier. It may match only the first
|
||
|
three, leaving the last one for the second subregexp. This ensures
|
||
|
that the full regexp matches successfully.</p><p>The regexp matcher accomplishes this via a process called
|
||
|
<a name="(tech._backtracking)"></a><span style="font-style: italic">backtracking</span>. The matcher tentatively allows the greedy
|
||
|
quantifier to match all four <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>’s, but then when it becomes
|
||
|
clear that the overall match is in jeopardy, it <span class="emph">backtracks</span> to a
|
||
|
less greedy match of three <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>’s. If even this fails, as in
|
||
|
the call</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=regexp.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._regexp-match%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">regexp-match</a></span><span class="hspace"> </span><span class="RktVal">#rx"a*aa"</span><span class="hspace"> </span><span class="RktVal">"aaaa"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">'("aaaa")</span></p></td></tr></table></blockquote><p>the matcher backtracks even further. Overall failure is conceded
|
||
|
only when all possible backtracking has been tried with no success.</p><p>Backtracking is not restricted to greedy quantifiers.
|
||
|
Nongreedy quantifiers match as few instances as
|
||
|
possible, and progressively backtrack to more and more
|
||
|
instances in order to attain an overall match. There
|
||
|
is backtracking in alternation too, as the more
|
||
|
rightward alternates are tried when locally successful
|
||
|
leftward ones fail to yield an overall match.</p><p>Sometimes it is efficient to disable backtracking. For example, we
|
||
|
may wish to commit to a choice, or we know that trying alternatives is
|
||
|
fruitless. A nonbacktracking regexp is enclosed in
|
||
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">(?></span><span class="hspace"></span></span>...<span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span>.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=regexp.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._regexp-match%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">regexp-match</a></span><span class="hspace"> </span><span class="RktVal">#rx"(?>a+)."</span><span class="hspace"> </span><span class="RktVal">"aaaa"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#f</span></p></td></tr></table></blockquote><p>In this call, the subregexp <span class="RktInBG"><span class="hspace"></span><span class="RktIn">?>a+</span><span class="hspace"></span></span> greedily matches all four
|
||
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>’s, and is denied the opportunity to backtrack. So, the
|
||
|
overall match is denied. The effect of the regexp is therefore to
|
||
|
match one or more <span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>’s followed by something that is
|
||
|
definitely non-<span class="RktInBG"><span class="hspace"></span><span class="RktIn">a</span><span class="hspace"></span></span>.</p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://download.racket-lang.org/releases/8.6/doc/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="regexp-alternation.html" title="backward to "9.7 Alternation"" data-pltdoc="x">← prev</a> <a href="regexp.html" title="up to "9 Regular Expressions"" data-pltdoc="x">up</a> <a href="Looking_Ahead_and_Behind.html" title="forward to "9.9 Looking Ahead and Behind"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|