http://www.perlmonks.org?node_id=1079118

japhy has asked for the wisdom of the Perl Monks concerning the following question:

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