I'm trying to understand the difference between compilers and interpreters.
I understand that a compiler translates a high level language directly into machine code, all in one go, seeing the entire code.
And I understand that an interpreter - rather than translating into machine code - it directly executes the commands, one at a time.
What I don't understand is how the interpreter does this? Doesn't it also end up having to produce machine code in order to do the execution?
So my question is - aren't compilers and interpreters both producing machine code, while the interpreter is just going the extra step and executing it, line by line (and then discarding the executed machine code)?
Best How To :
A compiler parses the language and generates machine code.
An interpreter parses the language and executes the program straight away by calling specialized functions, it does not generate code.
A= B + C makes a compiler generate
Load R0, $B;
Load R1, $C;
Add R1, R0;
Store $A, R1;
and an interpreter directly invoke its run-time routines while parsing
As usual, reality is more complicated as there can be pre-parsed intermediate forms as well as intermediate code that is interpreted or just-in-time compiled, but you get the idea.
I warmly recommend http://compilers.iecc.com/crenshaw/, which will show you how a parser can be embodied in either a compiler or an interpreter.