Compiler

0
(0)

A compiler is a program that translates text written in a programming language into machine code. Compilers enable computers to understand different programming languages, including high-level ones, which are closer to human understanding and far removed from the hardware.

The process of a compiler working with code is called compilation. Essentially, a compiler is a complex “translator” that assembles, or compiles, a program into an executable file. An executable file is a set of instructions for the computer that it understands and can execute.

Programming languages ​​that are translated using compilers are called compiled languages.

What is a compiler for?

Computers inherently don’t understand the meaning of anything written in any programming language. Computer language is machine code—zeros and ones—that encode information and commands. Writing programs in machine code is virtually impossible: even the simplest operation would take many hours of work for the programmer. Therefore, programming languages ​​more understandable to humans and specialized programs that translate these languages ​​into machine code emerged. These programs are called compilers.

Without a compiler, any code in a compiled programming language would be just text to the computer—it wouldn’t recognize the commands and wouldn’t be able to execute them. Therefore, a compiler is necessary for programs to run. Without one, nothing would work.

Another task of the compiler is to assemble all modules, such as linked libraries, into a single file. The executable file must contain everything necessary for the program to function properly and fully execute instructions.

Compiler and Interpreter: What’s the Difference?

Compilation isn’t the only approach to “translating” a human-readable programming language into machine language. There are also interpreters and bytecode, but the technologies involved are entirely different.

An interpreter is also a program that “translates” text in a high-level programming language, but it does so differently. Rather than compiling all the code into a single executable file for later execution, it executes the code line by line. This is slightly slower, but sometimes more convenient. Languages ​​that use interpreters are called interpreted.

Bytecode is an “intermediate link” between the compilation and interpretation approaches. A program is converted into a specific code that runs on a dedicated virtual machine. There are relatively few languages ​​that operate this way; the most famous and prominent example is Java.

What languages ​​use compilers?

Popular compiled languages ​​today include  Swift and  Go, as well as C/C++ and Objective-C. Other examples include Visual Basic, Haskell, Pascal/Delphi, Rust, as well as Lisp, Prolog, and other lesser-known languages. Of course, assembly language—a very low-level language written directly in machine code—is also compiled.

Separately, we can highlight languages ​​that are transformed into bytecode—this is also a type of compilation. These include Java, Scala, and  Kotlin, as well as  C#  and the languages ​​of the .NET platform.

What languages ​​are compilers written in?

Other languages.  Writing a compiler in machine code is difficult and time-consuming, sometimes practically impossible. Therefore, they are written in existing languages: it turns out that most languages ​​are written in other languages.

For example, one of the Go language compilers is partially written in C++, the very first C++ compiler was written in assembler, and the assembler itself was written in machine code.

Same language.  Writing a compiler for a programming language using different versions of the same language is allowed and is actively used in development. This is necessary so that compilers are more flexible and “smart” and can support more features, as assembly is quite primitive and doesn’t solve all problems.

It looks like this:

  • The first, simpler compiler is written in assembler;
  • The second one is written in the required language and compiled by the first compiler;
  • The second compiler, translated into machine code, compiles its own source code – the result is a newer and more powerful version of the same one.
365-768

For example, most modern compilers for C/C++ are written in C/C++. Such compilers are called self-compiling.

Why can one language have multiple compilers?

Most programming languages ​​have multiple compilers. These are also called implementations. The initial implementation is written by the language’s creator, and alternatives are developed over time. Why is this done? The goals can vary:

  • write a more modern and functional compiler, update the language;
  • optimize the language and make it more efficient;
  • create a free implementation that the community can build on;
  • fix bugs that exist in existing implementations;
  • transfer the language to another platform, and so on.

Each development team has its own approaches to optimization, porting, and other goals. Therefore, different compilers for the same language may differ in speed, architectural features, purpose, and other parameters. The language syntax remains the same, but there are special situations where the same line of code may execute differently depending on the compiler.

What types of compilers are there?

Compilers come in a wide variety. Some are highly specialized, for example, running only on a specific processor family and optimized for it. Others are more general—so-called cross-compilers—that can support multiple operating systems.

A single compiler can “know” several programming languages. A prime example of this is GCC, or the GNU Compiler Collection, a cross-compiler for multiple operating systems and languages, completely free and open source. GNU software is written in it.

There are also so-called compiler compilers. They generate compilers for a language based on its formal description.

How compilers work

In simple terms, they “read” the input program and translate its commands into corresponding sets of machine code. The details are more complex and vary depending on the implementation. For example, there are modular, flexible compilers written in high-level languages, debug compilers capable of eliminating some syntax errors, and so on.

The compilation itself can be:

  • line-by-line – each line is translated into machine code one by one, which is similar to interpretation, but technically different;
  • batch – the code is broken down into blocks, or batches, and compiled block by block;
  • conditional – the compilation features depend on the conditions that are specified in the source code of the program being compiled.

First, the compiler parses what’s written, then analyzes the commands, and then generates machine code. It doesn’t run the program; running it is a separate action.

365-899

Advantages of compiled languages

  • Compiled languages ​​are generally faster than interpreted languages ​​and easier to optimize.
  • The final code size of compiled languages ​​is generally smaller than that of interpreted languages.
  • Compiled languages ​​offer much greater control over hardware resources. This doesn’t mean they’re all low-level, but the opposite is true: almost all low-level languages ​​are compiled.
  • As processors become more powerful, compiled languages ​​are the ones that can take full advantage of their capabilities.
  • The code produced by the compiler is better optimized for specific devices and hardware architectures, uses memory efficiently, and does not waste unnecessary resources.
  • Compiled languages ​​are usually powerful and highly performant, which is why games and other heavily loaded applications are written in them.

Disadvantages of compiled languages

  • Unlike interpreted languages, compiled languages ​​do not execute code immediately—it must first be compiled, which is an extra step and takes extra time.
  • The code is more difficult to debug: it has to be recompiled for every change, even minor ones. The process of finding and fixing errors can be quite non-obvious.
  • Machine code is tightly coupled to the platform architecture and varies across systems. Therefore, compiled languages ​​are not cross-platform by default. Porting a language to another operating system requires writing a new compiler. There are exceptions, however, in the form of universal cross-compilers that work across multiple platforms, but these aren’t suitable for all applications.
  • Another problem for beginners is that compiled languages ​​are often more complex than interpreted ones. Learning them from scratch can be difficult, although there are exceptions.

How to use the compiler

A novice developer rarely interacts directly with a compiler. They download a programming language, including its compiler, and then work in a code editor or IDE. The development environment automatically launches the compiler every time the user clicks the build or run button. There’s no need to invoke it manually. Sometimes, the environment may include multiple compilers and select the appropriate one for each situation.

Therefore, there’s no point in touching the compiler at the early stages—it’s just worth remembering that it’s there to better understand what’s going on. However, it can be useful if you want to compile something without a development environment, for example, directly from the command line. In this case, you’ll have to invoke it with a special command—it’s specific to each solution.

Every software has documentation, so if you want to know more about the compiler you are using, you can read it.

Learn more about the structure and operation of programming languages ​​in our courses – gain a new profession and become a sought-after IT specialist.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email
CONTINUE LEARNING Next: Program code →

Leave a Reply

Your email address will not be published. Required fields are marked *

heavy equipment transport fort bend tx. Heavy equipment transport ramsey mn.