News for hackers. Stuff that matters.
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.