Subj : Re: Compiler and an interpreter To : comp.programming From : Rob Thorpe Date : Sun Jul 31 2005 01:23 pm Gerry Quinn wrote: > In article <1122649871.905133.250950@o13g2000cwo.googlegroups.com>, > gswork@mailcity.com says... > > > > anjali wrote: > > > Suppose in a source-code file, the 4th line contains the first syntax > > > error.Can anybody please tell me how the compiler and interpreter will > > > behave when the given file will be the input to them? > > > > > > My guess is that the compiler will go through the entire file and will > > > report all the syntax errors in the file while the interpreter will > > > stop its execution exactly after the 4th line and will say that there > > > is a syntax error in the 4th line. > > > > Frequently yes, but in fact either can be made to do either, depending > > on how they have been designed! > > Typically an interpreter will stop on the first error. Compilers in my > experience usually carry on until a fixed number of errors (e.g. 100) > have been found, and then stop. > > Usually in C++, if there are eight errors found, there really are about > eight. When there are 100 errors, you left out a curly bracket > somewhere above the first one, or something like that. The number of errors that are produced by the compiler is a product of many things, including the language design. If the compiler writer is aiming to write a fast compiler, and aiming to write it fast they may well decide to bail out at the first error. Quite a few compilers do that. It's not as stupid as it seems, since the first error is often the only informative one. On the other hand the compiler writer may decide to pay more attention to errors. The normal way to do this is to 1) add productions to the grammar to take care of common error cases 2) skip over heavily errored blocks to the next comprehensible statement. If they do then they must not compromise their compilers speed/memory performance too much by doing this. For instance if they add enough extra productions to change the parser needed they're in trouble, f.e.g changing from LALR to LR. Almost everyone stops short of doing this. Also to stop things going mad they commonly bail out after some number of errors, such as the 100 you mention. Top down and bottom up compilers have different strengths and weaknesses in spotting errors. .