Vous êtes sur la page 1sur 22

COMPILER DESIGN

16E41A0531
Phases of compiler
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.
3
4
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.
5
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.

6
Intermediate Code Generation
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.

7
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.

8
Symbol Table
It is a data-structure maintained
throughout all the phases of a compiler.
All the identifier's names along with their
types are stored here. The symbol table
makes it easier for the compiler to quickly
search the identifier record and retrieve it.
The symbol table is also used for scope
management.

9
 Total=base price+(no . of units*2)
 Lexical Analyzer:-
 id1=id2+(id3*2)
 Syntax Analyzer:-

10
11
 Temp1=int to real(2)
 Temp2=id3*Temp1
 Temp3=id2+Temp2
 Id1=temp3
 Code Optimizer:-
 Temp1=id3*2.0
 id1=id2+Temp1

12
 movF R2,id3
 mulF R2,#2.0
 movF R1,id2
 ADDF R1,R2
 movF id1,R1

13
 There are different ways in which parameter data can
be passed into and out of methods and functions. Let
us assume that a function B() is called from another
function A(). In this case A is called the “caller
function” and B is called the “called function or callee
function”. Also, the arguments which A sends to B are
called actual arguments and the parameters of B are
called formal arguments.

14
Important methods of Parameter
Passing
 Pass By Value : This method uses in-mode semantics.
Changes made to formal parameter do not get
transmitted back to the caller. Any modifications to
the formal parameter variable inside the called
function or method affect only the separate storage
location and will not be reflected in the actual
parameter in the calling environment. This method is
also called as call by value.

15
// C program to illustrate
// call by value
#include <stdio.h>

void func(int a, int b)


{
a += b;
printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
int x = 5, y = 7;

// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}

16
Pass by reference(aliasing) : This
technique uses in/out-mode semantics. Changes
made to formal parameter do get transmitted
back to the caller through parameter passing. Any
changes to the formal parameter are reflected in
the actual parameter in the calling environment
as formal parameter receives a reference (or
pointer) to the actual data. This method is also
called as <em>call by reference. This method is
efficient in both time and space.

17
// C program to illustrate
// call by reference
#include <stdio.h>

void swapnum(int* i, int* j)


{
int temp = *i;
*i = *j;
*j = temp;
}

int main(void)
{
int a = 10, b = 20;

// passing parameters
swapnum(&a, &b);

printf("a is %d and b is %d\n", a, b);


return 0;
}
18
 COUSINS OF COMPILER
 1. Pre processor
 2. Assembler
 3. Loader
 4.Link-editor

19
Preprocessor
A preprocessor is a program that processes its
input data to produce output that is used as
input to another program. The output is said
to be a preprocessed form of the input data,
which is often used by some subsequent
programs like compilers.

They may perform the following functions :

1.Macro processing
2. File Inclusion
3.Rational Preprocessors
4.Language extension
20
Assembler
Assembler creates object code by translating assembly
instruction mnemonics into machine code. There are two
types of assemblers:

· One-pass assemblers go through the source code


once and assume that all symbols will be defined before any
instruction that references them.

· Two-pass assemblers create a table with all


symbols and their values in the first pass, and then use the
table in a second pass to generate code
21
Linker and Loader
A linker or link editor is a program that takes one or more
objects generated by a compiler and combines them into a
single executable program. Three tasks of the linker are

1.Searches the program to find library routines used by


program, e.g. printf(), math routines.

2. Determines the memory locations that code from each


module will occupy and relocates its instructions by adjusting
absolute references 3. Resolves references among files.

A loader is the part of an operating system that is responsible


for loading programs in memory, one of the essential stages in
the process of starting a program.
22

Vous aimerez peut-être aussi