Professional Documents
Culture Documents
Compilers:: 1 MR - Binu Vargis Asstprofessor Computerscienceengg
Compilers:: 1 MR - Binu Vargis Asstprofessor Computerscienceengg
Compilers:
A compiler is a program that reads a program
written in one language the source
language and translates it into an
equivalent program in another language
the target language
Compilers:
As an important part of this translation
process, the compiler reports to its user the
presence of errors in the source program.
1
01-03-2017
Compilers:
Compilers:
At first glance, the variety of compilers may
appear overwhelming.
2
01-03-2017
3
01-03-2017
Compilers HISTORY:
Throughout the 1950s, compilers were
considered notoriously difficult programs to
write.
4
01-03-2017
Analysis
Synthesis
5
01-03-2017
errors
6
01-03-2017
7
01-03-2017
Linear Analysis:
Hierarchical Analysis:
Semantic Analysis:
8
01-03-2017
9
01-03-2017
10
01-03-2017
11
01-03-2017
Linear Analysis:
12
01-03-2017
13
01-03-2017
14
01-03-2017
IF if if
LPAREN ( (
15
01-03-2017
Hierarchical Analysis:
16
01-03-2017
17
01-03-2017
Assignment
Statement
:=
Identifier Expression
Position +
Expression Expression
Identifier *
Initial
Expression Expression
Identifier Number
Rate 60
18
01-03-2017
19
01-03-2017
Semantic Analysis:
20
01-03-2017
21
01-03-2017
22
01-03-2017
23
01-03-2017
24
01-03-2017
25
01-03-2017
26
01-03-2017
27
01-03-2017
28
01-03-2017
29
01-03-2017
Code Optimization:
The code optimization phase attempts to
improve the intermediate code, so that faster-
running machine code will result.
30
01-03-2017
Code Optimization:
Some optimizations are trivial.
Code Optimization:
Temp1 := id3 * 60.0
id := id2 + temp1
31
01-03-2017
Code Optimization:
That is, the compiler can deduce that the
conversion of 60 from integer to real
representation can be done once and for all at
compile time, so the inttoreal operation can
be eliminated.
Code Optimization:
Besides, temp3 is used only once, to transmit
its value to id1. It then becomes safe to
substitute id1 for temp3, whereupon the last
statement of 1.3 is not needed and the code
of 1.4 results.
32
01-03-2017
33
01-03-2017
34
01-03-2017
a = 5;
b = 3;
Print a,b;
35
01-03-2017
Frequency reduction
Some instructions can be moved out of a loop
If keeping it out doesnt make in difference better
reduce the frequency of executing it.
while (i<=100)
z = 5;
{
while (i<=100)
z = 5;
{
x = z * I;
x = z * I;
i++;
i++;
}
} Mr.Binu Vargis AsstProfessor
71
ComputerScienceEngg
Strength Reduction
Replacement of time consuming operation by a
faster operation.
In some cases multiplication can be replaced by an
addition operation.
36
01-03-2017
37
01-03-2017
Preprocessor
Source program
Compiler
Assembler
LIBRARY, RELOCATABLE
Loader/link-editor OBJECT FILES
38
01-03-2017
Macro Processing:
File inclusion:
Rational Preprocessors:
Language extensions:
Preprocessors:
Macro Processing:
39
01-03-2017
Preprocessors:
File inclusion:
A preprocessor may include header files into the
program text.
Preprocessors:
defs.h main.c
40
01-03-2017
Preprocessors:
Rational Preprocessors:
These processors augment older languages with
more modern flow-of-control and data-structuring
facilities.
Preprocessors:
Language extensions:
These processors attempt to add capabilities to
the language by what amounts to built-in macros.
For example, the language Equal is a database
query language embedded in C. Statements
beginning with ## are taken by the preprocessor
to be database-access statements, unrelated to C,
and are translated into procedure calls on routines
that perform the database access.
41
01-03-2017
z = x + (y * A)
42
01-03-2017
Postfix Notation
Operator appears after operands.
Traversing is done from right to left.
z=x+y*A
Quadruple representation:
OPERATOR OPERAND 1 OPERAND 2 RESULT
* Y A Temp1
+ X Temp1 Temp2
= Temp2 - z
43
01-03-2017
Triple representation:
OPERATOR OPERAND 1 OPERAND 2
Triple representation of z = x + y * A
2 + X (1)
3 = Z (2)
Code Generation
The final phase of the compiler is the
generation of target code
44
01-03-2017
Code Generation
Memory locations are selected for each of the
variables used by the program.
T1 = x + y MOV R1,x
ADD R1,y
MOV T1, R1
T2 = T1 + z MOV R1, T1
ADD R1, z
MOV T2, R1
x = T2 MOV R1, T2
MOV X, R1
Mr.Binu Vargis AsstProfessor
90
ComputerScienceEngg
45
01-03-2017
Assignment 3:
1. Consider the statement
4.0
R= 2
Explain the processing of above statement with
respect to all phases of compiler.
2. For the statement given below, generate intermediate
code in the format:
i) Postfix notation
ii) Abstract syntax tree
iii) Quadruple
iv) Triple
v) Three address code
+
=
( )
Mr.Binu Vargis AsstProfessor 91
ComputerScienceEngg
46
01-03-2017
47
01-03-2017
48
01-03-2017
LANGUAGE PROCESSOR
DEVELOPMENT TOOLS
There are several software tools that help in
the production of a compiler. Two best known
are :
1. LEX : a lexical analyser generator
2. YAAC: a parser generator
49
01-03-2017
LEX
At lexical phase, we use regular expressions to
specify patterns to LEX
It generates code that allows to scan and
match strings in the i/p
LEX is a software tool that takes i/p a
specification of Regular Expression
Also takes action as i/p on recognising each of
these patterns.
The o/p of LEX is a C- Program for lexical
analyses Mr.Binu Vargis AsstProfessor
99
ComputerScienceEngg
C a.out
COMPILER
50
01-03-2017
%% (OPTIONAL)
Mr.Binu Vargis AsstProfessor
102
ComputerScienceEngg
51
01-03-2017
LEX Specifications
Declarations
%%
Translation rules
%%
User functions
Any of these three sections can be empty but the %%
between the declaration & rules cannot be omitted.
Declarations Section
This section contains entries that:
Include standard I/O header file.
Define global variables.
Define the list rule as the place to start
processing.
Define the tokens used by the parser.
Define the operators and their precedence.
52
01-03-2017
Rules Section
The rules section defines the rules that parse
the input stream.
Programs Section
The programs section contains the following subroutines. Because these
subroutines are included in this file, you do not need to use the yacc library when
processing this file.
53
01-03-2017
54
01-03-2017
YACC
It creates a C-Program for parser.
Available as a command on the Unix system.
C
y.tab.c a.out
COMPILER
55
01-03-2017
Steps:
YACC Specification file is prepared.
YACC transforms the file containing
specifications into a C Program called y.tab.c,
which is a parser in C Language.
Y.tab.c is run through C Compiler which
produces object program a.out.
The parser a.out can be run to parse a source
containing stream of tokens.
The input of YACC is divided into three sections
as given below: Mr.Binu Vargis AsstProfessor
111
ComputerScienceEngg
Declarations
%%
Translation rules
%%
56
01-03-2017
FIRST PART
DECLARATIONS section consists of token
declarations & C code bracketed by
% { .%}
YACC definitions.
%start
%token
%union
%type
57
01-03-2017
58
01-03-2017
Third Part
Contains valid C code that supports the
language processing.
Symbol Table implementation.
Functions that might be called by actions
associated with the productions in the second
part.
59
01-03-2017
60
01-03-2017
61
01-03-2017
OVER.....
62