# Compiler
A program that translates source code written in a high-level programming language into a lower-level language (like machine code, assembly language, or bytecode) that a computer can execute.
**Pronunciation:** kuhm-PAI-luhr
**Difficulty:** Medium
**Synonyms:** Translator, Code Generator
**Categories:** Programming Languages, Software Development, Computer Science
**Tags:** Translation, Source Code, Machine Code, Bytecode, Assembly, Programming, Tooling
Canonical: https://scaleengineer.com/glossaries/compiler
---
## Definitions

- **Compiler in Software Development:** A **compiler** is a special computer program that acts as a **translator**, converting source code written in a high-level programming language (like C++, Java, or Rust) into a lower-level language. The output, often called object code or machine code, can be directly executed by the computer's central processing unit (CPU). Some compilers, like the Java compiler, translate source code into an intermediate language called bytecode, which is then executed by a virtual machine (like the JVM).

### Key Concepts
*   **Source Code:** The human-readable code written by a programmer.
*   **Target/Object Code:** The low-level code produced by the compiler.
*   **Compilation Process:** This typically involves several phases:
    1.  **Lexical Analysis (Scanning):** Breaks the source code into a stream of tokens (e.g., keywords, identifiers, operators).
    2.  **Syntax Analysis (Parsing):** Checks if the tokens form a valid sequence according to the language's grammar, often building an Abstract Syntax Tree (AST).
    3.  **Semantic Analysis:** Checks the code for semantic correctness (e.g., type checking, ensuring variables are declared).
    4.  **Intermediate Code Generation:** Creates a machine-independent representation of the code.
    5.  **Optimization:** Improves the intermediate code to make the final program faster or smaller.
    6.  **Code Generation:** Translates the optimized intermediate code into the final target language (e.g., machine code).

### Examples
*   `gcc` (GNU Compiler Collection) for C, C++, and other languages.
*   `javac` for Java, which compiles `.java` files into `.class` files containing bytecode.
*   `clang` for C, C++, and Objective-C.
*   `rustc` for the Rust programming language.

### Usage
Compilers are essential tools for developers. The process of turning source code into an executable program is called compilation. For example, to create a runnable program from a C++ file named `hello.cpp`, a developer would run a command like `g++ hello.cpp -o hello`.

## Etymology

From the Latin word "compilare," meaning "to plunder or put together." In the 15th century, it meant "to collect from various sources into a literary work." The computing sense emerged in the 1950s, referring to the process of "compiling" a program from various subroutines and instructions.

## First used

Early 1950s

## Historical context

Early computers were programmed directly in machine code or, later, assembly language, which was tedious and error-prone. The concept of a compiler was introduced to abstract away this complexity and increase programmer productivity.

*   **1952:** Grace Hopper is credited with developing the first compiler, A-0. It was more of a linker or loader, as it assembled a program from a library of subroutines, but it was a foundational step.
*   **1954-1957:** The first "complete" compiler is often considered to be the one for FORTRAN (Formula Translation), developed by a team led by John Backus at IBM. This compiler was a major breakthrough, proving that high-level languages could generate machine code efficient enough for practical scientific and engineering applications.
*   **Late 1950s - 1960s:** The success of FORTRAN spurred the development of other high-level languages and their compilers, such as COBOL, LISP, and ALGOL.
*   **1970s:** The development of compiler theory and tools like Lex (a lexical analyzer generator) and Yacc (Yet Another Compiler-Compiler) made it much easier to create new compilers, democratizing language creation.

Modern compilers are highly complex pieces of software that perform sophisticated optimizations to generate fast and efficient code. They are a cornerstone of modern software development.

## Q&A

- **What is the primary function of a compiler?:** A compiler's primary function is to translate source code from a high-level programming language into a lower-level language, such as machine code or bytecode, that a computer can execute.
- **What is the difference between a compiler and an interpreter?:** A compiler translates the entire source code into an executable file before execution. An interpreter, on the other hand, reads and executes the source code line by line without creating a separate executable file.
- **Name two common phases in the compilation process.:** Two common phases are Lexical Analysis (scanning the code into tokens) and Syntax Analysis (parsing the tokens to check for grammatical correctness).

## Usage examples

- The developer ran the C++ **compiler** to turn their source code into an executable file for Windows.
- A key difference between Java and C is that the Java **compiler** produces platform-independent bytecode, whereas a C **compiler** generates native machine code.
- Modern **compilers** are incredibly sophisticated, performing numerous optimizations to ensure the final program runs as efficiently as possible.
- Before the linker can do its job, the **translator** must first convert each source file into an object file.

## Related terms

- Interpreter
- Linker
- Debugger
- Transpiler
- Just-In-Time (JIT) Compilation
- Ahead-Of-Time (AOT) Compilation
- Abstract Syntax Tree (AST)

## Popular related terms

- Interpreter
- Linker
- Transpiler
