1

I have gone through lot of explanations about a compiler and interpreter. I think I understood the difference between compiler and interpreter clearly. I'll explain my learning through the following example. Let's say we have the three lines of code in a file.

a=3;
b=4;
a=a+b;
  1. A compiler converts these three lines into machine code( or into a language which is lower than current language) and processor (if it's machine code) runs it to produce output.
  2. An interpreter goes through each line, as in it first processes a=3 and produces output according to language specifications. Then, it goes to next line and does the same.

What I don't understand is "how can an interpreter produce output of some code, without having the computing components like ALU of a processor?"

My understanding is that any program should be run by a processor in machine code as processor understands only that.

Or does an interpreter store the machine level instructions that should be run for a function in a high level language and asks the processor to run those machine instructions whenever that specific function comes in code of that high level language?

0

2 Answers 2

3

An interpreter, in its simplest form, first reads an instruction line, parses it to create something like an abstract syntax tree (like described here), which can be more easily processed. Then the processing takes place, where the expressions and subexpressions of the AST are evaluated in a specific order.

During the evaluation, each terminal expression will be mapped to a predefined function inside the interpreter program itself, and the interpreter will call that predefined function. And if you assume the interpreter program itself is a compiled program, that predefined function exists in form of machine code, which can be executed directly by the CPU.

So an interpreter does not store the machine level instructions that should be run for a function. But it contains functions for basic expressions, which were already compiled beforehand.

6
  • Thanks for the response. Just taking an example here. I found out that Sun JVM' s interpreter is written in C. So, This C program goes through each byte code instruction and converts it into a predefined function in C which will thereby be converted into machine code and run by processor. This process repeats for every statement. Am I correct?
    – AV94
    Sep 8, 2016 at 20:23
  • @Anil, Yes. To be clear "... which will thereby be converted into machine code and run by processor...": This part has happened at compile time of the C program, at runtime it is a matter of selecting those predefined and precompiled function to execute on the processor.
    – Erik Eidt
    Sep 8, 2016 at 20:34
  • @anil: almost. When the program runs, the interpreter does not really "convert it into a predefined function in C which will thereby be converted into machine code". It is more correct to say it dispatches each byte code instruction to a function which was written in C but was already compiled beforehand.
    – Doc Brown
    Sep 8, 2016 at 20:34
  • Okay. Looks like all the complicated things (assembly language codes and intial compilers etc) have been written three-four decades back through lot of effort and Now, we are working just on simpler Application level code.
    – AV94
    Sep 9, 2016 at 4:24
  • @anil: I did not write "which was written in C but was already compiled beforehand" to refer to something created some decades ago. I wrote this to make clear the interpreter does not convert anything to machine code at run time.
    – Doc Brown
    Sep 9, 2016 at 13:51
2

Or does an interpreter store the machine level instructions that should be run for a function in a high level language and asks the processor to run those machine instructions...

That. More or less.

"how can an interpreter produce output of some code, without having the computing components like ALU of a processor?"

By using a predefined set of executable machine instructions defined within the CPU and used by the compiler writer to produce executable instructions. The ALU interprets these instructions; a program doesn't manipulate the ALU directly.

Here is an example. When run through a C compiler, the C program

int main() {
  printf("Hello World!");
  return 0;
}

gets translated into the machine instructions:

LC0:
        .string "Hello World!"
        .text
.globl main
        .type   main, @function
main:
        push    ebp
        mov     ebp, esp
        and     esp, -16
        sub     esp, 16
        mov     eax, OFFSET FLAT:.LC0
        mov     DWORD PTR [esp], eax
        call    printf
        mov     eax, 0
        leave
        ret

The function printf, in turn, compiles to its own set of instructions.

Now, the way that it does this is specific to a given compiler. The techniques involved in creating a compiler or interpreter are widely varied, involving many disciplines, the subjects of which fills numerous textbooks. But the basic principle is the same: you start out with a human-readable language, go through several steps, and eventually arrive at machine-executable code.

1
  • I understood I guess. If an interpreter for langauge A is written in B, then that interpreter program written in B would be going through each statement of code written in A and translates that into a langauge known to the processor (could be B itself or directly machine level) and processor would run it. If the generated instructions are not machine level, some compiler that knows the language would compile it and ultimately gives it to processor
    – AV94
    Sep 8, 2016 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.