Check you understanding at AS level first
Compilers, interpreters and assemblers - Describe the function of translation programs in making source programs executable by the computer.
Compilers, interpreters and assemblers - Describe the purpose and give examples of the use of compilers, interpreters and assemblers, and distinguish between them.
Compilers, interpreters and assemblers - Distinguish between and give examples of translation and execution errors.
The purpose of an assembler is to translate assembly language into machine (executable) code
An interpreter takes a single source code instruction as input, translates and executes it.
Converts high level languages into machine code one instruction at a time on-the-fly while the program is running.
Each instruction is converted to machine code once the previous instruction has been executed
Good for debugging code because the program stops as soon as the error has been found.
Running code this way is much slower running compiled code
Machine code is not saved
Both source code and interpreter are needed for the code to be executed
A compiler takes the entire program as input to produce a machine code version of the program.
Converts high level languages into machine code before the program is run.
Saves the machine code, so the sources code is no longer needed
Software is normally distributed as compiled machine ocde. For proprietary software this is good because other people can not copy the code and use it for their own applications
For it to be executed only the machine code is needed, a translator is not
Compiled code can only be run on the spefici processor and OS which it was compiled ofr in the first place
An assembler's source code is low level code
An assembly instruction which will translate to one machine code instruction
Interpretation needs to be repeated each time the program is run
Compliers translate high level source code
Singles lines of high level code compiles to many machine code instructions.
A compiled program can be re-run without further translation.
Errors are reported after compilation has finished.
One error may cause many related/spurious errors
Recompiling after fixing an error adds time to the process
Once compiled the program will run quickly i.e. the object code will be efficient because the compiler will translate directly to the native code of the specified machine. It will optimise the code for the target hardware
Protection of intellectual property i.e. protected by law against copying, theft, or other use that is not permitted by the owner.
For the program developer debugging can be easier as interpreter will stop translation at the point where the error occurred. This highlights the error for the programmer to deal with
Code is more portable as it is not machine dependent and will run on different hardware or in a browser
For security when downloading code from the Internet it can be checked before interpreting on the local machine
Syntax Error - Problem with spelling, punctuation or grammar.
Error = If without ENDIF
Linking Error - Calling a standard function where the correct library has not been linked to the program.
Error = When the Square-root function is used and the library that calculates the Square-Root has not been linked to the program.
Semantic Error - variable declared illegally i.e. doesn't exist
Runtime/Execution - a runtime error is an error that only occurs when the program is running and is difficult to foresee before a program is compiled and run.
Error = Program requests more memory when none is available so the program crashes
Logical Error - An error that causes a program to output an incorrect answer (that does not necessarily crash the program)
Error = An algorithm that calculates a person's age from their date of birth but ends up giving negative numbers
Rounding Error - When a number is approximated to the nearest whole number/tenth/hundredth etc
Error = 34.5 rounded to nearest whole number is 35, an error of +0.5
Truncation Error - When a number is approximated to a whole number/tenth/hundredth etc nearer zero
Error = 34.9 rounded to whole number is 34, an error of +0.9
Compilation process
See AS page (link at top)
Comments and unneeded spaces are removed
Keywords, constants, opperators and identifiers are replaced by 'tokens'
A symbol table is created which holds the addresses of variables, labels and subroutines (the tokens)
Tokens are checked to see if they match the spelling and grammar expected, using standard language definitions.
e.g. Checks for missing brackets, incorrect statement structure and the wrong order of tokens
This is done by parsing each token to determine if it uses the correct syntax for the programming language. (using BNF or Regular expressions)
If syntax errors are found, error messages are produced
May add information to the symbol table, e.g. data type, scope, structure definitions (i.e. record, array, function, class etc)
Creates an abstract syntax tree (AST is a hierarchical tree that represents the syntactic structure of a program unambiguously)
Variables are checked to ensure that they have been properly declared (before use) and used
Checks all operations are legal for particular data types, e.g. Type checking (int + String is invalid)
Variables are checked to ensure they are of the correct data type, e.g. float values are not being assigned to integers
Checks function/procedure calls use correct arguments
Ensures identifiers in expressions exist in the symbol table
Use abstract syntax tree to generate code
Assign registers (considers recycling of registers)
generate binary instructions (by finding common abstract syntax tree patterns and matching them to blocks of machine code)
Produces binary instruction so that it is either as fast (speed of execution) or as efficient (efficient use of memory) as possible, or a trade off between the two
Optimises instructions
removes unreachable code
simplifying expressions (e.g. reducing the lines of code required)
loop optimisation
Considers recycling of registers