You are on page 1of 17

WHAT IS COMPILER

a program that reads a program written in one language


and translates it into an equivalent program in another
language.

1
LANGUAGE PROCESSING SYSTEM

2
PHASES OF A COMPILER
The two important parts in compilation are:
⚫ Analysis (Machine Independent/Language Dependent)
⚫ Synthesis (Machine Dependent/Language
independent)

3
4
ANALYSIS AND SYNTHESIS
Analysis:
⚫ breaks up the source program into pieces
⚫ creates an intermediate representation
Synthesis:
⚫ constructs the desired (target) program from the
intermediate representation.

5
ANALYSIS OF THE SOURCE PROGRAM
1. Lexical / Linear Analysis (scanning)
⚫ Scans the source code
⚫ Represents lexemes in the form of tokens as:
<token-name, attribute-value>
⚫ Token smallest meaningful element that a compiler
understands.
⚫ Example:
Identifiers, Keywords, Literals, Operators and Special
symbols.

6
7
2. Syntax / Hierarchical Analysis – Parsing
Tokens are grouped hierarchically
The result is generally a parse tree.
Expressions, statements, declarations etc... are identified
by using the results of lexical analysis.
Most syntactic errors in the source program are caught in
this phase.

8
9
3. Semantic Analysis
Checks for semantic errors in the source program
(e.g. type mismatch)
Stores type information in the symbol table or the
syntax tree.
Types of variables, function parameters, array
dimensions, etc.

10
11
INTERMEDIATE CODE GENERATION
While generating machine code directly from
source code is possible, it entails two problems:
⚫ With m languages and n target machines, we

need to write m × n compilers.


 The code optimizer cannot be reused.
A machine-independent code optimizer may be
written.

12
13
CODE OPTIMIZATION
Changes the IC by removing such inefficiencies
Improvement in time, space, or power
consumption.
It changes the structure of programs

14
15
CODE GENERATION
Converts IC to machine code.
Must handle all aspects of machine
architecture
Storage allocation decisions are made
here
 Register allocation and assignment are
the most important problems.

16
17

You might also like