Translators 1
Translators 1
Translators 1
Source Code
- Refers to the program instructions written in English-like statements (High Level Language)
by the programmer, e.g. the Visual basic Code as typed by the programmer.
- Source code is human understandable language and cannot be understood by the computer.
- This therefore needs to be translated into machine code (binary) which the computer can
understand.
Object Code
- Refers to a machine code version of a source code.
- All programs written in source code must be converted to object code for the computer to
understand them.
TRANSLATORS
- These are programs used to convert High level Language programs as written by the
programmer (source code) into machine code that the computer can execute.
- Translators are in three types, which are assemblers, interpreters and compilers, which
are further explained below:
1. Assemblers:
- These are programs used to translate assembly language instructions (program) into
machine language instructions before execution.
- Since each computer has its own assembly language, it also has its own assembler.
- Assembler programs are written either using assembly language, or using high level
languages like C.
- Assemblers are simpler to program as compared to compilers.
- Assemblers are in two types: One-Pass Assemblers and Two-Pass Assemblers
One-Pass Assemblers
- These go through the source code once and assume that all the symbols will be defined
before any instruction that reference them. This is equivalent to declaring variables at
the top of the source code regardless of where they are used in a high level language
program.
- It has an advantage of speed since only one phase in gone through
Two-Pass Assemblers.
- Creates a table with all the symbols and their values in the first pass and then use the
table in the second pass to generate code.
- Has an advantage that symbols can be defined at any stage in the source code.
- Two-pass assemblers make programs easier to read and to maintain.
Uses of Assemblers
- The uses of assemblers include:
They generate machine code that is equivalent to assembly language.
They are used to check the validity of instructions, that is, checking for syntax errors in
an instruction.
They also assign memory locations to variables.
2. Interpreters
- These are programs that translate and run one (command) instruction of a program at a
time before going to the next instruction until the end of the program, e.g. the BASIC
interpreter.
- Interpreter must be present to run the program. It is used during program writing (coding)
because it easily aids in debugging.
- An interpreter translates one instruction at a time and then executes it.
- The translated program retains the source code.
- They do not produce the machine code version (object code) of a program; hence translation
is repeated every time the program is executed.
- If the program is run 100 times, translation for each instruction is also carried out 100 times.
Functions of Interpreters
- They translate each instruction in turn into machine language and run it.
- Allocates storage space to variables.
- They check syntax error in a program statement.
- Gives error messages to the user
- Finds wrong and reserved words of the program
- Determines wrong use of variables
Advantages of interpreters
- It is easy to find and correct syntax errors in interpreted programs.
- There is no need for lengthy recompilation each time an error is discovered.
- It is very fast to run programs for the first time.
- Allows development of small program segments that can be tested on their own without
writing the whole program.
- It is easier to partially test and debug programs, especially during the programming
stage.
- It is very fast to run small programs.
- individual segments can be run, allowing errors to be isolated
- running will be necessary after very minor changes
- continual compilation of whole code is wasteful/time consuming
Disadvantages of interpreters
- They are very slow in running very large programs.
- They do not produce an object code of a source code and hence difficult to use since
conversion takes place every time the program is run.
3. Compilers
- These are programs that convert a high level language program into its machine code
equivalent at one go (at once) and then run it, e.g. the COBOL compiler.
- Compiler must be present for compiling the program only and NOT during the running
process.
- Creates an object code version of the source code
- Once compiled, the program no longer needs conversion since the machine code version is
the one that will be run, until some changes are made to the program code.
- Compilers run faster when called and therefore may be held as library routines.
- Once compiled, the program can then be run even on a computer without the compiler since
the program will already be in machine code.
- The compilation processes involves many complex stages which will be looked later in this
course.
Functions of Compilers
- They check syntax errors in program statements.
- They allocate storage space to variables.
- Translate the whole program into machine code at one go.
- Run object code of the program.
- Produces a program listing which indicates position of errors in a program.
- Gives error messages to the user
- Finds wrong and reserved words of the program
- Determines wrong use of variables
Advantages of Compilers
- The object code can be saved on the disc and run when needed without the need for
compilation.
- Compiled programs run faster since only the object code is run.
- The object code can run on any computer, even those without the compiler. Therefore
compiled programs can be distributed to many users and used without any problems.
- The object code is more secure since can cannot be read without the need for reverse
engineering.
- Compilers indicate the line numbers with syntax errors and therefore assist
programmers in debugging programs.
- They are appropriate even for very large programs.
Disadvantages of Compilers
- Slower than interpreters for running programs for the first time.
- They can cause the computer to crash.
- Difficult to find errors in compiled program.
- There is need for lengthy recompilation each time an error is discovered.