You are on page 1of 4

Chapter 3.

2 Functions and Purposes of Translators


What is a translator program? 1. A translator program converts source code written in a high level programming language to machine code. 2. Translator programs are of three kinds: Compilers, Interpreters and Assemblers Why a program written in a high level programming language needs to be translated? This is because: 1. Computer can only understand binary code 2. High level language close to human language How errors in the reserved words used in high level language instructions are recognized by the translator program? 1. Translator program maintains a dictionary of reserved words and compares the reserved word used in the program with those words in the dictionary 2. If the reserved word used in the program is not found in the dictionary then an error has been made How errors in the variables used in high level language instructions are recognized by the translator program? 1. Variable names must follow the rules of the programming language 2. Translator tries the rules against the variable names used. If the variable name used does not follow the rules then an error has been made What is compiler? A compiler translates source code written in a high level programming language to object code which can be saved as an object code file and later run to execute the program. Compiler does this by scanning through the source code in one run. What is interpreter?

2
An interpreter converts source code written in a high level programming language to machine language line by line and executes each line. What is assembler? Assembler converts assembly language code into machine language. What is object code? Object code is the product obtained when source code is translated by a compiler. Object code may almost be machine code or close to machine code.
Compiler

Source Code

Object Code

Interpreter

Machine Code

Assembly Code

Assembler

Advantages of the interpreter 1. Does not use a lot of computer resources 2. Produces an error message as soon as an error is encountered and indicates the line of source code where the error occurred 3. Interpreter is useful in program development because errors can be found and corrected as soon as they are encountered. Disadvantages of the interpreter 1. Program executes slowly because each line of the source code has to be translated whenever the program has to be executed 2. Object code is not produced which can be run later to execute the program fast Advantages of compiler

3
1. Program executes fast 2. Once the object code is created it is saved as a separate file which is executed subsequently when the program has to be executed Disadvantages of compiler 1. Uses a lot of computer resources such as a large amount of computers memory 2. When an error in a program occurs it is difficult to indicate exactly the line of source code where the error occurred Stages of program translation
Source Code Lexical Analysis Syntax Analysis Semantic Analysis Intermediate Language Code Generation Code Optimization Object Code

What happens during the lexical analysis stage of compilation? 1. The compiler replaces groups of characters by tokens and converts the source code into a stream of tokens 2. The compiler creates a symbol table which is accessed by using a hashing algorithm. In this stage the symbol table contains only the variable names 3. The compiler removes the redundant characters, white spaces, tabs and comments 4. The compiler creates some error diagnostics such as illegal variable names

What happens during the syntax analysis stage of compilation? 1. The compiler analyzes the tokens to check for grammatical correctness 2. The compiler parses the program to find out whether the correct number of brackets has been used in expressions and determine the priorities of the arithmetic operators. 3. The compiler checks whether the control constructs are correct 4. Data types and scope of variables are added to the symbol table 5. The compiler creates some error diagnostics What happens during the code generation stage of compilation? 1. The compiler optimizes the code by removing any duplicate or redundant instructions which increases the speed of execution and reduces the size of object code 2. The compiler produces the object code or machine code 3. The compiler produces the results intended by the source code 4. Memory addresses of variables are added to the symbol table

You might also like