Compiler Design

Published on February 2017 | Categories: Documents | Downloads: 57 | Comments: 0 | Views: 260
of 24
Download PDF   Embed   Report

Comments

Content

NAVEEN KUMAR.T.P

COMPILER DESIGNING

1
A seminar report on

COMPILER DESIGNING
Submitted in partial fulfillment of requirement of III semester in
MASTER OF COMPUTER APPLICATIONS
Of
Visvesvaraya Technological University
By
NAVEEN KUMAR.T.P
1SJ04MCA16
Under the guidance of
Mr.B.R.PRAKASH, M.C.A, CCNA.
Lecturer, Dept., of M.C.A
S. J. C. Institute of Technology
Chickballapur – 562 101.

Sri Adichunchanagiri Shikshana Trust
Sri Jagadguru Chandrashekaranatha Swamiji Institute of Technology
Chickballapur – 562 101
2005
Sri Adichunchanagiri Shikshana Trust
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

2
Sri Jagadguru Chandrashekaranatha Swamiji Institute of Technology

Department of Master of Computer Applications
Chickballapur – 562 101
2005

CERTIFICATE
This is to certify that the seminar entitled

COMPILER DESIGNING
Submitted in partial fulfillment of the requirement of the degree of
MASTER OF COMPUTER APPLICATIONS
Is a result of the bonafide work carried out by
NAVEEN KUMAR.T.P
1SJ04MCA16
During the academic year 2005

Internal Guide,
Mrs. V.PRAKASH, M.C.A.
C. A
Lecturer, Dept., of M.C.A
S. J. C. Institute of Technology
Chickballapur – 562 101.

S.J.C.I.T.

CHAIRMAN

Mr. N. SRIDHARA M.Sc, M. S
Dr. Gangabhoraiah
HOD, Dept., of M.
S. J. C. Institute of Technology
Chickballapur – 562 101

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

3

ACKNOWLEDGEMENT
I consider it as a great privilege to extend our heart felt
gratitude to our esteemed principal Dr H.S. Ningappa, of our institute for
his untiring zeal, which constantly inspired me towards the attainment of
ever lasting knowledge through out the course. I am greatly indebted to
him for his encouragement.
I would like to express my sincere and heartfelt gratitude to my
seminar coordinator Dr. Gangabhoraiah, for his valuable suggestions
and constant encouragement.
My sincere thanks to Prof N.Sridhara, H.O.D, Dept of M.C.A, for
his constant encouragement and providing the necessary facilities.
No amount of words would be enough to describe, for the
support, valuable suggestions and technical expertise given by my
guide Mr.B.R.Prakash without him the seminar wouldn’t have seen the
light of the day.

I also would like to thank all the staff members of MCA and friends
who helped me to make this seminar a success
NAVEEN
KUMAR.T.P.
1SJ04MCA16

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

4

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

5

INDEX
SL .NO.

CONTENTS

1.

INTRODUCTION

2.

PHASES OF A COMPILER

PAGE
6

7
3.

COMPILATION PROCESS

8 - 11

4.

COMPILER DESIGN OPTIONS

12 -

17

S.J.C.I.T.

5.

COMPILER CONSTRUCTION TOOLS

6.

CONCLUSION

19

7.

BIBLIOGRAPHY

20

Dept. of MCA

18

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

6

1. INTRODUCTION
Compilers were came into existence in 1950‘s. The first compiler
developed was the FORTRAN compiler. A compiler is system software. A
compiler reads a program written in one language – the source language
and translates it into an equivalent program in another language – the
target language. As an important part of this translation process, the
compiler reports to its user the presence of errors in the source
program.
The above explained concept is as shown in the below figure,

SOURCE
PROGRAM

COMPILER

TARGET
PROGRAM

ERROR
MESSAGES

FIG: COMPILER

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

7

2. PHASES OF A COMPILER
Conceptually, a compiler operates in phases, each of which
transforms the source program from one representation to another.

The different phases of the compiler are is as follows.

Phase I

:

Lexical analysis

Phase II

:

Parsing

Phase III

:

Semantic analysis

Phase IV

:

Intermediate code generator

:

Code optimization

:

Code generation

Phase V
Phase VI

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

8

SCHEMATIC REPRESENTATION OF PHASES OF COMPILER

SOURCE PROGRAM

LEXICAL
ANALYZER

SYNTAX
ANALYZER

SEMANTIC
ANALYZER
SYMBOL-TABLE
MANAGER

ERROR
HANDLER
INTERMEDIATE
CODE
GENERATOR

CODE
OPTIMIZER

CODE
GENERATOR

TARGET PROGRAM
PHASES OF A COMPILER
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

9

3. COMPILATION PROCESS
The compilation process includes the following primary steps.
INPUTING SOURCE PROGRAM
Original high-level language program
PRODUSING OBJECT PROGRAM
Machine language translation of the source program
FINAL STEP
Object program is written to an object file
Using the different phases does all these things. The different
phases that are mentioned previously are discussed as follows.

PHASE I : LEXICAL ANALYSIS
Compiler examines the individual characters in the source program
and groups them into syntactical units called tokens. The program that
performs lexical analysis is called as lexical analyzer or scanner. Its main
task is to read the input characters and produce as output a sequence of
tokens that the parser uses for syntax analysis.
This interaction summarized schematically in the following figure.

SOURCE
PROGRAM

LEXICAL
ANALYZER

GET NEXT TOKEN

PARSER
TOKEN
SYMBOL
TABLE

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

10

INTERACTION OF LEXICAL ANALYZER WITH PARSER
STEPS IN LEXICAL ANALYSIS

a. Input to a scanner: A high-level language statement from the
source
Program.
b. Scanner’s output:
A list of all the tokens in that statement.
The
Classification number of each token found
Since the lexical analyzer is the part of the compiler that
reads the source text, it may also perform certain secondary tasks
at the user interface. One such task is stripping out from the source
program comments and white space in the form of blank, tab, and
new line characters. Another is correlating error messages from the
compiler with the source program. Some times, lexical analyzers
are divided into a cascade of two phases, the first called
“scanning” and the second “lexical analysis”. The scanner is
responsible for doing simple tasks, while the lexical analyzer proper
does the more complex operations.
ISSUES IN LEXICAL ANALYSIS:
1. Simpler design is perhaps the most important
consideration. The separation of lexical analysis from
syntax analysis often allows us to simplify one or the
other of these phases.
2. Compiler efficiency is improved. A separate lexical
analyzer allows us to construct a specialized and
potentially more efficient processor for the task.
3. Compiler portability is enhanced. Input alphabet
peculiarities and other device specific anomalies can be
restricted to the lexical analyzer.
Specialized tools have been designed to help automate the
construction of lexical analyzers and parsers when they are
separated

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

11

PHASE II : PARSING

A compiler determines whether the tokens recognized by the
scanner are a syntactically legal statement. In this compiler model, the
parser obtains a string of tokens from the lexical analyzer and verifies
that the string can be generated by the grammar for the source
language. We expect the parser to report any syntax errors in an
intelligible fashion. It should also recover from commonly occurring
errors so that it can continue processing the remainder of its input.
There are two methods commonly used in compilers are top-down or
bottom-up.
Output of a parser:
A parse tree, if such a tree exists
An error message, if a parse tree cannot be
constructed
Successful construction of a parse tree is proof that the statement
is correctly formed
The methods commonly used in compilers are classified as being
either top-down or bottom – up. As indicated by their names, top-down
parsers build parse trees from the top (root) to the bottom (leaves),
while bottom-up parsers start from the leaves and work up to the root.
In both case, the input to the parser is scanned from left to right, one
symbol at a time.
PHASE III : SEMANTIC ANALYSIS AND CODE GENERATION

Semantic analysis
The compiler makes first pass over parse tree to determine
whether all branches of the tree are semantically valid
If they are valid, the compiler can generate machine language
instructions
If not, there is a semantic error; machine language instructions are
not generated
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

12

PHASE IV: INTERMEDIATE CODE GENERATOR
The front end translates a source program into an intermediate
representation from which the back end generates target code.

PHASE V : CODE OPTIMIZATION
To create an efficient target language program, a programmer
needs more than an optimizing compiler. The best program
transformations are those that yield the most benefit for the least effort.

Criteria for Code-Improving Transformations:
1. A transformation must preserve the meaning of programs.
2. A transformation must, on the average, speed up programs
by a measurable amount. Sometimes we are interested in
reducing the space taken by the compiled code, although the
size of code has less importance than it once had.
3. A transformation must be worth the effort. It does not make
sense for a compiler writer to expend the intellectual effort to
implement a code improving transformation and to have the
compiler expend the additional time compiling source
programs if this effort is not repaid when the target programs
are executed.

PHASE VI: CODE GENERATION
Compiler makes the second pass over the parse tree to produce
the translated code. It takes an input an intermediate representation of
the source program and produces as output as equivalent target
program,. The code generation techniques presented in this chapter can
be used whether or not an optimization phase occurs before code
generation, as in some so called “optimizing” compilers. Such a phase
tries to transform the intermediate code into a form from which more
efficient target code can be produced. We shall talk about code
optimization in detail afterwards.

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

13

The requirements traditionally imposed on a code generator are
severe. The output code must be correct and of high quality, meaning
that it should make effective use of the resources of the target machine.
Moreover, the code generator itself should run efficiently.

ISSUES IN THE DESIGN OF A CODE GENERATOR :
1. Input to the code generator
2. Target program
3. Memory management
4. Instruction selection

INTERMEDIATE
CODE

SOURCE
PROGRAM

FRONT
END

INTERMEDIATE
CODE

CODE
OPTIMIZER

CODE
GENERATOR

TARGET
PROGRAM

SYMBOL
TABLE

FIGURE TO SHOW THE POSITION OF CODE GENERATOR AND THE
CODE OPTIMIZER

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

14

Example to explain the function of different phases :
Consider the statement,
Position := initial + rate * 60

Lexical Analyzer
id1 := id2 + id3 * 60

Syntax Analyzer

:=
id1

id2

+
id3

*

60

:=
id1

S.J.C.I.T.

id2

+
id3

Dept. of MCA

*
Inttoreal
|
60

2005

Semantic Analyzer
NAVEEN KUMAR.T.P

COMPILER DESIGNING

15

Intermediate code generator

Temp1 : = Inttoreal(60)
Temp2 := id3 * temp1
Temp3 : = id2 + temp2
Id1 : = temp3

CODE OPTIMIZER

Temp1 : = id3 * 60.0
Id1 : = id2 + temp1

CODE GENERATOR

S.J.C.I.T.

MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
Dept. of MCA
ADDF R2, R1
MOVF R1, id1

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

16

4. COMPILER DESIGN OPTIONS
In this section we consider some of the possible alternatives
for the design and construction of a compiler. The discussions in
this section are necessarily very brief. Our purpose is to introduce
terms and concepts rather than to give a comprehensive
discussion of any of these topics.
Now we are going to discuss briefly the general question of
dividing a compiler into passes, and consider the advantages of
one-pass and multi-pass designs. The interpreter, which execute an
intermediate form of the program instead of translating it into
machine code. The introduction to P-Code systems, which compile
high-level language programs into object code for a hypothetical
machine. In the last section we are going to discuss the compilerwriting systems, which use software tools to automate much of the
process of compiler construction.
DIVISION INTO PASSES
In this design, the compiler was driven by the parsing process.
The lexical scanner was called when the parser needed another
input token, and a code generation routine was invoked as the
parser recognized each language construct. The object code
produced was not highly efficient. Most of the code optimization
techniques discussed could not be applied in such a one pass
compiler. However, the compilation process itself, which required
only one pass over the program and no intermediate codegeneration step, was quite efficient.
Such a one-pass compiler can translate not all languages. In C
declarations of variables must appear in the program before the
statements that use these variables. However, in some languages
the declaration of an identifier may appear after it has been used
in the program. One-Pass compilers must have the ability to fix up
forward references in jump instructions, using techniques like those
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

17

discussed for one-pass assemblers. Forward references to data
items, however, present a much more serious problem.
Consider, for example, the assignment statement,
x:=y*z
If all of the variables x, y and z are of type integer, the object
code for this statement might consist of a simple integer
multiplication followed by storage of the result. If the variables are
a mixture of real and integer types, one or more conversion
operations will need to be included in the object code, and floating
point arithmetic instructions may be used. Obviously the compiler
cannot decide what machine instructions to generate for this
statement unless information about the operands is available. The
statement may even be illegal for certain combinations of operand
types. Thus a language that allows forward references to data
items cannot be compiled in one pass.
There are a number of factors that should be considered in
deciding between one-pass and multi-pass compiler designs. If
speed of compilation is important a one-pass design might be
preferred. If programs are executed many times for each
compilation, or if they process large amounts of data, then speed
of execution becomes more important than speed of compilation.
In such case, we might prefer a multi-pass compiler design that
could incorporate sophisticated code optimization techniques. Multi
pass compilers are also used when the amount of memory, or other
system resource, is severely limited. The requirement of each pass
can be kept smaller if the work of compilation is divided into
several passes.
Other factors may also influence the design of the compiler. If
a compiler is divided into several passes, each pass becomes
simpler and therefore, easier to understand write and test.
Different passes can be assigned to different programmers and can
be written and tested in parallel, which shortens the overall time
required for compiler construction.
INTERPRETERS
An interpreter processes a source program written in a high
level language, just as a compiler does. The main difference is that
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

18

interpreters execute a version of the source program directly,
instead of translating it into machine code.
An interpreter usually performs lexical and syntactic analysis
functions like those we have described of a compiler, and then
translates the source program into an internal form.
After translating the source program into an internal form, the
interpreter executes the operations specified by the program.
During this phase, an interpreter can be viewed as a set of
subroutines. The internal form of the program drives the execution
of these subroutines.
The process of translating a source program into some
internal form is simpler and faster than compiling it into machine
code. However, execution of the translated program by an
interpreter is much slower than execution of the machine code
produced by a compiler. Thus an interpreter would not normally be
used if speed of execution is important. If speed of translation is of
primary concern and execution of the translated program will be
short, then an interpreter may be a good choice.
The real advantage of an interpreter over a compiler,
however, is in the debugging facilities that can easily be provided.
The interpreter usually retains the symbol table, source line
numbers, and other information from the source program. During
execution, these can be used to produce symbolic dumps of data
values, traces of program execution related to the source
statements, etc. Thus interpreters are especially attractive in an
educational environment where the emphasis is on learning and
program testing.
P-CODE COMPILERS
P-Code compilers (Pseudo code compilers) are very similar in
concept to interpreters. In, both cases, the source program is
analyzed and converted into an intermediate form, which is then
executed interpretively. With a P-Code compiler, however, this
intermediate form is the machine language for a hypothetical
computer, often called as pseudo-machine or P-Machine. The
process of using such a P-Code compiler is illustrated in the figure.
The source program is compiled, with the resulting object program
being in P-Code. This P-Code program is then read and executed
under the control of a P-Code interpreter.
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

19

The main advantage of this approach is portability of
software. It is not necessary for the compiler to generate different
code for different computers, because the P-Code object programs
can be executed on any machine that has a P-Code interpreter.

SOURCE
PROGRAM

P-CODE
COMPILER

COMPILE

OBJECT PROGRAM
(P - CODE)

P-CODE
INTERPRETE
R

EXECUTE

TRANSLATION AND EXECUTION USING A P-CODE COMPILER
The design of a P-Machine and the associated P-Code is often
related to the requirements of the language being compiled. This is
particularly useful on machines with severely limited memory size.
Obviously the interpretive execution of a P-Code program may be
much slower than the execution of the equivalent machine code. If
execution speed is important, some P-Code compilers support the
use of machine language subroutines. By rewriting a small number
of commonly used routines in machine language, rather than PCode, it is often possible to achieve substantial improvements in
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

20

performance. Of course, this approach sacrifices some of the
portability associated with the use of P-Code compilers.

COMPILER – COMPILERS
A compiler-compiler is a software tool that can be used to help
in the task of compiler construction. Such tools are also often
called compiler generators or translator writing systems.
The process of using typical compiler-compilers is illustrated
in the following figure.

SCANNER

LEXICAL
RULES

GRAMMER

COMPILE
R
-COMPILE
RS

SEMANTIC
ROUTINES

PARSER

CODE
GENERATOR

FIG : AUTOMATED COMPILER CONSTRUCTION USING
COMPILER – COMPILERS
The user (i.e. the compiler writer) provides a description of the
language to be translated. This description may consist of a set of
lexical rules for defining tokens and a grammar for the source
language. Some compiler-compilers use this information to
generate a scanner and a parser directly. Others create tables for
use by standard table driven scanning and parsing routines that
are supplied by the compiler-compilers.
In addition to the description of the source language, the user
provides a set of semantic or code-generation routines. Often there
is one such routine for each rule of the grammar.
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

21

The main advantage of using compiler-compilers is, of course,
easing of compiler construction and testing. The amount of work
required from the user varies considerably from one compilercompiler to another depending upon the degree of flexibility
provided. YACC is an example for a compiler-compiler.

5. COMPILER CONSTRUCTON TOOLS
The following are the list of some useful compiler – construction
tools.
1. Parser

Generator: These produce syntax analyzers,

normally from input that is based on a context-free
grammar.
2. Scanner Generator: These automatically generate lexical
analyzers, normally from a specification based on regular
expressions.
3. Syntax-directed translation engines: These produce
collections of routines that walk the parse tree generating
intermediate code.
4. Automatic Code generators: Such a tools takes a
collection of

rules that define the translation of each

operation of the intermediate language into the machine
language for the target machine.
5. Data-flow Engines: Much of the information needed to
perform

good

code

optimization

involves

“data-flow

analysis” the gathering of information about how values are
transmitted from one part of a program to each other part.

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

22

6. CONCLUSION
Finally we can conclude our discussion. In

this

session

we

discussed the following things.
First, we discussed what a compiler is and how actually it
works. Then we discussed the different phases of the compiler. I.e.
the compiler has the four phases. Next we discussed about the
compilation process. In this stage we had seen how actually using
the different phases can do the compilation process. After this we
discussed the different options that are available for the compiler
designing. I.e. dividing into parses, P-Code compiler. At last we
discussed about the different types of the compiler generating
tools that are available to the programmers.

S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

23

7. BIBLIOGRAPHY
REFERENCE:
1. COMPILERS PRINCIPLES, TECHNIQUES, AND TOOLS
(ALFRED V.AHO, RAVI SETHI, JEFFREY D.ULLMAN)
2. SYSTEM SOFTWARE
(LELAND L.BECK)
3. PC-QUEST MAXINE
WEB SITES:

www.cse.spsu.edu
www.cs.tau.ac.in
www.modern-compiler-design.com
www.advanced-compiler-design.com
www.pearsoned.co.in
S.J.C.I.T.

Dept. of MCA

2005

NAVEEN KUMAR.T.P

COMPILER DESIGNING

24

S.J.C.I.T.

Dept. of MCA

2005

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close