
- Compiler design tutorials how to#
- Compiler design tutorials generator#
- Compiler design tutorials code#
There’s one more thing you need to decide on: whether to target 32-bit or 64-bit assembly.

Basically, do whatever you like, but I’m only going to talk about hand-writing a lexer and parser for the rest of this series, so if you want to use bison and flex you’re on your own.
Compiler design tutorials generator#
You could probably also use a scanner generator for lexing, but hand-write your own parser. Using a parser generator is probably easier, but I haven’t tried it so I could be wrong.
Compiler design tutorials how to#
In this series of posts, I’ll show you how to write a lexer (or scanner) and recursive descent parser by hand. You also need to decide whether to write your own parser and lexer or use automatic parser and scanner generators (e.g. I started writing nqcc in Python, which I know very well, then got fed up and switched to OCaml, which I didn’t know well at all, and it was definitely worth it. It will be SO MUCH EASIER to build and traverse an AST if you do. You can implement the compiler in whatever language you like, but I’d recommend using a language with sum types and pattern matching 1, like OCaml, Haskell, or Rust. Preliminariesīefore you start, you need to decide on two things: what language to write your compiler in, and how to handle parsing and lexing. I’ve also written some test programs that you can use to validate that each stage of your compiler works correctly. I’ll cover arithmetic operations, conditionals, local variables, function calls, and perhaps more. This series is adapted from Ghuloum’s paper - the original paper is about compiling Scheme, so I had to make some adjustments to compile C instead. Every step is small enough to feel manageable, and at the end of the every step you have a working compiler. In step one, you just return constants in a later step you handle addition and subtraction and so on. Then you add new language features, one step at a time. I really like Ghuloum’s approach: you start by compiling a tiny, trivial subset of your source language all the way down to x86 assembly.

I’ve been working on my own C compiler, nqcc for the past several weeks, using Abdulaziz Ghuloum’s An Incremental Approach to Compiler Construction as a roadmap.

Fun examples of these are Whitespace, Lolcode, and Brainfuck. Toy language: This is when you make a programming language which doesn't fix an issue, but is for learning. When making a compiler, you need to decide which of 2 types of language the compiler will be. Constructing a compiler is not easy, so keep pushing it's worth the effort.
Compiler design tutorials code#
