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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:41:19+00:00 2026-06-04T23:41:19+00:00

So I’ve been trying to write a calculator with Scala’s parser, and it’s been

  • 0

So I’ve been trying to write a calculator with Scala’s parser, and it’s been fun, except that I found that operator associativity is backwards, and that when I try to make my grammar left-recursive, even though it’s completely unambiguous, I get a stack overflow.

To clarify, if I have a rule like:
def subtract: Parser[Int] = num ~ “-” ~ add { x => x._1._1 – x._2 }
then evaluating 7 – 4 – 3 comes out to be 6 instead of 0.

The way I have actually implemented this is that I am composing a binary tree where operators are non-leaf nodes, and leaf nodes are numbers. The way I evaluate the tree is left child (operator) right child. When constructing the tree for 7 – 4 – 5, what I would like for it to look like is:

-
-   5
7   4   NULL   NULL

where – is the root, its children are – and 5, and the second -‘s children are 7 and 4.

However, the only tree I can construct easily is

-
7   -
NULL   NULL   4   5

which is different, and not what I want.

Basically, the easy parenthesization is 7 – (4 – 5) whereas I want (7 – 4) – 5.

How can I hack this? I feel like I should be able to write a calculator with correct operator precedence regardless. Should I tokenize everything first and then reverse my tokens? Is it ok for me to just flip my tree by taking all left children of right children and making them the right child of the right child’s parent and making the parent the left child of the ex-right child? It seems good at a first approximation, but I haven’t really thought about it too deeply. I feel like there must just be some case that I’m missing.

My impression is that I can only make an LL parser with the scala parsers. If you know another way, please tell me!

  • 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-04T23:41:22+00:00Added an answer on June 4, 2026 at 11:41 pm

    Scala’s standard implementation of parser combinators (the Parsers trait) do not support left-recursive grammars. You can, however, use PackratParsers if you need left recursion. That said, if your grammar is a simple arithmetic expression parser, you most definitely do not need left recursion.

    Edit

    There are ways to use right recursion and still keep left associativity, and if you are keen on that, just look up arithmetic expressions and recursive descent parsers. And, of course, as, I said, you can use PackratParsers, which allow left recursion.

    But the easiest way to handle associativity without using PackratParsers is to avoid using recursion. Just use one of the repetition operators to get a List, and then foldLeft or foldRight as required. Simple example:

    trait Tree
    case class Node(op: String, left: Tree, right: Tree) extends Tree
    case class Leaf(value: Int) extends Tree
    
    import scala.util.parsing.combinator.RegexParsers
    
    object P extends RegexParsers {
      def expr = term ~ (("+" | "-") ~ term).* ^^ mkTree
      def term = "\\d+".r ^^ (_.toInt)
      def mkTree(input: Int ~ List[String ~ Int]): Tree = input match {
        case first ~ rest => ((Leaf(first): Tree) /: rest)(combine)
      }
      def combine(acc: Tree, next: String ~ Int) = next match {
        case op ~ y => Node(op, acc, Leaf(y))
      }
    }
    

    You can find other, more complete, examples on the scala-dist repository.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.