1 #include <Teuchos_MathExpr.hpp>
7 Language make_language() {
11 prods[PROD_PROGRAM](
"program") >>
"statements",
"expr?";
12 prods[PROD_NO_STATEMENTS](
"statements");
13 prods[PROD_NEXT_STATEMENT](
"statements") >>
"statements",
"statement",
";",
"S?";
14 prods[PROD_ASSIGN](
"statement") >>
"name",
"S?",
"=",
"S?",
"expr";
15 prods[PROD_NO_EXPR](
"expr?");
16 prods[PROD_YES_EXPR](
"expr?") >>
"expr";
17 prods[PROD_EXPR](
"expr") >>
"ternary";
18 prods[PROD_TERNARY_DECAY](
"ternary") >>
"add_sub";
19 prods[PROD_OR_DECAY](
"or") >>
"and";
20 prods[PROD_AND_DECAY](
"and") >>
"comp";
21 prods[PROD_ADD_SUB_DECAY](
"add_sub") >>
"mul_div";
22 prods[PROD_MUL_DIV_DECAY](
"mul_div") >>
"neg";
23 prods[PROD_NEG_DECAY](
"neg") >>
"pow";
24 prods[PROD_POW_DECAY](
"pow") >>
"scalar";
25 prods[PROD_TERNARY](
"ternary")
26 >>
"or",
"?",
"S?",
"add_sub",
":",
"S?",
"add_sub";
27 prods[PROD_OR](
"or") >>
"or",
"||",
"S?",
"and";
28 prods[PROD_AND](
"and") >>
"and",
"&&",
"S?",
"comp";
29 prods[PROD_GT](
"comp") >>
"add_sub",
">",
"S?",
"add_sub";
30 prods[PROD_LT](
"comp") >>
"add_sub",
"<",
"S?",
"add_sub";
31 prods[PROD_GEQ](
"comp") >>
"add_sub",
">=",
"S?",
"add_sub";
32 prods[PROD_LEQ](
"comp") >>
"add_sub",
"<=",
"S?",
"add_sub";
33 prods[PROD_EQ](
"comp") >>
"add_sub",
"==",
"S?",
"add_sub";
34 prods[PROD_BOOL_PARENS](
"comp") >>
"(",
"S?",
"or",
")",
"S?";
35 prods[PROD_ADD](
"add_sub") >>
"add_sub",
"+",
"S?",
"mul_div";
36 prods[PROD_SUB](
"add_sub") >>
"add_sub",
"-",
"S?",
"mul_div";
37 prods[PROD_MUL](
"mul_div") >>
"mul_div",
"*",
"S?",
"pow";
38 prods[PROD_DIV](
"mul_div") >>
"mul_div",
"/",
"S?",
"pow";
39 prods[PROD_POW](
"pow") >>
"scalar",
"^",
"S?",
"pow";
40 prods[PROD_CALL](
"scalar")
41 >>
"name",
"S?",
"(",
"S?",
"args?",
")",
"S?";
42 prods[PROD_NO_ARGS](
"args?");
43 prods[PROD_SOME_ARGS](
"args?") >>
"args";
44 prods[PROD_FIRST_ARG](
"args") >>
"ternary";
45 prods[PROD_NEXT_ARG](
"args") >>
"args",
",",
"S?",
"ternary";
46 prods[PROD_NEG](
"neg") >>
"-",
"S?",
"neg";
47 prods[PROD_VAL_PARENS](
"scalar") >>
"(",
"S?",
"ternary",
")",
"S?";
48 prods[PROD_CONST](
"scalar") >>
"constant",
"S?";
49 prods[PROD_VAR](
"scalar") >>
"name",
"S?";
50 prods[PROD_NO_SPACES](
"S?");
51 prods[PROD_SPACES](
"S?") >>
"spaces";
52 out.tokens.resize(NTOKS);
53 out.tokens[TOK_SPACE](
"spaces",
"[ \t\n\r]+");
54 out.tokens[TOK_NAME](
"name",
"[_a-zA-Z][_a-zA-Z0-9]*");
55 out.tokens[TOK_ADD](
"+",
"\\+");
56 out.tokens[TOK_SUB](
"-",
"\\-");
57 out.tokens[TOK_MUL](
"*",
"\\*");
58 out.tokens[TOK_DIV](
"/",
"\\/");
59 out.tokens[TOK_POW](
"^",
"\\^");
60 out.tokens[TOK_LPAREN](
"(",
"\\(");
61 out.tokens[TOK_RPAREN](
")",
"\\)");
62 out.tokens[TOK_COMMA](
",",
",");
63 out.tokens[TOK_CHECK](
"?",
"\\?");
64 out.tokens[TOK_CHOOSE](
":",
":");
65 out.tokens[TOK_GT](
">",
">");
66 out.tokens[TOK_LT](
"<",
"<");
67 out.tokens[TOK_GEQ](
">=",
">=");
68 out.tokens[TOK_LEQ](
"<=",
"<=");
69 out.tokens[TOK_EQ](
"==",
"==");
70 out.tokens[TOK_AND](
"&&",
"&&");
71 out.tokens[TOK_OR](
"||",
"\\|\\|");
72 out.tokens[TOK_CONST](
"constant",
73 "(0|([1-9][0-9]*))(\\.[0-9]*)?([eE]\\-?[1-9][0-9]*)?");
74 out.tokens[TOK_SEMICOLON](
";",
";");
75 out.tokens[TOK_ASSIGN](
"=",
"=");
81 if (ptr.strong_count() == 0) {
82 ptr.reset(
new Language(make_language()));
89 if (ptr.strong_count() == 0) {
96 SymbolSetReader::SymbolSetReader():
97 Reader(ask_reader_tables())
101 SymbolSetReader::~SymbolSetReader()
105 void SymbolSetReader::at_shift(any& result,
int token, std::string& text) {
106 if (token == TOK_NAME) result = text;
109 void SymbolSetReader::at_reduce(any& ,
int prod, std::vector<any>& rhs) {
110 if (prod == PROD_VAR) {
111 std::string& name =
any_ref_cast<std::string>(rhs.at(0));
112 variable_names.insert(name);
113 }
else if (prod == PROD_CALL) {
114 std::string& name =
any_ref_cast<std::string>(rhs.at(0));
115 function_names.insert(name);
119 std::set<std::string> get_variables_used(std::string
const& expr) {
120 SymbolSetReader reader;
122 reader.read_string(result, expr,
"get_variables_used");
123 return reader.variable_names;
126 std::set<std::string> get_symbols_used(std::string
const& expr) {
127 SymbolSetReader reader;
129 reader.read_string(result, expr,
"get_symbols_used");
130 auto set = std::move(reader.variable_names);
131 set.insert(reader.function_names.begin(), reader.function_names.end());
135 class CalcReader :
public Reader {
137 CalcReader():Reader(MathExpr::ask_reader_tables()) {
138 unary_function_map[
"sqrt"] = &std::sqrt;
139 unary_function_map[
"sin"] = &std::sin;
140 unary_function_map[
"cos"] = &std::cos;
141 unary_function_map[
"tan"] = &std::tan;
142 unary_function_map[
"asin"] = &std::asin;
143 unary_function_map[
"acos"] = &std::acos;
144 unary_function_map[
"atan"] = &std::atan;
145 unary_function_map[
"exp"] = &std::exp;
146 unary_function_map[
"log"] = &std::log;
147 unary_function_map[
"log10"] = &std::log10;
148 binary_function_map[
"atan2"] = &std::atan2;
150 virtual ~CalcReader() =
default;
157 virtual void at_shift(any& result_any,
int token, std::string& text) {
160 case MathExpr::TOK_NAME: {
161 std::string& result = make_any_ref<std::string>(result_any);
165 case MathExpr::TOK_CONST: {
166 result_any = std::atof(text.c_str());
171 virtual void at_reduce(any& result,
int prod, std::vector<any>& rhs) {
174 case MathExpr::PROD_PROGRAM: {
176 "Calculator needs an expression to evaluate!");
177 swap(result, rhs.at(1));
180 case MathExpr::PROD_NO_STATEMENTS:
181 case MathExpr::PROD_NO_EXPR:
182 case MathExpr::PROD_NEXT_STATEMENT: {
185 case MathExpr::PROD_ASSIGN: {
186 std::string
const& name = any_ref_cast<std::string>(rhs.at(0));
187 double value = any_cast<
double>(rhs.at(4));
188 variable_map[name] = value;
191 case MathExpr::PROD_YES_EXPR:
192 case MathExpr::PROD_EXPR:
193 case MathExpr::PROD_TERNARY_DECAY:
194 case MathExpr::PROD_OR_DECAY:
195 case MathExpr::PROD_AND_DECAY:
196 case MathExpr::PROD_ADD_SUB_DECAY:
197 case MathExpr::PROD_MUL_DIV_DECAY:
198 case MathExpr::PROD_POW_DECAY:
199 case MathExpr::PROD_NEG_DECAY:
200 case MathExpr::PROD_SOME_ARGS:
201 swap(result, rhs.at(0));
203 case MathExpr::PROD_TERNARY:
204 result = any_cast<
bool>(rhs.at(0)) ?
205 any_cast<double>(rhs.at(3)) :
206 any_cast<double>(rhs.at(6));
208 case MathExpr::PROD_OR:
209 result = any_cast<
bool>(rhs.at(0)) || any_cast<bool>(rhs.at(3));
211 case MathExpr::PROD_AND:
212 result = any_cast<
bool>(rhs.at(0)) && any_cast<bool>(rhs.at(3));
214 case MathExpr::PROD_GT:
215 result = any_cast<
double>(rhs.at(0)) > any_cast<double>(rhs.at(3));
217 case MathExpr::PROD_LT:
218 result = any_cast<
double>(rhs.at(0)) < any_cast<double>(rhs.at(3));
220 case MathExpr::PROD_GEQ:
221 result = any_cast<
double>(rhs.at(0)) >= any_cast<double>(rhs.at(3));
223 case MathExpr::PROD_LEQ:
224 result = any_cast<
double>(rhs.at(0)) <= any_cast<double>(rhs.at(3));
226 case MathExpr::PROD_EQ:
227 result = any_cast<
double>(rhs.at(0)) == any_cast<double>(rhs.at(3));
229 case MathExpr::PROD_BOOL_PARENS:
230 result = any_cast<
bool>(rhs.at(2));
232 case MathExpr::PROD_ADD:
233 result = any_cast<
double>(rhs.at(0)) + any_cast<double>(rhs.at(3));
235 case MathExpr::PROD_SUB:
236 result = any_cast<
double>(rhs.at(0)) - any_cast<double>(rhs.at(3));
238 case MathExpr::PROD_MUL:
239 result = any_cast<
double>(rhs.at(0)) * any_cast<double>(rhs.at(3));
241 case MathExpr::PROD_DIV:
242 result = any_cast<
double>(rhs.at(0)) / any_cast<double>(rhs.at(3));
244 case MathExpr::PROD_POW:
245 result = std::pow(any_cast<double>(rhs.at(0)), any_cast<double>(rhs.at(3)));
247 case MathExpr::PROD_CALL: {
248 std::string& name = any_ref_cast<std::string>(rhs.at(0));
249 CallArgs& args = any_ref_cast<CallArgs>(rhs.at(4));
251 "Only unary and binary functions supported!\n");
254 "Unknown unary function name \"" << name <<
"\"\n");
255 Unary fptr = unary_function_map[name];
256 result = (*fptr)(args.a0);
259 "Unknown binary function name \"" << name <<
"\"\n");
260 Binary fptr = binary_function_map[name];
261 result = (*fptr)(args.a0, args.a1);
265 case MathExpr::PROD_NO_ARGS: {
266 CallArgs& args = make_any_ref<CallArgs>(result);
270 case MathExpr::PROD_FIRST_ARG: {
271 CallArgs& args = make_any_ref<CallArgs>(result);
272 args.a0 = any_cast<
double>(rhs.at(0));
276 case MathExpr::PROD_NEXT_ARG: {
277 CallArgs& args = any_ref_cast<CallArgs>(rhs.at(0));
278 args.a1 = any_cast<
double>(rhs.at(3));
280 swap(result, rhs.at(0));
283 case MathExpr::PROD_NEG:
284 result = - any_cast<
double>(rhs.at(2));
286 case MathExpr::PROD_VAL_PARENS:
287 result = any_cast<
double>(rhs.at(2));
289 case MathExpr::PROD_CONST:
290 result = any_cast<
double>(rhs.at(0));
292 case MathExpr::PROD_VAR:
293 std::string
const& name = any_ref_cast<std::string>(rhs.at(0));
294 auto it = variable_map.find(name);
296 "variable " << name <<
" not defined!");
297 double value = it->second;
303 typedef double (*Unary)(double);
304 typedef double (*Binary)(double, double);
305 std::map<std::string, Unary> unary_function_map;
306 std::map<std::string, Binary> binary_function_map;
307 std::map<std::string, double> variable_map;
310 Reader* new_calc_reader() {
311 return new CalcReader();
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Productions productions
vector of productions
ValueType & any_ref_cast(any &operand)
Keep the convenient behavior of Teuchos::any_cast w.r.t. references, but don't confuse it with the be...
RCP< const ReaderTables > ReaderTablesPtr
an RCP to a const ReaderTables
ReaderTablesPtr make_reader_tables(Language const &language)
constructs ReaderTables for the given Language.
RCP< const Language > LanguagePtr
an RCP to a const Language