SlideShare a Scribd company logo
1 of 33
Lexical Analysis
Lexical
Analyzer
Lexical Analysis
Lexical
Analyzer
Parser
Lexical Analysis
Source
Program
Lexical
Analyzer
Parser
Lexical Analysis
Source
Program
Lexical
Analyzer
Parser
Lexical Analysis
Source
Program
Lexical
Analyzer
Parser
Lexical Analysis
Source
Program
Lexical
Analyzer
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
get next token
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
get next token
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Lexical Analysis
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Lexical Analysis
• It is the first phase of a compiler.
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Lexical Analysis
• It is the first phase of a compiler.
• Its main task is to read input characters and produce tokens.
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Lexical Analysis
• It is the first phase of a compiler.
• Its main task is to read input characters and produce tokens.
• “get next token” is a command sent from the parser to the lexical analyzer.
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Lexical Analysis
• It is the first phase of a compiler.
• Its main task is to read input characters and produce tokens.
• “get next token” is a command sent from the parser to the lexical analyzer.
• On receipt of the command, the lexical analyzer scans the input until it
determines the next token, and returns it.
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Lexical Analysis
• It is the first phase of a compiler.
• Its main task is to read input characters and produce tokens.
• “get next token” is a command sent from the parser to the lexical analyzer.
• On receipt of the command, the lexical analyzer scans the input until it
determines the next token, and returns it.
• It skips comments and whitspaces while creating these tokens.
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Lexical Analysis
• It is the first phase of a compiler.
• Its main task is to read input characters and produce tokens.
• “get next token” is a command sent from the parser to the lexical analyzer.
• On receipt of the command, the lexical analyzer scans the input until it
determines the next token, and returns it.
• It skips comments and whitspaces while creating these tokens.
• If any error is present then LA will correlate that error with source file and
line number.
Source
Program
Lexical
Analyzer
token
get next token
Parser
Symbol
Table
Rest of compiler
Issues in Lexical Analysis
• Simpler Design
Issues in Lexical Analysis
• Simpler Design
• Compiler Efficiency is Improved
Issues in Lexical Analysis
• Simpler Design
• Compiler Efficiency is Improved
• Compiler portability is enhanced
Tokens, patterns, and lexemes
• A TOKEN is a set of strings over the source
alphabet.
Tokens, patterns, and lexemes
• A TOKEN is a set of strings over the source
alphabet.
• A PATTERN is a rule that describes that set.
Tokens, patterns, and lexemes
• A TOKEN is a set of strings over the source
alphabet.
• A PATTERN is a rule that describes that set.
• A LEXEME is a sequence of characters
matching that pattern.
Tokens, patterns, and lexemes
• A TOKEN is a set of strings over the source
alphabet.
• A PATTERN is a rule that describes that set.
• A LEXEME is a sequence of characters
matching that pattern.
• E.g. in Pascal, for the statement
const pi = 3.1416;
Tokens, patterns, and lexemes
• A TOKEN is a set of strings over the source
alphabet.
• A PATTERN is a rule that describes that set.
• A LEXEME is a sequence of characters
matching that pattern.
• E.g. in Pascal, for the statement
const pi = 3.1416;
• The substring pi is a lexeme for the token
identifier
Example tokens, lexemes, patterns
Token Sample Lexemes Informal description of pattern
if if if
While While while
Relation <, <=, = , <>, > >= < or <= or = or <> or > or >=
Id count, sun, i, j, pi, D2 Letter followed by letters and digits
Num 0, 12, 3.1416, 6.02E23 Any numeric constant
Tokens
• Together, the complete set of tokens form the set of terminal
symbols used in the grammar for the parser.
• In most languages, the tokens fall into these categories:
– Keywords
– Operators
– Identifiers
– Constants
– Literal strings
– Punctuation
Token attributes
• If there is more than one lexeme for a token,
we have to save additional information about
the token.
• Example: the token number matches lexemes
10 and 20.
• Code generation needs the actual number, not
just the token.
• With each token, we associate ATTRIBUTES.
Normally just a pointer into the symbol table.
Example attributes
• For C source code
E = M * C * * 2
Example attributes
• For C source code
E = M * C * * 2
• We have token/attribute pairs
<ID, ptr to symbol table entry for E>
<Assign_op, null>
<ID, ptr to symbol table entry for M>
<Mult_op,null>
<ID, ptr to symbol table entry for C>
<exp_op, null>
<num, integer value 2>
Lexical errors
• When errors occur, we could just crash
• It is better to print an error message then
continue.
• Possible error recovery(Panic Mode) actions are:
– Delete a character
– Insert a missing character
– Replace an incorrect character by a correct character
– Transpose adjacent characters

More Related Content

What's hot

Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 
Address in the target code in Compiler Construction
Address in the target code in Compiler ConstructionAddress in the target code in Compiler Construction
Address in the target code in Compiler ConstructionMuhammad Haroon
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and LexemeA. S. M. Shafi
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Basic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler ConstructionBasic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler ConstructionMuhammad Haroon
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 

What's hot (20)

Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
Address in the target code in Compiler Construction
Address in the target code in Compiler ConstructionAddress in the target code in Compiler Construction
Address in the target code in Compiler Construction
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Compiler design
Compiler designCompiler design
Compiler design
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Code generation
Code generationCode generation
Code generation
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Analysis of the source program
Analysis of the source programAnalysis of the source program
Analysis of the source program
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Basic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler ConstructionBasic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler Construction
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 

Similar to Role-of-lexical-analysis

Compiler Design
Compiler Design Compiler Design
Compiler Design waqar ahmed
 
Cd ch2 - lexical analysis
Cd   ch2 - lexical analysisCd   ch2 - lexical analysis
Cd ch2 - lexical analysismengistu23
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisNayemid4676
 
Compilers in computer programming
Compilers in computer programmingCompilers in computer programming
Compilers in computer programmingChetan Pandey
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical AnalyzerArchana Gopinath
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxRossy719186
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxYashaswiniYashu9555
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 

Similar to Role-of-lexical-analysis (20)

Lexical Analysis.pdf
Lexical Analysis.pdfLexical Analysis.pdf
Lexical Analysis.pdf
 
Compiler Design
Compiler Design Compiler Design
Compiler Design
 
Cd ch2 - lexical analysis
Cd   ch2 - lexical analysisCd   ch2 - lexical analysis
Cd ch2 - lexical analysis
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Compilers in computer programming
Compilers in computer programmingCompilers in computer programming
Compilers in computer programming
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
SS & CD Module 3
SS & CD Module 3 SS & CD Module 3
SS & CD Module 3
 
Module 2
Module 2 Module 2
Module 2
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Module4 lex and yacc.ppt
Module4 lex and yacc.pptModule4 lex and yacc.ppt
Module4 lex and yacc.ppt
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptx
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 

Recently uploaded

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 

Recently uploaded (20)

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 

Role-of-lexical-analysis