LinuxCommandLibrary

bison

Create a parser from a grammar definition

TLDR

Compile a bison definition file

$ bison [path/to/file.y]
copy

Compile in debug mode, which causes the resulting parser to write additional information to stdout
$ bison [[-t|--debug]] [path/to/file.y]
copy

Specify the output filename
$ bison [[-o|--output]] [path/to/output.c] [path/to/file.y]
copy

Be verbose when compiling
$ bison [[-v|--verbose]]
copy

SYNOPSIS

bison [OPTION]... [FILE.y]

PARAMETERS

-d, --defines[=FILE]
    Also create FILE.h (or FILE.output.h if using -o); also output macro definitions there

-l, --no-lines
    Don't generate '#line' directives in output

-o, --output=FILE
    Name of the output file (default: FILE.tab.c)

-t, --debug
    Instrument the parser for tracing (yydebug)

-v, --verbose
    Create FILE.output describing parser states and conflicts

-V, --version
    Display version info and exit

-h, --help
    Display this help and exit

--file-prefix=STRING
    Use STRING as prefix of output files

-p, --name-prefix=STRING
    Prefix parser function names with STRING

-S, --skeleton=FILE
    Specify skeleton template FILE

--language=LANGUAGE
    Generate parser in LANGUAGE (c, c++, java)

-W, --warnings[=CATEGORY]
    Report warnings in specified categories

DESCRIPTION

Bison is a general-purpose parser generator that converts an annotated LALR(1) context-free grammar specification into a C, C++, or other language parser implementation.

It reads a grammar file (typically ending in .y) defining tokens, rules, and actions, then outputs source code for a deterministic parser that recognizes valid input sequences. Bison extends Yacc with features like push parsers, locations tracking, and multiple output languages.

Commonly paired with Flex for lexical analysis, it powers tools from compilers (GCC) to interpreters. Users define %token declarations, precedence rules to resolve shift/reduce conflicts, and semantic actions in C blocks. Bison reports parser states, conflicts, and generates tables for efficient runtime parsing.

CAVEATS

Resolves some shift/reduce and reduce/reduce conflicts automatically but warns; ambiguous grammars may require %left/%right/%nonassoc precedence. LALR(1) limitation: cannot handle all nondeterministic grammars. Debug traces require recompilation with yydebug=1.

OUTPUT FILES

Generates FILE.tab.c (parser), FILE.tab.h (with -d), FILE.output (with -v for conflicts/states), FILE.dot (graph with --graph).

GRAMMAR EXAMPLE

%{ #include <stdio.h> %}
%% start : expr { printf("Result: %d\n", $1); } ;
expr : expr '+' expr { $$ = $1 + $3; } | NUM ;
NUM : '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' { $$ = $1 - '0'; } ;
%%

HISTORY

Originated 1984-85 by Robert Corbett at Symbolics; Richard Stallman ported to Unix for GNU in 1985. Became official GNU project in 1988. Evolved with %define api.value, push parsing (2000s), and Java/C++ backends.

SEE ALSO

yacc(1), flex(1), byacc(1), re2c(1)

Copied to clipboard