I've got a series of boolean logic statements, such as "(green OR yellow) AND (light OR banana)" or "peace AND justice AND liberty AND NOT (corrupt OR evil)".

I have a parser which builds trees out of these statements: [AND, [OR, green, yellow], [OR, light, banana]] for the first, and [AND, peace, justice, liberty, [NOT, [OR, corrupt, evil]]] for the second.

I would now like to walk these trees to build a list of the minimal inputs which will pass the boolean logic. So for the first, I'd like to build the list ("green light", "green banana", "yellow light", "yellow banana"), and for the second I'd like to build the list ("peace justice liberty"). (There is no need to generate the various permutations of the terms "peace", "justice", and "liberty".)

I know this will be a recursive process, and I know an "OR" branch with X children will require locally producing X copies of the list, but I'm having trouble figuring out how to start. It's super easy with something like "a OR b OR c", or "a AND b AND c", but when the recursion comes into play, that's where my programmer's block kicks in.

Can anyone offer some pointers to move me in the right direction?

Jeffrey Pinyan (Perl, PHP ugh, JavaScript) — @PrayingTheMass
Melius servire volo
Catholic Liturgy

In reply to Walking a boolean tree to produce matching inputs by japhy

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.