2
$\begingroup$

I started to learn about compilers and interpreters. But I still can't understand how program execution happens under the hood.

This is what I have understood so far.

Compiler

The Compiler does not run a code, instead, just converts the code into another (low-level) code. This part is clear to me.

Interpreter

First I thought the interpreter converts some high-level code or byte code into machine language and then executes it. But then I realized that interpreters not convert code into machine code, instead, that instruction becomes an input to the interpreter application and it activate some particular function of the interpreter, which has been already converted into the machine code.

Refer to this pseudo code. Imagine I create a programming language that is interpreted by an interpreter created by python.

add 5 + 3
sub 5 - 3

Interpreter

def add(a, b):
    print(a+b)

def sub(a,b)
    print(a-b)


# Parsing
code = [] #list of instructinos

for instruction in code:
    if instruction.command == "add":
        add(instruction.a, instruction.b)
    elif instruction.command == "sub":
        sub(instruction.a, instruction.b)
    else:
        print("incorrect instruction)

I am not sure whether my understanding of the interpreter is correct. If it is not, I want to know how the interpreter works internally. I mean how the interpreter executes a code without translating it into machine code.

$\endgroup$

2 Answers 2

3
$\begingroup$

I mean how the interpreter executes a code without translating it into machine code

TL;DR It's the interpreter's (machine) code that executes your program's code.

To explain it better, consider these lines taken from your example:

def sub(a,b)
    print(a-b)

I think you got this point wrong: when you write x-y in an interpreted programming language it's because you want to know the result (for example to save it into a variable) rather than to be printed. Also, the variables of the interpreter are not mixed with the ones of the interpreted programs: for example the variables x and y could have some meaning for the interpreter and another meaning for the final program. Usually the interpreter does not make any assumption on the program's variable, it just stores them into a "memory" which is just a set of pairs <variable_name, value>.

So, when there is an instruction like z = x-y in your program, the interpreter does the following:

  1. look into the memory, retrieve the current value for x
  2. look into the memory, retrieve the current value for y
  3. compute x-y (call the result R).
  4. set the value for z to be R in the memory.

Point 3. in the list above is crucial. If this was a real interpreter, it would not be a Python program but a compiled one, thus the implementation of point 3. would be a bunch of machine code that eventually contains a sub machine instruction, or some other sequence of machine instructions that together implement the subtraction.

So every instruction of every language is executed as machine code eventually:

  • in the compiled languages the higher level instructions are translated into machine code;
  • in the interpreted languages it's the interpreter's machine code to be executed.

Read the last paragraph again.

Actually you could write an interpreter using Python, but in this case it's the Python interpreter's machine code to be executed. Or it could have been any chain of pairs <interpreted language $L_i$, interpreter for $L_i$>, the point is that the last interpreter must be compiled, and thus at the very last it's just machine code to be executed because that is the only languages that the processor understands

$\endgroup$
2
$\begingroup$

I mean how the interpreter executes a code without translating it into machine code.

There is a fuzzy line between interpreters and compilers, but the basic idea is this:

  • If it is translated into machine code before being executed, it is probably a "compiler".
  • If it is translated into machine code while being executed, it is probably an "interpreter".

That last one sounds a bit odd, but think about it analogously to human language translation. You could give a document to a translator who translates it from one language to another. A "static" translation, if you will. Or you could have someone translate as a person is speaking. This is a "dynamic" translation.

This latter option is usually what we think of as "interpretation". An "interpreter" takes some kind of high-level representation, and works out what it means in terms of the native machine's code while the program is being run.

$\endgroup$
1
  • $\begingroup$ "If it is translated into machine code while being executed, it is probably an "interpreter"" – No. If it is translated, then it is not an interpreter, it is a compiler. Compiler is just a different name for translator. An interpreter doesn't translate, it interprets. $\endgroup$ Dec 26, 2020 at 18:28

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.