Here is a summary of the declarations used to define a grammar:
Declare the collection of data types that semantic values may have (see The Collection of Value Types).
Declare a terminal symbol (token type name) with no precedence or associativity specified (see Token Type Names).
Declare a terminal symbol (token type name) that is right-associative (see Operator Precedence).
Declare a terminal symbol (token type name) that is left-associative (see Operator Precedence).
Declare a terminal symbol (token type name) that is nonassociative (see Operator Precedence). Using it in a way that would be associative is a syntax error.
Declare the type of semantic values for a nonterminal symbol (see Nonterminal Symbols).
Specify the grammar's start symbol (see The Start-Symbol).
Declare the expected number of shift-reduce conflicts (see Suppressing Conflict Warnings).
In order to change the behavior of bison, use the following directives:
Insert code verbatim into the output parser source at the default location or at the location specified by qualifier. See %code Summary.
In the parser implementation file, define the macro
DEBUGwith ‘%define api.prefix prefix’, see Multiple Parsers in the Same Program) to 1 if it is not already defined, so that the debugging facilities are compiled. See Tracing Your Parser.
Define a variable to adjust Bison's behavior. See %define Summary.
Write a parser header file containing macro definitions for the token type names defined in the grammar as well as a few other declarations. If the parser implementation file is named name.c then the parser header file is named name.h.
For C parsers, the parser header file declares
YYSTYPEis already defined as a macro or you have used a
>tag without using
%union. Therefore, if you are using a
%union(see More Than One Value Type) with components that require other definitions, or if you have defined a
YYSTYPEmacro or type definition (see Data Types of Semantic Values), you need to arrange for these definitions to be propagated to all modules, e.g., by putting them in a prerequisite header that is included both by your parser and by any other module that needs
Unless your parser is pure, the parser header file declares
yylvalas an external variable. See A Pure (Reentrant) Parser.
If you have also used locations, the parser header file declares
yyllocusing a protocol similar to that of the
yylval. See Tracking Locations.
This parser header file is normally essential if you wish to put the definition of
yylexin a separate source file, because
yylextypically needs to be able to refer to the above-mentioned declarations and to the token type codes. See Semantic Values of Tokens.
If you have declared
%code provides, the output header also contains their code. See %code Summary.
The generated header is protected against multiple inclusions with a C preprocessor guard: ‘YY_PREFIX_FILE_INCLUDED’, where PREFIX and FILE are the prefix (see Multiple Parsers in the Same Program) and generated file name turned uppercase, with each series of non alphanumerical characters converted to a single underscore.
For instance with ‘%define api.prefix "calc"’ and ‘%defines "lib/parse.h"’, the header will be guarded as follows.#ifndef YY_CALC_LIB_PARSE_H_INCLUDED # define YY_CALC_LIB_PARSE_H_INCLUDED ... #endif /* ! YY_CALC_LIB_PARSE_H_INCLUDED */
Specify how the parser should reclaim the memory associated to discarded symbols. See Freeing Discarded Symbols.
Specify a prefix to use for all Bison output file names. The names are chosen as if the grammar file were named prefix.y.
Specify the programming language for the generated parser. Currently supported languages include C, C++, and Java. language is case-insensitive.
Generate the code processing the locations (see Special Features for Use in Actions). This mode is enabled as soon as the grammar uses the special ‘@n’ tokens, but if your grammar does not use it, using ‘%locations’ allows for more accurate syntax error messages.
Don't generate any
#linepreprocessor commands in the parser implementation file. Ordinarily Bison writes these commands in the parser implementation file so that the C compiler and debuggers will associate errors and object code with your source file (the grammar file). This directive causes them to associate errors with the parser implementation file, treating it as an independent source file in its own right.
Deprecated version of
%define api.pure(see api.pure), for which Bison is more careful to warn about unreasonable usage.
Require version version or higher of Bison. See Require a Version of Bison.
Specify the skeleton to use.
If file does not contain a
/, file is the name of a skeleton file in the Bison installation directory. If it does, file is an absolute file name or a file name relative to the directory of the grammar file. This is similar to how most shells resolve commands.
Generate an array of token names in the parser implementation file. The name of the array is
]is the name of the token whose internal Bison token code number is i. The first three elements of
yytnamecorrespond to the predefined tokens
"$undefined"; after these come the symbols defined in the grammar file.
The name in the table includes all the characters needed to represent the token in Bison. For single-character literals and literal strings, this includes the surrounding quoting characters and any escape sequences. For example, the Bison single-character literal
'+'corresponds to a three-character name, represented in C as
"'+'"; and the Bison two-character literal string
"\\/"corresponds to a five-character name, represented in C as
When you specify
%token-table, Bison also generates macro definitions for macros
- The highest token number, plus one.
- The number of nonterminal symbols.
- The number of grammar rules,
- The number of parser states (see Parser States).
Write an extra output file containing verbose descriptions of the parser states and what is done for each type of lookahead token in that state. See Understanding Your Parser, for more information.
Pretend the option --yacc was given, i.e., imitate Yacc, including its naming conventions. See Bison Options, for more.