Engineering a compiler through examples: building a mathematical expression engine - Part 1

Starting today, we are launching an in-depth series on compilation, exploring how a program written in a high-level language is parsed and translated for machine comprehension. Our goal is to provide a comprehensive understanding of compiler engineering, and throughout this series, we will illustrate key concepts by developing a mathematical expression engine.

What are we aiming to achieve ?

Compilation is the art of translating a program written in a high-level language (such as C# or Java) into code that can be directly executed by a machine. The compilation process consists of several phases, which we will explore step by step. It also relies on numerous fascinating theoretical concepts, such as finite automata and context-free grammars.

Since these concepts can seem intimidating at first, we will illustrate them by building a mathematical expression engine. This engine will allow users to input expressions like $2x + 3y$ and perform operations such as differentiation with respect to $x$ for instance. A compiler must therefore recognize that $x$ and $y$ are variables and translate them into a structured representation that supports various computations (such as differentiation, numerical integration and so forth).
The examples will be implemented in C#, but they can easily be adapted to any programming language. By the end of our journey, we will have built a simple yet powerful mathematical expression engine.

The following textbooks prove useful on this topic.

Without further ado and as usual, let's begin with a few prerequisites to correctly understand the underlying concepts.

Engineering a compiler through examples: building a mathematical expression engine - Part 2