Module 2
Module 2
Preprocessor:
It is a program that reads the source code and prepares it for the translator
Roles of Preprocessor:
Compiler:
Compiler is a program that reads a program in high level language known
Some of the functions are written by user and others are stored in a file
(header file)
The process of loading the program into primary memory for execution is
known as loader
Executable code: It is low-level and machine-specific code that the machine
can easily understand. Once the job of the linker and loader is done the object
code (relocatable machine code) is finally converted it into executable code.
Lexical Analyzer reads the stream of characters from left to right and
groups the characters into meaningful sequences called Lexeme
Syntax Analyzer
It is a second phase of compiler
(a+b) *c
Semantic Analyzer
Semantic Analyzer takes parse tree as its input and construct meaningful
The semantic analyser uses the parse tree and the information in the
symbol table to check the source code for meaning
Roles of a Semantic Analyzer
Type Checking
Type Conversion
Evaluation of an expression
Example:
o float x = 20.2;
o float y = x*30;
o In the above code, the semantic analyzer will typecast the integer
30 to float 30.0 before multiplication
Intermediate code generation takes a parse tree as its input and construct
one or more intermediate representations, which can have a variety of
forms
Roles of Intermediate Code Generation
o Consider,
total = count + rate * 5
Intermediate code with the help of address code method is:
t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3
Code Optimization
It is optional phase of a compiler
Can become
b =c * 10.0
f = e+b
Code Generation:
It is a final phase of a compiler
The code generator takes intermediate or optimized intermediate code
and required information in symbol table as its input and maps it into the
target code
error handler whose task is to handle the errors so that the compilation
can proceed
The errors are reported in the form of a message, when the input
characters from the input do not form the token, the lexical analyzer
detects it as error
Syntax analysis phase large number of errors can be detected. Such errors
are popularly called syntax errors
I. Data Types
Data type refers to the type of value a variable has.
A data type can be specified based on two rules
a) Legal values for variables of the type-
Example – int x- for a variable ‘x’, legal value that can assign is all
integers
b) Legal operation on legal value of the type- Example – We are not
supposed to add an integer and and a float.
Having checked the legality of each operation and
determined the need for type conversion, the compiler must
generate type specific code to implement an operation.
Check the legality
Type conversion
Generate type specific code to implement an operation.
II. Data Structure
In programming language, we are a supposed to declare and use
many data structures like array, stack, queue etc
To compile an element in a data structure- Compiler must develop
a memory mapping -ie compiler will map to the memory word
where element is placed.
III.Scope Rules
It determines the accessibility of variables declared in a program.
Scope of a variable is the part where the entity is accessible.
Normally scope of a variable is restricted to the block of program
in which it is declared
Here compiler performs two operations- Scope Analysis and
Name resolutions to determine variable designated by the use of a
name in the source program.
IV. Control Structures
It is a feature for altering the flow of control during the execution
of a program
This includes – Conditional control transfer
-Iteration control
- function calls
Here compiler must ensure that a source program does not violate
the semantics of control structure.
Linkers and Loaders
When it comes to the execution of any programs, linker and loader
play a very important role.
4 Linker is used to join all the Loader is used to allocate the address to
modules. executable files.