r/computerscience • u/ShadowGuyinRealLife • Sep 10 '26
General Is the Choice Between an Interpreter or Compiler Defined By the Language?
Compilers take the human readable source code and turn it into a set of instructions the processor can understand. For example, C source code goes through a C compiler which then gives a binary that can be read by computers. This needs to be done for specific hardware. For example, a x86 processor can't read instructions for the RISC-V.
Languages like Java are different. The source code is turned into a bytecode. This byte code is the same no matter the target system. On the target system, the executable is an interpreter. So an interpreter can read the bytecode and then use this to determine what calculations the program wants to do.
I was thinking programing languages are about logic. They define behavior. So is it possible in principle to make a compiler that can turn Java or CLISP source code into a binary that can run on the target system? There wouldn't be a reason to do so, but in principle could it be done?
20
u/jmtd CS BSc 2001-04, PhD 2017- Sep 10 '26
The Java runtime is both an interpreter and compiler. Just muddying the waters
67
u/SV-97 Sep 10 '26
It's not defined by the language. There are interpreters for C and similar languages, compilers for Lisps and Java, and also languages that technically are "interpreted" but compile the "interpreter" and program into a single native executable instead of having a dedicated program (indeed C, in the way that most people use and understand it, still includes a runtime system. As another example there's Haskell's GHC. This latter one is also interesting because if you look at what it does it's *far* from what you might expect from an interpreter). And of course JITs also blur the line somewhat.
FWIW: I think Tannenbaum wrote in his computer architecture book that "it's interpreters all the way down" because what is your OS + loader if not an interpreter for "native code", and what's your "machine" if not an hardware-implemented interpreter of the language defined by your ISA ;)
19
u/braaaaaaainworms Sep 10 '26
Interpreter vs compiler is an implementation detail. You can interpret a language considered to be compiled and you can compile a language considered to be interpreted, because there's nothing inherent about some language that makes it impossible to write an implementation that can compile python and interpret c
3
u/PhilNEvo Sep 10 '26
You can make a compiler or interpreter for everything and anything. You could make a C or heck, even assembly interpreter. You can also make a python or scratch compiler. A lot of modern developed "compilers" aren't even truly "compilers" in the way you describe it. A lot of them are just new "frontend" parts of the compiler that converts from a language you have defined to an intermediate representation(IR), that then gets fed into LLVM which converts it to a binary.
1
u/MilkEnvironmental106 Sep 10 '26
C# is an example of a language that can both be interpreted and compiled. Rust is also primarily compiled, but Miri is a rust interpreter used to identify undefined behaviour. So yes, and there are very valid reasons for doing so.
1
u/gwenbeth Sep 10 '26
Some languages do work better as interpreters. Especially if you can turn data into code. A good example is tcl (which is a language i have done some weird stuff in). I could totally generate new code on the fly and redefine a function while the program is running. When you have an interpreted language its easy to look into a running program and make changes on the fly. This is something I could do with ruby on rails. With just in time compilers you can get a lot of the benefits of both a compiler and interpreter but at the cost of a big environment. Every language that can be compiled can be easily interpreted, but not always easily the other way around. The reason we don't usually see things like c interpreters is that the things that c is really good for is usually the same things that compiled code is good for: deploying to embedded machines with low resources, places where speed is important, code that we need to stay static, code that we need to keep from being read, etc.
1
u/Interesting_Buy_3969 Sep 10 '26
Well, a language technically can define whether its official toolchain should be interpreter or compiler (or sometimes both!), but it doesn't have to because a language is roughly just a set of rules and not a tool to execute the code written in it.
That is, if a language has a compiler, it still is allowed to have an interpreter, and vice versa, but it remains virtually the same language with the same syntax.
Having said that, a language designer can adjust his/her choices so that a language is naturally more compileable or interpretable (not sure what's the right English word here but you get it). For example, as someone in this thread said, typing system influences this heavily.
1
u/drgrd Sep 10 '26
Each computer platform has exactly one machine language. Whether you are using an interpreted language or a compiled language, an x86 machine requires x86 code and cannot execute anything else. similarly for Risc-V, MIPS, ARM, or whatever.
Compilers produce machine code all at once, beforehand; Interpreters produce machine code on demand, in real time.
Compilers can produce more efficient machine code because they see the whole picture, but compiling takes time, and code must be re-compiled for different machines. Compiled languages often have machine-specific quirks that means the codebase may need to be rewritten for different machines.
Interpreters do not require a compile step, so the development process is more efficient, but the resulting code may be slower. The same codebase can be interpreted onto different machines as long as there is a translation engine for that machine.
bytecode is halfway between compilers and interpreters. when you move from a high level language to bytecode, you are compiling, and because the compiler sees the whole codebase, efficiencies can be made. the resulting bytecode still needs to be translated to machine code in real time as it is executed, but bytecode is closer to machine code, so the translation is easier and more efficient; and because the bytecode is universal, one codebase can be used for multiple machines.
it's a tradeoff: it adds complexity, makes less efficient code, and ignores machine-specific optimizations; but it means you can avoid maintaining multiple codebases for multiple machines.
1
u/JGhostThing Sep 10 '26
The difference doesn't lie in the languages. There has been a Java compiler (that compiled to object files, like a C compiler). There have been interpreters for C. There was even a compiler for APL.
0
u/wolfkeeper Sep 10 '26
All compiled languages can be interpreted (with some precompilation) but not all interpreted languages can be precompiled. However, some interpreters do on the fly compilation and recompilation.
0
u/jonathancast Sep 10 '26
A programming language, in practice, is one component of an ecosystem. That ecosystem is why people choose a language, and it's biased towards either a compiled or interpreted approach to implementing the language.
Sometimes the language design affects things, too. C is designed to be easily compiled to very fast assembly language code, but it doesn't have very good programmer ergonomics by modern standards. So most people who pick it want a fast language, which means compiled.
On the other hand, the core Java language would compile really well, but the ecosystem around it relies heavily on dynamic loading and even dynamic bytecode generation, so a compiler that wanted to support all of it would have to provide those features at runtime, too. Which is why the official Java implementation has been based on precompiling to bytecode and JITting to runtime for so long.
Still higher level languages like Python wouldn't compile very well. Every function name in Python is an assignable variable, so every function call has to use an indirect function call instruction. Even worse, Python has callable objects, so, without a JIT, at least, a compiler would have to insert a dynamic typecheck in front of every function call. And you could forget about most optimizations, like inlining functions. The compiler simply doesn't have enough static information to do a good job. There are Python compilers, but they don't support the whole language because there wouldn't be any point.
0
u/realdreamer1993 Sep 11 '26 edited Sep 11 '26
So is it possible in principle to make a compiler that can turn Java or CLISP source code into a binary that can run on the target system?
Yes it will be possibe. but to accomplised that by yourself maybe will need 200 years of work ? I mean the compiler must be comprehensive and accepted by community...
I think it wont need new invention, maybe need lot of research and massive amount of work..
-1
u/WA_von_Linchtenberg Sep 13 '26
MSc in advanced Computing (CS + CE) here.
In my humble POV, comparing a compiler and an interpreter as monolithic tools is the wrong level of granularity.
A compiler is not a single program doing a single job, and neither is an interpreter. Both are pipelines made of distinct functional modules. Some of these modules are shared between the two architectures, and some are specific to one or the other.
The meaningful comparison happens at the module level, not at the tool level.
here's a "no too false" architecture.
Compilation pipeline (functional modules) and comparaison
A typical compilation toolchain such as GCC or Clang processes source code through a sequence of stages :
A. pre-processing
The lexer, the parser, and the abstract syntax tree construction are common to both pipelines. Both architectures must read source text and build a structured representation of the program before anything else can happen.
B. processing
3/4 of this steps are specific of compilation toolchain. Their purpose is optimising code for efficiency of execution on ONE & ONLY ONE hardware. You don't care the time it takes to produce the optimized code. At the opposite on an interpreted language, you want a "quick" execution (decoding in fact) so you don't want a threshold "time of optimizing"/"time of decoding". That why for calculus oriented module Python wrapped C compiled code but not for scripting.
Back to our modules.
4. A semantic analyzer walks this tree to check type consistency, scope rules, and other constraints that the grammar alone cannot express.
5. A "front end" step then lowers the tree into an intermediate representation, which is a platform-independent encoding of the program logic. This module is common and globally the same cause no optimisation at this step.
6. A "middle end" applies optimization passes on this representation: dead code elimination, constant propagation, loop unrolling, and similar transformations.
7. The back end maps the optimized intermediate representation onto the instruction set of the target processor, allocates registers, and emits assembly text.
C. post-processing (after the compiler but in the toolchain)
Hardware related so Compilation toolchain only steps. These exist because the compiler must produce a self-contained binary that runs without further translation.
8. An assembler converts this text into relocatable object files containing machine code.
9. Finally, a linker merges multiple object files, resolves cross-references between them, and produces the executable binary.
Interpretation pipeline (functional modules)
Here're the divergences :
* At processing time, a bytecode compiler translates the tree into a compact instruction sequence designed for a stack-based virtual machine. But the optimizing is split between compiling and runtime. This bytecode is not native machine code; it is an internal format that the virtual machine can decode quickly. As saif quickly is the key : this induce the internal differences between the modules.
At runtime,
* At post processing, we have instead a "run time" (runtime) . A module loader reads bytecode files from disk or triggers compilation on the fly, and feeds the instructions to an evaluation loop. The evaluation loop is the core of the virtual machine: it fetches one bytecode instruction at a time, decodes it, and dispatches the corresponding operation. All of this runs on top of a runtime object system that manages the creation, mutation, and destruction of heap-allocated objects, and a memory manager that tracks reference counts and runs a garbage collector to reclaim unreachable memory.
So, the modules specific to the interpretation pipeline are the bytecode compiler, the evaluation loop, the runtime object system, and optionals elements like a garbage collector. These exist because the interpreter defers most decisions to runtime and must manage a live environment of mutable objects while the program executes.
So, your question !
Wikipedia's page about compiling has a good drawing about this compilation toolchain : https://en.wikipedia.org/wiki/Compiler#/media/File:Compiler.svg . you can see what is language-driven (common steps for both compiling and interpreting basically) then the hardware-driven steps with the second graph in the center. Wikipedia i a good source for understanding this processes if you need more.
Being "compiled" or "interpreted" is not fundamentally an intrinsic property of a programming language. It is primarily a property of a particular implementation, execution platform, and toolchain for that language.
The same language can have several execution models: an interpreter, an ahead-of-time compiler, a bytecode VM, a JIT compiler, or combinations of these approaches.
Consequently, saying "Python is interpreted" or "C is compiled" is useful as a broad pedagogical shorthand, but it is technically incomplete. What is actually being described is a particular language implementation and its associated execution toolchain.
Note too that you have a "bridge" between the two options : a "Just-In-Time compiler" (JIT). A Just-In-Time (JIT) compiler is a compiler that performs compilation during program execution, rather than entirely before it. A JIT-based runtime can initially interpret bytecode and monitor the program while it runs, then identify frequently executed ("hot") code paths and compile and optimize them into native machine code. This allows it to combine some of the portability and fast startup of interpretation with some of the execution performance of native compilation.
It also makes the distinction between "compiled" and "interpreted" even less absolute: a single implementation can interpret some code and compile other parts at runtime, depending on actual execution behavior.
Hope this help.
Glossary (Wikipedia references given by AI and validated manually)
-4
u/VibrantGypsyDildo Sep 11 '26
If you write in C or C++, it is compiled.
The languages that we consider interpreted often invent some form of JIT (just-in-time) compilation. Java is probably almost as good as C/C++. I saw news about removing GIL (global interpreter lock) in future versions Python.
You are 10-20 late with this question (it does not make your question not valid though). Just choose whatever language you like, it is the privilege you have now.