📄 sec-error.html
字号:
<HTML
><HEAD
><TITLE
>The Error Token</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.74b"><LINK
REL="HOME"
TITLE="Happy User Guide"
HREF="happy.html"><LINK
REL="UP"
TITLE="Using Happy"
HREF="sec-using.html"><LINK
REL="PREVIOUS"
TITLE="Monadic Parsers"
HREF="sec-monads.html"><LINK
REL="NEXT"
TITLE="Generating Multiple Parsers From a Single Grammar"
HREF="sec-multiple-parsers.html"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Happy User Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="sec-monads.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 2. Using <SPAN
CLASS="APPLICATION"
>Happy</SPAN
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sec-multiple-parsers.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SEC-ERROR"
>2.6. The Error Token</A
></H1
><P
><SPAN
CLASS="APPLICATION"
>Happy</SPAN
> supports a limited form of error
recovery, using the special symbol <TT
CLASS="LITERAL"
>error</TT
> in a grammar
file. When <SPAN
CLASS="APPLICATION"
>Happy</SPAN
> finds a parse error during
parsing, it automatically inserts the <TT
CLASS="LITERAL"
>error</TT
> symbol; if
your grammar deals with <TT
CLASS="LITERAL"
>error</TT
> 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
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>close : '}' { () }
| error { () }</PRE
></TD
></TR
></TABLE
><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 <TT
CLASS="LITERAL"
>let x
= e in e'</TT
>: the layout system inserts an open brace before
<TT
CLASS="LITERAL"
>x</TT
>, and the occurrence of the <TT
CLASS="LITERAL"
>in</TT
> symbol
generates a parse error, which is interpreted as a close brace
by the above rule.</P
><P
>Note for <TT
CLASS="LITERAL"
>yacc</TT
> users: this form of error recovery
is strictly more limited than that provided by <TT
CLASS="LITERAL"
>yacc</TT
>.
During a parse error condition, <TT
CLASS="LITERAL"
>yacc</TT
> 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 <TT
CLASS="LITERAL"
>yacc</TT
> error recovery is
notoriously hard to describe, and the semantics depend heavily
on the workings of a shift-reduce parser. Furthermore,
different implementations of <TT
CLASS="LITERAL"
>yacc</TT
> 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
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sec-monads.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="happy.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sec-multiple-parsers.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Monadic Parsers</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sec-using.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Generating Multiple Parsers From a Single Grammar</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -