Error recovery (long)

Guy Harris guy at sun.uucp
Sun Jun 1 08:50:03 AEST 1986


> Regarding error recovery in C compilers, I like the error recovery
> provided by the Data General C compiler.  Here is an example of a 
> botched program:
> 
> 	main() {
> 		int	a = 0	/* missing ";" */
> 
> 		printf("a: %s\n",  (a == 1) ? "1" : "?"; /* missing ")" */
> 	}
> 
> When compiled the following is written on stderr:
> 
> 	Error 502 severity 2 beginning on line 4 (Line 4 of file main.c) 
> 		printf("a: %s\n",  (a == 1) ? "1" : "?";
> 		^
> 	Syntax Error.
> 	A symbol of type ";" has been inserted before this symbol.

I think the Berkeley Pascal compiler does the same sort of thing.  I don't
know whether this would be practical for the C compiler or not.

> I don't know the parsing method used in this compiler; it does not seem
> to suffer from poor error recovery as do many recursive-descent parsers.

For what it's worth, many (if not most) UNIX C compilers don't use recursive
descent parsers; they are based on PCC which uses YACC.

> While on the subject of compilers, I would like to share two other features
> of this compiler that I find useful.  I have not found these features in
> other C compilers that I have used, although I have heard that the VAX/VMS
> C compiler is very good.
> 
> The first feature is the ability to generate a stack trace ("traceback") 
> in the event of a serious error.  There are two compiler switches that
> control the amount of information in a traceback.  The "-Clineid" switch
> causes the offending line number to be included while the "-Cprocid" switch 
> causes the procedure name to be included.

You can certainly get the equivalent of that in many UNIX systems; the only
difference is that you go into a debugger when the program drops core and
ask the debugger for a stack trace.  I'm not convinced that getting a
traceback "for free" is any better than going "dbx mumble" or "sdb mumble"
when you get the "Segmentation violation - core dumped" error you so dislike
and asking for a stack trace.  (If you've compiled the program with the "-g"
flag, it will give you line numbers in the stack trace.  It will also permit
you to examine variables in the program.  Merely being told what the call
stack looked like may not be sufficient to enable you to figure out what
happened, so you may have to go into a debugger anyway, even on the DG
system.)

> The second feature is the ability to declare certain data structures as
> "read only." This is done via a compiler switch "-R" and applies to all 
> data structures that are initialized to a constant value within the 
> compilation unit.

Much too crude.  What you want is the feature which will appear in ANSI C,
where you can specify which objects are to be constants and which are not.
The objects which are to be constants can be put in sharable read-only
regions.  The trouble with the "-R" flag as you describe it is that you have
to make sure that the initialized data structures which are to be read-only
must be in separate source files from the ones which are to be read-write.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy at sun.arpa



More information about the Comp.lang.c mailing list