<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>6.4. Grammar</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-grammar-files.html" title="Chapter 6. Syntax of Grammar Files"><link rel="prev" href="sec-directives.html" title="6.3. Directives"><link rel="next" href="sec-module-trailer.html" title="6.5. Module Trailer"></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">6.4. Grammar</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="sec-directives.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Syntax of Grammar Files</th><td width="20%" align="right"> <a accesskey="n" href="sec-module-trailer.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-grammar"></a>6.4. Grammar</h2></div></div></div><p>The grammar section comes after the directives, separated from them by a double-percent (<code class="literal">%%</code>) symbol. This section contains a number of <span class="emphasis"><em>productions</em></span>, each of which defines a single non-terminal. Each production has the following syntax:</p><a class="indexterm" name="id2550789"></a><pre class="programlisting"> <non-terminal> [ :: { <type> } ] : <id> ... {[%] <expression> } [ | <id> ... {[%] <expression> } ... ] </pre><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 class="xref" href="sec-type-signatures.html" title="2.4. Type Signatures">Section 2.4, “Type Signatures”</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 <code class="literal">$1 ... $n</code>. It is an error to refer to <code class="literal">$i</code> when <code class="literal">i</code> is larger than the number of symbols on the right hand side of the current rule. The symbol <code class="literal">$</code> may be inserted literally in the Haskell expression using the sequence <code class="literal">\$</code> (this isn't necessary inside a string or character literal).</p><p>Additionally, the sequence <code class="literal">$></code> can be used to represent the value of the rightmost symbol.</p><p>A semantic value of the form <code class="literal">{% ... }</code> is a <span class="emphasis"><em>monadic action</em></span>, and is only valid when the grammar file contains a <code class="literal">%monad</code> directive (<a class="xref" href="sec-directives.html#sec-monad-decl" title="6.3.5. Monad Directive">Section 6.3.5, “Monad Directive”</a>). Monadic actions are discussed in <a class="xref" href="sec-monads.html" title="2.5. Monadic Parsers">Section 2.5, “Monadic Parsers”</a>.</p><a class="indexterm" name="id2550899"></a><p>Remember that all the expressions for a production must have the same type.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sec-directives.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sec-grammar-files.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sec-module-trailer.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.3. Directives </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 6.5. Module Trailer</td></tr></table></div></body></html>