473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Math parser

Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek
Nov 17 '05 #1
12 12371
Hi,

You should be able to find something in the groups
This is a homework in almost all computer sciences careers :)

I did a search by "math parser" in google and found this:
http://www.c-sharpcorner.com/Code/20...hExpParser.asp

this is the link of the search
http://groups.google.com.my/groups?q...ublic.dotnet.*
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Janiek Buysrogge" <J.*********@Televic.com> wrote in message
news:2j********************************@4ax.com...
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek

Nov 17 '05 #2
Thank you, the links are very helpful.

Janiek

On Fri, 28 Oct 2005 09:25:50 -0400, "Ignacio Machin \( .NET/ C# MVP
\)" <ignacio.machin AT dot.state.fl.us> wrote:
Hi,

You should be able to find something in the groups
This is a homework in almost all computer sciences careers :)

I did a search by "math parser" in google and found this:
http://www.c-sharpcorner.com/Code/20...hExpParser.asp

this is the link of the search
http://groups.google.com.my/groups?q...ublic.dotnet.*
cheers,


Nov 17 '05 #3
wouldn't it just be best to do the math then convert the answer to string?

double math = Math.Pow((23 + 48) , 2) - (7.76 * 3.14);
Console.WriteLine (Convert.ToString(math));
"Janiek Buysrogge" wrote:
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek

Nov 17 '05 #4
If you are willing to use C# version 2, you are free to use the source code
at:
http://www.frontiernet.net/~fredm/dps/Contents.htm . See chapter 3.

"Janiek Buysrogge" <J.*********@Televic.com> wrote in message
news:2j********************************@4ax.com...
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek

Nov 17 '05 #5
Mike,

The expression will be formed at runtime as opposed to compile time.
That's why the OP is looking for an expression evaluator.

Brian

mike wrote:
wouldn't it just be best to do the math then convert the answer to string?

double math = Math.Pow((23 + 48) , 2) - (7.76 * 3.14);
Console.WriteLine (Convert.ToString(math));
"Janiek Buysrogge" wrote:
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek


Nov 17 '05 #6
Janiek,

I'm just throwing out some more options here.

1) You could use a compiler compiler. ANTLR is a popular one that
generates C# code. It would take some time to familiarize yourself
with EBNF grammars and how ANTLR works in general though.

<http://www.antlr.org>

2) Write your own parser. For an arithmetic expression evaluator it's
really not too difficult. There is a simple approach that inolves 3
basic steps. First, tokenize the expression. Second, convert the
infix notation to postfix notation. Finally, perform the evaluation
using postfix notation. There are other algorithms as well, but I
think infix to postfix is pretty easy to learn and it works well.

3) Download. Personally, I haven't seen any free ones written in C#
that I thought were worth downloading. The problem with the ones I've
seen is that they don't implement operator precedence and associativity
correctly, they don't accept user defined function, etc. It was an
issue for me, but it may not be an issue for you.

Brian

Janiek Buysrogge wrote:
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek


Nov 17 '05 #7
Hello Janiek,

You can download csharp parser at http://cis.paisley.ac.uk/crow-ci0/CSTools47.zip.
And as an example they math expression parser.
You cant extended it also if you like.

Oleg
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)"; parser should
calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek

Nov 17 '05 #8
If you don't mind dynamic code, you could do something like so:

string code = "Math.Pow(23 + 48, 2) - (7.76 * 3.14)";
string result = Eval.StringEval(code);
Console.WriteLine("Results: " + result);

Output
-------------------
Results: 5016.6336

Eval Class
--------------------
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;

public static class Eval
{
private static string funcprefix = "using System;\r\n"
+ "public delegate void Proc();\r\n"
+ "public class Wrapper { \r\n"
+ " public static object Set(string name, object value) { \r\n"
+ " AppDomain.CurrentDomain.SetData(name, value);\r\n"
+ " return value; \r\n"
+ " }\r\n"
+ " public static object Get(string name) { \r\n"
+ " return AppDomain.CurrentDomain.GetData(name);\r\n"
+ " }\r\n"
+ " public static object Invoke(Proc proc) { \r\n"
+ " proc();\r\n"
+ " return null; \r\n"
+ " }\r\n"
+ " public static object Eval() { return \r\n";
static string funcsuffix = "; \r\n} }";
public static string StringEval(string expr)
{
string program = funcprefix + expr + funcsuffix;
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;

CompilerResults results =
CodeDomProvider.CreateProvider("C#").CompileAssemb lyFromSource(cp, new
string[]{program});
if ( results.Errors.HasErrors )
{
if ( results.Errors[0].ErrorNumber == "CS0029" )
return StringEval("Invoke(delegate { " + expr + "; })");
throw new Exception(results.Errors[0].ErrorText);
}
else
{
Assembly assm = results.CompiledAssembly;
Type target = assm.GetType("Wrapper");
MethodInfo method = target.GetMethod("Eval");
object result = method.Invoke(null, null);
return result == null ? null : result.ToString();
}
}
}

--
William Stacey [MVP]

"Janiek Buysrogge" <J.*********@Televic.com> wrote in message
news:2j********************************@4ax.com...
Hello,

Does anyone know if there is a library or a sample project that can
parse strings with mathematical expressions inside ?

eg. string math = "(23 + 48) ^ 2 - (7.76 * 3.14)";
parser should calculate the result of this.

Atm I have my own parser, but it can only handle very simple
expressions with 2 operands eg. (34 * 89), I tried to expand the
functionality, but it's getting ugly (loads of substrings).

Any pointers ?

Thanks,

Janiek

Nov 17 '05 #9
Hello,

Thank you all for your contribution, I really appreciate it.

I will investigate further, the extendable parser seems the quickest
way, but the dynamic code also seems very interesting, it was a topic
of C# which I hadn't explored yet.

Again, thx for all the info,

Janiek
Nov 17 '05 #10
Janiek,

One thing to keep in mind if you decide to use the dynamic code
approach is security. Many of the BCL method calls could be used
maliciously.

Brian

Janiek Buysrogge wrote:
Hello,

Thank you all for your contribution, I really appreciate it.

I will investigate further, the extendable parser seems the quickest
way, but the dynamic code also seems very interesting, it was a topic
of C# which I hadn't explored yet.

Again, thx for all the info,

Janiek


Nov 17 '05 #11
Hello,
There are math parser components for C# (csharp), java, COM, and Delphi
at
<a href="http://www.bestcode.com">http://www.bestcode.com</a>
<a
href="http://www.bestcode.com/html/bcparser_net.html">http://www.bestcod
e.com/html/bcparser_net.html</a>is bcParser.NET, a Math Parser Component
for .NET Developers. Written in C# (CSharp) is excellent match for
Visual Studio .NET and Delphi.NET users. VB.NET and C# examples
included. (C# Source code included.)

<a
href="http://www.bestcode.com/html/tbcparser.html">http://www.bestcode.c
om/html/tbcparser.html</a> is TbcParser, a VCL math expression parser
component for Delphi and C++ Builder.

<a
href="http://www.bestcode.com/html/bcparserx.html">http://www.bestcode.c
om/html/bcparserx.html</a> is bcParserX, a Math Parser COM component for
Visual Basic, Visual C++ Developers.

<a
href="http://www.bestcode.com/html/jbcparser.html">http://www.bestcode.c
om/html/jbcparser.html</a>is JbcParser, a Math Parser for Java
Developers.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #12
He is trying to evaluate the expression that is given at runtime. It is
not known at compile time.
There are math parser components for C# (csharp), java, COM, and Delphi
at
<a href="http://www.bestcode.com">http://www.bestcode.com</a>
<a
href="http://www.bestcode.com/html/bcparser_net.html">http://www.bestcod
e.com/html/bcparser_net.html</a>is bcParser.NET, a Math Parser Component
for .NET Developers. Written in C# (CSharp) is excellent match for
Visual Studio .NET and Delphi.NET users. VB.NET and C# examples
included. (C# Source code included.)

<a
href="http://www.bestcode.com/html/tbcparser.html">http://www.bestcode.c
om/html/tbcparser.html</a> is TbcParser, a VCL math expression parser
component for Delphi and C++ Builder.

<a
href="http://www.bestcode.com/html/bcparserx.html">http://www.bestcode.c
om/html/bcparserx.html</a> is bcParserX, a Math Parser COM component for
Visual Basic, Visual C++ Developers.

<a
href="http://www.bestcode.com/html/jbcparser.html">http://www.bestcode.c
om/html/jbcparser.html</a>is JbcParser, a Math Parser for Java
Developers.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Karalius, Joseph | last post by:
Can anyone explain what is happening here? I haven't found any useful info on Google yet. Thanks in advance. mmagnet:/home/jkaralius/src/zopeplone/Python-2.3.5 # make gcc -pthread -c...
4
by: romek chronowski | last post by:
Hi I'm looking for a code in c or c++ that chcecks math syntax e.g it should check that sin(x+2) is valid syntax but sin(x-+y) or sin((x) is invalid .Thanks in advance (it is very important to me)
3
by: philippe mordellet | last post by:
hello everybody is it possible to ask the user to write a math function (ie : y = ax + b) in a textbox, and use this function to plot the curve in a window (how do Mathematica guys manage this,...
2
by: Faraz | last post by:
Thanks Dave, To repeat the question: I am developing a WinForms application and I need a Math Expression validator, not an expression evaluator. So I want to be able to say... if ((sales - tax)...
6
by: PIEBALD | last post by:
Anyone got an infix to postfix (RPN) math notation converter? I've looked around a bit and haven't found anything quite what I want. I just want a method that will take a string in infix notation...
11
by: rob | last post by:
I have the following scenario. A user requests some math calculations from a server. The data and a library of basic formulas reside on the server. Now the user should be able to create more...
0
by: UncleRic | last post by:
Environment: Mac OS X (10.4.10) on MacBook Pro I'm a Perl Neophyte. I've downloaded the XML::Parser module and am attempting to install it in my working directory (referenced via PERL5LIB env): ...
0
by: Dancorbier | last post by:
uCalc Fast Math Parser 2.95 is now available. This component allows your applications to evaluate math expressions defined at runtime. The two major new enhancements in this version are faster...
0
by: jpecci | last post by:
I am approaching parsing for the first time and I can't find which solution is best for me. I need to process command line strings implementing mathematical operations (defined by me) on objects...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.