⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sec-grammar.html

📁 編譯器 像YACC的編譯及語法產生器
💻 HTML
字号:
<HTML
><HEAD
><TITLE
>Grammar</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="Syntax of Grammar Files"
HREF="sec-grammar-files.html"><LINK
REL="PREVIOUS"
TITLE="Directives"
HREF="sec-directives.html"><LINK
REL="NEXT"
TITLE="Module Trailer"
HREF="sec-module-trailer.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-directives.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 4. Syntax of Grammar Files</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sec-module-trailer.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SEC-GRAMMAR"
>4.4. Grammar</A
></H1
><P
>The grammar section comes after the directives, separated
      from them by a double-percent (<TT
CLASS="LITERAL"
>%%</TT
>) symbol.
      This section contains a number of
      <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>productions</I
></SPAN
>, each of which defines a single
      non-terminal.  Each production has the following syntax:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;non-terminal&#62; [ :: { &#60;type&#62; } ]
        :  &#60;id&#62; ... {[%] &#60;expression&#62; }
      [ |  &#60;id&#62; ... {[%] &#60;expression&#62; }
        ... ]</PRE
></TD
></TR
></TABLE
><P
>The first line gives the non-terminal to be defined by the
      production and optionally its type (type signatures for
      productions are discussed in <A
HREF="sec-type-signatures.html"
>Section 2.4</A
>).</P
><P
>Each production has at least one, and possibly many
      right-hand sides.  Each right-hand side consists of zero or more
      symbols (terminals or non-terminals) and a Haskell expression
      enclosed in braces.</P
><P
>The expression represents the semantic value of the
      non-terminal, and may refer to the semantic values of the
      symbols in the right-hand side using the meta-variables
      <TT
CLASS="LITERAL"
>$1 ... $n</TT
>.  It is an error to
      refer to <TT
CLASS="LITERAL"
>$i</TT
> when <TT
CLASS="LITERAL"
>i</TT
>
      is larger than the number of symbols on the right hand side of
      the current rule. The symbol <TT
CLASS="LITERAL"
>$</TT
> may be
      inserted literally in the Haskell expression using the sequence
      <TT
CLASS="LITERAL"
>\$</TT
> (this isn't necessary inside a
      string or character literal).</P
><P
>A semantic value of the form <TT
CLASS="LITERAL"
>{% ... }</TT
> is a
      <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>monadic action</I
></SPAN
>, and is only valid when the grammar
      file contains a <TT
CLASS="LITERAL"
>%monad</TT
> directive (<A
HREF="sec-directives.html#SEC-MONAD-DECL"
>Section 4.3.4</A
>).  Monadic actions are discussed in
      <A
HREF="sec-monads.html"
>Section 2.5</A
>.</P
><P
>Remember that all the expressions for a production must
      have the same type.</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-directives.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-module-trailer.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Directives</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sec-grammar-files.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Module Trailer</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -