| 1 |
/* $Id: example.cpp,v 1.6 2015/01/21 03:00:42 sarrazip Exp $ |
| 2 |
C++ BoolStuff example program. |
| 3 |
|
| 4 |
Written by Pierre Sarrazin <http://sarrazip.com/> |
| 5 |
This file is in the public domain. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <boolstuff/BoolExprParser.h> |
| 9 |
#include <assert.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
using namespace std; |
| 13 |
using namespace boolstuff; |
| 14 |
|
| 15 |
|
| 16 |
static |
| 17 |
ostream & |
| 18 |
operator << (ostream &out, const set<string> &s) |
| 19 |
{ |
| 20 |
for (set<string>::const_iterator it = s.begin(); it != s.end(); it++) |
| 21 |
out << *it << " "; |
| 22 |
return out; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
int main() |
| 27 |
{ |
| 28 |
BoolExprParser parser; |
| 29 |
string line; |
| 30 |
|
| 31 |
while (getline(cin, line)) |
| 32 |
{ |
| 33 |
try |
| 34 |
{ |
| 35 |
BoolExpr<string> *expr = parser.parse(line); |
| 36 |
|
| 37 |
assert(expr != NULL); // any error must have thrown an exception |
| 38 |
|
| 39 |
cout << "Original expression : " << flush; |
| 40 |
expr->print(cout); |
| 41 |
cout << "\n"; |
| 42 |
|
| 43 |
BoolExpr<string> *dnf = BoolExpr<string>::getDisjunctiveNormalForm(expr); |
| 44 |
|
| 45 |
// NOTE: 'expr' is now unusable. |
| 46 |
// The original tree has been transformed by the preceding call. |
| 47 |
// |
| 48 |
expr = NULL; |
| 49 |
|
| 50 |
cout << "Disjunctive normal form : "; |
| 51 |
if (dnf != NULL) |
| 52 |
cout << dnf; |
| 53 |
else |
| 54 |
cout << "FALSE"; |
| 55 |
|
| 56 |
cout << "\n"; |
| 57 |
|
| 58 |
if (dnf != NULL) |
| 59 |
{ |
| 60 |
typedef vector<const BoolExpr<string> *> V; |
| 61 |
typedef V::const_iterator IT; |
| 62 |
|
| 63 |
V termRoots; |
| 64 |
dnf->getDNFTermRoots(inserter(termRoots, termRoots.end())); |
| 65 |
for (IT it = termRoots.begin(); it != termRoots.end(); it++) |
| 66 |
{ |
| 67 |
const BoolExpr<string> *term = *it; |
| 68 |
set<string> positives, negatives; |
| 69 |
term->getTreeVariables(positives, negatives); |
| 70 |
|
| 71 |
cout << "Term : " << term << "\n"; |
| 72 |
cout << " Positives: " << positives << "\n"; |
| 73 |
cout << " Negatives: " << negatives << "\n"; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
cout << endl; |
| 78 |
delete dnf; |
| 79 |
|
| 80 |
// Note that we do not destroy 'expr', because it was implicitly |
| 81 |
// destroyed by the call to boolstuff_get_disjunctive_normal_form(). |
| 82 |
} |
| 83 |
catch (BoolExprParser::Error &err) |
| 84 |
{ |
| 85 |
cout << "Error #" << err.code << " at character #" << err.index << endl; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return EXIT_SUCCESS; |
| 90 |
} |