Menu
×
Tutorials Ms Word Tutorial Ms Excel Tutorial Ms PowerPoint Tutorial C Language Tutorial C++ Tutorial C Sharp Tutorial Visual Basic Tutorial HTML Tutorial CSS Tutorial JavaScript Tutorial WordPress Tutorial
     ❯   

ADVERTISEMENT

What is a compiler?

A compiler is a special kind of program that changes source code from one programming language to another, either machine code or bytecode. Most of the time, the source code is written in a high-level language that humans can understand, like C++ or Java. The source code is written by a programmer in a code editor or an IDE with an editor. The source code is then saved to one or more text files. The files are read by a compiler that supports the source programming language. The code is then analyzed and changed into a format that can be used on the target platform.

Compilers and Object Code: Bridging Source Code to Machine Code for Targeted Operating Systems and Architectures

Certain operating systems and computer architectures are targeted by compilers that turn source code into machine code. Object code is another name for this kind of output, which has nothing to do with object-oriented programming. Because the machine code that is sent out is made up of only binary bits (1s and 0s), the processors on the target computers can read it and run it. A compiler might, for instance, make machine code for the Linux x64 platform or the Linux ARM 64-bit platform.

Compiling to Bytecode: A Versatile Approach for Cross-Platform Execution with Java and Just-In-Time Compilation

There are some compilers that can change source code to bytecode instead of machine code. Bytecode is a middle language that was first used in the programming language Java. It can be run on any system that has a Java virtual machine (JVM) or bytecode interpreter. The JVM or interpreter takes the bytecode and turns it into instructions that the hardware processor can follow. Besides running the code, a JVM also lets a just-in-time compiler recompile the bytes.

More than just machine code or bytecode, some compilers can turn source code into another high-level programming language. What is this kind of compiler also known as? A transpiler, a transcompiler, a source-to-source translator, or something else. A developer might use a transpiler to change COBOL to Java, for instance.

A compiler has to make sure that the logic of the output code always matches the logic of the source code, no matter what type of output or source language is used. They also have to make sure that nothing is lost when the code is converted. In the strictest sense, a compiler is a translator who has to make sure the output is correct and keeps all the original logic.

How does a compiler work?

There are different ways that compilers look at source code and turn it into output code. Even though they are different, these steps are usually what they do:

  • Lexical analysis. The compiler separates the source code into lexemes, which are short pieces of code that show specific patterns in the code. Then, the lexemes are turned into tokens so that they can be analyzed for syntax and meaning.
  • Syntax analysis. Based on the rules for the source language, the compiler checks that the syntax of the code is correct. This process is also known as parsing. In this step, the compiler usually makes abstract syntax trees that show how certain pieces of code are structured logically.
  • Semantic analysis. The compiler checks that the logic in the code is correct. This step is more than just looking at the syntax; it also checks that the code is correct. As an illustration, the semantic analysis could check to see if variables have been given the correct types or have been declared correctly.
  • IR code generation. When the code goes through all three stages of analysis, the compiler makes an intermediate representation (IR) of it. It’s easier to change the source code into a different format when there is IR code. However, it must be an exact copy of the source code in every way, leaving out no features.
  • Optimization. Before making the final code, the compiler makes the IR code run faster and better. What kind of optimization is done and how much is done depends on the compiler. Some compilers let users set how much optimization they want to use.
  • Output code generation. Using the optimized IR code, the compiler makes the final output code.
Spread the love
Scroll to Top
Scroll to Top