Chapter 8. Error Reporting and Recovery
The previous chapters discussed techniques for finding errors within bison grammars. In this chapter, we turn our attention to the other side of error detection—how the parser and lexical analyzer detect errors. This chapter presents some techniques to incorporate error detection and reporting into a parser. We’ll make a modified version of the SQL parser from Chapter 4 that demonstrates them.
Bison provides the
yyerror() routine, which are
typically sufficient for early versions of a tool. However, as any program
begins to mature, especially a programming tool, it becomes important to
provide better error recovery, which allows for detection of errors in
later portions of the file, and to provide better error reporting.
Error reporting should give as much detail about the error
as possible. The default bison error declares only that it found a
syntax error and stops parsing. In our examples, we used
yylineno to report the line number. This
provides the location of the error but does not report any other errors
within the file or where in the specified line the error occurs. The
bison locations feature, described later in this chapter, is an easy way
to pinpoint the location of an error, down to the exact line and
character numbers. In our example, we print out the locations, but
precise location information would also allow a visual interface to
highlight the relevant text.
It is often useful to categorize ...