What are some good ways to report errors when compiling code in a new programming language? One answer may be to just stop after the first error, but this can be unsatisfactory because it takes additional time to fix the error, re-compile, and then fix the next error. In contrast, if the language implementation reports all the errors in advance, the programmer can fix them all in one go and save several seconds of time spent waiting on the program to build.
To clarify what I'm asking, suppose we have the following sample program in C++ that is supposed to print "Hello World". However, this program was not transcribed properly and has a few errors.
#include <iostream>
int main() {
cout << "Hello World!"
}
Now, our friend the compiler comes along and starts reading this program. First, it will see that we forgot to qualify cout
with the namespace (it should be std::cout
instead), so the program needs to issue an error. My question is, what code mechanism should the program use to do so? For instance, you may consider a scheme that uses error codes, or a scheme that uses exceptions (but I would expect an answer to go a bit more in-depth). However, after reporting the error, the compiler might want to continue parsing the remainder of the program to report other issues. (It doesn't necessarily have to, but as pointed out above, doing so can be better for the user in that it allows the user to fix multiple errors at once.) It will see that the line of code also doesn't end in a semicolon, so it will again need to report that error as well.