parse boolean logic

T

Thomas Reat

I have a perl script that must take some boolean logic as input.
Something like:

((a || b) & !c) || d || (e && f)

Is there any module out there that can help me parse this? And
evaluate it as well?

Ideally I would pass it the expression and a hash (or callback) to get
the values of each variable. And it returns the value of the whole
expression. But if there's nothing that does that, I'm interested in
anything that helps with this.
 
S

Sam Holden

I have a perl script that must take some boolean logic as input.
Something like:

((a || b) & !c) || d || (e && f)

Is there any module out there that can help me parse this? And
evaluate it as well?

Parse::RecDescent on cpan makes such a thing really easy.

One of the examples in the docs is essentially the grammar
you have above (though I don't think the example does evaluation
of the parse tree - but that's easy too).
 
B

Bob Walton

Thomas said:
I have a perl script that must take some boolean logic as input.
Something like:

((a || b) & !c) || d || (e && f)

&&----------^

perhaps?


If so, you could evaluate it right in Perl, with something like:

use strict;
use warnings;
#set up variable values
my %h;
$h{$_}=int(rand(2)) for('a'..'f');
#set up string
my $string='((a || b) && !c) || d || (e && f)';
my $res=compute($string,\%h); #call sub
#do stuff with $res
print "res=$res\n";
sub compute{ #string,hashref varname=>value,...
my $string=shift;
my $href=shift;
$string=~s/(\w+)/\$\$href{$1}/g;
my $result=eval($string);
#prints for debug/demo
print "$_=$$href{$_}, " for sort keys %h;
print "result=$result\n";
$result;
}
 
S

Steffen Müller

Thomas said:
I have a perl script that must take some boolean logic as input.
Something like:

((a || b) & !c) || d || (e && f)

Is there any module out there that can help me parse this? And
evaluate it as well?

Ideally I would pass it the expression and a hash (or callback) to get
the values of each variable. And it returns the value of the whole
expression. But if there's nothing that does that, I'm interested in
anything that helps with this.

As Sam pointed out earlier, Parse::RecDescent can easily parse such
expressions. You can find a working example grammar for more involved
*arithmetic* expressions in the Math::Symbolic::parser module that is
part of the Math::Symbolic distribution. It should be fairly easy to
modify it to work for boolean logic.

http://search.cpan.org/src/SMUELLER/Math-Symbolic-0.124/lib/Math/Symbolic/Parser.pm

Steffen
 
P

pkent

Steffen Müller said:
....
As Sam pointed out earlier, Parse::RecDescent can easily parse such
expressions. You can find a working example grammar for more involved
*arithmetic* expressions in the Math::Symbolic::parser module that is
....

Also, I've found Yapp really good.
http://search.cpan.org/~fdesar/Parse-Yapp-1.05/lib/Parse/Yapp.pm

It was pretty simple to implement arbitrary arithmetic and logical
expressions. To implement just the boolean logic as you have above would
be even easier. In my implementation I used the parser to create a tree
of nodes, of arbitrary depth, and then an evaluation routine walked over
that tree, recursively, deciding its truth (because all you need to get
as a result is a true or false value).

P
 
D

David Combs

...

Also, I've found Yapp really good.
http://search.cpan.org/~fdesar/Parse-Yapp-1.05/lib/Parse/Yapp.pm

It was pretty simple to implement arbitrary arithmetic and logical
expressions. To implement just the boolean logic as you have above would
be even easier. In my implementation I used the parser to create a tree
of nodes, of arbitrary depth, and then an evaluation routine walked over
that tree, recursively, deciding its truth (because all you need to get
as a result is a true or false value).

Kent, perhaps you could post your code here, so we could
all learn how to do this very-useful kind of task?

Thanks!

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top