10.2. Bison File Format

A Bison file has a similar form to a Flex file.

%{
   What is written here is copied
   verbatim to the beginning of
   file y.tab.c, the C file that
   Bison generates.
%}

Here, you define tokens, precedence
of tokens and types of attributes.

%%

Here, you write the grammar.

%%

What is written here is copied
verbatim to the end of file y.tab.c.

Defining tokens and precedence

Bison recognizes character constants, such as '=', as tokens. But they must be a single character. You need to tell Bison about other tokens.

Line

%token TOK_ARROW
tells Bison that TOK_ARROW is a token.


Writing the grammar

Write the grammar between the two lines that have only %% on them.

Use a colon instead of → in a production. After the first production in a group, a | introduces another right-hand side with the same left-hand side as before. For example,

expression : expression '+' expression

           | expression '*' expression

           | TOK_NUMBER
           ;

describes three productions

expression expression + expression
expression expression * expression
expression TOK_NUMBER

Note the semicolon at the end of the group of productions. It is required.

Programming languages can have large grammars, with a lot of nonterminals. It is a good idea to use descriptive names for nonterminals rather than single-character names.


ε-productions

To write an ε-production, write nothing at all after : or |. But you probably want to write a comment (ignored by Bison). For example,

  StatementList : /* empty */
                | …
creates an erasing production for StatementList.