Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7197003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:53:13+00:00 2026-05-28T20:53:13+00:00

Given a string containing a mathematical expression, given a set of functions/commands and given

  • 0

Given a string containing a mathematical expression, given a set of functions/commands and given a set of assigned variables, are there tools that .NET provides to quickly build a parser?

I would like to build a simple parser that analyzes an expression and breaks it into its simplest components, for example:

d*(abs(a-b)+sqrt(c))

becomes

  1. f = abs(a-b) and g = sqrt(c)
  2. e = f + g
  3. d*e
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:53:14+00:00Added an answer on May 28, 2026 at 8:53 pm

    Check out veparser as well. Here is a sample code that shows how you can build an expression evaluator( the code parses the expression and directly calculates the output ). This sample can be modified to store the evaluation tree instead of running it.

    using System;
    using VeParser;
    
    public class MathEvaluator : CharParser
    {
        protected override Parser GetRootParser()
        {
            Func<double, double, double> productFunc = (value1, value2) => value1 * value2;
            Func<double, double, double> divideFunc = (value1, value2) => value1 / value2;
            Func<double, double, double> sumFunc = (value1, value2) => value1 + value2;
            Func<double, double, double> subtractFunc = (value1, value2) => value1 - value2;
            Func<double, double> negativeFunc = value => -value;
            Func<double, double> posititveFunc = value => value;
    
    
            var dot = token('.');
            var op = token('(');
            var cp = token(')');
            var sumOp = create(sumFunc, token('+'));
            var subtractOp = create(subtractFunc, token('-'));
            var positiveOp = create(posititveFunc, token('+'));
            var negativeOp = create(negativeFunc, token('-'));
            var productOp = create(productFunc, token('*'));
            var divideOp = create(divideFunc, token('/'));
    
            // Numbers
            var deciamlPlaceValue = 1M;
            var decimalDot = run(() => { deciamlPlaceValue = 1; }, dot);
            var digit = consume((n, d) => n * 10 + char.GetNumericValue(d), keep(Digit));
            var decimalDigit = consume((n, d) => { deciamlPlaceValue = deciamlPlaceValue * 10; return (double)((decimal)n + ((decimal)char.GetNumericValue(d)) / deciamlPlaceValue); }, keep(Digit));
            var number = any(
                /* float */  create(0, seq(zeroOrMore(digit), decimalDot, oneOrMore(decimalDigit))),
                /* int   */  create(0, oneOrMore(digit))
            );
    
            var expression = createReference();
            var simpleExpression = createReference();
            // Unary
            var unaryOp = any(positiveOp, negativeOp);
            var unaryExpression = update(d => d.action(d.value),
                        createNew(seq(set("action", unaryOp), set("value", expression))));
            // Binary
            var binaryOp = any(sumOp, subtractOp, productOp, divideOp);
    
            var binaryExpressinoTree = update(x => x.value1, createNew(
                seq(
                    set("value1", simpleExpression),
                    zeroOrMore(
                        update(d => { var r = base.CreateDynamicObject(); r.value1 = d.action(d.value1, d.value2); return r; },
                            seq(
                                set("action", binaryOp),
                                set("value2", simpleExpression))))
                )));
    
    
            var privilegedExpressoin = seq(op, expression, cp);
    
            setReference(simpleExpression, any(privilegedExpressoin, unaryExpression, number));
    
            setReference(expression, any(binaryExpressinoTree, simpleExpression));
    
            return seq(expression, endOfFile());
        }
    
        public static object Eval(string expression)
        {
            MathEvaluator me = new MathEvaluator();
            var result = me.Parse(expression.ToCharArray());
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a text string containing a type name, is there some way to get
Is there a built-in function/method that can check if a given string is a
Is there a way to determine the display length of a given string (in
I've been looking for a way to hash a given string in C# that
Given a string containing jpeg image data, is it possible to load this directly
Given a charset string, such as utf-8, iso-8859-1, us-ascii etc, is there any built-in
Given a string containing an absolute URL with a querystring parameter, what is the
Given a string of length N containing characters [A-Z], how do I determine the
I'm given a string hex_txt containing a 4 digit hex number in the way
I would like to convert a string containing a valid Erlang expression to its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.