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

  • Home
  • SEARCH
  • 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 8239715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:14:23+00:00 2026-06-07T20:14:23+00:00

I am working on a Parsing logic that needs to take operator precedence into

  • 0

I am working on a Parsing logic that needs to take operator precedence into consideration. My needs are not too complex. To start with I need multiplication and division to take higher precedence than addition and subtraction.

For example: 1 + 2 * 3 should be treated as 1 + (2 * 3). This is a simple example but you get the point!

[There are couple more custom tokens that I need to add to the precedence logic, which I may be able to add based on the suggestions I receive here.]

Here is one example of dealing with operator precedence: http://jim-mcbeath.blogspot.com/2008/09/scala-parser-combinators.html#precedencerevisited.

Are there any other ideas?

  • 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-07T20:14:25+00:00Added an answer on June 7, 2026 at 8:14 pm

    This is a bit simpler that Jim McBeath’s example, but it does what you say you need, i.e. correct arithmetic precdedence, and also allows for parentheses. I adapted the example from Programming in Scala to get it to actually do the calculation and provide the answer.

    It should be quite self-explanatory. There is a heirarchy formed by saying an expr consists of terms interspersed with operators, terms consist of factors with operators, and factors are floating point numbers or expressions in parentheses.

    import scala.util.parsing.combinator.JavaTokenParsers
    
    class Arith extends JavaTokenParsers {
    
      type D = Double
    
      def expr:   Parser[D]    = term ~ rep(plus | minus)     ^^ {case a~b => (a /: b)((acc,f) => f(acc))} 
      def plus:   Parser[D=>D] = "+" ~ term                   ^^ {case "+"~b => _ + b}
      def minus:  Parser[D=>D] = "-" ~ term                   ^^ {case "-"~b => _ - b}
      def term:   Parser[D]    = factor ~ rep(times | divide) ^^ {case a~b => (a /: b)((acc,f) => f(acc))}
      def times:  Parser[D=>D] = "*" ~ factor                 ^^ {case "*"~b => _ * b }
      def divide: Parser[D=>D] = "/" ~ factor                 ^^ {case "/"~b => _ / b} 
      def factor: Parser[D]    = fpn | "(" ~> expr <~ ")" 
      def fpn:    Parser[D]    = floatingPointNumber          ^^ (_.toDouble)
    
    }
    
    object Main extends Arith with App {
      val input = "(1 + 2 * 3 + 9) * 2 + 1"
      println(parseAll(expr, input).get) // prints 33.0
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code that deals with parsing files (mainly XML, but there
I'm working on a Haskell project that involves tying a big knot: I'm parsing
I'm working on a project that is trying to use context-free grammars for parsing
I've started working on a WebForms (not MVC) application that will reside on a
I working for an app that requires google login. I need to access provider_uid,
I am working on parsing a big xml and storing it into a temporary
I'm working on an existing data parsing program that overlays a struct onto a
Working on parsing a bunch of databases put together in an older, more freewheeling
I am working on parsing a website in my application. This is the website:
Hello Android programmers, I am working on parsing a website in my application. This

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.