I’ll illustrate the doubt in my mind by using the following example I came up with.
Consider a C++ code written below :
#include<iostream.h>
void main()
{
cout<<"LULZ \n";
cout<<"\n Enter anything\n";
int a;
cin>>a;
goto noexistence;
}
Now, my question is that how will the output to the end user will differ if this piece of code is compiled and then, in a different case, interpreted? When we’ll compile it, it will give a compile time error and will complain that no such label exists (noexistence).
But what will happen when this piece of code is interpreted? I don’t know if there is an interpreter out there for C/C++ (I heard about CINT ROOT from CERN but I don’t know how to operate it) but theoretically speaking, how would interpretation catch this fallacy where the label is not defined?
I am asking this question because I’ve read a lot of times that in interpretation, “the code is executed line by line”. I could never grasp this notion of “line by line”. Can anyone shed some light on it as this question is eating me right now hehe.
An interpreter reads the input code and quickly parses it before execution.
Sometimes, parsing is just reading lines into a linked list. If this is all parsing an interpreter implements, then it would start scanning all input lines for the goto label and would print an error saying label not found.
A smarter interpreter can create a jump table as it proceeds executing for the lines already executed. In this case, the label can be searched in this table (for backward references) and also scanned in the following lines (for forward references).
Another interpreter can choose to parse all token in all input, and create a jump table before running the first statement. Such an interpreter would only check the jump table and print out an error message.
So it really all depends. However, a compiler parses, and compiles the input into an executable before any execution attempt.