Subj : Re: Compiler and an interpreter To : comp.programming From : Rob Thorpe Date : Fri Jul 29 2005 11:36 am 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. This depends on the formal structure of the language and the design of the compiler/interpreter. For the purposes of this discussion languages can be split into two types, in which either: 1. Only a complete file/module is an acceptable input 2. Some small unit of the language is an acceptable input. C, C++ and Pascal are example of type 1, there are many more. Bash, lisp and ML are examples of type 2, again there are many more. If the language is of type 1 then the compiler/interpreter must read the whole file/module before beginning to do anything with it in earnest. In doing so it will find any syntax errors. If the language is of type 2 then the compiler/interpreter can begin to act on the input as it's going along. A compiler may do this by writing out machine code, an interpreter may do this by performing the actions specified. The compiler/interpreter doesn't have to be written this way though, it may choose to do nothing with the input until it reaches the end of the file/module. Most compilers read the entire input file before writing out code. Many interpreters act of input a line at a time. But not all, Perl for example reads the whole file before beginning to execute it. .