News for hackers. Stuff that matters.
Re:Challenging LR Parsing by sreekotay on Wednesday September 16, 2020 @12:01PM (#24493913)
You know --- more and more --- I kinda like the REBOL approach --- forget precedence.

Rather, just go L to R except where superceded by parens. Explicit and easy to understand.

(Though I say that not having lived it in production, lol - so take with a grain of salt)

Over time, readability/grokability tends to trump other factors...

Re:Challenging LR Parsing by jcranmer on Wednesday September 16, 2020 @12:08PM (#24493995)
Because parsing "a += b * c" as "(a += b) * c" is a wonderful, intuitive program.

(This does assume that op= is an expression in your language.)

Re:Challenging LR Parsing by sreekotay on Wednesday September 16, 2020 @02:37PM (#24495863)
Edit: Realized what I was tring to say: Intuitive might be the wrong thing to chase.

Intuitive to who? :) Trivial programs are trivial. We might be stuck in the BCPL derived syntax that trade a false notion of intuitive for complexity of implentation and maintenance. (And I do mean might --- it's not hard not to think incrementally from the languages we know) --- but "inituitive" is EXACTLY the problem: I guess I was arguing we might consider searching for simple and predictable (even if that comes at the cost of slightly higher initial learning?)

Re:Challenging LR Parsing by sreekotay on Wednesday September 16, 2020 @06:05PM (#24498533)
e.g. maybe write it as "b * c += a" ? ... lol full on LR - no ambiguiity.
Re:Challenging LR Parsing by phkahler on Wednesday September 16, 2020 @12:46PM (#24494438)
Including precedence rules will take care of that.
Re:Challenging LR Parsing by masklinn on Wednesday September 16, 2020 @01:19PM (#24494841)
That… would be jcranmer's point, they're replying to a toplevel comment advocating for not having precedence.
Re:Challenging LR Parsing by dTal on Wednesday September 16, 2020 @01:00PM (#24494607)
The APL approach, which is mostly just simple RL-associative + grouping parentheses, works surprisingly well in practice - although that probably owes something to the simplicity of the syntax and the way idiomatic APL uses mainly the built-in 1- and 2-argument functions.
Re:Challenging LR Parsing by sreekotay on Wednesday September 16, 2020 @02:40PM (#24495898)
That was the thought: is there some non-BCPL like grammar that makes sense. Like... (not fully formed thought here) JSON is easy but verbose - YAML is nightmare by cleaner. Maybe we're looking at it wrong....
Re:Challenging LR Parsing by dTal on Thursday September 17, 2020 @05:41AM (#24502727)
As a nearby comment observes, this leads to s-expressions.

The connection between APL syntax and s-expressions is surprisingly close. Start with prefix notation and right-associativity, add grouping parens, and you have something that looks like lisp. The Nial language is an outright fusion of the two that's fun and enlightening to play with.

https://en.wikipedia.org/wiki/Nial https://github.com/danlm/QNial7

Re:Challenging LR Parsing by tlb on Wednesday September 16, 2020 @12:59PM (#24494586)
Smalltalk too. I really liked it.

In practice, C compilers warn you if you try to take advantage of && having higher precedence than ||, so other than a few very simple rules wise programmers use extra parens anyway.

Re:Challenging LR Parsing by mamcx on Wednesday September 16, 2020 @12:18PM (#24494122)
I suspect is not that bad to force to group things manually (1 + (....)) but is truly precedence the biggest issue? I think even without that hand-coded parsers are still superior for error reporting and other factors?
Re:Challenging LR Parsing by jraph on Wednesday September 16, 2020 @12:49PM (#24494478)
I just wrote a parser in TypeScript that parses itself (for fun, and not quite done yet).

Precedence and associativity is a painful aspect of parsing JavaScript… and probably C, Java and many programming languages that look like C. It took me a non negligible part of the time to think about how to handle this. With the presence of infix, suffix and prefix operators, and weird operators like new…(), ? … : …, and a lot of rules and exceptions, it's a lot of fun (for instance, yield is an operator in JavaScript. It takes an optional expression. However, 1 + yield is forbidden…). The grammar of JavaScript expressions is… not simple.

And then, you have Automatic Semicolon Insertion, which forces you to backtrack and snapshot / restore the lexer state when consuming white characters EVERYWHERE (well, not literally everywhere, but still) because things may behave differently if there is a new line. And of course, new lines cannot happen in some constructs (there are a few exceptions to the general behavior of ASI), so you need to take care of this too.

And then you have the comma operator that is allowed in some places taking an expression (especially in older constructs, and disallowed in other places (especially in newer constructs). So you need to handle this too.

Parsing the rest is easy. It's just that there are a lot of things in the language so it's a bit long to write.

Re:Challenging LR Parsing by mamcx on Wednesday September 16, 2020 @01:10PM (#24494734)
Even with pratt parsing? Or using other method?
Re:Challenging LR Parsing by masklinn on Wednesday September 16, 2020 @01:21PM (#24494868)
I can only expect an other method as both are trivial using a pratt parser (though right-associativity still feels a bit weird).
Re:Challenging LR Parsing by jraph on Wednesday September 16, 2020 @01:51PM (#24495232)
Indeed, I wrote it by hand, the naive way.
Re:Challenging LR Parsing by hinkley on Wednesday September 16, 2020 @04:02PM (#24497000)
The standing rule has always been 5th grade math comprehension.

As with most things, switching from too much to none is generally not an improvement.

Perhaps languages should have 8 levels of precedence instead of 18.

Re:Challenging LR Parsing by ChrisSD on Wednesday September 16, 2020 @12:06PM (#24493964)
That kind of thinking inevitably leads to S-expressions.
Re:Challenging LR Parsing by sreekotay on Wednesday September 16, 2020 @02:42PM (#24495924)
You might be right about that. Maybe we need more bracket flexibility or something. Or something built for TOOLING - nobody just writes freeform anymore?
Re:Challenging LR Parsing by zokier on Wednesday September 16, 2020 @12:25PM (#24494201)
Or RPN
Re:Challenging LR Parsing by sreekotay on Wednesday September 16, 2020 @02:44PM (#24495958)
Maybe - REBOL was interesting in the functions are all RL to precedence and operators are all LR precedence. It's clean(-ish) without being too verbose/messy. It's not perfect - just thought it was an interesting direction to ponder...