remusao / Bison-Flex-CPP-template

A C++ template for Bison / Flex projects
MIT License
34 stars 7 forks source link

Bison Flex C++ Template

Usage

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

$ git clone https://github.com/remusao/Bison-Flex-C---template.git 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).

Customization

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:

For more information on how to customize scanner, parser:

Dependencies

Acknowledgement

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.