compilers and interpreters. translation to machine language every high level language needs to be...

8
Compilers and Interpreters

Upload: kathlyn-wells

Post on 21-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

Compilers and Interpreters

Page 2: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

Translation to machine language

• Every high level language needs to be translated to machine code• There are different ways to do it• Two main ways are compilers and interpreters

• What’s the difference?• Compilers are more thorough, generate faster executables and the executable

is easier to distribute• Interpreters are more dynamic, are easier to use in the development stage of

a program

Page 3: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

A compiler

• Compilers are more thorough• When they translate source code to machine code (object code) they

translate (ideally) the entire program• If they encounter a syntax error, they report it but they also try to continue

the translation process• Compiling a buggy program might cause hundreds of error messages

(compilers are easily confused!)• Programmers learn to fix the first one reported then try another compile

• When a program is successfully compiled, there exists a stand-alone file which has the executable code in it, ready to run by giving it to the CPU

Page 4: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

Interpreters, on the other hand,

• Interpreters are more “dynamic”• They translate a statement at a time, then hand the resulting machine code to

the CPU which executes it• Then they translate the next statement of the program, give it to the CPU

which executes it• And this process continues until the program is over OR a syntax error is

encountered

• The executable code is not saved in a runnable form, it is generated on the fly and mostly discarded when the program is done• The FIRST syntax error the interpreter runs into ends the program

execution process right there. No other code is translated at all.

Page 5: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

Distributing programs

• A compiled program has an exe file, which can run on any computer that has the right CPU – the computer does not have to have the compiler application installed• An interpreted program NEEDS the interpreter available to run, so if

you give a copy of your program to a friend, they will need to get Python also in order to run your program

Page 6: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

Speed of execution

• Compiled programs have complete executable files, they don’t need any more translation – they run fast• Interpreted programs need to have the interpreter available and they

will be translated every time the program is executed – they are slower

Page 7: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

Implications for Python

• In an interpreted program, if a statement is not executed, it is not translated• Which means if there is a syntax error in the program, it will not be

detected until that particular part is executed• Which means TESTING is very important!• Example: a function definition in the program has a syntax error in it,

but the function is not called when the programmer is testing the whole program – that error will NOT be detected• So you must be thorough enough in your testing to make sure all your

code gets executed at least one time!

Page 8: Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways

More implications for Python

• There are languages which have only one or the other, not both, so you use what you have• Python is usually interpreted, the interpreter is given away for free• There are Python compilers available• Most programmers like Python for being quick to write a program in• They use Python to develop the program, then compile it to distribute

it (or sometimes convert it to a compiled language like C for speed)