Compiler
- In Turkish
- Derleyici
In short
A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
What is a compiler?
A compiler converts the human-readable code you write into instructions a computer can execute. For languages like C, C++, Go, and Rust, the compiler produces machine code for a specific processor, packaged as an executable file. Other compilers, such as those for Java and C#, produce an intermediate format called bytecode, which a virtual machine then runs.
Compilation happens in stages. The compiler first reads the source code and breaks it into tokens, then parses those tokens into a tree that represents the program's structure, checks it for errors such as type mismatches, optimizes it, and finally generates the output code. Because all of this happens before the program runs, many mistakes are caught at compile time, long before a user sees them.
A compiler is like a translator who converts an entire book into another language before it is published, so readers get a finished translation. An interpreter, by contrast, is like a live interpreter who translates a speech sentence by sentence as it is being given. Languages like Python and JavaScript are traditionally interpreted, although modern engines such as V8 use just-in-time (JIT) compilation to turn frequently run code into machine code while the program is running.
The line between compilers and interpreters is blurry in practice, since many languages use both. A related term is transpiler, a compiler that translates between languages at a similar level, such as the TypeScript compiler (tsc), which turns TypeScript into JavaScript. Compile-time errors are also different from runtime errors: the first stop the program from building, while the second happen while it runs.
At a glance
Key takeaways
- A compiler translates source code into machine code, bytecode, or another language.
- It works in stages: tokenizing, parsing, checking, optimizing, and generating code.
- Compile-time errors are caught before the program ever runs.
- An interpreter runs code directly instead of translating it all ahead of time.
- A transpiler, like the TypeScript compiler, converts code between similar high-level languages.
Example
# Compile a C program into a native executable, then run it
gcc hello.c -o hello
./hello
# Compile Java source into bytecode for the Java Virtual Machine
javac Hello.java
java Hello
# Transpile TypeScript into JavaScript, then run it with Node.js
npx tsc app.ts
node app.jsReaders ask
What is the difference between a compiler and an interpreter?
A compiler translates the whole program ahead of time into another form, usually machine code, that runs later. An interpreter reads and executes the source code directly while the program runs; many modern languages combine both approaches.
Is JavaScript compiled or interpreted?
Both. JavaScript is often described as interpreted, but modern engines like V8, used in Chrome and Node.js, start with an interpreter and then compile frequently used code into optimized machine code on the fly, which is called just-in-time compilation.
What is a transpiler?
A transpiler, or source-to-source compiler, translates code from one high-level language to another, such as TypeScript to JavaScript. Tools like Babel also use transpiling to convert modern JavaScript into older syntax for compatibility.
Often compared
- Compiler vs InterpreterA compiler translates a whole program before it runs, while an interpreter executes source code step by step at runtime, trading raw speed for faster feedback.
- Compiler vs TranspilerA compiler turns code into a lower-level form a machine or VM runs, while a transpiler outputs another high-level language, like TypeScript to JavaScript.
See also
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- ExceptionProgramming Fundamentals, p. 19An exception is an event that interrupts a program's normal flow when an error occurs, which code can catch and handle instead of letting the program crash.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- WebAssemblyWeb Development, p. 59WebAssembly is a compact binary code format that runs in web browsers at near-native speed, letting languages like C++, Rust, and Go power fast web code.
- JIT CompilationProgramming Fundamentals, p. 32JIT compilation turns the busiest parts of a program into machine code while it runs, combining an interpreter's quick start with compiled speed.
- Programming LanguageProgramming Languages, p. 25A programming language is a formal language with exact rules for writing instructions a computer can carry out, such as Python, JavaScript, Java or C.
Spotted a mistake or something missing on this page?Suggest an edit