Interpreter
- In Turkish
- Yorumlayıcı
In short
An interpreter is a program that runs source code directly, step by step, instead of first translating the whole program into a separate executable file.
What is an interpreter?
An interpreter executes a program by reading its code and carrying out each instruction as it goes, rather than producing a standalone executable file first. You hand it a script, and it starts running right away. Python, Ruby, PHP, JavaScript and shell languages like Bash are usually run by interpreters, which is why they are often called interpreted languages.
Very few modern interpreters run raw source text line by line. Most first parse the code into a tree, and many then compile it into bytecode, a compact set of instructions for a virtual machine, which the interpreter's main loop executes. CPython works this way, and JavaScript engines like V8 go further with just-in-time (JIT) compilation, turning frequently run code into native machine code while the program runs. Many interpreters also offer a REPL (read-eval-print loop), an interactive prompt where you type one line and see the result immediately.
An interpreter is like a live interpreter at a conference who translates a speech sentence by sentence as it is given, while a compiler is like a translator who converts a whole book before it is published. Interpretation makes the edit-and-run cycle fast and lets the same script run on any machine that has the interpreter installed, at the cost of slower execution and errors that only appear when the faulty line actually runs.
Interpreter vs compiler describes an implementation, not a language. The same language can be handled both ways: Java is compiled to bytecode that the JVM then interprets and JIT-compiles, and Python has both the CPython interpreter and ahead-of-time compilers. So "interpreted language" is shorthand for how a language is usually run, not a strict rule.
Key takeaways
- An interpreter runs code directly, without producing a separate executable first.
- Most interpreters parse code and run bytecode on a virtual machine rather than raw text.
- JIT compilation inside an interpreter turns hot code into fast machine code at runtime.
- Interpreted code is quick to change and test but usually runs slower than compiled code.
- Being compiled or interpreted describes an implementation, not the language itself.
Example
# Read each instruction and carry it out immediately
program = ["push 2", "push 3", "add", "print"]
stack = []
for line in program:
op, *args = line.split()
if op == "push":
stack.append(int(args[0]))
elif op == "add":
stack.append(stack.pop() + stack.pop())
elif op == "print":
print(stack[-1]) # 5Readers ask
What is the difference between an interpreter and a compiler?
A compiler translates the whole program ahead of time into another form, such as machine code, and you run the result later. An interpreter executes the program directly, translating as it goes, so there is no separate build step.
Is Python interpreted?
The standard implementation, CPython, compiles source code to bytecode and then interprets that bytecode in a virtual machine. From a developer's point of view it behaves like an interpreted language, because you run the .py file directly.
What is a REPL?
A REPL, short for read-eval-print loop, is an interactive prompt provided by many interpreters. It reads one expression, evaluates it, prints the result and waits for the next, which makes it handy for experimenting.
Often compared
See also
- CompilerProgramming Fundamentals, p. 11A 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.
- PythonProgramming Languages, p. 26Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
- 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.
- TranspilerWeb Development, p. 48A transpiler is a tool that translates source code from one language or language version into another at a similar level, such as TypeScript into JavaScript.
- ShellOperating Systems, p. 27A shell is a program that reads commands typed by a user or written in a script and asks the operating system to run them, usually through a text interface.
- RubyProgramming Languages, p. 28Ruby is a dynamic, object-oriented programming language designed for developer happiness, best known for readable syntax and the Ruby on Rails web framework.
- 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