Compiler
A compiler is a program that translates source code written in a high-level programming language into a lower-level form, such as machine code or bytecode, that a computer or virtual machine can execute.
Definition
A compiler is a program that translates source code written in a high-level programming language into a lower-level form, such as machine code or bytecode, that a computer or virtual machine can execute.
Overview
Compilers sit between the code a programmer writes and the instructions a machine actually runs. A traditional compiler, such as those used for C or C++, translates source code ahead of time into native machine code specific to a target processor architecture, producing a standalone executable that runs without the compiler present. Compilation typically proceeds through several stages: lexical analysis (breaking source text into tokens), parsing (building a syntax tree that represents the program's structure), semantic analysis (checking types and meaning), optimization (improving performance without changing behavior), and code generation (producing the final output). Some languages, like Java, compile to an intermediate bytecode format executed by a virtual machine rather than directly to native machine code, trading some raw performance for portability across platforms. Not all languages are compiled ahead of time — interpreted languages like early JavaScript or Python execute source code directly (or increasingly, via just-in-time compilation that compiles code as it runs). The line between compiled and interpreted has blurred over the decades, with most modern language runtimes using some hybrid of interpretation and just-in-time compilation to balance startup speed with execution performance.
Key Concepts
- Translates high-level source code into machine code or bytecode
- Processes code through lexing, parsing, and semantic analysis stages
- Performs optimizations to improve performance without changing behavior
- Produces standalone executables for ahead-of-time compiled languages
- Distinct from interpreters, which execute source code directly
- Just-in-time (JIT) compilers blend compilation and interpretation
- Reports syntax and type errors before a program ever runs