I read the following documentation about compiler and interpreter somewhere :-

A compiler searches all the errors of a program and lists them. If the program is error free then it converts the code of program into machine code and then the program can be executed by separate commands.

An interpreter checks the errors of a program statement by statement. After checking one statement, it converts that statement into machine code and then executes that statement. The process continues until the last statement of program occurs.

My doubt came from the following code:

int main()
{
    printf("hello")
    scanf("%d",&j);
    return 0;
}

I am using MINGW GCC compiler. When I compile the above code following things happen:

Firstly I get the error

error: expected ';' before 'scanf()'

After I correct the above error then I get the second error

error: 'j' undeclared (first use in this function)

So I wanted to know that why both the errors are not listed at one time?

有帮助吗?

解决方案

Compiler and interpreters are technically two different things, though the boundaries can be pretty fluid sometimes.

A compiler is basically nothing more than a language translator. It takes a source language as input and generates a destination language as output.

An interpreter takes a language (be it high-level or low-level) and executes the code described by the language.

The confusion is mostly because most modern script languages contains both a compiler and an interpreter, where the compiler takes the script language and creates a lower-level equivalent (similar to binary machine language) that the interpreter then reads and executes.


As for your problems with the compiler errors, it's most likely because the compiler can't continue parsing the scanf call due to the first error, and simply skips it (including the undeclared variable).

You should also know that in C some errors can actually cause more errors in code that is otherwise correct for example

int j
printf("Enter something: ");
scanf("%d", &j);

You will get an error because of the missing semicolon after the declaration of the variable j, but you will also get an error with the scanf line as the compiler can't find the variable j, even though the scanf call is otherwise correct.

Another typical example of errors that will give follow-up errors in unrelated code, is to forget the terminating semicolon of a structure in a header file. If it's the last structure you might not even get any error in the header file, just unrelated errors in the source file you include the header file in.

其他提示

The documentation you are quoting is a bit misleading.

Both compilers and interpreters aim to report as much errors as possible but finding "all the errors of a program" is impossible. (cf. Halting Problem)

So, a compiler doesn't "search for errors", rather, it parses your source into a tree representation (AST) and then tries to transform that tree into another "tree" for another language (say, machine code).

An interpreter also parses your code but the transformation is done in parts at runtime.

So in your example, the missing semicolon causes the parser to fail so the compiler doesn't even get to the compilation stage (which reports the second error).

As others have said, the distinction between compilers and interpreters is not that clear anymore. Similar techniques are used, interpreters often compile to machine code, etc.

The compiler definition you are quoting is not the best one. One would think that the most important characteristic of a compiler is that it finds errors. Though of course it is very important part of the compiler's job, the main one is to translate the source code into some other form - not even necessarily machine code. In the old days some compilers did not bother with listing all the errors found - at least in one case the entire messaging was that the compiler found an error somewhere in the source and stopped. And even now sometimes it is not possible to find all errors in one go.

A common compiler behavior when an error is detected is to try to recover the error and continue the parsing to check other errors.

When the compiler detects the missing semicolon error it usually try to recover the error skipping input until the next semicolon, for that reason the scanf("%d",&j) statement is not parsed and the missing j definition error is not detected.

The text you are quoting is problematic. While in general true, usually the compiler doesn't have a separate "error check" phase.

What it really does is it tries to read your code right away, and if your code has errors in it, it will fail while trying to read.

The difference between interpreter and compiler isn't when it checks for errors, it is when it actually runs the code. A compiler tries to read the program fully, then run it, an interpreter reads ~one statement (or even just one sub-expression), then runs it, reads another, runs it.

Differences between compiler and Interpreter are given below :

  1. Compiler takes entire program whereas Interpreter takes single statement as input.
  2. Compiler generate intermediate object code whereas Interpreter can not generate intermediate object code .
  3. Compiler execute the program entirely whereas Interpreter execute the program line by line.
  4. Compiler usually faster whereas Interpreter usually slower.
  5. Compiler give less error diagnostics than interpreter.
  1. A compiler directly changes the source code into the machine language, whereas an interpreter produces a middle code and then executes this code in order to form a machine understandable code.
  2. Compiler reads entire program for compilation. Interpreter reads single statement at a time.
  3. Compiler displays all errors and warning together. Interpreter displays single error at a time it reads single instruction at a time.
  4. Compiler requires more memory because of object generation, every time when program is being compiled an intermediate code will be generated. An interpreter needs less memory to interpret the program as interpreter does not generates any intermediate code.
  5. In compiler debugging is comparatively difficult. In an interpreter debugging is easy.
  6. Compiler takes large amount of time to analyse the source code but the overall execution time is comparatively faster. An interpreter takes less amount of time to analyse the source code but the overall execution time is slower.
  7. Once a program is compiled, its source code is not useful for running the code. For interpreted programs, the source code is needed to run the program every time.
  8. In compiler the compilation is done before execution. In an interpreter compilation and execution take place simultaneously.
  9. Compiler does not allow a program to run until it is completely error-free.Interpreter runs the program from first line and stops execution only if it encounters an error.
  10. Examples of programming languages that use compilers: C, C++, COBOL Examples of programming languages that use interpreters: BASIC, Visual Basic, Python, Ruby, PHP, Perl, MATLAB, Lisp.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top