473,473 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12382
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.