Chapter 6. Syntax of Grammar Files

Table of Contents

6.1. Lexical Rules
6.2. Module Header
6.3. Directives
6.3.1. Token Type
6.3.2. Tokens
6.3.3. Parser Name
6.3.4. Partial Parsers
6.3.5. Monad Directive
6.3.6. Lexical Analyser
6.3.7. Precedence declarations
6.3.8. Expect declarations
6.3.9. Error declaration
6.3.10. Attribute Type Declaration
6.3.11. Attribute declaration
6.4. Grammar
6.4.1. Parameterized Productions
6.5. Module Trailer

The input to Happy is a text file containing the grammar of the language you want to parse, together with some annotations that help the parser generator make a legal Haskell module that can be included in your program. This section gives the exact syntax of grammar files.

The overall format of the grammar file is given below:

<optional module header>
<directives>
%%
<grammar>
<optional module trailer>

If the name of the grammar file ends in .ly, then it is assumed to be a literate script. All lines except those beginning with a > will be ignored, and the > will be stripped from the beginning of all the code lines. There must be a blank line between each code section (lines beginning with >) and comment section. Grammars not using the literate notation must be in a file with the .y suffix.

6.1. Lexical Rules

Identifiers in Happy grammar files must take the following form (using the BNF syntax from the Haskell Report):

        id      ::= alpha { idchar }
                  | ' { any{^'} | \' } '
                  | " { any{^"} | \" } "

        alpha   ::= A | B | ... | Z
                  | a | b | ... | z

        idchar  ::= alpha
                  | 0 | 1 | ... | 9
                  | _