compilers and interpreters. these are programs which translate computer programs from high-level...

9
compilers and interpreters

Upload: jacoby-bedingfield

Post on 29-Mar-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters

Page 2: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters• These are programs which translate

computer programs from high-level languages such as Pascal, C++, Java or JavaScript into the raw 1s and 0s which the computer can understand, but the human programmers cannot:

Page 3: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters• You write this The computer translates it ... into this, which it can run

Page 4: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters• Compilers• Compilers were the first sort of translator program to be written. • The idea is simple: You write the program, then hand it to the compiler

which translates it. Then you run the result. • The compiler takes the file that you have written and produces another

file from it.• The compiler has another task apart from translating your program. • It also checks it to make sure that it is grammatically correct. • Only when it is sure that there are no grammatical errors does it do the

translation. • Any errors that the compiler detects are called compile-time errors or

syntax errors. • If it finds so much as one syntax error, it stops compiling and reports

the error to you.

Page 5: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters• Interpreters• An interpreter is also a program that translates a high-level language into a

low-level one, but it does it at the moment the program is run.• You write the program using a text editor or something similar, and then

instruct the interpreter to run the program.• It takes the program, one line at a time, and translates each line before

running it: • It translates the first line and runs it, then translates the second line and

runs it etc.• The interpreter has no "memory" for the translated lines, so if it comes

across lines of the program within a loop, it must translate them afresh every time that particular line runs.

• Consider this simple Basic program: • 10 FOR COUNT = 1 TO 1000• 20 PRINT COUNT * COUNT• 30 NEXT COUNT

Page 6: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters• Line 20 of the program displays the square

of the value stored in COUNT and this line has to be carried out 1000 times.

• The interpreter must also translate that line 1000 times, which is clearly an inefficient process.

• Examples of interpreted languages are Basic, JavaScript and LISP.

Page 7: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

Diff compilers and interpreters• We usually prefer to write computer programs in languages we

understand rather than in machine language, but the processor can only understand machine language.

• So we need a way of converting our instructions (source code) into machine language.

• This is done by an interpreter or a compiler. • An interpreter reads the source code one instruction or line at a

time, converts this line into machine code and executes it. • The machine code is then discarded and the next line is read.• The advantage of this is it's simple and you can interrupt it while it

is running, change the program and either continue or start again. • The disadvantage is that every line has to be translated every time it

is executed, even if it is executed many times as the program runs. • Because of this interpreters tend to be slow.

Page 8: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

Diff compilers and interpreters• A compiler reads the whole source code and translates

it into a complete machine code program to perform the required tasks which is output as a new file.

• This completely separates the source code from the executable file.

• The biggest advantage of this is that the translation is done once only.

• The program that is run is already translated into machine code so is much faster in execution.

• The disadvantage is that you cannot change the program without going back to the original source code, editing that and recompiling .

Page 9: Compilers and interpreters. These are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into

compilers and interpreters