Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > by-pkgid > b58781bec61bd4b203ca2a689d92e9ba > files > 36

happy-1.11-2mdk.i586.rpm

<HTML
><HEAD
><TITLE
>Directives</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="Module Header"
HREF="sec-module-header.html"><LINK
REL="NEXT"
TITLE="Grammar"
HREF="sec-grammar.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-module-header.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-grammar.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SEC-DIRECTIVES"
>4.3. Directives</A
></H1
><P
>This section contains a number of lines of the form:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%&#60;directive name&#62; &#60;argument&#62; ...</PRE
></TD
></TR
></TABLE
><P
>The statements here are all annotations to help
      <SPAN
CLASS="APPLICATION"
>Happy</SPAN
> generate the Haskell code for the grammar.
      Some of them are optional, and some of them are required.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SEC-TOKEN-TYPE"
>4.3.1. Token Type</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%tokentype   { &#60;valid Haskell type&#62; }</PRE
></TD
></TR
></TABLE
><P
>(mandatory) The <TT
CLASS="LITERAL"
>%tokentype</TT
> directive gives the
        type of the tokens passed from the lexical analyser to the
        parser (in order that <SPAN
CLASS="APPLICATION"
>Happy</SPAN
> can supply types for
        functions and data in the generated parser).</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SEC-TOKENS"
>4.3.2. Tokens</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%token &#60;name&#62; { &#60;Haskell pattern&#62; }
       &#60;name&#62; { &#60;Haskell pattern&#62; }
       ...</PRE
></TD
></TR
></TABLE
><P
>(mandatory) The <TT
CLASS="LITERAL"
>%token</TT
> directive is used to
        tell <SPAN
CLASS="APPLICATION"
>Happy</SPAN
> about all the terminal symbols used
        in the grammar.  Each terminal has a name, by which it is
        referred to in the grammar itself, and a Haskell
        representation enclosed in braces.  Each of the patterns must
        be of the same type, given by the <TT
CLASS="LITERAL"
>%tokentype</TT
>
        directive.</P
><P
>The name of each terminal follows the lexical rules for
        <SPAN
CLASS="APPLICATION"
>Happy</SPAN
> identifiers given above.  There are no
        lexical differences between terminals and non-terminals in the
        grammar, so it is recommended that you stick to a convention;
        for example using upper case letters for terminals and lower
        case for non-terminals, or vice-versa.</P
><P
><SPAN
CLASS="APPLICATION"
>Happy</SPAN
> will give you a warning if you try
        to use the same identifier both as a non-terminal and a
        terminal, or introduce an identifier which is declared as
        neither.</P
><P
>To save writing lots of projection functions that map
        tokens to their components, you can include
        <TT
CLASS="LITERAL"
>$$</TT
> in your Haskell pattern. For
        example:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%token INT { TokenInt $$ }
       ...</PRE
></TD
></TR
></TABLE
><P
>This makes the semantic value of <TT
CLASS="LITERAL"
>INT</TT
> refer to the first argument
of <TT
CLASS="LITERAL"
>TokenInt</TT
> rather than the whole token, eliminating the need for
any projection function.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SEC-PARSER-NAME"
>4.3.3. Parser Name</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%name &#60;Haskell identifier&#62; [ &#60;non-terminal&#62; ]
...</PRE
></TD
></TR
></TABLE
><P
>(optional) The <TT
CLASS="LITERAL"
>%name</TT
> directive is followed by
        a valid Haskell identifier, and gives the name of the
        top-level parsing function in the generated parser.  This is
        the only function that needs to be exported from a parser
        module.</P
><P
>If the <TT
CLASS="LITERAL"
>%name</TT
> directive is omitted, it
        defaults to <TT
CLASS="LITERAL"
>happyParse</TT
>.</P
><P
>The <TT
CLASS="LITERAL"
>%name</TT
> directive takes an optional
	second parameter which specifies the top-level non-terminal
	which is to be parsed.  If this parameter is omitted, it
	defaults to the first non-terminal defined in the
	grammar.</P
><P
>Multiple <TT
CLASS="LITERAL"
>%name</TT
> directives may be
	given, specifying multiple parser entry points for this
	grammar (see <A
HREF="sec-multiple-parsers.html"
>Section 2.7</A
>).  When
	multiple <TT
CLASS="LITERAL"
>%name</TT
> directives are given, they
	must all specify explicit non-terminals.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SEC-MONAD-DECL"
>4.3.4. Monad Directive</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%monad { &#60;type&#62; } { &#60;then&#62; } { &#60;return&#62; }</PRE
></TD
></TR
></TABLE
><P
>(optional) The <TT
CLASS="LITERAL"
>%monad</TT
> directive takes three
        arguments: the type constructor of the monad, the
        <TT
CLASS="LITERAL"
>then</TT
> (or <TT
CLASS="LITERAL"
>bind</TT
>) operation, and the
        <TT
CLASS="LITERAL"
>return</TT
> (or <TT
CLASS="LITERAL"
>unit</TT
>) operation.  The type
        constructor can be any type with kind <TT
CLASS="LITERAL"
>* -&#62; *</TT
>.</P
><P
>Monad declarations are described in more detail in <A
HREF="sec-monads.html"
>Section 2.5</A
>.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SEC-LEXER-DECL"
>4.3.5. Lexical Analyser</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%lexer { &#60;lexer&#62; } { &#60;eof&#62; }</PRE
></TD
></TR
></TABLE
><P
>(optional) The <TT
CLASS="LITERAL"
>%lexer</TT
> directive takes two
        arguments: <TT
CLASS="LITERAL"
>&#60;lexer&#62;</TT
> is the name of the lexical
        analyser function, and <TT
CLASS="LITERAL"
>&#60;eof&#62;</TT
> is a token that
        is to be treated as the end of file.</P
><P
>Lexer declarations are described in more detail in <A
HREF="sec-monads.html#SEC-LEXERS"
>Section 2.5.2</A
>.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SEC-PREC-DECLS"
>4.3.6. Precedence declarations</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>%left     &#60;name&#62; ...
%right    &#60;name&#62; ...
%nonassoc &#60;name&#62; ...</PRE
></TD
></TR
></TABLE
><P
>These declarations are used to specify the precedences
	and associativity of tokens.  The precedence assigned by a
	<TT
CLASS="LITERAL"
>%left</TT
>, <TT
CLASS="LITERAL"
>%right</TT
> or
	<TT
CLASS="LITERAL"
>%nonassoc</TT
> declaration is defined to be
	higher than the precedence assigned by all declarations
	earlier in the file, and lower than the precedence assigned by
	all declarations later in the file.</P
><P
>The associativity of a token relative to tokens in the
	same <TT
CLASS="LITERAL"
>%left</TT
>, <TT
CLASS="LITERAL"
>%right</TT
>, or
	<TT
CLASS="LITERAL"
>%nonassoc</TT
> declaration is to the left, to
	the right, or non-associative respectively.</P
><P
>Precedence declarations are described in more detail in
	<A
HREF="sec-precedences.html"
>Section 2.3</A
>.</P
></DIV
></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-module-header.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-grammar.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Module Header</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"
>Grammar</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>