SlideShare ist ein Scribd-Unternehmen logo
1 von 51
Chapter 3 Lexical Analysis
Outline ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The role of lexical analyzer Lexical Analyzer Parser Source program token getNextToken Symbol table To semantic analysis
Why to separate Lexical analysis and parsing ,[object Object],[object Object],[object Object]
Tokens, Patterns and Lexemes ,[object Object],[object Object],[object Object]
Example Token Informal description Sample lexemes if else comparison id number literal Characters i, f Characters e, l, s, e < or > or <= or >= or == or != Letter followed by letter and digits Any numeric constant Anything but “ sorrounded by “ if else <=, != pi, score, D2 3.14159, 0, 6.02e23 “ core dumped” printf(“total = %d”, score);
Attributes for tokens ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lexical errors ,[object Object],[object Object],[object Object],[object Object],[object Object]
Error recovery ,[object Object],[object Object],[object Object],[object Object],[object Object]
Input buffering ,[object Object],[object Object],[object Object],[object Object],E  =  M *  C * * 2  eof
Sentinels ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],E  =  M  eof  *  C * * 2  eof eof
Specification of tokens ,[object Object],[object Object],[object Object],[object Object],[object Object]
Regular expressions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Regular definitions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Extensions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recognition of tokens ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Recognition of tokens (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transition diagrams ,[object Object]
Transition diagrams (cont.) ,[object Object]
Transition diagrams (cont.) ,[object Object]
Transition diagrams (cont.) ,[object Object]
Architecture of a transition-diagram-based lexical analyzer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lexical Analyzer Generator - Lex Lexical Compiler Lex Source program lex.l lex.yy.c C compiler lex.yy.c a.out a.out Input stream Sequence of tokens
Structure of Lex programs declarations %% translation rules %% auxiliary functions Pattern  {Action}
Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Int installID() {/* funtion to install the lexeme, whose first character is pointed to by yytext, and whose length is yyleng, into the symbol table and return a pointer thereto */ } Int installNum() { /* similar to installID, but puts numerical constants into a separate table */ }
Finite Automata ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Finite Automata ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Finite Automata State Graphs ,[object Object],[object Object],[object Object],[object Object],a
A Simple Example ,[object Object],[object Object],1
Another Simple Example ,[object Object],[object Object],[object Object],0 1
And Another Example ,[object Object],[object Object],0 1 0 1 0 1
And Another Example ,[object Object],[object Object],[object Object],1 1
Epsilon Moves ,[object Object],[object Object],A B 
Deterministic and Nondeterministic Automata ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Execution of Finite Automata ,[object Object],[object Object],[object Object],[object Object],[object Object]
Acceptance of NFAs ,[object Object],[object Object],1 0 1 ,[object Object],0 1 1 0
NFA vs. DFA (1) ,[object Object],[object Object],[object Object]
NFA vs. DFA (2) ,[object Object],NFA DFA ,[object Object],0 1 0 0 0 1 0 1 0 1
Regular Expressions to Finite Automata ,[object Object],Regular expressions NFA DFA Lexical Specification Table-driven  Implementation of DFA
Regular Expressions to NFA (1) ,[object Object],[object Object],[object Object],[object Object],A  a
Regular Expressions to NFA (2) ,[object Object],[object Object],A B  A B    
Regular Expressions to NFA (3) ,[object Object],A   
Example of RegExp -> NFA conversion ,[object Object],[object Object],[object Object], 1 C E 0 D F   B   G    A H 1 I J
Next Regular expressions NFA DFA Lexical Specification Table-driven  Implementation of DFA
NFA to DFA. The Trick ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
NFA -> DFA Example 1 0 1         A B C D E F G H I J ABCDHI FGABCDHI EJGABCDHI 0 1 0 1 0 1
NFA to DFA. Remark ,[object Object],[object Object],[object Object],[object Object],[object Object]
Implementation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Table Implementation of a DFA S T U 0 1 0 1 0 1 0 1 S T U T T U U T U
Implementation (Cont.) ,[object Object],[object Object],[object Object]
Readings ,[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expressionvaluebound
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksMohammad Vaseem Akaram
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchHema Kashyap
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 

Was ist angesagt? (20)

Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star search
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 

Andere mochten auch

Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc pptpssraikar
 
ひろ子 in Objective-C
ひろ子 in Objective-Cひろ子 in Objective-C
ひろ子 in Objective-CTaketo Sano
 
2.5 Trillion Oil Scam
2.5 Trillion Oil Scam2.5 Trillion Oil Scam
2.5 Trillion Oil Scamtheoilman
 
07 09 04 Ctqi Standard
07 09 04 Ctqi Standard07 09 04 Ctqi Standard
07 09 04 Ctqi StandardKieran F Ring
 
Isotopes And Radioactivity 09
Isotopes And Radioactivity 09Isotopes And Radioactivity 09
Isotopes And Radioactivity 09Paula Mills
 
The Art Of Writing A Business Plan (Zafar)
The Art Of Writing A Business Plan (Zafar)The Art Of Writing A Business Plan (Zafar)
The Art Of Writing A Business Plan (Zafar)Naeem Zafar
 
Cmc chapter 09
Cmc chapter 09Cmc chapter 09
Cmc chapter 09Jane Hamze
 
Python and R for quantitative finance
Python and R for quantitative financePython and R for quantitative finance
Python and R for quantitative financeLuca Sbardella
 
09. Development Plan For Pcmc
09. Development Plan For Pcmc09. Development Plan For Pcmc
09. Development Plan For PcmcRanjit Gadgil
 
Abc analysis1234
Abc analysis1234Abc analysis1234
Abc analysis1234Ashok Reddy
 
Seductive Interactions (Idea 09 Version)
Seductive Interactions (Idea 09 Version)Seductive Interactions (Idea 09 Version)
Seductive Interactions (Idea 09 Version)Stephen Anderson
 
Infographics Jayan Narayanan
Infographics   Jayan NarayananInfographics   Jayan Narayanan
Infographics Jayan NarayananJayan Narayanan
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)Martina Goehring
 
mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"mediscript Team
 
Unbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_ÖsterreichUnbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_ÖsterreichFESD GKr
 
E Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 PersoenlichkeitE Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 Persoenlichkeitthomasabauer
 
Kelantan
KelantanKelantan
Kelantanwmzuri
 

Andere mochten auch (20)

Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
ひろ子 in Objective-C
ひろ子 in Objective-Cひろ子 in Objective-C
ひろ子 in Objective-C
 
Tagging - web 2 expo 2008
Tagging - web 2 expo 2008Tagging - web 2 expo 2008
Tagging - web 2 expo 2008
 
2.5 Trillion Oil Scam
2.5 Trillion Oil Scam2.5 Trillion Oil Scam
2.5 Trillion Oil Scam
 
07 09 04 Ctqi Standard
07 09 04 Ctqi Standard07 09 04 Ctqi Standard
07 09 04 Ctqi Standard
 
Isotopes And Radioactivity 09
Isotopes And Radioactivity 09Isotopes And Radioactivity 09
Isotopes And Radioactivity 09
 
The Art Of Writing A Business Plan (Zafar)
The Art Of Writing A Business Plan (Zafar)The Art Of Writing A Business Plan (Zafar)
The Art Of Writing A Business Plan (Zafar)
 
Cmc chapter 09
Cmc chapter 09Cmc chapter 09
Cmc chapter 09
 
Python and R for quantitative finance
Python and R for quantitative financePython and R for quantitative finance
Python and R for quantitative finance
 
09. Development Plan For Pcmc
09. Development Plan For Pcmc09. Development Plan For Pcmc
09. Development Plan For Pcmc
 
Abc analysis1234
Abc analysis1234Abc analysis1234
Abc analysis1234
 
Seductive Interactions (Idea 09 Version)
Seductive Interactions (Idea 09 Version)Seductive Interactions (Idea 09 Version)
Seductive Interactions (Idea 09 Version)
 
PMP Preparation - 09 Human Resource Management
PMP Preparation - 09 Human Resource ManagementPMP Preparation - 09 Human Resource Management
PMP Preparation - 09 Human Resource Management
 
Infographics Jayan Narayanan
Infographics   Jayan NarayananInfographics   Jayan Narayanan
Infographics Jayan Narayanan
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
Lernen Mit Web 2.0 (IHK Stuttgart Juli 2009)
 
mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"mediscript Kalendar 2013 "Lernen heisst Entdecken"
mediscript Kalendar 2013 "Lernen heisst Entdecken"
 
Unbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_ÖsterreichUnbezahlte arbeitszeit in_Österreich
Unbezahlte arbeitszeit in_Österreich
 
E Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 PersoenlichkeitE Co C Bratislava 09 Persoenlichkeit
E Co C Bratislava 09 Persoenlichkeit
 
Kelantan
KelantanKelantan
Kelantan
 

Ähnlich wie 02. chapter 3 lexical analysis

Ähnlich wie 02. chapter 3 lexical analysis (20)

Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
 
Ch3
Ch3Ch3
Ch3
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Regular Expressions To Finite Automata
Regular Expressions To Finite AutomataRegular Expressions To Finite Automata
Regular Expressions To Finite Automata
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
compiler Design course material chapter 2
compiler Design course material chapter 2compiler Design course material chapter 2
compiler Design course material chapter 2
 
SS UI Lecture 5
SS UI Lecture 5SS UI Lecture 5
SS UI Lecture 5
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Compiler Design ug semLexical Analysis.ppt
Compiler Design ug semLexical Analysis.pptCompiler Design ug semLexical Analysis.ppt
Compiler Design ug semLexical Analysis.ppt
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptx
 
Lexical analysis, syntax analysis, semantic analysis. Ppt
Lexical analysis, syntax analysis, semantic analysis. PptLexical analysis, syntax analysis, semantic analysis. Ppt
Lexical analysis, syntax analysis, semantic analysis. Ppt
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 

02. chapter 3 lexical analysis

  • 1. Chapter 3 Lexical Analysis
  • 2.
  • 3. The role of lexical analyzer Lexical Analyzer Parser Source program token getNextToken Symbol table To semantic analysis
  • 4.
  • 5.
  • 6. Example Token Informal description Sample lexemes if else comparison id number literal Characters i, f Characters e, l, s, e < or > or <= or >= or == or != Letter followed by letter and digits Any numeric constant Anything but “ sorrounded by “ if else <=, != pi, score, D2 3.14159, 0, 6.02e23 “ core dumped” printf(“total = %d”, score);
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. Lexical Analyzer Generator - Lex Lexical Compiler Lex Source program lex.l lex.yy.c C compiler lex.yy.c a.out a.out Input stream Sequence of tokens
  • 24. Structure of Lex programs declarations %% translation rules %% auxiliary functions Pattern {Action}
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44. Next Regular expressions NFA DFA Lexical Specification Table-driven Implementation of DFA
  • 45.
  • 46. NFA -> DFA Example 1 0 1         A B C D E F G H I J ABCDHI FGABCDHI EJGABCDHI 0 1 0 1 0 1
  • 47.
  • 48.
  • 49. Table Implementation of a DFA S T U 0 1 0 1 0 1 0 1 S T U T T U U T U
  • 50.
  • 51.