CH1 3
CH1 3
CH1 3
In order to convert the source code into machine language code, the compiler has the
types.
NATIVE CODE: The compiler used to compile a source code for same type of
platform only.
CROSS COMPILER: The compiler used to compile a source code for different kinds
of platforms. They produce an executable machine code for a platform.
1
TYPES OF COMPILERS
ONE PASS COMPILER: compiles whole process in a single pass.
THREADED CODE COMPILER: It replace string by appropriate binary
code.
INCREMENTAL COMPILER: It compiles only changed lines from source
code and update object code.
SOURCE COMPILER: It converts source code high level language in
assembly language only.
DECOMPILER: Basically, it is not a compiler. It is just the reverse of the
compiler. It converts the machine code into high-level language.
2
Uses/Application of Compilers
Helps to make the code independent of the platform.
3
Features of a Compiler
The features are as follows:
Compilation speed.
The correctness of machine code.
Preserve the correct the meaning of the code
Speed of the target machine code.
Good error detection/handling and reporting
Recognize legal and illegal program constructs
Checking the code correctly according to grammar.
4
Phases of Compiler Design
Lexical Analysis
Syntax Analysis
Code optimization
Code generation
Each phase:
Transforms the source program from One representation
into another representation.
They communicate with error handlers
They communicate with the symbol table
7
Lexical Analysis
Lexical Analysis is the first phase when compiler scans the source code.
The tool used is called lexical analyzer (LA) or scanner (Scanner Generator).
This process can be left to right, character by character, and group these characters into
tokens.
Here, the character stream from the source program is grouped in meaningful sequences
by identifying the tokens.
It makes the entry of the corresponding tickets into the symbol table and passes that
token to next phase.
8
Lexical Analysis
9
Lexical Analysis
Tokens can be classified as:
1. Identifier: - these are names chosen to represent data items, functions etc.
2. Keywords: - these are names chosen by computer language designer to
represent facts in a computer language. keywords cannot be used as identifiers.
3. Operators: - these are used to perform operations on operands.
4. Separators: - these are punctuation marks used to group together a sequence of
tokens that have a single meaning.
5. Comments: - increases understandability of a program.
10
Lexical Analysis
A token describes a pattern of characters having
same meaning in the source program. (such as
identifiers, operators, keywords, numbers,
delimiters and so on)
General Form: <token-name, attribute-value>
❖Token-name -> an abstract symbol that is used during syntax
analysis
❖Attribute-value-> entry in the symbol table for
this token.
11
Lexical Analysis
Example:
x = y + 10
Tokens
X identifier
= Assignment operator
Y identifier
+ Addition operator
10 Number
12
Lexical Analysis
position = initial + rate * 60
<id,1><=> <id,2><+><id,3><*><60>
position : mapped into a token (id, 1); Id - identifier and 1 points to the symbol- table
13
Lexical Analysis
Generally Lexical Analysis…….
Skips or ignores comments and white spaces out side of lexemes
Identifies candidate tokens from the given lexemes ( from source
program)
Put information about tokens in symbol table
Detect lexical errors and report errors to the user
Note:
A (Deterministic) Finite State Automaton can be used in the
implementation of a lexical analyzer.
14
Syntax Analysis
The tool used in syntax analysis is called Syntax Analyzer (SA) or parser
The parser or SA uses the first components of the tokens produced by the LA
to create a tree-like intermediate representation that depicts the grammatical
structure of the token stream.
17
Syntax Analysis
Depending on how the parse tree is created, there are different parsing
techniques
Top-down parsing:
construction of the parse tree starts at root , and proceeds towards the
leaves.
19
Semantic Analysis
Semantic analysis checks the semantic consistency of the code.
It uses the syntax tree of the previous phase along with the
symbol table to verify that the given source code is semantically
consistent.
20
Semantic Analysis
Functions of Semantic analyses phase are
Helps you to store type information gathered and save it in
symbol table.
Allows you to perform type checking.
In the case of type mismatch, where there are no exact type
correction rules which satisfy the desired operation a semantic
error is shown.
Collects type information and checks for type compatibility
Checks if the source language permits the operands or not
21
Semantic Analysis
Example from the above expression:
position=initial + rate*60
22
Semantic Analysis
The output of the semantic analysis is the same with
syntax tree plus meaning associated with each expressions
appreciable.
=
+
<Id,1>
*
<id,2>
Inttofloat(60)
<id,3>
60.0
23
Semantic Analysis
Example
float x = 20.2;
float y = x*30;
24
Intermediate Code Generation
Once the semantic analysis phase is over the compiler,
generates intermediate code for the target machine. It
represents a program for some abstract machine.
25
Intermediate Code Generation
26
Intermediate Code Generation
For example,
Position = initial + rate * 60
Intermediate code with the help of address code method is:
t1 := int_to_float(60)
t2 := rate * t1
t3 := initial + t2
Position := t3
27
Code Optimization
The next phase of is code optimization.
29
Code Optimization
Example:
a = intofloat(60)
b=c*a
d=e+b
f=d
Can become
b =c * 60.0
f = e+b
30
Code Generation
Code generation is the last and final phase of a compiler. It gets inputs from code
optimization phases and produces the page code or object code as a result.
The objective of this phase is to allocate storage and generate relocatable machine
code.
The instructions in the intermediate code are converted into machine instructions.
This phase coverts the optimize or intermediate code into the target language.
The target language is the machine code. Therefore, all the memory locations and
registers are also selected and allotted during this phase. The code generated by this
phase is executed to take inputs and generate expected outputs.
31
Code Generation
Example:
32