Essence parsers can perform recovery from parsing errors in the manner of Yacc [4] and Bison [3]. The basic idea is that the author of a grammar can specify special error productions at critical places in a grammar designed to "catch" parsing errors. This allows printing specially tailored error messages as well as some control over attribute evaluation in such a case.
Error productions contain a special grammar symbol $error on the right-hand side. ($Error must not be explicitly declared as a terminal or nonterminal in the define-grammar form.)
When an error occurs during parsing, an Essence parser pretends that it has just seen $error in the input. It will go back to the last LR state capable of accepting $error as the next symbol in the input. Moreover, it discards terminals from the input until the next input terminal is acceptable as the next input symbol after it has consumed $error. Subsequently, the parser resumes work as usual.
To prevent excessive avalanching of error messages, the parser makes sure that it consumes at least three input terminals after an attempted error recovery until it attempts recovery again.
Here is an example for the constant arithmetic expressions grammar guaranteed to catch all errors:
(define-grammar g10-error g10-error-symbol (E T P) (+ - * / l r n) E (((E T) $1) ((E $error) 0) ((E T + E) (+ $1 $3)) ((E T - E) (- $1 $3)) ((T P) $1) ((T P * T) (* $1 $3)) ((T P / T) (/ $1 $3)) ((P n) $1) ((P l E r) $2) ((P l $error r) 0)))Apart from the first catch-all rule containing $error, the parser will also, when encountering an error inside a parenthesized expression, skip until the next closing parenthesis to resume parsing.