In modern society, computers are one of the most fundamental and fascinating parts of our world, helping us humans with just about everything. Whether you’re placing an online order or creating your own app, this technology has become a necessity of today. But how do we communicate with computers, or in other words, make them execute what we desire? How do computers understand and run the programs that we write? What is the bridge that connects humans and computers?
Compilers! Though so often used, most do not know the inner workings of these elusive engineering masterpieces.
There is a multi-step process (Fig 1) that converts the code that we write, known as the source code, into computer-readable code known as machine code. For this to be done, the compiler must generate a structural model: the blueprints for individual components of our source code. Our source code must abide by the syntactical rules of the programming language we’re coding in; this basically means that our code must fit a certain format and be written a certain way so that the compiler can analyze it correctly. So long as we abide by these rules, the process can then begin.
First, our program goes through lexical analysis, a process that transforms source code into smaller pieces of syntax called tokens. This step is first because of its sheer importance: by breaking down our source code into smaller chunks and components, we are closer to generating a structural model of our code—allowing us to understand the components of our program on a deeper level.
Tokens, like parentheses, numbers, and program function names, are like puzzle pieces of our source code. The compiler then takes these tokens to mix and match them. For example, the three tokens ‘input’, ‘(‘, and ‘)’ can be pieced into the function ‘input()’, which is a function for getting the user’s input! In addition, by going through the tokens and categorizing and combining them into larger structural pieces, the compiler can inspect whether a token abides by the syntax of our code, checking for any errors. Then, after this step, the compiler combines these components into the main representation of our source program.
This amazing structure (also known as the Abstract Syntax Tree to us computer science nerds) represents all the expressions, statements, and parts of our code in a hierarchical format, basically allowing us to see all of the different parts of our code in an organized way. The compiler, having produced a final representation of our original, human-made code, can go and convert it to machine code, or code that the computer can understand and execute. Now, this is no easy task, but after it is done, you’ve successfully compiled and converted your code, fully representing it in a manner that can be executed and run cleanly.
Compilers—magnificent masterpieces of engineering that enable humanity to talk with computers—are the fundamental technology that transforms our creative visions into executable code. From web development to game development, from a microwave to a supercomputer, compilers are used everywhere!
Through compilers, we are able to make computer programs that can be executed successfully; without them, software engineering as a whole might not exist. So next time you place an order on Amazon and eagerly await the delivery, think about how compilers played a crucial role in transforming your clicks into a seamless online shopping experience—just like they do for countless applications you use every day.