no guarantees whatsoever...
use strict; use warnings; my @equations; while(<DATA>){ chomp; next unless $_; # ignore empty next if $_ =~ /^\s*#/; # ignore comment s/(\w+)/\$$1/g; # variables # ops by decending precedence s/'/^1/g; # negation by xor 1 s/\*/ and /g; # and s/\+/ or /g; # or s/=(.*)$/= ( $1 )/; # assignment after ( RHS ) push @equations,"$_;\n"; } my $format = "%s | %s %s %s %s\n"; $\="\n"; #print @equations; for my $eq (@equations) { print "\n $eq"; printf $format, qw/Y A B C D/; for my $A (0,1) { for my $B (0,1) { for my $C (0,1) { for my $D (0,1) { my $Y=undef; eval $eq; printf $format, $Y, $A, $B, $C, $D; } } } } } __DATA__ #Y=A+B' #Y=A*B' #Y=A' #Y=A*B #Y=A+B Y = A + (B*C) Y = A + (B' + (C*D)') Y = A*(B*(C'+D)')
$Y = ( $A or ($B and $C) ); Y | A B C D 0 | 0 0 0 0 0 | 0 0 0 1 0 | 0 0 1 0 0 | 0 0 1 1 0 | 0 1 0 0 0 | 0 1 0 1 1 | 0 1 1 0 1 | 0 1 1 1 1 | 1 0 0 0 1 | 1 0 0 1 1 | 1 0 1 0 1 | 1 0 1 1 1 | 1 1 0 0 1 | 1 1 0 1 1 | 1 1 1 0 1 | 1 1 1 1 $Y = ( $A or ($B^1 or ($C and $D)^1) ); Y | A B C D 1 | 0 0 0 0 1 | 0 0 0 1 1 | 0 0 1 0 1 | 0 0 1 1 1 | 0 1 0 0 1 | 0 1 0 1 1 | 0 1 1 0 0 | 0 1 1 1 1 | 1 0 0 0 1 | 1 0 0 1 1 | 1 0 1 0 1 | 1 0 1 1 1 | 1 1 0 0 1 | 1 1 0 1 1 | 1 1 1 0 1 | 1 1 1 1 $Y = ( $A and ($B and ($C^1 or $D)^1) ); Y | A B C D 0 | 0 0 0 0 0 | 0 0 0 1 0 | 0 0 1 0 0 | 0 0 1 1 0 | 0 1 0 0 0 | 0 1 0 1 0 | 0 1 1 0 0 | 0 1 1 1 0 | 1 0 0 0 0 | 1 0 0 1 0 | 1 0 1 0 0 | 1 0 1 1 0 | 1 1 0 0 0 | 1 1 0 1 1 | 1 1 1 0 0 | 1 1 1 1
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
In reply to Re: Parsing Boolean expressions (hack)
by LanX
in thread Parsing Boolean expressions
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |