Math parser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Janiek Buysrogge

    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


  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Math parser

    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:


    this is the link of the search



    cheers,

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



    "Janiek Buysrogge" <J.Buysrogge@Te levic.com> wrote in message
    news:2j84m1ltbc ga685oatjpr46a9 a722dm8s2@4ax.c om...[color=blue]
    > 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
    >
    >[/color]


    Comment

    • Janiek Buysrogge

      #3
      Re: Math parser

      Thank you, the links are very helpful.

      Janiek

      On Fri, 28 Oct 2005 09:25:50 -0400, "Ignacio Machin \( .NET/ C# MVP
      \)" <ignacio.mach in AT dot.state.fl.us > wrote:
      [color=blue]
      >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,[/color]

      Comment

      • mike

        #4
        RE: Math parser

        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.WriteLi ne (Convert.ToStri ng(math));


        "Janiek Buysrogge" wrote:
        [color=blue]
        > 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
        >
        >
        >[/color]

        Comment

        • Fred Mellender

          #5
          Re: Math parser

          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.Buysrogge@Te levic.com> wrote in message
          news:2j84m1ltbc ga685oatjpr46a9 a722dm8s2@4ax.c om...[color=blue]
          > 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
          >
          >[/color]


          Comment

          • Brian Gideon

            #6
            Re: Math parser

            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:[color=blue]
            > 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.WriteLi ne (Convert.ToStri ng(math));
            >
            >
            > "Janiek Buysrogge" wrote:
            >[color=green]
            > > 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
            > >
            > >
            > >[/color][/color]

            Comment

            • Brian Gideon

              #7
              Re: Math parser

              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:[color=blue]
              > 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[/color]

              Comment

              • Oleg Kap

                #8
                Re: Math parser

                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
                [color=blue]
                > 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
                >[/color]


                Comment

                • William Stacey [MVP]

                  #9
                  Re: Math parser

                  If you don't mind dynamic code, you could do something like so:

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

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

                  Eval Class
                  --------------------
                  using System;
                  using System.Collecti ons.Generic;
                  using System.Reflecti on;
                  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.Curre ntDomain.SetDat a(name, value);\r\n"
                  + " return value; \r\n"
                  + " }\r\n"
                  + " public static object Get(string name) { \r\n"
                  + " return AppDomain.Curre ntDomain.GetDat a(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(stri ng expr)
                  {
                  string program = funcprefix + expr + funcsuffix;
                  CompilerParamet ers cp = new CompilerParamet ers();
                  cp.GenerateExec utable = false;
                  cp.GenerateInMe mory = true;

                  CompilerResults results =
                  CodeDomProvider .CreateProvider ("C#").CompileA ssemblyFromSour ce(cp, new
                  string[]{program});
                  if ( results.Errors. HasErrors )
                  {
                  if ( results.Errors[0].ErrorNumber == "CS0029" )
                  return StringEval("Inv oke(delegate { " + expr + "; })");
                  throw new Exception(resul ts.Errors[0].ErrorText);
                  }
                  else
                  {
                  Assembly assm = results.Compile dAssembly;
                  Type target = assm.GetType("W rapper");
                  MethodInfo method = target.GetMetho d("Eval");
                  object result = method.Invoke(n ull, null);
                  return result == null ? null : result.ToString ();
                  }
                  }
                  }

                  --
                  William Stacey [MVP]

                  "Janiek Buysrogge" <J.Buysrogge@Te levic.com> wrote in message
                  news:2j84m1ltbc ga685oatjpr46a9 a722dm8s2@4ax.c om...[color=blue]
                  > 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
                  >
                  >[/color]


                  Comment

                  • Janiek Buysrogge

                    #10
                    Re: Math parser

                    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

                    Comment

                    • Brian Gideon

                      #11
                      Re: Math parser

                      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:[color=blue]
                      > 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[/color]

                      Comment

                      • veli seirawan

                        #12
                        Re: Math parser

                        Hello,


                        There are math parser components for C# (csharp), java, COM, and Delphi
                        at
                        <a href="http://www.bestcode.co m">http://www.bestcode.co m</a>


                        <a
                        href="http://www.bestcode.co m/html/bcparser_net.ht ml">http://www.bestcod
                        e.com/html/bcparser_net.ht ml</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.co m/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.co m/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.co m/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 ***

                        Comment

                        • veli seirawan

                          #13
                          RE: Math parser

                          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.co m">http://www.bestcode.co m</a>


                          <a
                          href="http://www.bestcode.co m/html/bcparser_net.ht ml">http://www.bestcod
                          e.com/html/bcparser_net.ht ml</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.co m/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.co m/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.co m/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 ***

                          Comment

                          Working...