Chapter 6. Syntax of Grammar Files
Prev |  | Next

## Chapter 6. Syntax of Grammar Files

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
                      | _

Prev |  | Next
Chapter 5. Invoking Happy | Home | 6.2. Module Header