Translation and Errors Flashcards
Translator
A program that converts source code into machine code.
What do compilers do?
They take the whole source code and translate it into machine code all in one go. Once converted, the object code can be run unassisted at any time.
What are the advantages of compilers?
Compiled programs run quickly, because they’ve already been translated.
A compiled program can be supplied as an executable file. An executable file is a file that is ready to run. Since an executable file cannot be easily modified, programmers prefer to supply executables rather than source code.
Compilers optimise code. Optimised code can run quicker and take up less memory space.
What are the disadvantages of compilers?
Because the source code is translated as a whole, there must be enough memory space to hold the source code, the compiler and the generated object code. There also needs to be temporary working space for the compiler to perform the translation. Modern systems either have enough memory or use virtual memory to hold all the data.
Source code compiled on one platform will not run on another - the machine code is specific to the processor’s architecture.
The source code must be re-compiled every time the programmer changes the program.
Compilers do not usually spot errors - the program has to be compiled and run before errors are encountered. This makes it harder to see where the errors lie.
What is an interpreter?
An interpreter translates source code into machine code one instruction at a time (one line at a time). The resulting machine code is then executed immediately.
What are the advantages of interpreters?
Instructions are executed as soon as they are translated.
Errors can be quickly spotted - once an error is found, the program stops running and the user is notified at which part of the program the interpretation has failed. This makes interpreters extremely useful when developing programs.
What are the disadvantages of interpreters?
Interpreted programs run slowly as the processor has to wait for each instruction to be translated before it can be executed.
Additionally, the program has to be translated every time it is run.
Interpreters do not produce an executable file that can be distributed. As a result, the source code program has to be supplied, and this could be modified without permission.
Interpreters do not optimise code - the translated code is executed as it is.
What is an assembler?
Assemblers are a third type of translator. The purpose of an assembler is to translate assembly language into machine code.
Assemblers create one machine code instruction for each assembly instruction.
Name the stages of compiling a program.
lexical analysis symbol table construction syntax analysis semantic analysis code generation optimisation
What happens in lexical analysis?
Comments and unnecessary spaces are removed.
Keywords, constants and identifiers are replaced by ‘tokens’, which are symbolic strings to identify what the elements are.
What happens in symbol table construction?
A table stores the names and addresses of all variables, constants and arrays.
Variables are checked to make sure they have been declared and to determine the data types used.
What happens in syntax analysis?
Tokens are checked to see if they match the syntax of the programming language.
If syntax errors are found, error messages are produced.
What happens in semantic analysis?
Variables are checked to make sure they have been correctly declared and contain the correct data type.
Operations are checked to ensure that they are appropriate for the type of variable being used.
What happens in code generation?
Machine code is generated.
What is optimisation?
Code optimisation makes the program more efficient so it runs faster and uses fewer resources.