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.
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_ARROWtells Bison that TOK_ARROW is a token.
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.
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.