SlideShare a Scribd company logo
1 of 34
PROS AND CONS OF C AS A COMPILER LANGUAGE
AGENDA
● LANGUAGE PROCESSING SYSTEM
● WHAT IS A COMPILER ?
● WHY USE A COMPILER ?
● TYPES OF A COMPILER
● COMPILER DESIGN
● PROS AND CONS
LANGUAGE PROCESSING SYSTEM :
⮚ Computer system is made of hardware and software .
⮚ The hardware understands instructions in the form of electronic charge or
binary language in Software programming.
⮚ So the programs written in High Level Language are fed into a series of
tools and OS components to get the desired machine language.
⮚ This is known as Language Processing System.
PRE-PROCESSOR
● A special system software
● Performs preprocessing of High level language
● First step in language processing system
● Preprocessor mainly performs these tasks
i) Removing comments
ii) File inclusion
iii) Macro expansion.
ASSEMBLER
● Assembly language neither in binary form nor high level. It is an intermediate
state that is a combination of machine instructions and some other useful
data needed for execution.
● Assembler is a program that converts assembly language into machine code.
● Output of an assembler is an object file.
● Object file is a combination of machine instructions and the data required to
place these instructions in memory.
● Relocatable machine code can be loaded at any point and can be run. The
address within the program will be in such a way that it will cooperate for the
program movement.
LINKER
● Linker is a computer utility program that takes one or more object files
generated by the compiler and combines them into an single executable
object file.
● Linking is performed at both compile time and load time.
● It is performed at last step in compiling a program.
● Linking is of two types:
i) Static Linking
ii) Dynamic Linking
LOADER
● Loader is a part of a OS responsible for loading executable files into memory
and executes them by calculating the size of the program and creates
memory space for it.
● It initializes various registers to initiate execution.
● It converts the relocatable code into absolute code and tries to run the
program resulting in a running program or an error message (or sometimes
both can happen).
WHAT IS A COMPILER
● A compiler is a software program that transforms high-level source code that
is written by a developer in a high-level programming language into a low
level object code (binary code) in machine language, which is understood by
the processor.
● The process of converting high-level programming into machine language is
known as compilation.
● A compiler operates as a sequence of phases each of which transforms the
source program from one intermediate representation to another.
WHY USE A COMPILER?
● Verifies the entire program, so there are no syntax or semantic errors.
● The executable file is optimized by compiler, so it executes faster.
● Allows us to create an internal structure in memory.
● Allows the program to be machine independent if required(ie : no need to
execute the program on the same machine it was built).
● Helps in better understanding of language semantics and to handle language
performance issues.
● Translates entire program in other language and helps in checking for syntax
errors and data types .
Compiler Interpreter
Compiler generates an Intermediate Code. Interpreter generates Machine Code.
Compiler reads entire program for compilation.
Interpreter reads single statement at a time for
interpretation.
Compiler displays all errors and warning at time and
without fixing all errors program cannot be executed.
Since Interpreter reads single statement so an
interpreter display one error at a time and you have to
fix the error to interpret next statement.
Compiler needs more memory because of object (an
intermediate code) generation, every time when
program is being compiled an intermediate code
(object code) will be generated.
An Interpreter needs less memory to interpret the
program as interpreter does not generate any
intermediate code, it direct generates machine code.
Programming language like C, C++ use compilers.
Programming language like Python, Ruby use
interpreters.
Types of Compiler
• Native code compiler
• Cross compiler
• Source to source compiler
• One pass compiler
• Threaded code compiler
• Incremental compiler
• Source compiler
• Native code compiler:
• The compiler used to compile a source code for same type of platform only.
• The output generated by this type of compiler can only be run on the same type
of computer system and OS that the compiler itself runs on.
• Cross compiler:
• The compiler used to compile a source code for different kinds platform.
• Used in making software’s for embedded systems that can be used on multiple
platforms.
• Source to source compiler:
• Transcompiler or Transpier is a type of compiler that takes the source code of a
program written in one programming language as its input and produces the
equivalent source code in another programming language.
• One pass compiler:
• It is a type of compiler that compiles the whole process in only
one-pass.
• Threaded code compiler
• The compiler which simply replace a string by an appropriate binary
code.
• Incremental compiler:
• The compiler which compiles only the changed lines from the source
code and update the object code.
• Source compiler:
• The compiler which converts the source code high level language
code in to assembly language only.
STRUCTURE OF A COMPLIER
Two Phrase Complier
• Earlier, we depicted a compiler as a simple box that translates a source program into a target
program
• As the single-box model suggests, a compiler must both understand the source program that it
takes as input and map its functionality to the target machine.
• A design that decomposes compilation into two major pieces: a front end and a back end.
• The front end focuses on understanding the source-language program. The back end focuses on
mapping programs to the target machine.
• Intermediate representation (IR) becomes the compiler’s definitive representation for the code it
is translating
Three Phrase Complier
• The compiler writer can insert a third phase between the front end and the back end
• This middle section, or optimizer, takes an IR program as its input and produces a
semantically equivalent IR program as its output.
• This leads to the following compiler structure, termed a three-phase compiler.
• The optimizer may rewrite the IR in a way that is likely to produce a faster target program
from the back end or a smaller target program from the back end
• It may have other objectives, such as a program that produces fewer page faults or uses less
energy.
PHASES OF A COMPILER
• Any compiler must perform two major tasks
• Analysis of the source program
An intermediate representation is created from the give source code
• Synthesis of a machine-language program
Equivalent target program is created from the intermediate
representation.
• Lexical analysis is the first phase of compiler which is also termed as
scanning. This phase scans the source code as a stream of characters and
converts it into lexes or tokens
• Each token will have <token-name, attribute values>
• It deletes the blank spaces and comments. Once a token is generated the
corresponding entry is made in the symbol table.
• Input: stream of characters
• Output: Token
• Example:
c=a+b*5;
<id, 1> <=> < id, 2> < +> <id, 3 > < * > < 5>
Lexical Analysis
Syntax Analysis
• Syntax analysis is the second phase of compiler which is also called as parsing.
• Parser converts the tokens produced by lexical analyser into a tree like
representation called parse tree.
• A parse tree describes the syntactic structure of the input.
• It follows operator precedence. The root node will have the operators and the
child nodes will have operands.
• Input: Tokens
• Output: Syntax tree
Semantic Analysis
• It checks for the semantic consistency.
• Type information is gathered and stored in symbol table or in syntax
tree.
• Performs type checking.
Intermediate Code Generation
• Compiler generates an intermediate code of the source code for the
target machine.
• It generates abstract code. 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.
• It uses three address code and used some temporary variables.
t1 = int to float <5>
t2 = <id,3>* t1
t3 = <id,2> + t2
<id,1> = t3
Code Optimization
• Code optimization phase produces optimized intermediate code as output. It results in
faster running machine code.
• It can be done by reducing the number of lines of code for a program.
• During the code optimization, the result of the program is not affected.
• To improve the code generation, the optimization involves
⮚ Deduction and removal of dead code (unreachable code).
⮚ Calculation of constants in expressions and terms.
⮚ Collapsing of repeated expression into temporary string.
⮚ Moving code outside the loop.
⮚ Removal of unwanted temporary variables.
t1 = <id,3>* <5.0>
<id,1>= <id,2> + t1
Code Generation
• It gets input from code optimization phase and produces the target code or object code as result.
• Intermediate instructions are translated into a sequence of machine instructions that perform
the same task.
• The code generation involves
o Allocation of register and memory.
o Generation of correct references.
o Generation of correct data type.
o Generation of missing code.
MOV R1,<id,3>
MUL R1, #5.0
MOV R2<id,2>
ADD R1,R2
MOV <id,1>,R
Symbol Table Management
• The symbol table is a data structure containing a record of each variable name with fields
for the attributes of the name.
• The data structure should be designed to allow the complier to find the record for each
name quickly and to store or retrieve data from that record quickly.
• It is built in lexical and syntax analysis phases.
• The information is collected by the analysis phases of compiler and is used by synthesis
phases of compiler to generate code.
• It is used by compiler to achieve compile time efficiency.
Example
sum=old_sum+rate*50
<id,1>=<id,2>+<id,3><*><50>
Lexical
Analyzer
=
<id,1> +
<id,2> *
<id,3> 50
Syntax Analyzer
=
<id,1> +
<id,2> *
<id,3> inttofloat 50.0
Semantic
Analyzer
t1:=inttofloat 50.0
t2:=<id,3>*t1
t3:=<id,2>+t2
<id,1>:=t3
t1:=<id,3>*50.0
id1:=<id,2>+t1
Intermediate Code
Generator
Code Optimization
MOV R1 , <id,3>
MUL R1 , #50.0
ADD R1 , <id,2>
MOV <id,1> , R1
Code Generation
Pros and Cons of C for compiler
development
• Compiler uses methods like lexical analyzer which converts parsed
data into an executable binary code which when developed C could
construct a specialized and potentially more efficient processor for
the task.
• But additional runtime overhead is required to generate and debug
lexer table ,tokens.
• Many lexical tool applications are developed using C language.
Ex:lex
• In syntax analysis all types of syntax errors and position at which it
has occurred will be found
• The main feature of C is a simple set of keywords ,syntax and a clean
style which makes it suitable for this phase.
• The main drawback in syntax analysis phase is cannot determine
whether a token is valid or not.
• Code optimization is an approach for enhancing the performance of
the code when developed using C the compiler optimizes the code
for faster execution.
• C compiler produces the machine code very fast compared to other
language compilers.
• C compiler can compile around 1000 lines of code in a second or two.
• Low level access to memory.
• Tail call optimization is not supported.It is a method to avoid new
stack frame for a function because the calling function will simply
return a value that it gets from the called function.
• C language does not have an automatic garbage collection so code
optimization may have a little lack due to this.
• Efficient exception handling is not possible in C.
• THANK YOU

More Related Content

What's hot

Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
 
Introduction to C Language
Introduction to C LanguageIntroduction to C Language
Introduction to C Language
Kamal Acharya
 
Programming Languages
Programming LanguagesProgramming Languages
Programming Languages
Mohamed Omar
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
kelleyc3
 

What's hot (20)

C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
 
Life cycle of a computer program
Life cycle of a computer programLife cycle of a computer program
Life cycle of a computer program
 
Compilers
CompilersCompilers
Compilers
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Types of Programming Languages
Types of Programming LanguagesTypes of Programming Languages
Types of Programming Languages
 
System Programming Overview
System Programming OverviewSystem Programming Overview
System Programming Overview
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Toc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationToc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computation
 
C data type format specifier
C data type format specifierC data type format specifier
C data type format specifier
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 
Introduction to C Language
Introduction to C LanguageIntroduction to C Language
Introduction to C Language
 
Programming Languages
Programming LanguagesProgramming Languages
Programming Languages
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Case statements
Case statementsCase statements
Case statements
 
Type conversion
Type  conversionType  conversion
Type conversion
 
Design of a two pass assembler
Design of a two pass assemblerDesign of a two pass assembler
Design of a two pass assembler
 

Similar to Pros and cons of c as a compiler language

Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
انشال عارف
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 

Similar to Pros and cons of c as a compiler language (20)

Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
Chapter1.pdf
Chapter1.pdfChapter1.pdf
Chapter1.pdf
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course Material
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 

More from Ashok Raj

More from Ashok Raj (11)

Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
 
How c++ stored in ram
How c++ stored in ramHow c++ stored in ram
How c++ stored in ram
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
High Performance Computer
High Performance ComputerHigh Performance Computer
High Performance Computer
 
Super computers
Super computersSuper computers
Super computers
 
Programming language paradigms
Programming language paradigmsProgramming language paradigms
Programming language paradigms
 
Printers and its types
Printers and its typesPrinters and its types
Printers and its types
 
Microprocessor
MicroprocessorMicroprocessor
Microprocessor
 
Mother board
Mother boardMother board
Mother board
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
FULL stack -> MEAN stack
FULL stack -> MEAN stackFULL stack -> MEAN stack
FULL stack -> MEAN stack
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Recently uploaded (20)

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Pros and cons of c as a compiler language

  • 1. PROS AND CONS OF C AS A COMPILER LANGUAGE
  • 2. AGENDA ● LANGUAGE PROCESSING SYSTEM ● WHAT IS A COMPILER ? ● WHY USE A COMPILER ? ● TYPES OF A COMPILER ● COMPILER DESIGN ● PROS AND CONS
  • 3. LANGUAGE PROCESSING SYSTEM : ⮚ Computer system is made of hardware and software . ⮚ The hardware understands instructions in the form of electronic charge or binary language in Software programming. ⮚ So the programs written in High Level Language are fed into a series of tools and OS components to get the desired machine language. ⮚ This is known as Language Processing System.
  • 4.
  • 5. PRE-PROCESSOR ● A special system software ● Performs preprocessing of High level language ● First step in language processing system ● Preprocessor mainly performs these tasks i) Removing comments ii) File inclusion iii) Macro expansion.
  • 6. ASSEMBLER ● Assembly language neither in binary form nor high level. It is an intermediate state that is a combination of machine instructions and some other useful data needed for execution. ● Assembler is a program that converts assembly language into machine code. ● Output of an assembler is an object file. ● Object file is a combination of machine instructions and the data required to place these instructions in memory. ● Relocatable machine code can be loaded at any point and can be run. The address within the program will be in such a way that it will cooperate for the program movement.
  • 7. LINKER ● Linker is a computer utility program that takes one or more object files generated by the compiler and combines them into an single executable object file. ● Linking is performed at both compile time and load time. ● It is performed at last step in compiling a program. ● Linking is of two types: i) Static Linking ii) Dynamic Linking
  • 8. LOADER ● Loader is a part of a OS responsible for loading executable files into memory and executes them by calculating the size of the program and creates memory space for it. ● It initializes various registers to initiate execution. ● It converts the relocatable code into absolute code and tries to run the program resulting in a running program or an error message (or sometimes both can happen).
  • 9. WHAT IS A COMPILER ● A compiler is a software program that transforms high-level source code that is written by a developer in a high-level programming language into a low level object code (binary code) in machine language, which is understood by the processor. ● The process of converting high-level programming into machine language is known as compilation. ● A compiler operates as a sequence of phases each of which transforms the source program from one intermediate representation to another.
  • 10. WHY USE A COMPILER? ● Verifies the entire program, so there are no syntax or semantic errors. ● The executable file is optimized by compiler, so it executes faster. ● Allows us to create an internal structure in memory. ● Allows the program to be machine independent if required(ie : no need to execute the program on the same machine it was built). ● Helps in better understanding of language semantics and to handle language performance issues. ● Translates entire program in other language and helps in checking for syntax errors and data types .
  • 11. Compiler Interpreter Compiler generates an Intermediate Code. Interpreter generates Machine Code. Compiler reads entire program for compilation. Interpreter reads single statement at a time for interpretation. Compiler displays all errors and warning at time and without fixing all errors program cannot be executed. Since Interpreter reads single statement so an interpreter display one error at a time and you have to fix the error to interpret next statement. Compiler needs more memory because of object (an intermediate code) generation, every time when program is being compiled an intermediate code (object code) will be generated. An Interpreter needs less memory to interpret the program as interpreter does not generate any intermediate code, it direct generates machine code. Programming language like C, C++ use compilers. Programming language like Python, Ruby use interpreters.
  • 12. Types of Compiler • Native code compiler • Cross compiler • Source to source compiler • One pass compiler • Threaded code compiler • Incremental compiler • Source compiler
  • 13. • Native code compiler: • The compiler used to compile a source code for same type of platform only. • The output generated by this type of compiler can only be run on the same type of computer system and OS that the compiler itself runs on. • Cross compiler: • The compiler used to compile a source code for different kinds platform. • Used in making software’s for embedded systems that can be used on multiple platforms. • Source to source compiler: • Transcompiler or Transpier is a type of compiler that takes the source code of a program written in one programming language as its input and produces the equivalent source code in another programming language.
  • 14. • One pass compiler: • It is a type of compiler that compiles the whole process in only one-pass. • Threaded code compiler • The compiler which simply replace a string by an appropriate binary code. • Incremental compiler: • The compiler which compiles only the changed lines from the source code and update the object code. • Source compiler: • The compiler which converts the source code high level language code in to assembly language only.
  • 15. STRUCTURE OF A COMPLIER Two Phrase Complier • Earlier, we depicted a compiler as a simple box that translates a source program into a target program • As the single-box model suggests, a compiler must both understand the source program that it takes as input and map its functionality to the target machine. • A design that decomposes compilation into two major pieces: a front end and a back end. • The front end focuses on understanding the source-language program. The back end focuses on mapping programs to the target machine. • Intermediate representation (IR) becomes the compiler’s definitive representation for the code it is translating
  • 16. Three Phrase Complier • The compiler writer can insert a third phase between the front end and the back end • This middle section, or optimizer, takes an IR program as its input and produces a semantically equivalent IR program as its output. • This leads to the following compiler structure, termed a three-phase compiler. • The optimizer may rewrite the IR in a way that is likely to produce a faster target program from the back end or a smaller target program from the back end • It may have other objectives, such as a program that produces fewer page faults or uses less energy.
  • 17. PHASES OF A COMPILER • Any compiler must perform two major tasks • Analysis of the source program An intermediate representation is created from the give source code • Synthesis of a machine-language program Equivalent target program is created from the intermediate representation.
  • 18.
  • 19. • Lexical analysis is the first phase of compiler which is also termed as scanning. This phase scans the source code as a stream of characters and converts it into lexes or tokens • Each token will have <token-name, attribute values> • It deletes the blank spaces and comments. Once a token is generated the corresponding entry is made in the symbol table. • Input: stream of characters • Output: Token • Example: c=a+b*5; <id, 1> <=> < id, 2> < +> <id, 3 > < * > < 5> Lexical Analysis
  • 20. Syntax Analysis • Syntax analysis is the second phase of compiler which is also called as parsing. • Parser converts the tokens produced by lexical analyser into a tree like representation called parse tree. • A parse tree describes the syntactic structure of the input. • It follows operator precedence. The root node will have the operators and the child nodes will have operands. • Input: Tokens • Output: Syntax tree
  • 21. Semantic Analysis • It checks for the semantic consistency. • Type information is gathered and stored in symbol table or in syntax tree. • Performs type checking.
  • 22. Intermediate Code Generation • Compiler generates an intermediate code of the source code for the target machine. • It generates abstract code. 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. • It uses three address code and used some temporary variables. t1 = int to float <5> t2 = <id,3>* t1 t3 = <id,2> + t2 <id,1> = t3
  • 23. Code Optimization • Code optimization phase produces optimized intermediate code as output. It results in faster running machine code. • It can be done by reducing the number of lines of code for a program. • During the code optimization, the result of the program is not affected. • To improve the code generation, the optimization involves ⮚ Deduction and removal of dead code (unreachable code). ⮚ Calculation of constants in expressions and terms. ⮚ Collapsing of repeated expression into temporary string. ⮚ Moving code outside the loop. ⮚ Removal of unwanted temporary variables. t1 = <id,3>* <5.0> <id,1>= <id,2> + t1
  • 24. Code Generation • It gets input from code optimization phase and produces the target code or object code as result. • Intermediate instructions are translated into a sequence of machine instructions that perform the same task. • The code generation involves o Allocation of register and memory. o Generation of correct references. o Generation of correct data type. o Generation of missing code. MOV R1,<id,3> MUL R1, #5.0 MOV R2<id,2> ADD R1,R2 MOV <id,1>,R
  • 25. Symbol Table Management • The symbol table is a data structure containing a record of each variable name with fields for the attributes of the name. • The data structure should be designed to allow the complier to find the record for each name quickly and to store or retrieve data from that record quickly. • It is built in lexical and syntax analysis phases. • The information is collected by the analysis phases of compiler and is used by synthesis phases of compiler to generate code. • It is used by compiler to achieve compile time efficiency.
  • 27. = <id,1> + <id,2> * <id,3> 50 Syntax Analyzer
  • 28. = <id,1> + <id,2> * <id,3> inttofloat 50.0 Semantic Analyzer
  • 30. MOV R1 , <id,3> MUL R1 , #50.0 ADD R1 , <id,2> MOV <id,1> , R1 Code Generation
  • 31. Pros and Cons of C for compiler development • Compiler uses methods like lexical analyzer which converts parsed data into an executable binary code which when developed C could construct a specialized and potentially more efficient processor for the task. • But additional runtime overhead is required to generate and debug lexer table ,tokens. • Many lexical tool applications are developed using C language. Ex:lex
  • 32. • In syntax analysis all types of syntax errors and position at which it has occurred will be found • The main feature of C is a simple set of keywords ,syntax and a clean style which makes it suitable for this phase. • The main drawback in syntax analysis phase is cannot determine whether a token is valid or not. • Code optimization is an approach for enhancing the performance of the code when developed using C the compiler optimizes the code for faster execution. • C compiler produces the machine code very fast compared to other language compilers.
  • 33. • C compiler can compile around 1000 lines of code in a second or two. • Low level access to memory. • Tail call optimization is not supported.It is a method to avoid new stack frame for a function because the calling function will simply return a value that it gets from the called function. • C language does not have an automatic garbage collection so code optimization may have a little lack due to this. • Efficient exception handling is not possible in C.