

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.
#Strip diagram subtraction 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.

Here are some reasons to write a compiler: This is the first post in a series on writing your own C compiler.
