SlideShare a Scribd company logo
1 of 26
System Software (5KS03)
Unit 1 : Introduction to Compiling
Lecture : 5 Lex
A S Kapse,
Assistant Professor,
Department Of Computer Sci. & Engineering
Anuradha Engineering College, Chikhli
Contents…
 Introduction to Lex
 Phases of Compiler
Objectives…
 Upon completion of this lecture, you will be able
 To understand the Lexical Analysis
 To understand Role of lexical analyses
 To understand input buffering
Review…./ Concepts
 What do you mean by compiler?
 What do you mean by parser and scanner?
 What do you mean by grammer?
Architecture of a transition-diagram-
based lexical analyzer
TOKEN getRelop()
{
TOKEN retToken = new (RELOP)
while (1) { /* repeat character processing until a
return or failure occurs */
switch(state) {
case 0: c= nextchar();
if (c == ‘<‘) state = 1;
else if (c == ‘=‘) state = 5;
else if (c == ‘>’) state = 6;
else fail(); /* lexeme is not a relop */
break;
case 1: …
…
case 8: retract();
retToken.attribute = GT;
return(retToken);
}
Lexical Analyzer Generator - Lex
Lexical
Compiler
Lex Source program
lex.l
lex.yy.c
C
compiler
lex.yy.c a.out
a.outInput stream Sequence
of tokens
Structure of Lex programs
declarations
%%
translation rules
%%
auxiliary functions
Pattern {Action}
Example
%{
/* definitions of manifest constants
LT, LE, EQ, NE, GT, GE,
IF, THEN, ELSE, ID, NUMBER, RELOP */
%}
/* regular definitions
delim [ tn]
ws {delim}+
letter [A-Za-z]
digit [0-9]
id {letter}({letter}|{digit})*
number {digit}+(.{digit}+)?(E[+-]?{digit}+)?
%%
{ws} {/* no action and no return */}
if {return(IF);}
then {return(THEN);}
else {return(ELSE);}
{id}{yylval = (int) installID(); return(ID); }
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 */
}
9
Finite Automata
 Regular expressions = specification
 Finite automata = implementation
 A finite automaton consists of
 An input alphabet 
 A set of states S
 A start state n
 A set of accepting states F  S
 A set of transitions state input state
10
Finite Automata
 Transition
s1 a s2
 Is read
In state s1 on input “a” go to state s2
 If end of input
 If in accepting state => accept, othewise => reject
 If no transition possible => reject
11
Finite Automata State Graphs
 A state
• The start state
• An accepting state
• A transition
a
12
A Simple Example
 A finite automaton that accepts only “1”
 A finite automaton accepts a string if we can follow
transitions labeled with the characters in the string
from the start to some accepting state
1
13
Another Simple Example
 A finite automaton accepting any number of 1’s
followed by a single 0
 Alphabet: {0,1}
 Check that “1110” is accepted but “110…” is not
0
1
14
And Another Example
 Alphabet {0,1}
 What language does this recognize?
0
1
0
1
0
1
15
And Another Example
 Alphabet still { 0, 1 }
 The operation of the automaton is not completely
defined by the input
 On input “11” the automaton could be in either state
1
1
16
Epsilon Moves
 Another kind of transition: -moves

• Machine can move from state A to state B
without reading input
A B
17
Deterministic and Nondeterministic
Automata
 Deterministic Finite Automata (DFA)
 One transition per input per state
 No -moves
 Nondeterministic Finite Automata (NFA)
 Can have multiple transitions for one input in a given state
 Can have -moves
 Finite automata have finite memory
 Need only to encode the current state
18
Execution of Finite Automata
 A DFA can take only one path through the state
graph
 Completely determined by input
 NFAs can choose
 Whether to make -moves
 Which of multiple transitions for a single input to take
19
Acceptance of NFAs
 An NFA can get into multiple states
• Input:
0
1
1
0
1 0 1
• Rule: NFA accepts if it can get in a final state
20
NFA vs. DFA (1)
 NFAs and DFAs recognize the same set of
languages (regular languages)
 DFAs are easier to implement
 There are no choices to consider
21
NFA vs. DFA (2)
 For a given language the NFA can be simpler than
the DFA
0
1
0
0
0
1
0
1
0
1
NFA
DFA
• DFA can be exponentially larger than NFA
22
Regular Expressions to Finite Automata
 High-level sketch
Regular
expressions
NFA
DFA
Lexical
Specification
Table-driven
Implementation of DFA
23
Regular Expressions to NFA (1)
 For each kind of rexp, define an NFA
 Notation: NFA for rexp A
A
• For 

• For input a
a
Video on Compilers
1. Lexical Analysis : The role of lexical analyzer
2. Input buffering
Questions..
1. Define Token?
2. Explain procedure of specification of tokens .
3. Explain the phases of compiler?
4. What is mean by token? How it is recognised.
Homework..
1. Design NFA for (a|b)* abb.
2What is mean by analysis and synthesis.
3. Describe Difference between NFA & DFA

More Related Content

What's hot

Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
NFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikNFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikMudsaraliKhushik
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examplesankitamakin
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4Oliver Zeigermann
 
Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Srimatre K
 
Control structures c2 c3
Control structures c2 c3Control structures c2 c3
Control structures c2 c3Omar Al-Sabek
 
Lec 3 ---- dfa
Lec 3  ---- dfaLec 3  ---- dfa
Lec 3 ---- dfaAbdul Aziz
 
Turing machine and Halting Introduction
Turing machine and Halting IntroductionTuring machine and Halting Introduction
Turing machine and Halting IntroductionAmartyaYandrapu1
 
Flow control instructions
Flow control instructionsFlow control instructions
Flow control instructionsProdip Ghosh
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Digvijaysinh Gohil
 
Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Srimatre K
 
Function procedure c6 c7
Function procedure  c6 c7Function procedure  c6 c7
Function procedure c6 c7Omar Al-Sabek
 
Loops Basics
Loops BasicsLoops Basics
Loops BasicsMushiii
 

What's hot (20)

Finite automata
Finite automataFinite automata
Finite automata
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Finite automata
Finite automataFinite automata
Finite automata
 
NFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikNFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushik
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
 
Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5
 
Control structures c2 c3
Control structures c2 c3Control structures c2 c3
Control structures c2 c3
 
Chapt 06
Chapt 06Chapt 06
Chapt 06
 
8 statement level
8 statement level8 statement level
8 statement level
 
Lec 3 ---- dfa
Lec 3  ---- dfaLec 3  ---- dfa
Lec 3 ---- dfa
 
Turing machine and Halting Introduction
Turing machine and Halting IntroductionTuring machine and Halting Introduction
Turing machine and Halting Introduction
 
Flow control instructions
Flow control instructionsFlow control instructions
Flow control instructions
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
 
Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4
 
Function procedure c6 c7
Function procedure  c6 c7Function procedure  c6 c7
Function procedure c6 c7
 
ETAPS03 SC.ppt
ETAPS03 SC.pptETAPS03 SC.ppt
ETAPS03 SC.ppt
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops
LoopsLoops
Loops
 

Similar to Introduction to Lexical Analysis and Finite Automata

Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyserArchana Gopinath
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptNderituGichuki1
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxMeghnadh
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptxamara jyothi
 
MidtermI-review.pptx
MidtermI-review.pptxMidtermI-review.pptx
MidtermI-review.pptxamara jyothi
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxamara jyothi
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxranjan317165
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptxwoldu2
 
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdfFariyaTasneem1
 
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. Pptovidlivi91
 
1. finite_automata_new.ppt
1. finite_automata_new.ppt1. finite_automata_new.ppt
1. finite_automata_new.pptSanthoshS508159
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,8neutron8
 
Nondeterministic Finite Automat
Nondeterministic Finite AutomatNondeterministic Finite Automat
Nondeterministic Finite AutomatAdel Al-Ofairi
 

Similar to Introduction to Lexical Analysis and Finite Automata (20)

Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyser
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
SS UI Lecture 6
SS UI Lecture 6SS UI Lecture 6
SS UI Lecture 6
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptx
 
MidtermI-review.pptx
MidtermI-review.pptxMidtermI-review.pptx
MidtermI-review.pptx
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
 
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
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
 
1. finite_automata_new.ppt
1. finite_automata_new.ppt1. finite_automata_new.ppt
1. finite_automata_new.ppt
 
TOC Introduction
TOC Introduction TOC Introduction
TOC Introduction
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
Nondeterministic Finite Automat
Nondeterministic Finite AutomatNondeterministic Finite Automat
Nondeterministic Finite Automat
 

More from Avinash Kapse

More from Avinash Kapse (8)

Presentation1
Presentation1Presentation1
Presentation1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Presentation1
Presentation1Presentation1
Presentation1
 
SS UII Lecture 1
SS UII Lecture 1SS UII Lecture 1
SS UII Lecture 1
 
SS UI Lecture 4
SS UI Lecture 4SS UI Lecture 4
SS UI Lecture 4
 
SS UI Lecture 1
SS UI Lecture 1SS UI Lecture 1
SS UI Lecture 1
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
Ss ui lecture 1
Ss ui lecture 1Ss ui lecture 1
Ss ui lecture 1
 

Introduction to Lexical Analysis and Finite Automata

  • 1. System Software (5KS03) Unit 1 : Introduction to Compiling Lecture : 5 Lex A S Kapse, Assistant Professor, Department Of Computer Sci. & Engineering Anuradha Engineering College, Chikhli
  • 2. Contents…  Introduction to Lex  Phases of Compiler
  • 3. Objectives…  Upon completion of this lecture, you will be able  To understand the Lexical Analysis  To understand Role of lexical analyses  To understand input buffering
  • 4. Review…./ Concepts  What do you mean by compiler?  What do you mean by parser and scanner?  What do you mean by grammer?
  • 5. Architecture of a transition-diagram- based lexical analyzer TOKEN getRelop() { TOKEN retToken = new (RELOP) while (1) { /* repeat character processing until a return or failure occurs */ switch(state) { case 0: c= nextchar(); if (c == ‘<‘) state = 1; else if (c == ‘=‘) state = 5; else if (c == ‘>’) state = 6; else fail(); /* lexeme is not a relop */ break; case 1: … … case 8: retract(); retToken.attribute = GT; return(retToken); }
  • 6. Lexical Analyzer Generator - Lex Lexical Compiler Lex Source program lex.l lex.yy.c C compiler lex.yy.c a.out a.outInput stream Sequence of tokens
  • 7. Structure of Lex programs declarations %% translation rules %% auxiliary functions Pattern {Action}
  • 8. Example %{ /* definitions of manifest constants LT, LE, EQ, NE, GT, GE, IF, THEN, ELSE, ID, NUMBER, RELOP */ %} /* regular definitions delim [ tn] ws {delim}+ letter [A-Za-z] digit [0-9] id {letter}({letter}|{digit})* number {digit}+(.{digit}+)?(E[+-]?{digit}+)? %% {ws} {/* no action and no return */} if {return(IF);} then {return(THEN);} else {return(ELSE);} {id}{yylval = (int) installID(); return(ID); } 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 */ }
  • 9. 9 Finite Automata  Regular expressions = specification  Finite automata = implementation  A finite automaton consists of  An input alphabet   A set of states S  A start state n  A set of accepting states F  S  A set of transitions state input state
  • 10. 10 Finite Automata  Transition s1 a s2  Is read In state s1 on input “a” go to state s2  If end of input  If in accepting state => accept, othewise => reject  If no transition possible => reject
  • 11. 11 Finite Automata State Graphs  A state • The start state • An accepting state • A transition a
  • 12. 12 A Simple Example  A finite automaton that accepts only “1”  A finite automaton accepts a string if we can follow transitions labeled with the characters in the string from the start to some accepting state 1
  • 13. 13 Another Simple Example  A finite automaton accepting any number of 1’s followed by a single 0  Alphabet: {0,1}  Check that “1110” is accepted but “110…” is not 0 1
  • 14. 14 And Another Example  Alphabet {0,1}  What language does this recognize? 0 1 0 1 0 1
  • 15. 15 And Another Example  Alphabet still { 0, 1 }  The operation of the automaton is not completely defined by the input  On input “11” the automaton could be in either state 1 1
  • 16. 16 Epsilon Moves  Another kind of transition: -moves  • Machine can move from state A to state B without reading input A B
  • 17. 17 Deterministic and Nondeterministic Automata  Deterministic Finite Automata (DFA)  One transition per input per state  No -moves  Nondeterministic Finite Automata (NFA)  Can have multiple transitions for one input in a given state  Can have -moves  Finite automata have finite memory  Need only to encode the current state
  • 18. 18 Execution of Finite Automata  A DFA can take only one path through the state graph  Completely determined by input  NFAs can choose  Whether to make -moves  Which of multiple transitions for a single input to take
  • 19. 19 Acceptance of NFAs  An NFA can get into multiple states • Input: 0 1 1 0 1 0 1 • Rule: NFA accepts if it can get in a final state
  • 20. 20 NFA vs. DFA (1)  NFAs and DFAs recognize the same set of languages (regular languages)  DFAs are easier to implement  There are no choices to consider
  • 21. 21 NFA vs. DFA (2)  For a given language the NFA can be simpler than the DFA 0 1 0 0 0 1 0 1 0 1 NFA DFA • DFA can be exponentially larger than NFA
  • 22. 22 Regular Expressions to Finite Automata  High-level sketch Regular expressions NFA DFA Lexical Specification Table-driven Implementation of DFA
  • 23. 23 Regular Expressions to NFA (1)  For each kind of rexp, define an NFA  Notation: NFA for rexp A A • For   • For input a a
  • 24. Video on Compilers 1. Lexical Analysis : The role of lexical analyzer 2. Input buffering
  • 25. Questions.. 1. Define Token? 2. Explain procedure of specification of tokens . 3. Explain the phases of compiler? 4. What is mean by token? How it is recognised.
  • 26. Homework.. 1. Design NFA for (a|b)* abb. 2What is mean by analysis and synthesis. 3. Describe Difference between NFA & DFA