Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Finding all matches is very similar to finding a random match. Below is a simple modification of the logic that generates random matches on <a href="http://www.debuggex.com" rel="nofollow">www.debuggex.com</a>, assuming you already have a parse tree.</p> <p>The idea is that for every subtree, you return a list of all possible generated strings, given a string that was generated by all previous nodes in your parse tree.</p> <pre><code>AltTree.all = (prefix) -&gt; rets = [] for child in children rets.extend(child.all(prefix)) ConcatTree.all = (prefix) -&gt; prefixes = [prefix] for child in children newPrefixes = [] for p in prefixes newPrefixes.extend(child.all(p)) prefixes = newPrefixes return prefixes RepeatTree.all = (prefix) -&gt; prefixes = [prefix] rets = [] for i up to max newPrefixes = [] for p in prefixes newPrefixes.extend(onlyChild.all(p)) prefixes = newPrefixes if i &gt;= min rets.extend(prefixes) return rets CharsetTree.all = (prefix) -&gt; rets = [] for char in allValidChars(): rets.push(prefix + char) return rets </code></pre> <p>The rest of the trees are left as exercises (most notably the literal tree). </p> <p>Note that there are intentionally no optimizations for the sake of clarity. Calling <code>myTree.all('')</code> will generate a list such that every valid matching string appears once <strong>for every path that generates this string</strong>. You will probably want to add deduplication and get rid of the excessive copying.</p> <p>I should also add that this will only work for regular expressions that have a <em>small</em> number of total matching strings. This is because all of the strings are being stored. If you want to get around this limitation, you can <code>yield</code>ify this algorithm. You will need to maintain a stack (think of it as being a bread crumb trail) of where you are in the tree. When a new string is asked for, you will create it from the path you travelled, and then update the path.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload