Flex · Bison · LLVM 14 · MIT

An educational C compiler, built from scratch

A compiler for a teaching-sized subset of C. A Flex lexer and a Bison LALR(1) parser feed a scoped symbol table and type checker, and the LLVM C++ API generates IR that runs with lli. The whole thing is about 2,300 lines you can read in one sitting.

View on GitHub Quick start

Terminal demo: compiling a small C program, showing each token and grammar rule, the generated LLVM IR, running it with lli (exit code 42), and the test suite passing
One run, start to finish: source, tokens and grammar rules (-v), LLVM IR, lli, and the test suite.

How it works

Pipeline: source code goes to the Flex lexer, tokens go to the Bison parser, which uses the symbol table and calls the LLVM IR generator, which writes output.ll for lli Source .c Lexerlexical.l · Flex Parserparser.y · Bison Symbol table IR generatorllvm_ir.c · IRBuilder output.llrun with lli tokens actions
  1. Lexical analysis. lexical.l defines token patterns; Flex generates yylex(), which turns characters into tokens such as INT, IDENTIFIER and NUMBER.
  2. Syntax analysis. parser.y is an LALR(1) grammar. Precedence declarations resolve operator ambiguity and the dangling else.
  3. Semantic analysis. As each rule is reduced, its action looks identifiers up in a scoped symbol table, checks types, and validates function calls. Errors are reported with line numbers, and no IR is written.
  4. Code generation. The same actions call llvm_ir.c, which emits IR with LLVM's IRBuilder: an alloca per local, load/store for access. The module is verified and written to output.ll.

There is no separate syntax tree. The compiler does single-pass syntax-directed translation: IR is emitted the moment Bison reduces each rule. Bison reduces bottom-up, so a loop's condition is parsed before the loop rule itself runs. Control flow handles this with mid-rule actions and a stack of basic blocks.

One statement, step by step

For return a * 7;, here is what ./compiler -v prints, and the IR each reduction produces:

Processing token at line 3: RETURN, Lexeme: return
Processing token at line 3: IDENTIFIER, Lexeme: a
Processing token at line 3: MULT, Lexeme: *
Expanding rule: expr → IDENTIFIER (line 3)
Processing token at line 3: NUMBER, Lexeme: 7
Expanding rule: expr → NUMBER (line 3)
Expanding rule: expr → expr MULT expr (line 3)
Processing token at line 3: SEMI, Lexeme: ;
Expanding rule: stmt → RETURN expr SEMI (line 3)
; expr → IDENTIFIER
%a1 = load i32, i32* %a, align 4
; expr → NUMBER gives the constant 7
; expr → expr MULT expr
%multmp = mul i32 %a1, 7
; stmt → RETURN expr SEMI
ret i32 %multmp

Quick start

Ubuntu, Debian or WSL:

sudo apt install flex bison g++ llvm-14 llvm-14-dev
export PATH=/usr/lib/llvm-14/bin:$PATH

git clone https://github.com/CodeRafay/c-compiler-flex-bison-llvm.git
cd c-compiler-flex-bison-llvm
make                                  # builds ./compiler
./compiler testCaseExamples/test.c    # writes output.ll
lli output.ll; echo $?                # runs it, prints main's return value
make test                             # runs all 166 tests

What it supports

Language

  • int, float, bool, void, with C-style int→float conversion
  • Global and local variables; one-dimensional arrays
  • Functions, parameters, return values, prototypes
  • if/else if/else, while, for, switch with fallthrough, break, continue
  • Arithmetic, relational, logical, ++/-- operators
  • printf with format strings, scanf

Not supported yet

  • Pointers, structs, char, strings, double
  • Unbraced if/loop bodies, do … while, ?:
  • Compound assignment (+=) and casts
  • Short-circuit &&/|| (both sides are evaluated)
  • #define and the rest of the preprocessor

The full list is in the README.

Who it's for

Students in a compiler construction or compiler design course, developers learning how Flex and Bison fit together, and anyone who wants a hands-on introduction to generating LLVM IR. Every feature is covered by an automated test that compiles a program, runs it with lli, and checks its exit code and output. If you use it for coursework, follow your institution's academic integrity rules.