Professional Documents
Culture Documents
Chapter 3.2 - Functions and Purposes of Translators
Chapter 3.2 - Functions and Purposes of Translators
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