Implement an Interpreter using Bison, Flex, and Automake
04 Sep 2013
This is a small example on how to implement an interpreter using Flex, Bison (formerly known as Yacc), and the Autotools. The example is based on Ben Reichard's course material.
First you need to install the C compiler, the lexer, and the LALR parser generator for this project.
It also helps to install the readline wrapper utility.
You need to create the file Makefile.dist with the folowing content.
Then you create the file configure.ac with the following content.
Finally you create the file Makefile.am with the following content.
This completes the setup of the build system.
First create the file calc_bison.y with the implementation of the parser.
Then create the file calc_flex.l with the implementation of the lexer (tokenizer).
Then create the header file calculator.h for the parser.
Finally create the file calculator.c with the main program.
Compiling and running it
Above program is built using the following steps.
You can run the calculator as follows.
Alternatively you can run the interpreter with rlwrap to get command line history.
Here is a sample session using the calculator program.
The code is also available on Github. You can get a copy using Git: