SlideShare une entreprise Scribd logo
1  sur  26
Harcourt butler technical University Kanpur
 A compiler is a program that reads a program
written in one language and translates into
equivalent target language
Source- - Target
|
|
|
Error Message
COMPILER
 The Front end checks whether the
program is correctly written in terms of the
programming language syntax and
semantics
 The back end is responsible for translating
the source into assembly code.
Front End :
Lexical Analysis
Preprocessing
Syntax Analysis
Semantic Analysis
Back End
 Analysis
 Optimization
 Code generation
 Lexical Analyzer
 Syntax Analyzer
 Semantic Analyzer
 Intermediate code generator
 Code optimizer
 Code generator
Lexical
Syntax
Semantic
Code Generator
Code Optimizer
Intermediate Code Generator
Symbol
Table
manager
Error
Handler
 Also called Linear Analysis
 Characters read from left to right and
grouped into tokens that are a sequence of
characters with a collective meaning
Scans Input
Removes White spaces and comments
Manufacture Tokens
Generate Error if Any
◦ Example
◦ A=B+C
◦ Variable tokens - A ,B, C
◦ Symbolic token -- = +
11
Lex(Flex in recent implementation)
12
 The main job of a lexical analyzer (scanner) is
to break up an input stream into
tokens(tokenize input streams).
 Ex :-
a = b + c * d;
ID ASSIGN ID PLUS ID MULT ID SEMI
 Lex is an utility to help you rapidly generate
your scanners
PLLab, NTHU,Cs2403 Programming
Languages 13
(optional)
(required)
 Lex source is separated into three sections
by %% delimiters
 The general format of Lex source is
The absolute minimum Lex program is thus
{definitions}
%%
{transition rules}
%%
{user Code}
%%
 Declarations of ordinary C variables
,constants and Libraries.
%{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
%}
 flex definitions :- name definition
Digit [0-9] (Regular Definition)
14
15
Lex compiler
C compiler
a.out
Lex source
program
lex.yy.c
input
lex.yy.c
a.out
tokens
16
 yytext -- a string containing the lexeme
 yyleng -- the length of the lexeme
 yyin -- the input stream pointer
◦ the default input of default main() is stdin
 yyout -- the output stream pointer
◦ the default output of default main() is stdout.
17
 yylex()
◦ The default main() contains a call of yylex()
 yymore()
◦ return the next token
 yyless(n)
◦ retain the first n characters in yytext
 yywarp()
◦ is called whenever Lex reaches an end-of-file
◦ The default yywarp() always returns 1
PLLab, NTHU,Cs2403 Programming
Languages 18
Name Function
char *yytext pointer to matched string
int yyleng length of matched string
FILE *yyin input stream pointer
FILE *yyout output stream pointer
int yylex(void) call to invoke lexer, returns token
char* yymore(void) return the next token
int yyless(int n) retain the first n characters in yytext
int yywrap(void) wrapup, return 1 if done, 0 if not done
ECHO write matched string
REJECT go to the next alternative rule
INITAL initial start condition
BEGIN condition switch start condition
 Also called as Hierarchial Analysis
 A syntax tree[also called as parse tree] is generated
where
◦ Operators  Interior nodes
◦ Operands  Children of node for operators.
=
A + Interior
B C Children
 Characters grouped as tokens in Lexical Analysis are
recorded as Tables. Checks for semantic errors
 Collect TYPE information for the subsequent code
generation phase
 Sophisticated compilers typically perform multiple
passes over various intermediate forms.
 Many algorithms for code optimization are easier to
apply one at a time
 The input to one optimization relies on the
processing performed by another optimization
Concrete Parse tree
Abstract syntax tree
Converted into a linear sequence of instructions
Results in 3AC [ 3 Address Code]
 This phase attempts to improve the intermediate
code inorder to increase the running time
 Reduce the complexity of the code generated
 Leading to a faster execution of the program
 Increased Performance
 Platform Dependant/ Platform Independent
 Optimization can be automated by compilers or
performed by programmers
 Usually, the most powerful optimization is to find
a superior algorithm.
 Include activities like
◦ Optimization of LOOPS
◦ Optimization of Bottlenecks
 Succeeding step of Intermediate code
optimizer
 Consists of re-locatable machine
code/assembly code
 Intermediate instructions are converted
into a a sequence of machine instructions
THANK
YOU

Contenu connexe

Tendances

Capability maturity model cmm lecture 8
Capability maturity model cmm lecture 8Capability maturity model cmm lecture 8
Capability maturity model cmm lecture 8
Abdul Basit
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 

Tendances (20)

Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
DevOps seminar ppt
DevOps seminar ppt DevOps seminar ppt
DevOps seminar ppt
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
5. NFA & DFA.pdf
5. NFA & DFA.pdf5. NFA & DFA.pdf
5. NFA & DFA.pdf
 
Parsing
ParsingParsing
Parsing
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Capability maturity model cmm lecture 8
Capability maturity model cmm lecture 8Capability maturity model cmm lecture 8
Capability maturity model cmm lecture 8
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
Git interview questions | Edureka
Git interview questions | EdurekaGit interview questions | Edureka
Git interview questions | Edureka
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Anatomy of file write in hadoop
Anatomy of file write in hadoopAnatomy of file write in hadoop
Anatomy of file write in hadoop
 
How to implement DevOps in your Organization
How to implement DevOps in your OrganizationHow to implement DevOps in your Organization
How to implement DevOps in your Organization
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Introducing DevOps
Introducing DevOpsIntroducing DevOps
Introducing DevOps
 
Code generation
Code generationCode generation
Code generation
 

Similaire à Compiler design and lexical analyser

1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
Rakesh Kumar
 

Similaire à Compiler design and lexical analyser (20)

Compilers
CompilersCompilers
Compilers
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
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
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
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
 
Compiler
Compiler Compiler
Compiler
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
CD U1-5.pptx
CD U1-5.pptxCD U1-5.pptx
CD U1-5.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
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
 

Dernier

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Dernier (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 

Compiler design and lexical analyser

  • 1. Harcourt butler technical University Kanpur
  • 2.  A compiler is a program that reads a program written in one language and translates into equivalent target language Source- - Target | | | Error Message COMPILER
  • 3.
  • 4.  The Front end checks whether the program is correctly written in terms of the programming language syntax and semantics  The back end is responsible for translating the source into assembly code.
  • 5. Front End : Lexical Analysis Preprocessing Syntax Analysis Semantic Analysis
  • 6. Back End  Analysis  Optimization  Code generation
  • 7.  Lexical Analyzer  Syntax Analyzer  Semantic Analyzer  Intermediate code generator  Code optimizer  Code generator
  • 8. Lexical Syntax Semantic Code Generator Code Optimizer Intermediate Code Generator Symbol Table manager Error Handler
  • 9.  Also called Linear Analysis  Characters read from left to right and grouped into tokens that are a sequence of characters with a collective meaning Scans Input Removes White spaces and comments Manufacture Tokens Generate Error if Any
  • 10. ◦ Example ◦ A=B+C ◦ Variable tokens - A ,B, C ◦ Symbolic token -- = +
  • 11. 11 Lex(Flex in recent implementation)
  • 12. 12  The main job of a lexical analyzer (scanner) is to break up an input stream into tokens(tokenize input streams).  Ex :- a = b + c * d; ID ASSIGN ID PLUS ID MULT ID SEMI  Lex is an utility to help you rapidly generate your scanners
  • 13. PLLab, NTHU,Cs2403 Programming Languages 13 (optional) (required)  Lex source is separated into three sections by %% delimiters  The general format of Lex source is The absolute minimum Lex program is thus {definitions} %% {transition rules} %% {user Code} %%
  • 14.  Declarations of ordinary C variables ,constants and Libraries. %{ #include <math.h> #include <stdio.h> #include <stdlib.h> %}  flex definitions :- name definition Digit [0-9] (Regular Definition) 14
  • 15. 15 Lex compiler C compiler a.out Lex source program lex.yy.c input lex.yy.c a.out tokens
  • 16. 16  yytext -- a string containing the lexeme  yyleng -- the length of the lexeme  yyin -- the input stream pointer ◦ the default input of default main() is stdin  yyout -- the output stream pointer ◦ the default output of default main() is stdout.
  • 17. 17  yylex() ◦ The default main() contains a call of yylex()  yymore() ◦ return the next token  yyless(n) ◦ retain the first n characters in yytext  yywarp() ◦ is called whenever Lex reaches an end-of-file ◦ The default yywarp() always returns 1
  • 18. PLLab, NTHU,Cs2403 Programming Languages 18 Name Function char *yytext pointer to matched string int yyleng length of matched string FILE *yyin input stream pointer FILE *yyout output stream pointer int yylex(void) call to invoke lexer, returns token char* yymore(void) return the next token int yyless(int n) retain the first n characters in yytext int yywrap(void) wrapup, return 1 if done, 0 if not done ECHO write matched string REJECT go to the next alternative rule INITAL initial start condition BEGIN condition switch start condition
  • 19.  Also called as Hierarchial Analysis  A syntax tree[also called as parse tree] is generated where ◦ Operators  Interior nodes ◦ Operands  Children of node for operators. = A + Interior B C Children
  • 20.  Characters grouped as tokens in Lexical Analysis are recorded as Tables. Checks for semantic errors  Collect TYPE information for the subsequent code generation phase
  • 21.  Sophisticated compilers typically perform multiple passes over various intermediate forms.  Many algorithms for code optimization are easier to apply one at a time  The input to one optimization relies on the processing performed by another optimization
  • 22. Concrete Parse tree Abstract syntax tree Converted into a linear sequence of instructions Results in 3AC [ 3 Address Code]
  • 23.  This phase attempts to improve the intermediate code inorder to increase the running time  Reduce the complexity of the code generated  Leading to a faster execution of the program  Increased Performance
  • 24.  Platform Dependant/ Platform Independent  Optimization can be automated by compilers or performed by programmers  Usually, the most powerful optimization is to find a superior algorithm.  Include activities like ◦ Optimization of LOOPS ◦ Optimization of Bottlenecks
  • 25.  Succeeding step of Intermediate code optimizer  Consists of re-locatable machine code/assembly code  Intermediate instructions are converted into a a sequence of machine instructions