<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>2.6. The Error Token</title><link rel="stylesheet" href="fptools.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="Happy User Guide"><link rel="up" href="sec-using.html" title="Chapter 2. Using Happy"><link rel="prev" href="sec-monads.html" title="2.5. Monadic Parsers"><link rel="next" href="sec-multiple-parsers.html" title="2.7. Generating Multiple Parsers From a Single Grammar"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.6. The Error Token</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="sec-monads.html">Prev</a> </td><th width="60%" align="center">Chapter 2. Using <span class="application">Happy</span></th><td width="20%" align="right"> <a accesskey="n" href="sec-multiple-parsers.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-error"></a>2.6. The Error Token</h2></div></div></div><a class="indexterm" name="id2546602"></a><p><span class="application">Happy</span> supports a limited form of error recovery, using the special symbol <code class="literal">error</code> in a grammar file. When <span class="application">Happy</span> finds a parse error during parsing, it automatically inserts the <code class="literal">error</code> symbol; if your grammar deals with <code class="literal">error</code> explicitly, then it can detect the error and carry on.</p><p>For example, the <span class="application">Happy</span> grammar for Haskell uses error recovery to implement Haskell layout. The grammar has a rule that looks like this:</p><pre class="programlisting"> close : '}' { () } | error { () } </pre><p>This says that a close brace in a layout-indented context may be either a curly brace (inserted by the lexical analyser), or a parse error. </p><p>This rule is used to parse expressions like <code class="literal">let x = e in e'</code>: the layout system inserts an open brace before <code class="literal">x</code>, and the occurrence of the <code class="literal">in</code> symbol generates a parse error, which is interpreted as a close brace by the above rule.</p><a class="indexterm" name="id2546693"></a><p>Note for <code class="literal">yacc</code> users: this form of error recovery is strictly more limited than that provided by <code class="literal">yacc</code>. During a parse error condition, <code class="literal">yacc</code> attempts to discard states and tokens in order to get back into a state where parsing may continue; <span class="application">Happy</span> doesn't do this. The reason is that normal <code class="literal">yacc</code> error recovery is notoriously hard to describe, and the semantics depend heavily on the workings of a shift-reduce parser. Furthermore, different implementations of <code class="literal">yacc</code> appear to implement error recovery differently. <span class="application">Happy</span>'s limited error recovery on the other hand is well-defined, as is just sufficient to implement the Haskell layout rule (which is why it was added in the first place).</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sec-monads.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sec-using.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sec-multiple-parsers.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.5. Monadic Parsers </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 2.7. Generating Multiple Parsers From a Single Grammar</td></tr></table></div></body></html>