The+Function+and+Purpose+of+Translators

Different computers use machine code differently, so translators are used to get from high level code to low level.

eg. C#, C++, VB Assembly Language Machine Code
 * HIGH LEVEL ->LOW LEVEL**

The computer only "sees" the machine code, and the assembly language is a basic language that bridges the gap between high level languages and machine code. One line of assembly corresponds to one line of machine code, so there is no ambiguity. The machine code is all binary, so has much less restrictions, but it is very hard to work out what it is doing, so mistakes are more easily made.

Assembly is used as it is fast and hard coded into the CPU, so ADD A is assigned to machine code that will add whats in location A to the accumulator. Instructions are made using the opcode then the operand which contains a number or location. The opcodes are also known as mnemonics. In this example, ADD is the opcode and A is the operand.

Translators turn **source code** (the high level code the program is written in) to **object code** (the code the computer actually executes). This only generally happens one way (unless someone is trying to reverse engineer code).

Programs can be translated by **interpretation** or **compilation**.


 * Interpretation** is where source code is translated and ran one line at a time:
 * Uses less memory, but is very slow as it has to recompile every time it runs.
 * Can have detailed error reporting for each line
 * Simpler than compilation
 * Source code needed so less private
 * More useful during development


 * Complilation** is where source code is turned into object code:
 * Produces efficient code because it looks at everything
 * Quicker to translate a file than interpretation
 * Uses lots of memory
 * Difficult to locate errors

Compliation is much more complicated than interpretation. It is completed by **parsing**, which means it goes through all the code, looking for meaning. There are 3 stages:


 * 1) **Lexical Analysis** - The compiler parses code to look for valid instructions, which are converted into "tokens". It identifies variables and stores them in a symbol table and converts text into ASCII values. It then gets rid of things that aren't relevant (spaces, idents, comments, etc.)
 * 2) **Syntax Analysis** - The code is parsed again against the rules for the language. Variables are checked to make sure they have been correctly declared and other details are added to the symbol table (eg. data type)
 * 3) **Code Generation -** The code is now definitely valid, so the object code can now be created. The memory address of each variable is determined and added to the symbol table. Then the compiler generates intermediate code, which is then converted to machine code, by translating the tokens from the lexical analysis.


 * Virtual Machines** are sometimes used instead of machine code. These interpret intermediate code into machine code, so there are different virtual machines for different computer architectures. They are useful because they make testing easier (developers can test on the virtual machines and not need the actual computers) and the intermediate code can be used on any machine, with just some simple interpretation.

Also, there can be another stage, called **optimisation**. This is used because the object code produced is much longer than the source code, so they can be shortened using optimisation. It is useful as the program will then take up less space in memory and it can reduce the times the memory is accessed, making it faster.


 * Library Routines** are used to store common processes, which can then be referred to in a program so they don't have to be repeated and recompiled every time they are needed.