Compile a flex/bison compiler using Scons

While working with Flex and Bison to build a compiler, I wanted to stick with Python a little. That's why I use Scons to build C/C++ files and the final application. I did not find any complete Sconstruct file to build such application, that's why I want to share mine.

The following SConstruct file (configuration file for Scons) could be used to compile the Flex/Bison example written by Timo Bingmann.

# -*- coding: utf-8 -*-

# Damien Riquet (d.riquet@gmail.com)
# WTFPL Licence
# December, 2012

# SConstruct file to compile a compiler
# Here, we use Flex and Bison to do this
# Scons knows flex as lex and bison as yacc (older versions)

# This file has been designed to compile the Flex/Bison example written by Timo Bingmann
# It can be found here: http://panthema.net/2007/flex-bison-cpp-example/

# 0) Build environment
# --------------------
env = Environment()

# Add a Bison (yacc) flag so that it creates an header file (parser.h)
env.Append(YACCFLAGS='--defines=parser.h')

# g++ compile flags
env.Append(CXXFLAGS="-W -Wall -Wextra -ansi -g")

# List of source files needed for the final compilation
sources = ["driver.cc", "exprtest.cc"]

# 1) Generate C++ files from lexer and analyzer files
# --------------------
# We use here the CXXFile class that uses flex or bison accordingly to the source file
sources += env.CXXFile(source='scanner.ll')
sources += env.CXXFile(source='parser.yy')

# 2) Build the application thanks to Program class
# --------------------
env.Program("exprtest", sources)

Feel free to use this code. Beerware / WTFPL licence.

Comments !