Parse string expression and get result

  • Thread starter Thread starter wannabe geek
  • Start date Start date
W

wannabe geek

I need a way to successfully parse a string expression and return a number.
Some examples:
"1+1" = 2
"3+4*2" = 11
"(11*(8-3))/5" = 11
"X*2" = X*2
"If((1=1)=True,1,2)" = 1
"Sum(X,3,5)+Max(1,2,3)" = X+11

I am thinking about creating a function plotter and the user must be able to
create complex functions. I need to be able to use 'X' as a parameter. I also
need the ablility to use any number of custom functions. I will probably be
using only boolean and numeric (double perhaps?) values. I can foresee no
need for comments. Could somebody guide me to a webpage that is
straightforward and easy to follow? I would appreciate something quite
efficient since this will be iterating quite a few times.
 
wannabe said:
I need a way to successfully parse a string expression and return a number.
Some examples:
"1+1" = 2
"3+4*2" = 11
"(11*(8-3))/5" = 11
"X*2" = X*2
"If((1=1)=True,1,2)" = 1
"Sum(X,3,5)+Max(1,2,3)" = X+11

I am thinking about creating a function plotter and the user must be able to
create complex functions. I need to be able to use 'X' as a parameter. I also
need the ablility to use any number of custom functions. I will probably be
using only boolean and numeric (double perhaps?) values. I can foresee no
need for comments. Could somebody guide me to a webpage that is
straightforward and easy to follow? I would appreciate something quite
efficient since this will be iterating quite a few times.

You can make some rather sophisticated parsers or you
can be plain lazy and decide on C# syntax and dynamicly
generate some C# code and call that.

Arne
 
Arne said:
You can make some rather sophisticated parsers or you
can be plain lazy and decide on C# syntax and dynamicly
generate some C# code and call that.

Example:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;
using System.CodeDom.Compiler;

using Microsoft.CSharp;

namespace E
{
public interface IFuncEval
{
double Eval(double x);
}
public class FuncEvalFactory
{
private static int n = 0;
private static Dictionary<string,IFuncEval> m = new
Dictionary<string,IFuncEval>();
public static IFuncEval Create(string expr)
{
IFuncEval o = null;
if(m.ContainsKey(expr))
{
o = m[expr];
}
else
{
n++;
string cn = "FuncEval_" + n;
string src = "using System;" +
"using E;" +
"" +
"public class " + cn + " : IFuncEval" +
"{" +
" public double Eval(double x)" +
" {" +
" return " + expr + ";" +
" }" +
"}";
CodeDomProvider comp = new CSharpCodeProvider();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.ReferencedAssemblies.Add("System.dll");

param.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults res =
comp.CompileAssemblyFromSource(param, src);
Assembly asm = res.CompiledAssembly;
o = (IFuncEval)asm.CreateInstance(cn);
m.Add(expr, o);
}
return o;
}
}
public class MainClass
{
public static void Test(string expr)
{
Console.WriteLine(expr + ":");
IFuncEval f = FuncEvalFactory.Create(expr);
for(int i = -2; i <= 2; i++)
{
Console.WriteLine(" " + i + " -> " + f.Eval(i));
}
}
public static void Main(string[] args)
{
Test("2*x*x-3*x+4");
Test("Math.Exp(Math.Sqrt(x+2))");
Console.ReadKey();
}
}
}

Arne
 
Arne Vajhøj said:
You can make some rather sophisticated parsers or you
can be plain lazy and decide on C# syntax and dynamicly
generate some C# code and call that.

Arne

.

Since this app is supposed to be user-friendly, my parser should not be case
sensitive, and that complicates the dynamic C# thingie. So does the custom
function option and pretty much everything else I want. Dynamic C# would be
my last option, probably after forgetting the whole thing. So that leaves
building my own parser. But did you think that I could do a quarter of the
parsing, and use another tool to do the rest? I want something that will do
as much parsing as possible and I will have to do little or none.
 
wannabe said:
Since this app is supposed to be user-friendly, my parser should not be case
sensitive, and that complicates the dynamic C# thingie. So does the custom
function option and pretty much everything else I want. Dynamic C# would be
my last option, probably after forgetting the whole thing. So that leaves
building my own parser. But did you think that I could do a quarter of the
parsing, and use another tool to do the rest? I want something that will do
as much parsing as possible and I will have to do little or none.

If you want more control over it, then you will need to code
more.

You can do it all manually:
- parse string to tokens
- convert from infix to postfix
- evaluate using stack

I believe that is a basic CS exercise.

You could get your hands on a parser generator and generate
parsing code from a grammar.

There are more options.

Arne
 
* wannabe geek wrote, On 27-11-2009 18:15:
I need a way to successfully parse a string expression and return a number.
Some examples:
"1+1" = 2
"3+4*2" = 11
"(11*(8-3))/5" = 11
"X*2" = X*2
"If((1=1)=True,1,2)" = 1
"Sum(X,3,5)+Max(1,2,3)" = X+11

I am thinking about creating a function plotter and the user must be able to
create complex functions. I need to be able to use 'X' as a parameter. I also
need the ablility to use any number of custom functions. I will probably be
using only boolean and numeric (double perhaps?) values. I can foresee no
need for comments. Could somebody guide me to a webpage that is
straightforward and easy to follow? I would appreciate something quite
efficient since this will be iterating quite a few times.

You might also want to look at the following library:
http://blueanalysis.com/acidlibrary.php

Supports most (if not all) of your requirements. And it's fairly cheap.
I guess it wouldn't be feasible to write this library for the license
costs they're asking.
 
Acid Library comes close to what I was looking for. My main gripe is that I
was planning to use compiled .Net C# for custom functions, probably stored in
a Dictionary<string, someKindOfDelegate> variable. Acid Library would require
a complete rethink of that, since it requires custom functions to be strings.
I was thinking about my functions and I came to the conclusion that all
functions accept one parameter of type object[]. Such functions could notify
the application of improper parameters instead of me floundering around
trying to get some info from the original string.

Any ideas that could suit my purpose and be compatible with my methods would
be greatly appreciated, everybody! By the way, if it could possibly be free
that would be a bonus. I have no way to purchase things online. I am only a
14 year old newbie and under the control of my father. :-) Don't want to beg.
 
wannabe said:
[...]
Any ideas that could suit my purpose and be compatible with my methods would
be greatly appreciated, everybody! By the way, if it could possibly be free
that would be a bonus. I have no way to purchase things online. I am only a
14 year old newbie and under the control of my father. :-) Don't want to beg.

I believe that there are ways to purchase something online. I can
relate to your father's reluctance to purchase things online for you, at
least in spirit if not in particulars. And I'm not suggesting you act
against his instructions. But assuming his disinclination has to do
with his own concerns about online commerce, rather than some specific
restriction of your own behavior, I'm sure you can manage without his
direct help.

That said, for a 14 year old, maybe even the otherwise reasonable US$75
fee is too much. As Arne said, what you're asking about is in fact a
relatively basic CS exercise, especially to do it the way he suggests.
You will learn a lot by finding out more about the techniques he
mentions and implementing them in code. And in the end, you'll have a
nice parser you can use for whatever you want.

For bonus points, make it a good one and provide it under some kind of
open-source license. :)

And of course, if specific questions come up while you're writing the
parser that you need help with, please feel free to ask them here. As
you can see, there are a number of people willing to help out as they
are able.

Pete
 
Peter said:
wannabe said:
[...]
Any ideas that could suit my purpose and be compatible with my methods
would be greatly appreciated, everybody! By the way, if it could
possibly be free that would be a bonus. I have no way to purchase
things online. I am only a 14 year old newbie and under the control of
my father. :-) Don't want to beg.

I believe that there are ways to purchase something online. I can
relate to your father's reluctance to purchase things online for you, at
least in spirit if not in particulars. And I'm not suggesting you act
against his instructions. But assuming his disinclination has to do
with his own concerns about online commerce, rather than some specific
restriction of your own behavior, I'm sure you can manage without his
direct help. [...]

To be clear: what I mean by the above is "ways to purchase something
online other than the usual 'use your credit card' approach".


Obviously, there are ways to purchase things online. But I'm pretty
sure those "ways" go beyond the obvious.
 
I wrote a parser in C# a while back. It might be overkill for your needs
but you can find it at

http://sites.google.com/site/fredm/linguist-parsing-system

I think it is fairly well documented and could be used for your purpose. It
can be called from a C# program easily, and does not require a compilation
step. It is free for you to use and change for any purpose whatsoever.

A somewhat simpler approach can be found at
http://www.lulu.com/product/download/design-patterns-for-searching-in-c#/3074608Chapter 3 of that book contains a discussion of a recursive descent parserand you can download the code for that (also free).You could search Google for "recursive descent parser" and find the basicideas from which you could write your own parser."wannabe geek" <[email protected]> wrote in messagenews:[email protected]...> Acid Library comes close to what I was looking for. My main gripe is thatI> was planning to use compiled .Net C# for custom functions, probably storedin> a Dictionary<string, someKindOfDelegate> variable. Acid Library wouldrequire> a complete rethink of that, since it requires custom functions to bestrings.> I was thinking about my functions and I came to the conclusion that all> functions accept one parameter of type object[]. Such functions couldnotify> the application of improper parameters instead of me floundering around> trying to get some info from the original string.>> Any ideas that could suit my purpose and be compatible with my methodswould> be greatly appreciated, everybody! By the way, if it could possibly befree> that would be a bonus. I have no way to purchase things online. I am onlya> 14 year old newbie and under the control of my father. :-) Don't want tobeg.>> -->> Wannabe Geek>>> "Jesse Houwing" wrote:>>> You might also want to look at the following library:>> http://blueanalysis.com/acidlibrary.php>>>> Supports most (if not all) of your requirements. And it's fairly cheap.>> I guess it wouldn't be feasible to write this library for the license>> costs they're asking.>>>> -->> Jesse Houwing>> jesse.houwing at sogeti.nl>> .>>
 
Fred Mellender said:
I wrote a parser in C# a while back. It might be overkill for your needs
but you can find it at

http://sites.google.com/site/fredm/linguist-parsing-system

I think it is fairly well documented and could be used for your purpose. It
can be called from a C# program easily, and does not require a compilation
step. It is free for you to use and change for any purpose whatsoever.

I glanced at the pdf describing your parser. I suspect that yes, it would be
overkill. But I saw no mention of custom functions. I would want custom
function support in any library or otherwise that I am directed to.
Preferably requiring minimal tweaking of my idea of a dictionary (such as a
list).
 
Back
Top