This is a greatly simplified example of
Computer algebra system (CAS):
http://en.wikipedia.org/wiki/Computer_algebra_system[
^].
Traditional approach to it is based on parsing some symbolic language. You need to parse the expression and output the syntax errors if some are found. Please see some CodeProject articles:
Math Parser[
^],
Math Parser .NET[
^],
Parsing Mathematical Expressions with muParser[
^],
a Tiny Parser Generator v1.2[
^].
You can find some more:
http://www.codeproject.com/search.aspx?q=Parser+%22C%23%22&doctypeid=1[
^].
[EDIT]
So, what to do with this? You should parse the expression into the data structure called
binary expression tree:
http://en.wikipedia.org/wiki/Expression_tree[
^].
Such a simple thing as substitution of a numeric value and calculation of the numeric value of the expression would be just traversing a tree and performing the calculation on each node, passing the result to child nodes. If you want, you can compiler it into IL code during runtime, using
System.Reflection.Emit
— this is pretty advanced topic; mastering it would require good IL experience; and debugging would be pretty difficult.
[END EDIT]
There is another approach to this: you can wrap your expression in some valid C# or VB.NET code and try to compile it as an assembly using CodeDOM. Likewise, it can get all the compilation error diagnostics. Please see my past answers:
code generating using CodeDom[
^],
Create WPF Application that uses Reloadable Plugins...[
^].
—SA