CD Unit 1
CD Unit 1
1.Assembler
Assembler is a translator which is used to translate the assembly language code into
machine language code.
Assembly language is a low level programming language where we use the symbols
called mnemonics in place of machine codes. The assembler performs a one to one
mapping from mnemonic statement into machine codes and data. The translated
program is called as object program.
1
Advantages of Assembler
Assembler is Very fast translating assembly language to machine code as 1 to 1
relationship.
Efficiency in execution just like machine level language.
Disadvantages of Assembler
Assembly language is difficult to understand. it is a low-level programming
language.
It is difficult to maintain.
2.Interpreter
Interpreter converts the source program written in high level language into machine
languages.An interpreter converts each statement of the program line by line into
machine code. It means an interpreter translates the one line at a time into machine
language and executes it. Then moves towards the next line this goes on till the end of
the program. If no error encounters.
Interpreter stops and highlights the problem and will not move to next line when any
errors are encountered.
A interpreter requires a less storage space in primary memory than a compiler.
Advantages of Interpreter
Interpreter is that it makes easy to trace out and correct errors in the source
program.
Interpreters over compilers are that an error is found immediately.
2
Disadvantages of Interpreter
It is a time consuming process of translating and executing statements one by
one.
Programs execution is slow.
3.Compiler
The purpose of compiler is same as interpreter but unlike interpreters which translate
the source program line by line, compiler are the translators, which translate the entire
program into machine codes.
If source program contain errors, the compiler highlights a list of errors at the end of
the execution of the program. i.e. a compiler translates the whole program before
execution. A compiler is a larger program and occupies more memory space. It is
costlier than interpreter.
Advantages of compiler
Producers and executable file, and therefore the program can be run without
need of the source code.
A compiler converts a high-level program that can be executed many times.
Disadvantages of compiler
It is slow to execute as you have to finish the whole program.
It is not easy to debug as errors are shown at the end of the execution.
3
Difference between Compiler and Interpreter
Compiler are good for a very long Interpreter is good for small
9
program programs.
4
Linker
• Linker is a computer program that links and merges various object files together
in order to make an executable file.
Loader
• Loader is a part of operating system and is responsible for loading executable
files into memory and execute them.
• It calculates the size of a program (instructions and data) and creates memory
space for it. It initializes various registers to initiate execution.
5
The Analysis-Synthesis Model of Compilation
A compiler can broadly be divided into two phases based on the way they compile.
Analysis Phase:-
•Known as the front-end of the compiler, the analysis phase of the compiler reads the source
program, divides it into core parts and then checks for lexical, grammar and syntax errors.
•The analysis phase generates an intermediate representation of the source program and symbol
table, which should be fed to the Synthesis phase as input.
Synthesis Phase:-
• Known as the back-end of the compiler, the synthesis phase generates the target program with
the help of intermediate source code representation and symbol table.
A compiler can have many phases and passes.
• Pass : A pass refers to the traversal of a compiler through the entire program.
• Phase : A phase of a compiler is a distinguishable stage, which takes input from the previous
stage, processes and yields output that can be used as input for the next stage. A pass can have
more than one phase.
6
Phases of Compiler
The compilation process is a sequence of various phases. Each phase takes
input from its previous stage, has its own representation of source program,
and feeds its output to the next phase of the compiler. Let us understand the
phases of a compiler.
7
Lexical Analysis
The first phase of scanner works as a text scanner. This phase scans the source
code as a stream of characters and converts it into meaningful lexemes. Lexical
analyzer represents these lexemes in the form of tokens as:
<token-name, attribute-value>
Syntax Analysis
The next phase is called the syntax analysis or parsing. It takes the token produced
by lexical analysis as input and generates a parse tree (or syntax tree). In this
phase, token arrangements are checked against the source code grammar, i.e. the
parser checks if the expression made by the tokens is syntactically correct.
Semantic Analysis
Semantic analysis checks whether the parse tree constructed follows the rules of
language. For example, assignment of values is between compatible data types,
and adding string to an integer. Also, the semantic analyzer keeps track of
identifiers, their types and expressions; whether identifiers are declared before use
or not etc. The semantic analyzer produces an annotated syntax tree as an output.
After semantic analysis the compiler generates an intermediate code of the source
code for the target machine. It represents a program for some abstract machine. It
is in between the high-level language and the machine language. This intermediate
code should be generated in such a way that it makes it easier to be translated into
the target machine code.
Code Optimization
The next phase does code optimization of the intermediate code. Optimization can
be assumed as something that removes unnecessary code lines, and arranges the
sequence of statements in order to speed up the program execution without wasting
resources (CPU, memory).
8
Code Generation
In this phase, the code generator takes the optimized representation of the
intermediate code and maps it to the target machine language. The code generator
translates the intermediate code into a sequence of (generally) re-locatable machine
code. Sequence of instructions of machine code performs the task as the
intermediate code would do.
Symbol Table
9
10
11