Myself and a few others have been tasked with creating a Truth Table Generator for our first year project. We have a vast majority of the work done, save for a few things, most important of which being the function that actually converts statements (Ex: p V q) into boolean truth values.
Is it possible to convert a string statement with proper c++ argument syntax, such as "(p || !q)" (p and q being pre-defined variables with respective values) directly into a condition?
We've considered the route of many conditional statements based on what we can find via string analysis and operating on the variables based on findings, but I was wondering if there was a much more efficient way to actually code this. The code is designed to take pretty much any input for a truth table statement, convert it into proper c++ syntax, and separate the statement into components. That being said, there are quite a few options to account for if we were to do this via finding certain elements and operating on them with if-then or switch statements. If there is a more efficient route, I am open to suggestions and welcome any criticism of our process.
As a visual demonstration, this is what we were thinking of:
bool p=1, q=0;
string test= "(!p || q)"; // would supposedly return 0 if analyzed as a conditional statement
cout << endl << "Truth Table Generator Test" << endl;
cout << "p = " << p << endl << "q = " << q << endl;
cout << test << " = " << truthVal(test) // Used to signify the conversion from string to condition
Supposed output:
Truth Table Generator Test
p = 1
q = 0
(!p || q) = 0
It's not possible to do in C++ when called like this:
cout << test << " = " << truthVal(test)
However, rearranging slightly, it becomes possible:
bool p=1, q=0;
char test[]= "(!p || q)"; // would supposedly return 0 if analyzed as a conditional statement
cout << endl << "Truth Table Generator Test" << endl;
cout << "p = " << p << endl << "q = " << q << endl;
cout << test << " = " << truthVal(test, 'p', p, 'q', q)
The reason this is possible is because of 2 critical changes. test
is now a constant char array. As such it can be parsed with template metaprogramming. Note that different test
arrays can produce unique function instantiations. This is done, for instance, in Boost's printf library. Since template metaprogramming is Turing complete, an interpreter can be implemented that will test the p and q, or any extra number of variables at run time. You also need to define a convention to link the variables passed with those in the char array. I have done this here by supplying the variable name as a char but it could be done in other ways. You can even do type checking on the passed variables. But you cannot access variables that aren't passed in the function call. Essentially, you are implementing a compile time interpreter.
However, the template metaprogramming is a significant task. But doable.