News for hackers. Stuff that matters.
Re:GHC Parser Principles by sfvisser on Tuesday May 15, 2018 @05:02AM (#17072623)
Similar to what I'm noticing just right now in VSCode where the IDE warns you comments are not allowed in a JSON file. This means the parser happily parses comments and performs a separate check afterwards. Beats a "unexpected symbol '/' at 12:4"

Great UX!

Re:GHC Parser Principles by evincarofautumn on Tuesday May 15, 2018 @05:17AM (#17072670)
One thing I’ve learned in my years of working on compilers & programming language tech, which at first is counterintuitive, is that for the best user experience in a compiler, often you want your parser to be “total”, and not to reject any inputs.

Instead of bailing out at the first error, you parse invalid portions of the input and sync up to the next point where you can recover and keep parsing, annotating the resulting token stream or parse tree with error indicators such as “invalid number literal token” or “missing bracket”.

This lets you report all errors in a source file at once, so they can all be highlighted in an IDE for example, regardless of errors earlier in the file. The more semantic information you have about the correct parts of the program, the better error messages you can produce about the incorrect parts.

You can extend this to all areas of the compilation pipeline—for example, proceeding to resolve names and typecheck the definitions in a program that are syntactically correct, while ignoring the others and leaving them as error markers.

Re:GHC Parser Principles by Zalastax on Tuesday May 15, 2018 @07:45AM (#17073236)
This philosophy is an integral part to the good error reports in TypeScript: https://www.infoq.com/news/2016/05/anders-hejlsberg-compiler
Re:GHC Parser Principles by tom_mellior on Tuesday May 15, 2018 @02:30PM (#17076386)
> Instead of bailing out at the first error, you parse invalid portions of the input and sync up to the next point where you can recover and keep parsing

People like saying that, but I've never seen it work well or provide any benefits. Because these tools are necessarily imperfect, you will get false positives, i.e., an earlier error will confuse the system sufficiently to generate later errors that aren't really errors in the program. So you have to fix errors in order anyway. So there is no benefit to producing further messages. You just risk producing several pages of spurious diagnostics for a simple typo.

Of course this is not completely true, but it's true enough. Or if it isn't, I'd like to hear about examples of systems that never produce such false positives, and where for some reason it makes sense (and is actually beneficial) to change something later in the file first.

Until then, just give me the first error. I won't look at the rest anyway, so no need for the compiler to be "clever" about it.

Re:GHC Parser Principles by evincarofautumn on Tuesday May 15, 2018 @03:59PM (#17077324)
It works best in an IDE, or in my case in Emacs where I can jump to each error in turn. On the command line or in an interactive mode (REPL, notebook, &c.) I usually just want one error at a time. I’ve had fairly good luck with this in C# in Visual Studio or Xamarin Studio with Roslyn, although I don’t actually use .NET much—even though I worked on Mono for a few years, hah.

It also really depends on the grammar of the language how well you can do recovery. Ideally a language should be designed from the beginning to support it. Simple example: if a language allows nested function definitions with the same notation as top-level definitions, then you can’t reliably insert a missing closing bracket for a definition, because you can’t assume that the next one is a new top-level; it could be nested. (Although of course you can use heuristics like indentation to get around this.)

Anyway, the point of successful recovery is that the diagnostics later in the file are not spurious. And if the compiler supports recoverable parsing, you can still have a mode where it just bails out after the first error if that’s what you prefer.

Re:GHC Parser Principles by tom_mellior on Wednesday May 16, 2018 @05:31AM (#17081055)
> Anyway, the point of successful recovery is that the diagnostics later in the file are not spurious.

True if you only consider superficial syntax things. False in the real world, where compilers also check whether identifiers are declared, and with what type. If you have an error (syntax or semantic) and decide to press on, in general your symbol tables will be in a messed-up state, and you will generate spurious type or "undeclared identifier" errors. Also, in the real world, the compilers I have experience with do not only continue if they can be 100% sure that it makes sense, but they invariably continue further because they try to be "smart".

The IDE point is a semi-valid point since it doesn't scroll to the bottom-most, most useless errors like a terminal does. But I'm still not convinced that it adds value. That it enables situations where it's better to fix the second error before the first.

Re:GHC Parser Principles by evincarofautumn on Thursday May 17, 2018 @05:03AM (#17089833)
Yeah, it’s definitely a bad idea to press on beyond the point where you can reasonably recover, and actually better to err on the side of fewer diagnostics and less information per message if it means the messages aren’t misleading.

For example, if a compiler says “Expected semicolon before open bracket”…you should be able to insert a semicolon there! In my experience, that’s what a beginner programmer or someone unfamiliar with the language is going to try. If that’s not possible, the compiler is probably leaking implementation details of the parser, and should say something else. The most important thing is that the source location points to the actual code where something is wrong so the programmer can examine it. The message is secondary—make it actionable if possible, otherwise just avoid making it actively harmful.

But it also really depends on the language how well you can do recovery. Most languages in wide use are not designed to handle this, in both syntax & semantics.

Again, even if you don’t want to go for full error-recovery, it’s still a good thing to support in a compiler pipeline, because it makes it easier to produce good error messages if the compiler accepts a superset of the actual language—recognising common mistakes in order to offer fixes, for example.

Re:GHC Parser Principles by ufo on Tuesday May 15, 2018 @03:03PM (#17076713)
This works better in an IDE than if you interact with the compiler directly. If you look at the compiler output directly the false positive drown out the actual errors so it is hard to find out what you need to fix. But in an IDE the false positives just become red squiggly lines further down the file, and don't obfuscate the important red squigglys at the top.