A simplicistic approach would be to repeatedly apply these three rules to simplify things:
#!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"; };
In reply to Re^3: Evaluate arbitrary boolean expressions
by Corion
in thread Evaluate arbitrary boolean expressions
by gauss76
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |