Next: , Previous: , Up: Declarations   [Contents][Index]


3.7.12 Bison Declaration Summary

Here is a summary of the declarations used to define a grammar:

Directive: %union

Declare the collection of data types that semantic values may have (see The Union Declaration).

Directive: %token

Declare a terminal symbol (token type name) with no precedence or associativity specified (see Token Type Names).

Directive: %right

Declare a terminal symbol (token type name) that is right-associative (see Operator Precedence).

Directive: %left

Declare a terminal symbol (token type name) that is left-associative (see Operator Precedence).

Directive: %nonassoc

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.

Directive: %type

Declare the type of semantic values for a nonterminal symbol (see Nonterminal Symbols).

Directive: %start

Specify the grammar’s start symbol (see The Start-Symbol).

Directive: %expect

Declare the expected number of shift-reduce conflicts (see Suppressing Conflict Warnings).


In order to change the behavior of bison, use the following directives:

Directive: %code {code}
Directive: %code qualifier {code}

Insert code verbatim into the output parser source at the default location or at the location specified by qualifier. See %code Summary.

Directive: %debug

Instrument the parser for traces. Obsoleted by ‘%define parse.trace’. See Tracing Your Parser.

Directive: %define variable
Directive: %define variable value
Directive: %define variable {value}
Directive: %define variable "value"

Define a variable to adjust Bison’s behavior. See %define Summary.

Directive: %defines

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 YYSTYPE unless YYSTYPE is already defined as a macro or you have used a <type> 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 YYSTYPE macro 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 YYSTYPE.

Unless your parser is pure, the parser header file declares yylval as an external variable. See A Pure (Reentrant) Parser.

If you have also used locations, the parser header file declares YYLTYPE and yylloc using a protocol similar to that of the YYSTYPE macro and yylval. See Tracking Locations.

This parser header file is normally essential if you wish to put the definition of yylex in a separate source file, because yylex typically 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 requires or %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 */
Directive: %defines defines-file

Same as above, but save in the file defines-file.

Directive: %destructor

Specify how the parser should reclaim the memory associated to discarded symbols. See Freeing Discarded Symbols.

Directive: %file-prefix "prefix"

Specify a prefix to use for all Bison output file names. The names are chosen as if the grammar file were named prefix.y.

Directive: %language "language"

Specify the programming language for the generated parser. Currently supported languages include C, C++, and Java. language is case-insensitive.

Directive: %locations

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.

Directive: %name-prefix "prefix"

Rename the external symbols used in the parser so that they start with prefix instead of ‘yy’. The precise list of symbols renamed in C parsers is yyparse, yylex, yyerror, yynerrs, yylval, yychar, yydebug, and (if locations are used) yylloc. If you use a push parser, yypush_parse, yypull_parse, yypstate, yypstate_new and yypstate_delete will also be renamed. For example, if you use ‘%name-prefix "c_"’, the names become c_parse, c_lex, and so on. For C++ parsers, see the ‘%define api.namespace’ documentation in this section. See Multiple Parsers in the Same Program.

Directive: %no-lines

Don’t generate any #line preprocessor 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.

Directive: %output "file"

Generate the parser implementation in file.

Directive: %pure-parser

Deprecated version of ‘%define api.pure’ (see api.pure), for which Bison is more careful to warn about unreasonable usage.

Directive: %require "version"

Require version version or higher of Bison. See Require a Version of Bison.

Directive: %skeleton "file"

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.

Directive: %token-table

Generate an array of token names in the parser implementation file. The name of the array is yytname; yytname[i] is the name of the token whose internal Bison token code number is i. The first three elements of yytname correspond to the predefined tokens "$end", "error", and "$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 YYNTOKENS, YYNNTS, and YYNRULES, and YYNSTATES:

YYNTOKENS

The highest token number, plus one.

YYNNTS

The number of nonterminal symbols.

YYNRULES

The number of grammar rules,

YYNSTATES

The number of parser states (see Parser States).

Directive: %verbose

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.

Directive: %yacc

Pretend the option --yacc was given, i.e., imitate Yacc, including its naming conventions. See Bison Options, for more.


Next: , Previous: , Up: Declarations   [Contents][Index]