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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:10:55+00:00 2026-05-27T04:10:55+00:00

Suppose I would like to write a fairly simple programming language, and I want

  • 0

Suppose I would like to write a fairly simple programming language, and I want to implement operators such like 2 + 3 * 2 = 8

What is the general way to implement things like this?

  • 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-27T04:10:56+00:00Added an answer on May 27, 2026 at 4:10 am

    I’m not sure how much detail you’re interested in, but it sounds like you’re looking to implement a parser. There’s typically two steps:

    The lexer reads over the text and converts it to tokens. For example, it might read “2 + 3 * 2” and convert it to INTEGER PLUS INTEGER STAR INTEGER

    The parser reads in the tokens and tries to match them to rules. For example, you might have these rules:

    Expr := Sum | Product | INTEGER;
    Sum := Expr PLUS Expr;
    Product := Expr STAR Expr;
    

    It reads the tokens and tries to apply the rules such that the start rule maps to the tokens its read in. In this case, it might do:

    Expr := Sum
    Expr := Expr PLUS Expr
    Expr := INTEGER(2) PLUS Expr
    Expr := INTEGER(2) PLUS Product
    Expr := INTEGER(2) PLUS Expr STAR Expr
    Expr := INTEGER(2) PLUS Integer(3) STAR Expr
    Expr := INTEGER(2) PLUS Integer(3) STAR Integer(2)
    

    There are many types of parsers. In this example I read from left to right, and started from the initial expression, working down until I’d replaced everything with a token, so this would be an LL parser. As it does this replacement, it can generate an abstract syntax tree that represents the data. The tree for this might look something like:

    Screenshot of the AST

    You can see that the Product rule is a child of the Sum rule, so it will end up happening first: 2 + (3 * 2). If the expression had been parsed differently we might’ve ended up with this tree:

    Screenshot of the AST

    Now we’re calculating (2 + 3) * 2. It all comes down to which way the parser generates the tree


    If you actually want to parse expressions, odds are you don’t want to write the parser by hand. There are parser generators that take a configuration (called a grammar) similar to the one I used above, and generate the actual parser code. Parser generators will let you specify which rule should take priority, so for example:

    Expr := Sum | Product | INTEGER;
    Sum := Expr PLUS Expr; [2]
    Product := Expr STAR Expr; [1]
    

    I labeled the Product rule as priority 1, and Sum as priority 2, so given the choice the generated parser will favor Product. You can also design the grammar itself such that the priority is built-in (this is the more common approach). For example:

    Expr := Sum | INTEGER;
    Sum := Expr PLUS Product;
    Product := Term STAR INTEGER;
    

    This forces the Products to be under the Sums in the AST. Naturally this grammar is very limited (for example, it wouldn’t match 2 * 3 + 2), but a comprehensive grammar can be written that still embeds an order of operations automatically

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

Sidebar

Related Questions

Suppose I want to implement an interpreter for a functional language. I would like
Suppose I have $str = onetwo . I would like to write a reg
I would like to ask how licensing works on multiple instances? Suppose that I
Let's suppose I'm using the Northwind database and I would like to run a
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Would it suppose any difference regarding overhead to write an import loading all the
I would like to write tests for a C library, in C. I'd like
Suppose I would like to depict data flow between two servers in Visio. I
Suppose I want to write a function in R which is a function of
I would like to know the correct way to create a nested object in

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.