Bison is an replacement for the yacc. Bison is basically a parser generator similar to yacc.
This explains few bison command line options that you can use to generate your output file based on given input file.
For details on how to use bison grammar is very well covered in the GNU bison manual.
1. Basic Usage
Let us say you have the bison grammar file called test.h. By default, bison will create the output file with the same name as your input file, with .tab appended to the name.
For example, if your input file is test.h, the output file generated by bison will be test.tab.c as shown below.
# ls test.y # bison test.y # ls test.tab.c test.y
However, if you want to do this for a C++ code, then name your bison grammar file with extension .ypp (or y++), which will generate the output file with .cpp (or c++) appended to the name.
For example, if your input file is test.ypp, the output file generated by bison will be test.tab.cpp
# ls test.ypp # bison test.ypp # ls test.tab.cpp test.ypp
2. Emulate Yacc Parser
If you want bison to behave like yacc in POSIX format, pass -y option as shown below.
In this example, as you can see, it created the output file name using the typical yacc convention (i.e y.tab.c), instead of y.tab.cpp by bison.
# bison -y test.ypp # ls test.tab.c y.tab.c
3. Add Custom Prefix to Symbols
If you want to add your own custom prefix to the external symbols, use the -p option.
The following will add the prefix “MY-PROJECT” to all external symbols.
# bison -p MY-PROJECT test.y # grep MY-PROJECT test.tab.c #define yyparse MY-PROJECTparse #define yylex MY-PROJECTlex #define yyerror MY-PROJECTerror #define yylval MY-PROJECTlval ..
4. Add Token Table
To include a table for the token names in the output file, use the -k option.
# bison -k test.y
You’ll see the following in the output test.tab.c file, which indicates that the token table is enabled.
/* Enabling the token table. */ #ifndef YYTOKEN_TABLE # define YYTOKEN_TABLE 0 #endif
5. Generate Header File
To generate a header file along with the .c (or .cpp) file, use the –defines as shown below. The following will also generate a header file called myheader.h
# bison --defines=myheader.h test.y # ls myheader.h test.tab.c test.y
If you don’t specify a header file name, it will generate the header file using the default name by appending .tab.h to the input file
# bison --defines test.y # ls test.tab.h test.tab.c test.y
6. Report Option
You can also generate a report file using -r option as shown below. The default report file name is test.output
# ls test.y # bison -r all test.y # ls test.output test.tab.c test.y
If you want to specify your own report file name, use –report-file option nas shown below.
# ls test.y # bison -r all --report-file=myproject.log test.y # ls myproject.log test.tab.c test.y
In the above example, we’ve passsed “all” as the option for report. You can also pass any one of the following options for reports.
# bison -r test.y bison: invalid argument `test.y' for `--report' Valid arguments are: - `none' - `state', `states' - `itemset', `itemsets' - `lookahead', `lookaheads', `look-ahead' - `solved' - `all'
7. Skeleton File
You can also specify a skeleton file using -S option as shown below. By default, it will look for skeleton file in the /usr/share/bison directory (datadir) as shown below.
# bison -S skel test.y /usr/bin/m4: cannot open `/usr/share/bison/skel': No such file or directory bison: subsidiary program `/usr/bin/m4' failed (exit status 1)
If you have the skeleton file in the same directory as your input file, use ./ as shown below (or full path)
# bison -S ./skel test.y
8. Display Additional Information
The following will display the locale directory used by bison
# bison --print-localedir /usr/share/locale
The following will display the data directory used by bison
# bison --print-datadir /usr/share/bison
The following will display the bison version
# bison -V bison (GNU Bison) 2.4.1
9. Generate XML Output
To generate an xml output, use -x option as shown below.
# ls test.y # bison -x test.y # ls test.tab.c test.xml test.y
If you want to specify your own file name for the output XML, do the following:
# ls test.y # bison --xml=myproject.xml test.y # ls myproject.xml test.tab.c test.y
10. Generate Graph Output
To generate a graph output, use -g option. This will generate the output in the DOT language used by graphviz. By default it will generate graph file with .out extension.
# ls test.y # bison -g test.y # ls test.dot test.tab.c test.y
The output graph file format will look something like the following:
# cat test.dot digraph Automaton { 0 [label="0\n$accept -> . exp $end"] 0 -> 1 [style=solid label="\"0\""] 0 -> 2 [style=dashed label="exp"] 0 -> 3 [style=dashed label="a"] ... }
You can also specify your own output graph file name as shown below.
# ls test.y # bison --graph=myproject.dot test.y # ls myproject.dot test.tab.c test.y