Code Monkey home page Code Monkey logo

Comments (5)

johnynek avatar johnynek commented on May 29, 2024
  1. you don't need the defer(recurse) since recurse is already a value, the defer isn't doing anything.
  2. op first has to parse recurse so that's your infinite loop.
  3. I think your real question is how do you parse infix (or suffix operators).

A pattern I like is:

val nonOps: Parser[Expr] = subexp | lit
val operator: Parser[Expr => Expr] =
  (stringIn("+" :: "-" :: "*" :: "/" :: Nil), nonOps)
    .mapN { (op, right) =>
      { expr: Expr => Op(expr, op, right) }
    }

val result = Parser[Expr] = (nonOps ~ operator.rep0).map { case (init, ops) => ops.foldLeft(init) { (i, fn) => fn(i) }

result

So, you break the operator parsing apart. You have to be a bit more sophisticated if you want to get precedence correct.

from cats-parse.

re-xyr avatar re-xyr commented on May 29, 2024

What about having a deref[A](x: Reference[Parser[A]]): Parser[A] that derefs a parser in a Reference upon execution? If so we can first declare a list of References and then reassign the contents of them (where we can freely cross reference).

from cats-parse.

johnynek avatar johnynek commented on May 29, 2024

How is that different than defer?

The issue is that you still have to be careful to make some progress before doing recursion. Otherwise you set up an infinite loop.

from cats-parse.

re-xyr avatar re-xyr commented on May 29, 2024

Oh sorry - didn't read the API thoroughly. I think maybe we can explain how to deal with left recursion in README?

from cats-parse.

glamm avatar glamm commented on May 29, 2024

@johnynek Thanks for the response. That did answer my question but revealed that I need to learn a lot more about the complexities of parsers. I'll close this issue.

from cats-parse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.