Improving syntax error messages
Earlier, we saw a bit about the yacc
syntax error reporting mechanism. Yacc just calls a function named yyerror(s)
. Very rarely, this function can be called for an internal error such as a parse stack overflow, but usually when it is called, it is passed the string "parse error"
or "syntax error"
as its parameter. Neither is adequate for helping programmers find and fix their errors in the real world. If you write a function called yyerror()
yourself, you can produce a better error message. The key is to have extra information available that the programmer can use. Usually, that extra information will have to be placed in a global or public static variable in order for yyerror()
to access it. Let's look at how to write a better yyerror()
function in Unicon, and then in Java.
Adding detail to Unicon syntax error messages
In the Putting together a toy example section earlier in this chapter, you saw a Unicon implementation...