forked from ArashPartow/exprtk-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexprtk_truthtable_gen.cpp
164 lines (128 loc) · 4.71 KB
/
exprtk_truthtable_gen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk Truth Table Generator *
* Author: Arash Partow (1999-2021) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* http://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cmath>
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void truth_table_generator(const std::string& boolean_expression)
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::parser_error::type error_t;
typedef typename parser_t::settings_store settings_t;
symbol_table_t symbol_table;
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser(
settings_t(
settings_t::compile_all_opts +
settings_t::e_disable_usr_on_rsrvd)
.disable_all_base_functions ()
.disable_all_control_structures()
);
parser.enable_unknown_symbol_resolver();
parser.dec().collect_variables() = true;
parser.dec().collect_functions() = true;
if (!parser.compile(boolean_expression,expression))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
boolean_expression.c_str());
for (std::size_t i = 0; i < parser.error_count(); ++i)
{
error_t error = parser.get_error(i);
printf("Error: %02d Position: %02d "
"Type: [%s] "
"Message: %s "
"Expression: %s\n",
static_cast<int>(i),
static_cast<int>(error.token.position),
exprtk::parser_error::to_str(error.mode).c_str(),
error.diagnostic.c_str(),
boolean_expression.c_str());
}
return;
}
typedef typename parser_t::dependent_entity_collector::symbol_t dec_symbol_t;
std::deque<dec_symbol_t> symbol_list;
parser.dec().symbols(symbol_list);
if (symbol_list.empty())
{
printf("Error: No variables found to build truth table.\n");
return;
}
for (std::size_t i = 0; i < symbol_list.size(); ++i)
{
if (exprtk::details::imatch(symbol_list[i].first,"not"))
{
symbol_list.erase(symbol_list.begin() + i);
if (i >= symbol_list.size())
break;
}
if (parser_t::e_st_function == symbol_list[i].second)
{
printf("Error: call to function '%s' not allowed.\n",symbol_list[i].first.c_str());
return;
}
}
if (symbol_list.size() > 32)
{
printf("Error: no more than 32 unique variables allowed.\n");
return;
}
unsigned int upper_bound = 1 << symbol_list.size();
int index_width = static_cast<int>(std::ceil(std::log10(upper_bound)));
// Print truth table header
printf(" #%s ",std::string(index_width - 1,' ').c_str());
for (std::size_t i = 0; i < symbol_list.size(); ++i)
{
printf("| %s ",symbol_list[i].first.c_str());
}
printf("| %s \n",boolean_expression.c_str());
for (unsigned int i = 0; i < upper_bound; ++i)
{
for (std::size_t j = 0; j < symbol_list.size(); ++j)
{
symbol_table.get_variable(symbol_list[j].first)->ref() = T((i & (1 << (symbol_list.size() - j - 1))) ? 0 : 1);
}
int result = static_cast<int>(expression.value());
printf(" %*d ",index_width,i);
for (std::size_t j = 0; j < symbol_list.size(); ++j)
{
printf("| %d ",static_cast<int>(symbol_table.get_variable(symbol_list[j].first)->value()));
}
printf("| %d \n",result);
}
}
int main(int argc, char* argv[])
{
if (2 != argc)
{
printf("usage: exprtk_truthtable_gen <expression>\n");
return 1;
}
truth_table_generator<double>(argv[1]);
return 0;
}
/*
Examples:
./exprtk_truthtable_gen 'A and B or C or not(D)'
./exprtk_truthtable_gen '(A and not(B)) nor not(C xor D)'
./exprtk_truthtable_gen '(A nand not(B or C)) xor not(D xor E)'
*/