Created: 2012-03-23 22:25
Updated: 2019-01-22 06:32
License: mit

Bison Flex C++ Template


First check that the project is compiling without any error on your system before proceeding to any further customization.

$ git clone bison-flex-template
$ cd bison-flex-template
$ ./configure
$ make

This should produce an executable parser at the root of your bison-flex-template directory. By default it reads its input from stdin:

$ echo "foo" | ./parser
# 1.1 Unexpected token : f
# 1.2 Unexpected token : o
# 1.3 Unexpected token : o

By default, the scanner doesn't recognize any token except eol (end of line), and every character read is considered an unknown token, that's why every letter is reported as an error. We see that location is updated as each line starts with a couple line.column to indicate where the current token begins. The three lines are Lexer errors (unrecognised tokens).


I won't write a complete explanation on how Bison and Flex work but once you get a program structure that runs well (which can be quite difficult... that's why I decided to create this repository), we proceed as follows to customize our lexer and parser:

  • Write scanner src/parse/scan.ll, you can declare:
    • Named regex (follow the example of blank and eol
    • Flex options
    • Stacks to simulate contexts (useful for comments for example)
    • Tokens and associated code, one by line {blank} { /* C++ code goes here */ return TOKEN; } (TOKEN will be caught by Bison that will try to match it with the grammar)
  • Write parser src/parse/parse.yy, you can declare:
    • Tokens (names and types, they will be used both in scan.ll and parse.yy)
    • Bison options
    • Precedance rules
    • Grammar
    • Customize error reporting using the error() function
  • Interact with parser through the driver, as is shown in
    • Include driver.hh
    • Create a Driver object
    • Use one of parse() or parse_file() functions (read from stdin and a file respectively)

For more information on how to customize scanner, parser:


  • Bison
  • Flex
  • CMake
  • a recent C++ compiler (g++ or clang++)


The repository is born after I spent some time working my way to integrate Bison and Flex into my project. I thought this work could be useful in the futur, when a scanner or parser is needed.

As Bison and Flex can be complicated to use, there may be easier alternatives such as Boost spirit that better integrates in C++ code.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more