Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How compiler and interpreter both are used in one language?

I have read enough explanation about the definition of compiler, interpreter and "things" that use both. I didn't find how compiler and interpreter both are used in one language.

like image 550
Shahzaib Mazari Avatar asked Mar 02 '19 06:03

Shahzaib Mazari


People also ask

Which language use both interpreter and compiler?

Java can be considered both a compiled and an interpreted language because its source code is first compiled into a binary byte-code. This byte-code runs on the Java Virtual Machine (JVM), which is usually a software-based interpreter.

Can a languages be both compiled and interpreted?

Most programming languages can have both compiled and interpreted implementations – the language itself is not necessarily compiled or interpreted. However, for simplicity's sake, they're typically referred to as such.

Can a programming language have both compilers and interpreters available for it?

A high-level programming language is usually referred to as “compiled language” or “interpreted language.” However, in practice, they can have both compiled and interpreted implementations.

What is the similarity between interpreter and compiler?

Both compiler and interpreters do the same job which is converting higher level programming language to machine code. However, a compiler will convert the code into machine code (create an exe) before program run. Interpreters convert code into machine code when the program is run.


2 Answers

In Java the source code is first compiled to bytecode and then it's run by an interpreter (JVM - Java Virtual Machine).

bytecode is machine code for a virtual machine.

In Javascript there's a runtime (engine) that does just in time compilation (JIT). Basically, at execution time it's given a source code which it immediately converts to native code and then the code is executed. In Chrome's engine there are two modules that do compilation: one can execute code fast but the code isn't much optimized (ignition interpreter) and the other produces a highly performant code but compilation takes more time (turbofan compiler).

Why use both:

  • portability - when you use intermediate representation that is compiled AOT you can take this bytecode and run it on any architecture for which a Virtual Machine is provided. You can push the same Java bytecode to clients on Mac, PC or Linux. If they have JVM installed the code will run. For C or C++ you have to ship different program executable for each architecture
  • fast initial start and decent execution performance - compilation takes time (and the more optimized code the more time it needs for compilation generally) but nobody likes to wait. It's better to produce something that is not perfect (ignite phase) and then gradually improve the code by compiling hot paths into highly optimized machine code (turbofan phase). This is especially plausible today where we have CPUs with many cores but we are not able utilize them all because creating programs with many parallel threads is hard (so one core can execute program while the other can optimize code in the meantime)
like image 70
marzelin Avatar answered Nov 15 '22 07:11

marzelin



Java is first machine independent programming language; it uses both compiler and interpreter. Java compilers are designed in such a way that converts source code into platform independent form i-e byte codes. These byte codes are then converted to machine code by interpreter. This is how compiler and interpreter both used in one language. Any system having JVM will run these byte codes.

Java program  byte code  interpreted by VM  machine language

summary :

java compiler convert source code in to an intermediate language known as bytecode. This bytecode only can be executed in a virtual environment called JVM. Java virtual machine. JVM is an interpreter to java bytecode. It converts bytecode into machine language and executes line by line.

this is how both compiler and interpreter used in one language..if it found useful you may mention

like image 20
sabeen kanwal Avatar answered Nov 15 '22 09:11

sabeen kanwal