2. Introduction to Translator.
Assembler.
Interpreter.
Compiler.
Phases of Compiler.
Example
Created By Abhishek Singh 2
3. Translator is system software which translate source
language into target language.
Translator are of three type:-
Assembler.
Interpreter.
Compiler.
Created By Abhishek Singh 3
4. Assembler translates an assembly language program
(the source program) to the machine language
program (the object program).
Assembly language use Mnemonics like ADD,MULT
etc.
Form example:- MOV R1,a
Created By
Abhishek Singh 4
Assembler
Assembly
Language
Machine
Language
5. Interpreter translates a program line-by-line
(statement-by-statement) and carries out the specified
actions according to input.
Created By Abhishek Singh 5
Interpreter
Source
Program
Input
Output
Error messages
6. Compiler is a system software, which translates a
program written in a source language into an
equivalent program in a target language.
Created By Abhishek Singh 6
Compiler
Error messages
Source
Program
Target
Program
Input
Output
7. Compiler have six phase.
1. Lexical Analysis.
2. Syntax Analysis.
3. Semantic Analysis.
4. Intermediate Code Generation.
5. Code Optimization.
6. Code Generation.
Created By Abhishek Singh 7
8. In this phase of compilation full source code is
scanned and program is broken up into groups of
string called token.
Token is a sequence of characters having a collective
meaning.
For example:-
◦ total = count + rate * 10
Created By Abhishek Singh 8
total = count + rate * 10
9. In this phase token generated by lexical analyses are
grouped together to form a hierarchical structure.
The structure is known syntax tree. For building
syntax tree the production rules are designed. These
rule are according to Grammar.
Let take Grammar for total = count + rate * 10
E<- id
E<-number
E<-E1 + E2
E<-E1 * E2
E<-(E)
Created By Abhishek Singh 9
11. Semantic analysis is a phase of compiler that adds
semantical information to the parse tree and performs
certain checks based on this information.
For a program to be semantically valid mean, all
variables, functions, classes, etc. must be properly
defined.
Created By Abhishek Singh 11
13. This is a kind of code which is easy to generate and easily
converted to target code.
This code is in form like three address code.
For example
Intermediate for total = count + rate * 10
Here t1,t2 and t3 are temporary variable.
t1 := int to real (10)
t2 := rate * t1
t3 := count + t2
total := t3
Created By Abhishek Singh 13
14. The code optimization phase attempts to improve the
intermediate code, this phase increase the execution
speed and less consumption of memory.
◦ Optimize code for code total = count + rate * 10 is
t1 := rate * 10.0
total := count + t1
Created By Abhishek Singh 14
15. In this phase target code gets generated. The code
optimization phase instructions are translated into
sequence of target code.
Generated code is:-
◦ MOV R1,rate
◦ MUL R1,10.0
◦ MOV R2, count
◦ ADD R1,R2
◦ MOV total,R1
Created By Abhishek Singh 15
16. The main utility of a compiler is to record the
variable names used in the source program and gather
information about different attributes of each
variable.
These attributes can provide the information about
the storage allocation for name, its type, its scope
(where we use this value in program), and in the case
of function or subprogram, its name, number and
types of its arguments, the method of passing each
argument (like, by value or by reference), and the
type returns.
Created By Abhishek Singh 16
17. Mainly compilation error are gather in:-
1. Lexical Errors :-Lexical phase detect errors of inputs
such as illegal characters , End of line mark missing in
strings.
2. Syntactical Errors :- Syntactical phase detect errors of
syntax like, Incorrect sequence of tokens, Misspelled
keywords (e.g. TEHN).
3. Semantical Errors :-Semantical phase detect errors of
meaning like, Incorrect composition of types,
Multiple declaration of a variable and Variable use
before assignment.
Created By Abhishek Singh 17
18. Created By Abhishek Singh 18
Lexical Analysis
Syntax Analysis
Semantic Analysis
Source Program
Total=count + rate* 10;
Token
stream total = count + rate * 10
=
total +
*count
rate 10
Syntax
Tree
Semantic
Tree
total
=
+
*count
rate int to real
10
19. Created By Abhishek Singh 19
Intermediate Code
Generation
Code Optimization
Code Generation
10
total
=
+
*count
rate int to real
t1 := int to real (10)
t2 := rate * t1
t3 := count + t2
total := t3
Semantic
Tree
Intermediate
Code
Target
Code
Optimize
Code t1 := rate * 10
total := count + t1
MOV rate,R1
MUL #10.0, R1
MOV count, R2
ADD R2,R1
MOV R1,total
20. Created By Abhishek Singh 20
Editor Compiler Assembler
Linker
LoaderDebugger
Programmer
Source
Program
Assembly
Code
Machine
Code
Linking
Programmer
Does manual
Correction of
the code
Debugging
Results
Debug if Error
Executable
Image