rules_bison

April 27, 2026 ยท View on GitHub

Bazel rules for GNU Bison.

bison

load("@rules_bison//bison:bison.bzl", "bison")

bison(name, src, bison_options, language, skeleton)

Generate source code for a Bison parser.

This rule exists for special cases where the build needs to perform further modification of the generated .c / .h before compilation. Most users will find the bison_cc_library rule more convenient.

When generating a C/C++ parser the output groups cc_srcs and cc_hdrs provide access to the generated {name}.c / {name}.cc source and {name}.h header.

When generating a Java parser the output group java_srcs provides access to the generated {name}.java source.

Example

load("@rules_bison//bison:bison.bzl", "bison")

bison(
    name = "hello",
    src = "hello.y",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcA Bison source file.

Unless language is set, the source's file extension determines whether Bison operates in C or C++ mode:
  • Inputs with file extension .y generate outputs {name}.c and {name}.h.
  • Inputs with file extension .yy, .y++, .yxx, or .ypp generate outputs {name}.cc and {name}.h.
Labelrequired
bison_optionsAdditional options to pass to the bison command.

These will be added to the command args immediately before the source file.
List of stringsoptional[]
languageWhich language to generate the parser in.Stringoptional""
skeletonSpecify the skeleton to use.

This file is used as a template for rendering the generated parser. See the Bison documentation regarding the %skeleton directive for more details.
LabeloptionalNone

bison_cc_library

load("@rules_bison//bison:bison.bzl", "bison_cc_library")

bison_cc_library(name, deps, src, bison_options, conlyopts, copts, cxxopts, include_prefix,
                 language, linkstatic, skeleton, strip_include_prefix)

Generate a C/C++ library for a Bison parser.

Verbose descriptions of the parser are available in output group bison_report.

Example

load("@rules_bison//bison:bison.bzl", "bison_cc_library")

bison_cc_library(
    name = "hello_lib",
    src = "hello.y",
)

cc_binary(
    name = "hello",
    srcs = ["hello_main.c"],
    deps = [":hello_lib"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsA list of other C/C++ libraries to depend on.List of labelsoptional[]
srcA Bison source file.

Unless language is set, the source's file extension determines whether Bison operates in C or C++ mode:
  • Inputs with file extension .y generate outputs {name}.c and {name}.h.
  • Inputs with file extension .yy, .y++, .yxx, or .ypp generate outputs {name}.cc and {name}.h.
Labelrequired
bison_optionsAdditional options to pass to the bison command.

These will be added to the command args immediately before the source file.
List of stringsoptional[]
conlyoptsAdd these options to the C compilation command.

See cc_library.conlyopts for more details.
List of stringsoptional[]
coptsAdd these options to the C/C++ compilation command.

See cc_library.copts for more details.
List of stringsoptional[]
cxxoptsAdd these options to the C++ compilation command.

See cc_library.cxxopts for more details.
List of stringsoptional[]
include_prefixA prefix to add to the path of the generated header.

See cc_library.include_prefix for more details.
Stringoptional""
languageWhich language to generate the parser in.Stringoptional""
linkstaticDisable creation of a shared library output.

See cc_library.linkstatic for more details.
BooleanoptionalFalse
skeletonSpecify the skeleton to use.

This file is used as a template for rendering the generated parser. See the Bison documentation regarding the %skeleton directive for more details.
LabeloptionalNone
strip_include_prefixA prefix to strip from the path of the generated header.

See cc_library.strip_include_prefix for more details.
Stringoptional""

bison_java_library

load("@rules_bison//bison:bison.bzl", "bison_java_library")

bison_java_library(name, deps, src, bison_options, skeleton)

Generate a Java library for a Bison parser.

Verbose descriptions of the parser are available in output group bison_report.

Example

load("@rules_bison//bison:bison.bzl", "bison_java_library")

bison_java_library(
    name = "HelloParser",
    src = "hello.y",
)

java_binary(
    name = "HelloMain",
    srcs = ["HelloMain.java"],
    main_class = "HelloMain",
    deps = [":HelloParser"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsA list of other Java libraries to depend on.List of labelsoptional[]
srcA Bison source file.Labelrequired
bison_optionsAdditional options to pass to the bison command.

These will be added to the command args immediately before the source file.
List of stringsoptional[]
skeletonSpecify the skeleton to use.

This file is used as a template for rendering the generated parser. See the Bison documentation regarding the %skeleton directive for more details.
LabeloptionalNone

bison_toolchain_info

load("@rules_bison//bison:bison.bzl", "bison_toolchain_info")

bison_toolchain_info(name, bison_env, bison_tool)

Provides ToolchainInfo and TemplateVariableInfo for the Bison toolchain.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
bison_envAdditional environment variables to set when running bison_tool.Dictionary: String -> Stringoptional{}
bison_toolA FilesToRunProvider for the bison binary.Labelrequired

BisonToolchainInfo

load("@rules_bison//bison:bison.bzl", "BisonToolchainInfo")

BisonToolchainInfo(all_files, bison_tool, bison_env)

Provider for a Bison toolchain.

FIELDS

NameDescription
all_filesA depset containing all files comprising this Bison toolchain.
bison_toolA FilesToRunProvider for the bison binary.
bison_envAdditional environment variables to set when running bison_tool.

bison_register_toolchains

load("@rules_bison//bison:bison.bzl", "bison_register_toolchains")

bison_register_toolchains(version, extra_copts)

A helper function for Bison toolchains registration.

This workspace macro will create a bison_repository named bison_v{version} and register it as a Bazel toolchain.

PARAMETERS

NameDescriptionDefault Value
versionA supported version of Bison."3.3.2"
extra_coptsAdditional C compiler options to use when building Bison.[]

bison_toolchain

load("@rules_bison//bison:bison.bzl", "bison_toolchain")

bison_toolchain(ctx)

Returns the current BisonToolchainInfo.

PARAMETERS

NameDescriptionDefault Value
ctxA rule context, where the rule has a toolchain dependency on BISON_TOOLCHAIN_TYPE.none

RETURNS

A BisonToolchainInfo.

bison_repository

load("@rules_bison//bison:bison.bzl", "bison_repository")

bison_repository(name, extra_copts, extra_http_mirrors, extra_linkopts, http_mirrors, version)

Repository rule for GNU Bison.

The resulting repository will have a //bin:bison executable target.

Example

load("@rules_bison//bison:bison.bzl", "bison_repository")

bison_repository(
    name = "bison_v3.3.2",
    version = "3.3.2",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
extra_coptsAdditional C compiler options to use when building GNU Bison.List of stringsoptional[]
extra_http_mirrorsAdditional HTTP mirrors of the GNU Bison source archives.

These mirrors will be appended to the list of default GNU mirrors.
List of stringsoptional[]
extra_linkoptsAdditional linker options to use when building GNU Bison.List of stringsoptional[]
http_mirrorsIf set then this value will be used instead of the default HTTP mirror list.

The extra_http_mirrors attribute will be appended to this list.
List of stringsoptional[]
versionA supported version of GNU Bison.Stringrequired

bison_toolchain_repository

load("@rules_bison//bison:bison.bzl", "bison_toolchain_repository")

bison_toolchain_repository(name, bison_repository)

Toolchain repository rule for Bison toolchains.

Toolchain repositories add a layer of indirection so that Bazel can resolve toolchains without downloading additional dependencies.

The resulting repository will have the following targets:

  • //bin:bison (an alias into the underlying [bison_repository] (#bison_repository))
  • //:toolchain, which can be registered with Bazel.

Example

load(
    "@rules_bison//bison:bison.bzl",
    "bison_repository",
    "bison_toolchain_repository",
)

bison_repository(
    name = "bison_v3.3.2",
    version = "3.3.2",
)

bison_toolchain_repository(
    name = "bison",
    bison_repository = "@bison_v3.3.2",
)

register_toolchains("@bison//:toolchain")

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
bison_repositoryThe name of a bison_repository.Stringrequired

bison_repository_ext

bison_repository_ext = use_extension("@rules_bison//bison/extensions:bison_repository_ext.bzl", "bison_repository_ext")
bison_repository_ext.repository(name, extra_copts, extra_http_mirrors, extra_linkopts, http_mirrors,
                                version)

Module extension for declaring dependencies on GNU Bison.

The resulting repository will have the following targets:

  • //bin:bison (an alias into the underlying [bison_repository] (#bison_repository))
  • //:toolchain, which can be registered with Bazel.

Example

bison = use_extension(
    "@rules_bison//bison/extensions:bison_repository_ext.bzl",
    "bison_repository_ext",
)

bison.repository(name = "bison", version = "3.3.2")
use_repo(bison, "bison")
register_toolchains("@bison//:toolchain")

TAG CLASSES

repository

Attributes

NameDescriptionTypeMandatoryDefault
nameAn optional name for the repository.

The name must be unique within the set of names registered by this extension. If unset, the repository name will default to "bison_v{version}".
Nameoptional""
extra_coptsAdditional C compiler options to use when building GNU Bison.List of stringsoptional[]
extra_http_mirrorsAdditional HTTP mirrors of the GNU Bison source archives.

These mirrors will be appended to the list of default GNU mirrors.
List of stringsoptional[]
extra_linkoptsAdditional linker options to use when building GNU Bison.List of stringsoptional[]
http_mirrorsIf set then this value will be used instead of the default HTTP mirror list.

The extra_http_mirrors attribute will be appended to this list.
List of stringsoptional[]
versionA supported version of GNU Bison.Stringoptional"3.3.2"

bison_toolchains_ext

bison_toolchains_ext = use_extension("@rules_bison//bison/extensions:bison_toolchains_ext.bzl", "bison_toolchains_ext")
bison_toolchains_ext.toolchain(name, bison_env, bison_tool)

Module extension for declaring Bison toolchains with custom target binaries.

The resulting repository will have one subdirectory per named module tag, which contains a :toolchain target that can be registered with Bazel.

Example

bison_toolchains = use_extension(
    "@rules_bison//bison/extensions:bison_toolchains_ext.bzl",
    "bison_toolchain_ext",
)

bison_toolchains.toolchain(
    name = "custom",
    bison_tool = "//custom_bison:bison",
)
use_repo(bison_toolchains, "bison_toolchains")
register_toolchains("@bison_toolchains//custom:toolchain")

TAG CLASSES

toolchain

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the toolchain repository to create.Namerequired
bison_envAdditional environment variables to set when running bison_tool.Dictionary: String -> Stringoptional{}
bison_toolThe label of an bison executable target.Labelrequired