Question

I have created a lexer and a parser for my interpreter project.

I have a question with regards to how the error handling should be implemented.

Currently, I stop the lexer/parser after I found the first error and show it on the console.

But I am not sure if this is correct. So I would like to ask which is a better practice:

  1. Stop the scanner/parser whenever the first error is found and show it on the console.

  2. Compile all the errors and show it all after the scanning/parsing is complete.

Thank you very much.

Was it helpful?

Solution

You're users will appreciate strategy (2) a lot more, if you manage to do it reasonably well. It's a lot faster to fix a bunch of mistakes in one edit than to cycle through check/edit/check/edit/check/edit for every syntax error.

However, from your perspective, resuming a parse after an error is detected is a lot more difficult. You need to figure out (or, more accurately, guess) how to fix the syntax error you detected in order to ignore it and continue the parse. If you guess wrong, you can end up creating a lot of useless syntax error reports.

Most commonly-used interpreters stop at the first error detected, while most commonly-used compilers make some attempt to continue (although not all of them do so particularly well), and fail only if they reach some threshold.

In my experience, a language implementation which does good compile-time error handling has as much or more code dedicated to creating good error messages and recovering from syntax errors than the rest of the code which lexes and parses syntactically correct programs. But, as always, YMMV.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top