next up previous contents
Next: Flex and Bison working Up: Bison and Flex Previous: Flex   Contents

Bison

A Bison grammar file has four main sections, shown here with the appropriate delimiters:

     %{
     C DECLARATIONS
     %}
     
     BISON DECLARATIONS
     
     %%
     GRAMMAR RULES
     %%
     
     ADDITIONAL C CODE

The C DECLARATIONS section contains macro definitions and declarations of functions and variables that are used in the actions in the grammar rules. These are copied to the beginning of the parser file so that they precede the definition of yyparse().

The BISON DECLARATIONS section contains declarations that define terminal and nonterminal symbols.

The GRAMMAR RULES section contains one or more Bison grammar rules. The syntax of grammar rules is similar to BNFD.1.

A Bison grammar rule has the following general form:

     RESULT: COMPONENTS...  {ACTION}
     ;

Where RESULT is the nonterminal symbol that this rule describes and COMPONENTS are various terminal and nonterminal symbols that are put together by this rule. ACTION is a C code which will be run when the given grammar rule matches.

Multiple rules for the same RESULT can be written separately or can be joined with the vertical-bar character `|' as follows:

     RESULT:   RULE1-COMPONENTS...
             | RULE2-COMPONENTS...
             ...
             ;

They are still considered distinct rules even when joined in this way.

If the COMPONENTS in a rule is empty, it means that RESULT can match the empty string.

The ADDITIONAL C CODE section is copied verbatim to the end of the parser file, just as the C DECLARATIONS section is copied to the beginning.

If the last section is empty, you may omit the %% that separates it from the grammar rules.

The example of using Bison is given is Appendix F, as the source file for Bison to generate the parser for opening Osiris datafile by Controller module.


next up previous contents
Next: Flex and Bison working Up: Bison and Flex Previous: Flex   Contents
Igor Wojnicki 2001-02-21