This document explains how to construct a compiler using lex and yacc, the tools used to generate lexical analyzers and parsers. It is assumed that you can program in C, and understand data structures such as linked-lists and trees.

The overview describes the basic building blocks of a compiler and explains the interaction between lex and yacc. The next two sections describe lex and yacc in more detail. With this background, you will construct a sophisticated calculator. You will also implement conventional arithmetic operations and control statements, such as if-else and while. And with minor changes, you will convert the calculator into a compiler for a stack-based machine. The remaining sections discuss issues that commonly arise in compiler writing.