Course Introduction to Programming
Compiler
In this class, we will learn the basic concepts about the Compiler, understanding what it is and its importance in program development.
We will also study the main phases of the compilation process and analyze the different types of errors that can occur during its execution.
What is a compiler?
A compiler, as the name suggests, is the program that synthesizes code written in a programming language into machine language. That is, after finishing the code, if it is correct according to the rules of the programming language, the compiler transforms the code into a program, an executable file.
Some examples of compilers for C and C++ are: GCC and Clang
PS: a compiler can also be a program that translates code from one programming language to another ๐.
Importance
The compiler is extremely important because it ensures not only that your program works, but also that it is fast and efficient. Without a compiler, we would have to write all our programs in a language of zeros and ones, which would be almost impossible. Thus they:
- Allow programs to run much faster, because the code is already "ready" for the computer.
- Ensure error detection before execution, helping the programmer to fix problems.
- Facilitate portability, transforming high-level code for different platforms.
Phases of compilation
To verify that the code is correct, the compiler does not read the entire code at once; the compilation process is generally divided into three parts:
- Lexical analysis: this phase is like reading words. The compiler divides your code into "tokens" (the smallest units with meaning) and checks if the code uses the language's symbols.
- Ex: in the sentence "I played ball," all the words are in English, so the sentence is lexicographically correct. If you write something like "I @ played ball," the compiler would already know that the @ symbol does not belong to our vocabulary and would point out an error.
- Syntactic analysis: the compiler organizes the words into sentences. It checks if the structure of your code follows the grammatical rules of the language. This is the phase that checks if parentheses are closed, if braces are in the right place, etc.
- Ex: The sentence "The boy ran to the house" is perfect. But if you say "The boy ran to the house and," the sentence is incomplete. The compiler would point out a syntax error because the grammatical structure was not finished.
- Semantic analysis: the compiler checks if the code has logical meaning. The sentence might be grammatically correct, but the idea behind it may not have meaning.
- Ex: The sentence "The stone ran quickly." is constructed with valid words and the grammar is correct. However, its meaning is illogical, as stones do not run. The compiler finds these problems when you try to perform an operation that is not allowed, such as adding a text to a number.
Error Cases
If an error is found in the code during any of the compilation phases, one of the compiler's main tasks is to return a detailed error report to the programmer, indicating the type of error, the line where it occurred, and, whenever possible, suggestions for correction. This allows the developer to identify and fix problems quickly, preventing simple errors from turning into more complex failures during program execution. In addition, modern compilers usually differentiate between serious errors and warnings, helping the programmer to prioritize corrections and write more efficient and safer code. In the image below, the compiler returns the type of error, the line it is on, and even how to fix it.

Figure 1 : Imagem de como o compilador mostra o erro
Conclusion
In this class, you learned a summary of how the compiler works, the phases of compilation, and how important it is for error correction. It is important to note that a real compiler is much more complicated, but a general notion of how it works already helps the programmer a lot.
Now you understand what a compiler is and why it is essential for transforming your code into functional programs more quickly, safely, and efficiently ๐
