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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:42:51+00:00 2026-05-31T13:42:51+00:00

I want to learn how calculators work. For example, say we have inputs in

  • 0

I want to learn how calculators work. For example, say we have inputs in infix notation like this:

1 + 2 x 10 – 2

The parser would have to respect common rules in math. In the above example this means:

1 + (2 x 10) – 2 = 19 (rather than 3 x 10 – 2 = 28)

And then consider this:

1 + 2 x ((2 / 9) + 7) – 2

Does it involve an Abstract Syntax Tree? A binary tree? How is the order of operations ensured to be mathematically correct? Must I use the shunting-yard algorithm to convert this to postfix notation? And then, how would I parse it in postfix notation? Why convert in the first place?

Is there a tutorial which shows how these relatively simple calculators are built? Or can someone explain?

  • 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-31T13:42:52+00:00Added an answer on May 31, 2026 at 1:42 pm

    One way to do evaluate an expression is with a recursive descent parser.
    http://en.wikipedia.org/wiki/Recursive_descent_parser

    Here’s an example grammar in BNF form:
    http://en.wikipedia.org/wiki/Backus-Naur_form

    Expr ::= Term ('+' Term | '-' Term)*
    Term ::= Factor ('*' Factor | '/' Factor)*
    
    Factor ::= ['-'] (Number | '(' Expr ')')
    
    Number ::= Digit+
    

    Here * means the preceding element is repeated zero or more times, + means one or more repeats, square brackets means optional.

    The grammar ensures that the elements of highest precedence are collected together first, or in this case, evaluated first.
    As you visit each node in the grammar, instead of building an abstract syntax tree, you evaluate the current node and return the value.

    Example code (not perfect but should give you an idea of how to map BNF to code):

    def parse_expr():
      term = parse_term()
      while 1:
        if match('+'):
          term = term + parse_term()
        elif match('-'):
          term = term - parse_term()
        else: return term
    
    def parse_term():
      factor = parse_factor()
      while 1:
        if match('*'):
          factor = factor * parse_factor()
        elif match('/'):
          factor = factor / parse_factor()
        else: return factor
    
    def parse_factor():
      if match('-'):
        negate = -1
      else: negate = 1
      if peek_digit():
        return negate * parse_number()
      if match('('):
        expr = parse_expr()
        if not match(')'): error...
        return negate * expr
      error...
    
    def parse_number():
      num = 0
      while peek_digit():
        num = num * 10 + read_digit()
      return num
    

    To show how your example of 1 + 2 * 10 - 2 would evaluate:

    call parse_expr                              stream is 1 + 2 * 10 - 2
      call parse term
        call parse factor
          call parse number which returns 1      stream is now + 2 * 10 - 2
        match '+'                                stream is now 2 * 10 - 2
        call parse factor
          call parse number which returns 2      stream is now * 10 - 2
          match '*'                              stream is now 10 - 2
          call parse number which returns 10     stream is now - 2
          computes 2 * 10, return 20
        compute 1 + 20 -> 21
        match '-'                                stream is now 2
        call parse factor
          call parse number which returns 2      stream is empty
        compute 21 - 2, return 19
      return 19
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to learn .NET and I have 2 weeks time of this. I
i want to learn about the page flip effect like this one http://www.20thingsilearned.com/en-US Is
I want to learn a new programming language. I have in mind stuff like
I want to learn lLinux Kernel programming. What would be the starting points for
I want to learn Android development and create mobile apps. What would be the
i want to learn C++; and i already have a compiler. i already know
I want to learn the basic concepts like collision detection, rendering and others in
I want to learn how practical using an LDAP server (say AD) as a
I want to learn Ruby on Rails and have set up a test environment.
I want to learn C# and the .Net Framework as well. I have no

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.