3 ms·
It also helps if your syntax supports error recovery. For example, if 'if' statements end with 'fi', 'do' statements end with 'od', and function definitions sta
by Someone 11y ago
It also helps if your syntax supports error recovery. For example, if 'if' statements end with 'fi', 'do' statements end with 'od', and function definitions statements end with 'end', and you get 'if ... do ... fi', you can report that you are missing a 'od' in the program, and recover parsing after the 'fi'.
C is horrible in this respect; all nesting looks the same. Because of that it took clang lots of effort to get good error recovery, for example to correctly report a missing semicolon at the end of a header file instead of reporting an error in the file including it (http://blog.llvm.org/2010/04/amazing-feats-of-clang-error-recovery.html http://blog.llvm.org/2010/04/amazing-feats-of-clang-error-re... (NB: that page is 5 years old, and does not represent the current state of gcc))
gcc makes that job even harder by allowing nested function definitions. That means that accidentally forgetting a single '}', as in
void f( int i) {
if( i > 2) {
exit(EXIT_FAILURE);
}
void g() {}
[...]
void h() {}
will make the compiler think that g() and h() are nested function definitions. So, the error you get is a “missing closing brace" on the last line of your file. Without nested function support, the error would be reported on the 'void g() {}' line. Still incorrect, but potentially thousands of lines closer to the source.
The Algol compiler (helped by Algol's syntax) I used years ago was way better. It frequently gave errors of the form
"x undeclared. Assumed real"
(if, say, you call sin(x) without declaring x) or
"semicolon missing after 'end' (inserted)”
Compilation would still fail after such errors, but you often would get meaningful error messages for the entire program. That was quite a boon if the compilation is run in batch, but it still is a good idea nowadays.
- eru 11y agoAnother option is to make indentation meaningful to fix C's problem here.
- jimmaswell 11y agoif you follow a sane style guide, it'll be obvious where thid error is anyway from the first error line no. usually and it won't matter that you got garbage errors.