GithubHelp home page GithubHelp logo

Comments (5)

alexwarth avatar alexwarth commented on August 24, 2024 2

Hi Joseph,

I've known about Laurie's paper for a while, and (to put it mildly) I violently disagree with it :)

Who's to say that the result of applying a rule like

expr = expr "-" expr | num

should be left-associative? Or right-associative, for that matter? PEGs don't support left-recursion! The only "correct" result of applying this rule is non-termination. Of course that isn't terribly useful, so researchers have proposed extensions to PEGs that enable left-recursive rules to produce useful results. The semantics of these extensions isn't prescribed by Bryan Ford's definition of PEGs -- it's really up to the people who created the extensions.

Ohm's approach to left recursion happens to produce right-associative parse trees for your example. This makes sense if you think of left recursion as a kind of loop: the second application of expr in expr "-" expr is like a nested loop, which will consume as much input as it can before the "outer loop" gets a chance to consume more input. I can imagine a different semantics in which the result would be left-associative. That would be fine, too, but certainly no more correct or valid than our current semantics.

You asked if this is an issue in practice. It isn't. I'll go a step further and say that this kind of "ambiguous" rule definition is code smell. Do you really want people to wonder whether expr produces a left- or right-associative result? No! In a well-written grammar, it's obvious. In this case, you want the - operator to be left-associative so you're better off defining expr like this:

expr = expr "-" num | num

Now there's no question about it.

We have plans to release a bunch of exciting new features and tools for Ohm. I want to prioritize that work over this, which I don't consider to be a real issue. So I'm closing it.

Thanks!

from ohm.

pdubroy avatar pdubroy commented on August 24, 2024

Interesting that you bring this up, as Alex and I were just talking about it last week. In my experience, it doesn't come up that often -- but yes, when it does, it can be a bit unexpected.

Thanks for filing this -- I think it's something we should think about before 1.0.

from ohm.

josephg avatar josephg commented on August 24, 2024

Do you have some other examples of this problem? I have an idea on a way to solve this, but I want to make sure it generalises beyond expr = expr - expr | num.

from ohm.

josephg avatar josephg commented on August 24, 2024

Great! Thanks for taking the time to explain your thoughts. I didn't want to spend a bunch of time figuring out realtime parsing only for the recursion algorithm to change.

There's no rush for this, but it might make sense at some point to add a warning for left- and right- recursive rules, warning people that the associativity of the resultant parse tree is arbitrary.

from ohm.

alexwarth avatar alexwarth commented on August 24, 2024

Good idea! See #56.

from ohm.

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.