bison Command in Linux



Bison, also known as "GNU Bison," is a command-line tool used for generating parsers. It works in conjunction with the GNU Project’s parser generator tool, yacc (Yet Another Compiler Compiler).

  • When using Bison, input files (which contain your grammar rules) should follow the convention of ending in ".y". Unlike yacc, where the generated files have fixed names (like y.tab.c), Bison uses the prefix of the input file to name the generated files.
  • If you need to include C++ code within your grammar description file, you can use a C+±like extension for the input file (e.g., .ypp or .y++). Bison will then follow your chosen extension to name the output file. For instance:
  • If your grammar description file is named "parse.yxx", Bison will generate the parser in a file named parse.tab.cxx. This is different from yacc, which would produce "y.tab.c" or older Bison versions that would create "parse.tab.c".
  • Bison allows you to specify a grammar for a formal language (e.g., programming language, data language). It generates a parser that analyzes the structure of input conforming to the specified grammar. Common use cases include implementing compilers and interpreters.
  • Bison generates robust LALR(1) parsers using a C-like language for grammar rules and actions. Besides that, it packs features such as automatic syntax error reporting and support for semantic value types and semantic actions.
  • Bison focuses on parsing and does not handle lexical analysis (tokenization). It is typically used alongside tools like Flex (lexical analyzer generator) to create a complete parser.

The Bison command is available under the GNU General Public License (GPL) and is extensively used by developers due to its efficiency, flexibility, and community support.

Table of Contents

Here is a comprehensive guide to the options available with the bison command −

Syntax of bison Command

The following is the general syntax for the bison Command −

bison [OPTION]... FILE

bison Command Options

Bison supports both classic single-letter options and more descriptive mnemonic long-option names. Long options are denoted using two hyphens (–), unlike single-letter options that use just one (-).

You can use abbreviated forms for long option names, as long as they remain unique. When a long option requires an argument (e.g., --file-prefix), you connect the option name and the argument using an equals sign (=)."

Options Descriptions
-b file-prefix / --file-prefix=file-prefix Specifies a prefix to use for all Bison output file names. The names are chosen as if the input file were named file-prefix.c.
-d

Writes an extra output file containing macro definitions for the token type names defined in the grammar and the semantic value type YYSTYPE. It also includes a few extern variable declarations.

If the parser output file is named name.c, then this file is named name.h. This output file is essential if you wish to put the definition of yylex in a separate source file because yylex needs to be able to refer to token type codes and the variable yylval.

--defines=defines-file Similar to the -d option. The only difference is that it has an optional argument which is the name of the output filename.
-g

Outputs a VCG (Visualizing Compiler Graphs) definition of the LALR(1) grammar automaton computed by Bison.

If the grammar file is foo.y, the VCG output file will be foo.vcg.

--graph=graph-file

Similar to the -g option.

The only difference is that it has an optional argument which is the name of the output graph filename

-k / --token-table

Causes the name.tab.c output to include a list of token names in order by their token numbers. These token names are defined in the array yytname.

Also generates #defines for YYNTOKENS, YYNNTS, YYNRULES, and YYNSTATES.

-l / --no-lines

Suppresses the inclusion of #line preprocessor commands in the parser file.

Ordinarily, Bison puts these commands in the parser file so that the C compiler and debuggers associate errors with your source file (the grammar file).

This option associates errors with the parser file, treating it as an independent source file in its own right.

-n / --no-parser

Do not generate the parser code into the output; generates only declarations. The generated name.tab.c file will have only constant declarations.

Additionally, a name.act file is generated containing a switch statement body containing all the translated actions.

-o outfile / --output-file=outfile Specifies the name of the output file for the parser. Other output files’ names are constructed from outfile as described under the -v and -d switches.
-p prefix / --name-prefix=prefix

Renames the external symbols used in the parser so that they start with prefix instead of yy.

The precise list of symbols renamed includes yyparse, yylex, yyerror, yylval, yychar, and yydebug

-t / --debug In the parser file, defines the macro YYDEBUG to 1 if it is not already defined. Enables the debugging facilities to be compiled.
-v / --verbose

Writes an extra output file containing verbose descriptions of the parser states. Describes what is done for each type of look-ahead token in those states.

Also includes information about all conflicts (both resolved by operator precedence and unresolved ones). The verbose output file is named by removing .tab.c or .c from the parser output file name and adding .output instead.

-V / --version Prints the version number of Bison and exits.
-h / --help Prints a summary of the options available in Bison and exits.
-y / --yacc / --fixed-output-files

Equivalent to -o y.tab.c.

The parser output file is called y.tab.c, and the other outputs are named y.output and y.tab.h.

This switch imitates yacc’s output file name conventions.

A common shell script that substitutes for yacc often uses bison -y "$@".

Examples of bison Command in Linux

In this section, we’ll explore some examples that can help understand the bison command. To get started, ensure you create a file containing the necessary grammar rules.

  • Compile a Bison Definition File
  • Compile in debug mode
  • Specify the output filename
  • Emulating POSIX Yacc Behavior
  • Generate a Header File Along with the .c (or .cpp) File
  • Add a Custom Prefix to External Symbols
  • Generate a report file (default name: file.output)
  • Display the bison Version Information
  • Display the Directory Containing locale-dependent Data
  • Display a Graph of the Automaton

Compile a Bison Definition File

To generate a parser from a bison grammar file called my_file.y and produce an output file with the same name as the input file (with .tab appended), use the following syntax −

Compile Bison Definition File

Compile in debug mode

To generate additional information during the parsing process, you can use the following command −

Compile Bison in debug mode

Specify the output filename

To specify the output filename for the generated parser, use the following syntax −

Specify Bison output filename

Emulating POSIX Yacc Behavior

If you want to mimic the behavior of POSIX Yacc (where the output filename convention is different), you can use the "-y" flag −

Emulating POSIX Yacc Behavior

Generate a Header File Along with the .c (or .cpp) File

To generate a header file along with the .c (or .cpp) file, use the following command −

Generate Header File Along with .c File

Add a Custom Prefix to External Symbols

To add a custom prefix to external symbols in a Bison (Yacc) grammar file, you can use the "-p" flag followed by the desired prefix.

Add Custom Prefix to External Symbols

Generate a report file (default name: file.output)

To generate a report file with the default name "file.output" using Bison (Yacc), you can use the "-r" option followed by all −

bison -r all my_file.y

This will create a report file named "file.output" containing information about the grammar rules and conflicts encountered during parsing.

Generate Bison report file

Display the bison Version Information

To display the version information for Bison (GNU Yacc), you run this command −

bison -V
Display the bison Version Information

Display the Directory Containing locale-dependent Data

To display the directory containing locale-dependent data using bison, you can use the --print-localedir option −

bison --print-localedir
Bison Directory Containing locale-dependent Data

Display a Graph of the Automaton

To create a graph of the automaton, use the "-g" option along with your grammar file −

Display Bison Graph of Automaton

Conclusion

Bison is a feature-rich command line utility that provides extensive features for grammar specification, LR parsing algorithms, semantic actions, and error handling, enabling developers to efficiently create parsers for their language processing tasks.

Advertisements