C++: Extracting symbols/variables of an analytical mathematical expression

北慕城南 提交于 2019-12-07 18:06:51

问题


I have expressions that can be provided by the user, such as:

 a*sin(w*t) 
 a+b/c
 x^2+y^2/2

And I would like to just get the list of variables there. I don't need to do any substitutions. So, for the first formula it's gonna be {a,w,t}. For the second one {a,b,c}, and for the last one {x,y}.

The expression is primarily written to be parsed with Sympy, but I need to be able to get the list of variables in C++ for some checks. I would like to:

  • Avoid having to link the whole Python interpreter to my program
  • Avoid reinventing the wheel, as I saw there are many parsing libraries available, such as muparser, but I don't know if any of these provide this functionality

What's the easiest way to do this? How would you tackle this problem?


回答1:


Given an the input: const string input we can collect or variables into set<string> with a regex:

\b([a-zA-Z]\w*)(?:[^(a-zA-Z0-9_]|$)

You could use this in C++ as follows:

const regex re{ "\\b([a-zA-Z]\\w*)(?:[^(a-zA-Z0-9_]|$)" };
const set<string> output{ sregex_token_iterator(cbegin(input), cend(input), re, 1), sregex_token_iterator() };

Live Example

EDIT:

regex explanation:

  • \b asserts a \W character or the beginning or end of the string
  • ([a-zA-Z] captures anything begining with an alphabetic charachter
  • \w*) followed by any number of "word" characters
  • (?: specifies the start of my non-capturing optional match
  • [[^(a-zA-Z0-9_] the 1st option is a non-open-parenthesis \W character
  • |$) the other option is that the end of the input has been reached


来源:https://stackoverflow.com/questions/40997788/c-extracting-symbols-variables-of-an-analytical-mathematical-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!