0

I know a compiler takes a high level language , translates all of it into a machine language( or is it first assembly language and then machine language?), sends to CPU and now it is the job of CPU to actually run it and display the output/do whatever.

I know in case of an interpreter ,it translates first line of the high level programme into something( what is this something? is it machine level language?) and then that something gets executed and run( executed and run by the interpreter or by the CPU ?). After that it goes to next line of code and so on.

So in short, i am little confused of the role of the CPU in case of an interpreter.

user1906399
  • 753
  • 1
  • 13
  • 27
  • 2
    I don't think it's meaningful to ask if the program is executed "by the interpreter or by the CPU". The CPU is running the interpreter. It's like asking what kind of thoughts can you have that don't involve the brain. – JJJ Aug 08 '13 at 16:29
  • It varies. Different technologies use different methods, so I think any answer will only be correct in somewhat high and abstract levels. You could google for what you seek; Wikipedia's article on that is quite interesting: http://en.wikipedia.org/wiki/Interpreter_(computing) – Geeky Guy Aug 08 '13 at 16:30
  • https://stackoverflow.com/questions/33756149/what-is-the-relation-between-an-interpreter-and-cpu – Bergi Sep 19 '21 at 19:31

2 Answers2

2

There's no difference to the CPU between a compiled language and an interpreted one. The same steps are taken to convert source code into something the CPU understands – it's just a matter of when.

For compiled languages, this is done ahead of time. For interpreted languages, it happens at runtime.

In fact, there isn't even a hard-and-fast distinction between compiled and interpreted languages. Consider Java, for example, which is compiled ahead of time from source code to byte code. At runtime, that byte code is interpreted again by the JVM, and may be compiled further and optimized by the JIT.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

This is a really sensible question. The answer is shades of grey. It all depends how much of a run-time environment is built into the interpreter. The run time must store the state of variables etc. but it might do as you suggest and convert a line of the program to machine code and then execute that code. On the other hand it might use a higher level internal representation for the code to be executed and invoke a process which runs that. For example Python and Ruby generate their own internal code representation which is passed to the runtime. Java is a compiler, but converts the whole program to a byte code which is then executed by the run time. So there isn't a simple black and white answer, but it is the right question to help understand what is going on.

David Elliman
  • 1,379
  • 8
  • 15