Compiler Design
(CSE-405-F)
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Programming….Why?
What is a Compiler?
• A compiler is a computer program (or set of
programs) that transforms source code written in a
high level language (the source language) into low
level language (the target language may be assembly
language or machine language (0,1)).
What is a Compiler?
What is a Compiler?
Source Language Target Language
Introduction
Compiler basically does two things:
• Analysis: Compiling source code & detecting
errors in it.
• Synthesis: Translating the source code into
object code depending upon the type of
machine.
Introduction
• Program errors are difficult to track, if the
compiler is wrongly designed.
• Hence, compiler construction is a very
tedious and long process.
One-pass Compiler
• It compiles the that passes through the source
code of each compilation unit only once.
• They are faster than multi-pass compiler.
• Their efficiency is limited because they don’t
produce intermediate codes which can be
refined easily.
• Also known as ‘Narrow Compiler’.
Multi-pass Compiler
• It processes the source code or abstract syntax
tree of a program several times.
• It may create one or more intermediate codes
(easy to refine).
• Each pass takes output of previous phase as
input; hence requires less memory.
• Also known as ‘Wide Compiler’.
Type of Languages
1. Binary Level
Language
2. Assembly Level
Language
3. High Level
Language
Need for programming languages?
• Closer to human thinking behavior.
• Program’s size is shorter.
• Error detection is easy.
• Same program can be compiled in accordance
to different machine architectures.
History
• Software for early computers were
written in assembly language.
• The need of reusability of code gave
birth to programming languages.
• This need grow huge to overcome the
cost restriction of compiler.
History
• The concept of machine independent
programming gave birth to the need of
compilers in 1950s.
• The 1st compiler was written by
Grace Hopper in 1952 for A-0
programming language.
History
• The 1st complete compiler was developed by
FORTRAN team lead by John Backus @ IBM in
1957.
• COBOL was the 1st language to be compiled on
multiple platforms in 1960.
History
• Earlier compilers were written in
assembly languages.
• The 1st compiler in HLL was
created for LISP by Tim Hart &
Mike Levin @ MIT, USA in 1962,
which was a self-hosting
Compiler.
History
• Most compilers are made in C or Pascal
languages.
• However the trend is changing to self-hosting
compilers, which can compile the source code of
the same language in which they are created.
Why Study Compiler?
• Its essential to understand the
heart of programming by
computer science engineering
students.
• There may be need of
designing a compiler for any
software language in the
profession.
Why Study Compiler?
• Increases understanding of
language semantics.
• Helps to handle language
performance issues.
• Opportunity for non-trivial
programming project
Translators
• It is important to understand that compiler is a
kind of translator, which translates high level
language (human understandable) to low-level
language(machine understandable).
Translators
Translators - Preprocessor
• It 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.
Translators - Preprocessor
• The amount and kind of processing done depends on
the nature of the preprocessor.
• some preprocessors are only capable of performing
simple textual substitutions and macro expansions,
while others have the power of full-
fledged programming languages.
• It can also include macro processing, file inclusion and
language extensions.
Translators - Interpreter
• It is another common kind of language processor.
• Instead of producing a target program as a
translation, it directly execute the operations
specified in the source program on inputs supplied
by the user.
Translators - Interpreter
• This software converts the high-level language
into low-level language line by line.
• It takes less memory than compiler.
• It takes more time than compiler.
• It is more efficient in error detection than
compiler.
• It takes more time to build.
Translators - Interpreter
• Java language processors combine compilation
and interpretation.
• A Java source program may first be compiled into
an intermediate form called bytecodes.
• The bytecodes are then interpreted by a virtual
machine.
• A benefit of this arrangement is that bytecodes
compiled on one machine can be interpreted on
another machine, perhaps across a network.
Translators - Interpreter
Translators - Assembler
• This software converts the assembly language
(assembly instruction mnemonics) into machine
level language (opcodes i.e. 0,1).
• It offers reusability of assembly codes on different
machine platforms.
Translators - Assembler
• It generates instructions by evaluating the
mnemonics (symbols) in operation field and find
the value of symbol and literals to produce
machine code.
• Now, if assembler do all this work in one scan then
it is called single pass assembler, otherwise if it
does in multiple scans then called multiple pass
assembler.
Translators - Assembler
• Pass-1:
– Define symbols and literals and remember them in
symbol table and literal table respectively.
– Keep track of location counter
– Process pseudo-operations
• Pass-2:
– Generate object code by converting symbolic op-code
into respective numeric op-code
– Generate data for literals and look for values of
symbols
Translators - Assembler
Translators - Linker
• A linker is a program that allows a user
to link library programs or separate
modules of code into their own
programs.
• It is used to combine different modules
of object code into one single
executable code program.
Translators - Linker
• This may involve combining a program with library
programs, or involve recombining blocks of object
code from the same program, or a mixture of both.
• The linker program is used to recombine the
blocks of object code in RAM to get a working full
program.
• Linker is helpful if there is a shortage of RAM.
Translators - Loader
• A loader is a piece of software that chooses
exactly where to put object code in RAM, ready for
it to be run.
• It also adjusts the memory references in programs.
Translators - Loader
• Programs can be written by programmers using
either ‘absolute addressing’ or ‘relative addressing’.
• Relative addressing is more common because then
the loader can put the program anywhere in RAM -
absolute addressing isn't flexible.
• Linker & Loader may overlap.
Translators - Loader
Translators - Cross-Compiler
• If the compiled program can run on a computer
whose C.P.U or O.S is different from the one on
which the compiler runs, the compiler is known
as a cross-complier.
Translators - Cross-Compiler
Translator – Language Converter
• Also called Language Translator / Source to
source translator.
• It converts programs in one high-level language to
another high-level language.
• Ex: Cfront converts C++ into C language.
Translator – Language Rewriter
• It is a program that changes
form of expression of the same
language.
• It does not changes the
language of source code.
Translator - Decompiler
• It is a piece of software that converts the low-level
language to high-level language .
• It is not as famous, but may prove a useful tool
sometimes.
• Ex: JD Compiler, .JAD decompiler, NET Reflector
Translator – Compiler-Compiler
• It is a tool that creates a compiler, interpreter or
parser from the information provided on formal
description of any language.
• The earliest & most common type is parser-
generator.
• It is also called compiler-generator.
• Ex: Yacc, JavaCC, etc.
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
Thank You !!!

Introduction to Compilers

  • 1.
  • 2.
  • 3.
    What is aCompiler? • A compiler is a computer program (or set of programs) that transforms source code written in a high level language (the source language) into low level language (the target language may be assembly language or machine language (0,1)).
  • 4.
    What is aCompiler?
  • 5.
    What is aCompiler? Source Language Target Language
  • 6.
    Introduction Compiler basically doestwo things: • Analysis: Compiling source code & detecting errors in it. • Synthesis: Translating the source code into object code depending upon the type of machine.
  • 7.
    Introduction • Program errorsare difficult to track, if the compiler is wrongly designed. • Hence, compiler construction is a very tedious and long process.
  • 8.
    One-pass Compiler • Itcompiles the that passes through the source code of each compilation unit only once. • They are faster than multi-pass compiler. • Their efficiency is limited because they don’t produce intermediate codes which can be refined easily. • Also known as ‘Narrow Compiler’.
  • 9.
    Multi-pass Compiler • Itprocesses the source code or abstract syntax tree of a program several times. • It may create one or more intermediate codes (easy to refine). • Each pass takes output of previous phase as input; hence requires less memory. • Also known as ‘Wide Compiler’.
  • 10.
    Type of Languages 1.Binary Level Language 2. Assembly Level Language 3. High Level Language
  • 11.
    Need for programminglanguages? • Closer to human thinking behavior. • Program’s size is shorter. • Error detection is easy. • Same program can be compiled in accordance to different machine architectures.
  • 12.
    History • Software forearly computers were written in assembly language. • The need of reusability of code gave birth to programming languages. • This need grow huge to overcome the cost restriction of compiler.
  • 13.
    History • The conceptof machine independent programming gave birth to the need of compilers in 1950s. • The 1st compiler was written by Grace Hopper in 1952 for A-0 programming language.
  • 14.
    History • The 1stcomplete compiler was developed by FORTRAN team lead by John Backus @ IBM in 1957. • COBOL was the 1st language to be compiled on multiple platforms in 1960.
  • 15.
    History • Earlier compilerswere written in assembly languages. • The 1st compiler in HLL was created for LISP by Tim Hart & Mike Levin @ MIT, USA in 1962, which was a self-hosting Compiler.
  • 16.
    History • Most compilersare made in C or Pascal languages. • However the trend is changing to self-hosting compilers, which can compile the source code of the same language in which they are created.
  • 17.
    Why Study Compiler? •Its essential to understand the heart of programming by computer science engineering students. • There may be need of designing a compiler for any software language in the profession.
  • 18.
    Why Study Compiler? •Increases understanding of language semantics. • Helps to handle language performance issues. • Opportunity for non-trivial programming project
  • 19.
    Translators • It isimportant to understand that compiler is a kind of translator, which translates high level language (human understandable) to low-level language(machine understandable).
  • 20.
  • 21.
    Translators - Preprocessor •It 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.
  • 22.
    Translators - Preprocessor •The amount and kind of processing done depends on the nature of the preprocessor. • some preprocessors are only capable of performing simple textual substitutions and macro expansions, while others have the power of full- fledged programming languages. • It can also include macro processing, file inclusion and language extensions.
  • 23.
    Translators - Interpreter •It is another common kind of language processor. • Instead of producing a target program as a translation, it directly execute the operations specified in the source program on inputs supplied by the user.
  • 24.
    Translators - Interpreter •This software converts the high-level language into low-level language line by line. • It takes less memory than compiler. • It takes more time than compiler. • It is more efficient in error detection than compiler. • It takes more time to build.
  • 25.
    Translators - Interpreter •Java language processors combine compilation and interpretation. • A Java source program may first be compiled into an intermediate form called bytecodes. • The bytecodes are then interpreted by a virtual machine. • A benefit of this arrangement is that bytecodes compiled on one machine can be interpreted on another machine, perhaps across a network.
  • 26.
  • 27.
    Translators - Assembler •This software converts the assembly language (assembly instruction mnemonics) into machine level language (opcodes i.e. 0,1). • It offers reusability of assembly codes on different machine platforms.
  • 28.
    Translators - Assembler •It generates instructions by evaluating the mnemonics (symbols) in operation field and find the value of symbol and literals to produce machine code. • Now, if assembler do all this work in one scan then it is called single pass assembler, otherwise if it does in multiple scans then called multiple pass assembler.
  • 29.
    Translators - Assembler •Pass-1: – Define symbols and literals and remember them in symbol table and literal table respectively. – Keep track of location counter – Process pseudo-operations • Pass-2: – Generate object code by converting symbolic op-code into respective numeric op-code – Generate data for literals and look for values of symbols
  • 30.
  • 31.
    Translators - Linker •A linker is a program that allows a user to link library programs or separate modules of code into their own programs. • It is used to combine different modules of object code into one single executable code program.
  • 32.
    Translators - Linker •This may involve combining a program with library programs, or involve recombining blocks of object code from the same program, or a mixture of both. • The linker program is used to recombine the blocks of object code in RAM to get a working full program. • Linker is helpful if there is a shortage of RAM.
  • 33.
    Translators - Loader •A loader is a piece of software that chooses exactly where to put object code in RAM, ready for it to be run. • It also adjusts the memory references in programs.
  • 34.
    Translators - Loader •Programs can be written by programmers using either ‘absolute addressing’ or ‘relative addressing’. • Relative addressing is more common because then the loader can put the program anywhere in RAM - absolute addressing isn't flexible. • Linker & Loader may overlap.
  • 35.
  • 36.
    Translators - Cross-Compiler •If the compiled program can run on a computer whose C.P.U or O.S is different from the one on which the compiler runs, the compiler is known as a cross-complier.
  • 37.
  • 38.
    Translator – LanguageConverter • Also called Language Translator / Source to source translator. • It converts programs in one high-level language to another high-level language. • Ex: Cfront converts C++ into C language.
  • 39.
    Translator – LanguageRewriter • It is a program that changes form of expression of the same language. • It does not changes the language of source code.
  • 40.
    Translator - Decompiler •It is a piece of software that converts the low-level language to high-level language . • It is not as famous, but may prove a useful tool sometimes. • Ex: JD Compiler, .JAD decompiler, NET Reflector
  • 41.
    Translator – Compiler-Compiler •It is a tool that creates a compiler, interpreter or parser from the information provided on formal description of any language. • The earliest & most common type is parser- generator. • It is also called compiler-generator. • Ex: Yacc, JavaCC, etc.
  • 42.
    CONTACT ME AT: AkhilKaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 Thank You !!!