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

The Archive Base Latest Questions

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

Here is the code for a simple arithmetic expression parser I wrote: class ArithmeticExpressionParser<T>

  • 0

Here is the code for a simple arithmetic expression parser I wrote:

class ArithmeticExpressionParser<T> : Parser<T> where T : IConvertible
{
    dynamic num1, num2;
    public override T Parse(string expr)
    {
        base.Parse(expr);
        ParseExpression();
        return num1;
    }

    T ParseExpression()
    {
        if(PeekNextToken())
            num1 = ParseFactorial();

        if (Token == '+')
        {
            GetNextToken();
            num2 = ParseExpression();
            num1 += num2;
        }

        else if (Token == '-')
        {
            GetNextToken();
            num2 = ParseExpression();

            num1 -= num2;
        }

        return num1;
    }

    T ParseFactorial()
    {
        if(PeekNextToken())
            num1 = ParseNumber();

        if (Token == '*')
        {
            GetNextToken();
            num2 = ParseFactorial();

            num1 *= num2;
        }

        else if (Token == '/')
        {
            GetNextToken();
            num2 = ParseFactorial();

            num1 /= num2;
        }

        else if (Token == '%')
        {
            GetNextToken();
            num2 = ParseFactorial();

            num1 %= num2;
        }

        return num1;
    }


    T ParseNumber()
    {
        string temp = String.Empty;

        while (char.IsDigit(Token))
        {
            temp += Token;
            GetNextToken();
        }

        return (T)Convert.ChangeType(temp, typeof(T));           
    }
}

If info is needed about the entire Parser class, just comment and I’ll post it.
For now, here are the things I think are relevant:

    protected char GetNextToken()
    {
        do
        {
            if(Count++ >= expression.Length-1) break;

        } while (char.IsWhiteSpace(expression[Count]));

        return expression[Count];
    }

    protected bool PeekNextToken()
    {
        if (Count + 1 < expression.Length)
            return true;
        else
            return false;
    }

    string expression;

    protected char Token
    {
        get
        {
            return expression[Count];
        }
    }

    int count;
    protected int Count
    {
        get { return count;  }
        set
        {
            if (value < expression.Length)
                count = value;
        }
    }

Now the Problem is, it doesn’t give correct results.

  • 2+2*2 returns 8
  • 2+2+2+2 returns 16
  • 2+2-2 returns 0

…a lot of weird results.

Also, please suggest improvements, and better ways to achieve the same thing.

Note: I am trying to write a simple arithmetic Recursive Descent Parser

  • 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-28T00:44:13+00:00Added an answer on May 28, 2026 at 12:44 am

    I do not have the solution, but looking at the code – ‘ParseExpression’ and ‘ParseFactorial’ operate and return num1, which is member accesed by all. Taking your last example 2+2-2, the first of the operation which happens is 2-2 = 0, and this overwrites the the firsr read value into num1 which is equal to 2. And hence the num1 = 0 before you can complete the addition operation and hence you get 0.

    I checked that this logic gives out the resultant output provided for 3 examples.

    I did find one more bug, your code never reads the last character if there is no space or a delimiter.

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

Sidebar

Related Questions

Here is code of very simple expression evaluator using IronRuby public class BasicRubyExpressionEvaluator {
Here is a simple piece of code: import java.io.*; public class Read { public
So here is the simple code: [System.ComponentModel.DefaultValue(true)] public bool AnyValue { get; set; }
I'll make it simple, here's the code I've got. public ActionResult DeleteNonCIStaffUser(int id) {
So I'm just working on some simple arithmetic code. Here's what I got: echo
Here is my code - fairly simple: $(document).ready(function(){ $(.location).each(function(){ $(this).click(function(){ $(map).hide(); var class =
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here is a simple code that shows what I think is a bug when
enter code here Hi All, I have a simple windows service application that connects
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:

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.