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 8778421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:32:04+00:00 2026-06-13T19:32:04+00:00

I am asking this question, because I didn’t find yet any posts that are

  • 0

I am asking this question, because I didn’t find yet any posts that are C# related and there might be some build in methods for that I couldn’t find. If there are, please tell me so and I can close this question.

Basically I have the common situation:

  1. User types a function w.r.t. one or two variables into some TextBlock
  2. I take this string analyse it
  3. As a return I would like to have a delegate to a method that will take one or two inputs (the variables) and return the function value according to what the user typed in.

Now, I could probably think (and I would like to do this on my own, because I want to use my brain) of an algorithm of analysing the string step by step to actually find out, what has to be calculated first and in what way. E.g. First scan for parentheses, look for the expression within a group of parantheses and calculate that according to more general functions etc.

But in the end I would like to “create” a method of this analysis to be easily used as a normal delegate with a couple of arguments that will return the correct function value.
Are there any methods included in C# for that already, or would I have to go and program everything by myself?

As a remark: I don’t want to use anybody else’ library, only .NET libraries are acceptable for me.

Edit: After Matt pointed out expression trees, I found this thread which is a good example to my problem.

Edit2: The example pointed out does only include simple functions and will not be useful if I want to include more complex functions such as trigonometric ones or exponentials.

  • 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-06-13T19:32:06+00:00Added an answer on June 13, 2026 at 7:32 pm

    What you are describing is a parser. There are a number of different ways of implementing them, although generally speaking, for complex grammars, a “parser generator” is often used.

    A parser generator will take a description of the grammar and convert it into code that will parse text that conforms to the grammar into some form of internal representation that can be manipulated by the program, e.g. a parse tree.

    Since you indicate you want to avoid third-party libraries, I’ll assume that the use of a parser generator is similarly excluded, which leaves you with implementing your own parser (which fortunately is quite an interesting exercise).

    The Wikipedia page on Recursive descent parsers will be particularly useful. I suggest reading through it and perhaps adapting the example code therein to your particular use case. I have done this myself a number of times for different grammars with this as a starting point, so can attest to its usefulness.

    The output from such a parser will be a “parse tree”. And you then have a number of possibilities for how you convert this into an executable delegate. One option is to implement an Evaluate() method on your parse tree nodes, which will take a set of variables and return the result of evaluating the user’s expression. As others have mentioned, your parse tree could leverage .NET’s Expression trees, or you can go down the route of emitting IL directly (permitting you to produce a compiled .NET assembly from the user’s expression for later use as required).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched before asking this question. I didn't find an answer probably because I
I am asking this question here because it is my understanding that this is
I'm asking this question, because I can't figure out when I could find an
I'm asking this as a new question because people didn't seem to understand my
This might seem like a silly question, but after asking some questions on stackoverflow
I am asking this question because, I see that Roo include SPRING MVC and
I'm asking this question because I have gone through the tutorial posted here: Preventing
This must be a very stupid question because nobody else is asking it. I'm
This isn't strictly a programming question, but I'm asking it here because it's certainly
i'm sorry for asking this question because i think it has a really simple

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.