This always start out as fun, but rapidly descends(sic!) into a much harder, if not impossible regex problem once the full requirements become clear. If your post reflects your real requirements and they are not going to 'grow', you might be able to use something relatively simple like this?

This will handle white space delimited infix operators and (I think) unlimited nested parens. It will detect trivially unbalanced parens, but maybe not badly nested ones. Adding unary prefix operators shoudl be possible without to much effort.

#! perl -slw use strict; my $reTokenise = qr[\s*(\S+)(?:\s+((?i:and|or)))?]; sub parseExpr { my $expr = shift; my @tokens = $expr =~ m[$reTokenise]g; pop @tokens unless defined $tokens[ $#tokens ]; return '[' . join( '.', @tokens ) . ']'; } while( <DATA> ) { chomp; warn "Unbalanced parens '$_'" and next unless tr[(][] == tr[)][]; s[ \( ( [^()]+ ) \) ]{ parseExpr( $1 ) }xe while m[[()]]; $_ = parseExpr( $_ ) if m[\s]; print; } __DATA__ ((A or B) (A) or B) ((A) or B) A or B or C and D ( ( this OR that ) AND other ) ( A or ( B and (C or D) ) or (E and F) ) (A or (B or C) or (D and E))

Output:

c:\test>junk5 Unbalanced parens '((A or B)' at c:\test\junk5.pl line 15, <DATA> line + 1. Unbalanced parens '(A) or B)' at c:\test\junk5.pl line 15, <DATA> line + 2. [[A].or.B] [A.or.B.or.C.and.D] [[this.OR.that].AND.other] [A.or.[B.and.[C.or.D]].or.[E.and.F]] [A.or.[B.or.C].or.[D.and.E]]

Although it looks like it simply replaces spaces with dots and parens with brackets, it does actually parse correctly and substitutes a composite token for expressions as it goes.

Modifying it to substitute the appropriate truth value for the expression is trivial, but since you don't indicate the nature of 'this', 'that' & 'other'--eg. numeric, T or F, true or false etc.--it wasn't possible to post that.

If you think it will be useful and want to pursue it further, I've had a hankering to see how far it could be taken before it turns into an mess.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: logically recurse parentheses by BrowserUk
in thread logically recurse parentheses by rsiedl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.