in reply to Evaluate arbitrary boolean expressions

Do you want to do algebraic symbolic reduction of the terms or do you just want to evaluate it?

One approach would be to evaluate every atomic expression left-to-right until you have no more operators, and then using De Morgan's laws to resolve parentheses.

  • Comment on Re: Evaluate arbitrary boolean expressions

Replies are listed 'Best First'.
Re^2: Evaluate arbitrary boolean expressions
by gauss76 (Scribe) on Mar 13, 2018 at 10:44 UTC

    Thanks for the quick response.

    In answer to your question: I am only interested in whether the expression I generate is true or false. I do not need to do any algebraic symbolic reduction of the terms.

      A simplicistic approach would be to repeatedly apply these three rules to simplify things:

      • replace F || X with X and T && Y with Y
      • replace F && X with F and T || Y with T
      • replace an atomar term within parenthes with that atomar term

      #!perl -w use strict; my @terms = ( '( ( T || F ) && T )', '( ( T || F ) && ( F && F ))' ); sub simplify { my( $term ) = @_; $term =~ s!\s+!!g; # eliminate all whitespace my $changed; do { $changed = 0; $changed ||= ($term =~ s!F\|\|!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\|\|F!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!T\&\&!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\&\&T!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!F\&\&!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\&\&F!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!T\|\|!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\|\|T!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\(([FT])\)!$1!g); #print "$term ($changed)\n"; } while $changed; return $term } for my $t (@terms) { print "$t => " . simplify( $t ), "\n"; };