Comments (5)
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.
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.
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.
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.
Good idea! See #56.
from ohm.
Related Issues (20)
- Type definitions for parameterized rules are inconsistent with behavior HOT 1
- Semantic action '[...]' has the wrong arity: expected 3, got 0 HOT 5
- Semantic actions for comments defined under the space rule HOT 1
- Add ability to type operation parameters & return values HOT 11
- CLI: Line endings in generated files vary across systems and platforms HOT 1
- cli.js is reading incorrect package.json file for version HOT 2
- Typescript example causes error HOT 1
- space with applySyntactic causes failure
- `toAST` incorrectly makes some nodes intermediate nodes HOT 1
- Indentation-sensitive grammar forgets consumed dedents HOT 1
- Syntax error handling? HOT 2
- Indentation-sensitive parsing: dedent not working? HOT 3
- Typescript example fails to compile HOT 1
- "Built-in Rules" section of syntax reference docs are incorrectly formatted
- No indication of stack size exceeded
- Do we get comments from ohm? HOT 6
- Idiomatic way to do partial (prefix) parses
- NPM release behind latest commit
- Partially parse grammar
- Odd behavior: Alternation fails instead of trying second alternative
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ohm.