Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sounds like a case for <a href="http://search.cpan.org/perldoc?Parse::RecDescent" rel="noreferrer">Parse::RecDescent</a>:</p> <pre><code>use strict; use warnings; use Parse::RecDescent; my $text = '((((!((cond1) || (cond2) || (cond3)) &amp;&amp; (cond4))) &amp;&amp; (cond5)) &lt;= (((cond6) || (cond7) || (cond8)) || (cond9)))'; #$::RD_TRACE=1; my $grammar = q{ startrule: expr expr: operand operation(s?) { $return = @{$item[2]} ? { 'operations' =&gt; $item[2], 'lvalue' =&gt; $item[1] } : $item[1] } operation: /\|\||&amp;&amp;|&lt;=/ operand { $return = { 'op' =&gt; $item[1], 'rvalue' =&gt; $item[2] } } operand: '(' expr ')' { $return = $item[2] } operand: '!' operand { $return = { 'op' =&gt; '!', 'value' =&gt; $item[2] } } operand: /\w+/ }; my $parser = Parse::RecDescent-&gt;new($grammar); my $result = $parser-&gt;startrule($text) or die "Couldn't parse!\n"; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1; print Dumper $result; </code></pre> <p>The grammar, in English:</p> <p>The whole thing is an expression. An expression is an operand followed by zero or more binary operators and their operands. Each operand is either a parenthesized expression, '!' followed by an operand, or a word (e.g. <code>cond1</code>).</p> <p>Every node in the produced tree is in one of the following forms:</p> <ul> <li><code>cond1</code> - a condition</li> <li><code>{ 'op' =&gt; '!', 'value' =&gt; 'node' }</code> - ! applied to another node</li> <li><code>{ 'lvalue' =&gt; 'node', 'operations' =&gt; [ one or more of: { 'op' =&gt; 'binop', 'rvalue' =&gt; 'node' } ] }</code> - series of one or more operations representing node binop node binop node ...</li> </ul> <p>I didn't break series of binary operations (e.g. <code>((cond1) || (cond2) || (cond3))</code>) into a binary tree because you provided no information about precedence or associativity.</p> <p>Output for your example is:</p> <pre><code>$VAR1 = { 'lvalue' =&gt; { 'lvalue' =&gt; { 'lvalue' =&gt; { 'op' =&gt; '!', 'value' =&gt; { 'lvalue' =&gt; 'cond1', 'operations' =&gt; [ { 'op' =&gt; '||', 'rvalue' =&gt; 'cond2' }, { 'op' =&gt; '||', 'rvalue' =&gt; 'cond3' } ] } }, 'operations' =&gt; [ { 'op' =&gt; '&amp;&amp;', 'rvalue' =&gt; 'cond4' } ] }, 'operations' =&gt; [ { 'op' =&gt; '&amp;&amp;', 'rvalue' =&gt; 'cond5' } ] }, 'operations' =&gt; [ { 'op' =&gt; '&lt;=', 'rvalue' =&gt; { 'lvalue' =&gt; { 'lvalue' =&gt; 'cond6', 'operations' =&gt; [ { 'op' =&gt; '||', 'rvalue' =&gt; 'cond7' }, { 'op' =&gt; '||', 'rvalue' =&gt; 'cond8' } ] }, 'operations' =&gt; [ { 'op' =&gt; '||', 'rvalue' =&gt; 'cond9' } ] } } ] }; </code></pre>
 

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