What Does Compiler Mean?
A compiler is a software program that is responsible for changing initial programmed code into a more basic machine language closer to the “bare metal” of the hardware, and more readable by the computer itself. A high-level source code that is written by a developer in a high-level programming language gets translated into a lower-level object code by the compiler, to make the result “digestible” to the processor.
Formally, the output of the compilation is called object code or sometimes an object module. The object code is machine code that the processor can perform one instruction at a time.
Compilers are needed because of the way that a traditional processor executes object code. The processor uses logic gates to route signals on a circuit board, manipulating binary high and low signals to work the computer’s arithmetic logic unit. But that’s not how a human programmer builds the code: unlike this basic, binary machine language, the initial high-level code consists of variables, commands, functions, calls, methods and other assorted fixtures represented in a mixture of arithmetic and lexical syntax. All of that needs to be put into a form that the computer can understand in order to execute the program.
A compiler executes four major steps:
- Scanning: The scanner reads one character at a time from the source code and keeps track of which character is present in which line.
- Lexical Analysis: The compiler converts the sequence of characters that appear in the source code into a series of strings of characters (known as tokens), which are associated by a specific rule by a program called a lexical analyzer. A symbol table is used by the lexical analyzer to store the words in the source code that correspond to the token generated.
- Syntactic Analysis: In this step, syntax analysis is performed, which involves preprocessing to determine whether the tokens created during lexical analysis are in proper order as per their usage. The correct order of a set of keywords, which can yield a desired result, is called syntax. The compiler has to check the source code to ensure syntactic accuracy.
- Semantic Analysis: This step consists of several intermediate steps. First, the structure of tokens is checked, along with their order with respect to the grammar in a given language. The meaning of the token structure is interpreted by the parser and analyzer to finally generate an intermediate code, called object code.
The object code includes instructions that represent the processor action for a corresponding token when encountered in the program. Finally, the entire code is parsed and interpreted to check if any optimizations are possible. Once optimizations can be performed, the appropriate modified tokens are inserted in the object code to generate the final object code, which is saved inside a file.
Techopedia Explains Compiler
For an excellent example of what a compiler does, consider the comments encoded by programmers in amid the white space of a code base, delineated by tags that require the computer to “ignore” the alphanumeric text.
The reason that comments have to be flagged is that they are thoroughly unreadable by the processor. The processor can’t understand the words or signals, so it can’t produce anything from the comment code. But the same is largely true for parts of the code that are not comments: a function call like “get” means nothing to a computer. So the compiler takes the code and renders it in binary that drives the logical operations of the processor.
Although compiling is very useful in modern programming, it is not the only option available in software development. One of the best ways to explain a compiler in modern computing is to contrast it with a newer alternative method called interpretation, which uses a different type of software called an interpreter to put machine code together at runtime.
With the traditional compiler, the code is compiled once, before execution.
By contrast, an interpreter will put code together for every execution on demand.
That part of the explanation is clear. What becomes less clear is exactly how this happens, and which languages are compiled language and which are interpreted. For example, C++ is often held up as a prime example of a language that uses a compiler, although the appearance of CINT as a C++ interpreter makes that case a little more nuanced.
The appearance of the just-in-time compiler as a dynamic runtime compiling tool further muddies the waters in contrasting compiling and interpreting. Generally, a standalone program is called an interpreter, and compiling is the old-fashioned way of approaching turning high-level programming languages into machine code. Like much else in the computer science world, efficiencies and evolutions have spurred a sort of hybrid approach when it comes to using compilers and interpreters to translate a code base.