This is a tiny, C++, dependency-less library that evaluates boolean expressions in a text form.
#include "LibBoolEE.hpp"
int main() {
LibBoolEE::Vals vals = { { "A", true }, { "B", false } };
return LibBoolEE::resolve("A|B&B", vals); // returns 1
}
For further examples see test.cpp.
It is sufficient to add LibBoolEE.cpp
and LibBoolEE.h
to your project.
tt
(true) andff
(false) are formulas representing true and false respectively,- any variable of the form
[A-Za-Z0-9]+
is a formula, - for
φ
formula is!φ
formula, - for
φ
,ψ
formulas are(φ|ψ)
,(φ&ψ)
formulas representing logical disjunction and conjunction respectively, - nothing else is a formula.
- whitespaces are ignored
LibBoolEE::Val
is a shortcut for std::pair<std::string, bool>
and represents a pair (variable name, variable value)
, e.g. (VarA, true)
.
LibBoolEE::Vals
is a map of LibBoolEE::Val
.
bool LibBoolEE::resolve(const std::string & formula, const LibBoolEE::Vals & vals)
is a function that returns true if and only if the formula
is true in the valuation given by vals. Every variable present in the formula must have the corresponding key in vals
.
The code is released under GNU lGPLv3.