Well, depends on what you're going to be doing with the expressions. If the object is to generate complex expressions and then simplify them as much as possible, not worrying about cycles (maybe the result will be used millions of times in future programs?), then you can divide the expression into sections and test each section for all possible combinations of true / false, replacing sections that are always true or always false with a 1 or 0. If on the other hand you just want to get the results of a complex expression in as efficient a manner as possible, you can do something like the following:
my %vals = ('AND' => '&&', 'OR' => '||', 'XOR' => '^', 'NOT' => '!');
my $logic;
while ($logic = <DATA>) {
chomp($logic);
while(<DATA>) {
chomp; last if (!$_);
split(/ = /);
$vals{@_[0]} = @_[1];
}
print "$logic\n";
$logic =~ s/(\w+)/$vals{$1}/g;
print "$logic\n";
print eval($logic) . "\n\n";
}
__DATA__
a0 OR d1 AND d0
a0 = 1
d1 = 0
d0 = 1
d1 AND e1 AND f0
e1 = 1
f0 = 0
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.