FindBISON
Finds the Bison command-line parser generator and provides a CMake command to generate custom build rules for using Bison:
find_package(BISON [<version>] [...])
Bison is a parser generator that replaced earlier Yacc (Yet Another Compiler-Compiler). On Unix-like systems, most common implementation is GNU Bison. On Windows, this module looks for Windows-compatible Bison, if installed.
Result Variables
This module defines the following variables:
-
BISON_FOUND -
Boolean indicating whether (the requested version of) Bison was found.
-
BISON_VERSION -
The version of Bison found.
Cache Variables
The following cache variables may also be set:
-
BISON_EXECUTABLE -
The path to the
bisoncommand-line program.
Commands
This module provides the following command if bison is found:
-
bison_target -
Creates a custom build rule to generate a parser file from a Yacc file using Bison:
bison_target( <name> <input-yacc-file> <output-parser-file> [DEFINES_FILE <header>] [VERBOSE [<file>]] # The [<file>] argument is deprecated [REPORT_FILE <file>] [OPTIONS <options>...] [COMPILE_FLAGS <string>] # Deprecated )
Changed in version 3.14: When policy
CMP0088is set toNEW,bisonruns in theCMAKE_CURRENT_BINARY_DIRdirectory.-
<name> -
String used as an identifier for this command invocation.
-
<input-yacc-file> -
The path to an input Yacc source file (
.y). If given as a relative path, it will be interpreted relative to the current source directory (CMAKE_CURRENT_SOURCE_DIR). -
<output-parser-file> -
The path of the output parser file to be generated by Bison. If given as a relative path, it will be interpreted relative to the current Bison working directory.
-
DEFINES_FILE <header> -
Added in version 3.4.
By default, Bison can generate a header file containing the list of tokens. This option allows specifying a custom
<header>file to be generated by Bison. If given as a relative path, it will be interpreted relative to the current Bison working directory. -
VERBOSE [<file>] -
Enables generation of a verbose grammar and parser report. By default, the report file is created in the current Bison working directory and named
<output-parser-filename>.output.-
<file> -
Deprecated since version 3.7: Use
VERBOSE REPORT_FILE <file>.Specifies the path to which the report file should be copied. This argument is retained for backward compatibility and only works when the
<output-parser-file>is specified as an absolute path.
-
-
REPORT_FILE <file> -
Added in version 3.7.
Used in combination with
VERBOSEto specify a custom path for the report output<file>, overriding the default location. If given as a relative path, it will be interpreted relative to the current Bison working directory. -
OPTIONS <options>... -
Added in version 4.0.
A semicolon-separated list of extra options added to the
bisoncommand line. -
COMPILE_FLAGS <string> -
Deprecated since version 4.0: Superseded by
OPTIONS <options>....A string of space-separated extra options added to the
bisoncommand line. A semicolon-separated list will not work.
Command variables
This command also defines the following variables:
-
BISON_<name>_DEFINED -
Boolean indicating whether this command was successfully invoked.
-
BISON_<name>_INPUT -
The input source file, an alias for
<input-yacc-file>. -
BISON_<name>_OUTPUT_SOURCE -
The output parser file generated by
bison. -
BISON_<name>_OUTPUT_HEADER -
The header file generated by
bison, if any. -
BISON_<name>_OUTPUTS -
A list of files generated by
bison, including the output parser file, header file, and report file. -
BISON_<name>_OPTIONS -
Added in version 4.0.
A list of command-line options used for the
bisoncommand. -
BISON_<name>_COMPILE_FLAGS -
Deprecated since version 4.0: Superseded by
BISON_<name>_OPTIONSvariable with the same value.A list of command-line options used for the
bisoncommand.
-
Examples
Examples: Finding Bison
Finding Bison:
find_package(BISON)
Finding Bison with a minimum required version:
find_package(BISON 2.1.3)
Finding Bison and making it required (if Bison is not found, processing stops with an error message):
find_package(BISON 2.1.3 REQUIRED)
Example: Generating Parser
Finding Bison and adding input Yacc source file parser.y to be processed by Bison into parser.cpp source file with header parser.h at build phase:
find_package(BISON)
if(BISON_FOUND)
bison_target(MyParser parser.y parser.cpp DEFINES_FILE parser.h)
endif()
add_executable(Foo main.cpp ${BISON_MyParser_OUTPUTS})
Examples: Command-line Options
Adding additional command-line options to the bison executable can be passed as a list. For example, adding the -Wall option to report all warnings, and --no-lines (-l) to not generate #line directives:
find_package(BISON) if(BISON_FOUND) bison_target(MyParser parser.y parser.cpp OPTIONS -Wall --no-lines) endif()
Generator expressions can be used in the OPTIONS <options>... argument. For example, to add the --debug (-t) option only for the Debug build type:
find_package(BISON) if(BISON_FOUND) bison_target(MyParser parser.y parser.cpp OPTIONS $<$<CONFIG:Debug>:-t>) endif()
See Also
- The
FindFLEXmodule to find Flex scanner generator.
© 2000–2026 Kitware, Inc. and Contributors
Licensed under the BSD 3-clause License.
https://cmake.org/cmake/help/latest/module/FindBISON.html