SlideShare a Scribd company logo
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Lexical Analysis - Implementation
Lexical Analysis
Tokens
Tokens
Tokens -> RE
• Specification of Tokens: The Patterns corresponding
to a token are generally specified using a compact
notation known as regular expression.
• Regular expressions of a language are created by
combining members of its alphabet.
• A regular expression r corresponds to a set of strings
L(r) where L(r) is called a regular set or a regular
language and may be infinite.
RegEx
A regular expression is defined as follows:-
• A basic regular expression a denotes the set {a}
where a ∈Σ; L(a) ={a}
• The regular expression ε denotes the set {ε}
• If r and s are two regular expressions denoting the
sets L(r) and L(s) then; following are some rules for
regular expressions
RegEx
RegEx
Token Recognition
• Finite Automata are recognizers that can identify the
tokens occurring in input stream.
• Finite Automaton (FA) consists of:
– A finite set of states
– A set of transitions (or moves) between states:
– A special start state
– A set of final or accepting states
Token Recognition
Token Recognition
Token Recognition
Transition Digrams
Input Buffering
• Buffer Pairs: Due to amount of time taken to process
characters and the large number of characters that
must be processed.
• Specialized buffering techniques are developed to
reduce the amount of overhead required to process a
single input character.
Language to Specify LA
• Lex, allows one to specify a lexical analyzer by
specifying regular expressions to describe patterns for
tokens.
• The input notation for the Lex tool is referred to as the
Lex language and the tool itself is the Lex compiler.
• Behind the scenes, the Lex compiler transforms the
input patterns into a transition diagram and
generates code, in a file called lex.yy.c, that simulates
this transition diagram.
Language to Specify LA
• Creating LA with Lex:-
Language to Specify LA
In simple words:-
• LEX converts Lex source program to Lexical
analyzer.
• Lexical Analyzer converts input stream into tokens.
Lex Source Program - Structure
A Lex program has the following form:-
• Auxiliary Definitions – it denotes Res of the form
D1 = R1 //Di is the shortcut name for RE
D2 = R2 //Ri is the RE
• Translation Rules – Rules that tell LA which action
to take upon encountering these tokens.
Lex Source Program - Structure
Auxiliary Definitions:-
• D1 (letter) = A | B | C…| Z | a | b…| z (R1)
• D2 (digit) = 0 | 1| 2….. | 9 (R2)
• D3 (identifier) = letter (letter | digit)*
• D4 (integer) = digit+
• D5 (sign) = + | -
• D6 (signed-integer) = sign integer
Lex Source Program - Structure
Translation Rules:-
• The translation rules each have form: Pi {Actioni}
• Each pattern is a regular expression, which may use
the regular definitions of the declaration section.
• The actions are fragments of code , typically written
in C.
• Ex: for ‘keyword’-> begin {return 1}
• Ex: for ‘variable’-> identifier {install(); return 6}
Lex Source Program - Structure
Lex Source Program - Structure
Lex Source Program - Structure
Implementation of LA
• Lex generates LA as its o/p by taking Lex program
as i/p.
• Lex program is collection of patterns (REs) and their
corresponding actions.
• Patterns represent tokens to be recognized by LA to
be generated.
• For each pattern, corresponding NFA will be
designed.
Implementation of LA
• There can be ‘n’ no. of NFAs for ‘n’ no. of patterns.
• A start state is taken & using ε-transitions, all NFAs
are combined.
• The final state of each NFA show that it has found
its own token Pi.
• Convert NFA into DFA.
• The final state of DFA shows the token we have
found.
Implementation of LA
• If none of the states of DFA include any final states
of NFA, then an error is reported.
• If final state of DFA includes more than one final
state of NFA, then final state for pattern coming first
in transition rule has priority.
************ -----------------************
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

What's hot (20)

DOCX
Nonrecursive predictive parsing
alldesign
 
PDF
Artificial Intelligence Notes Unit 2
DigiGurukul
 
PPTX
Realibity design
Abhishek Tiwari
 
PPT
Lesson 08
University of Haripur
 
PPTX
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
PPTX
Parsing in Compiler Design
Akhil Kaushik
 
PPT
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
PDF
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
PPTX
Regular expressions
Ratnakar Mikkili
 
PPTX
Specification-of-tokens
Dattatray Gandhmal
 
PPTX
Assembly language (addition and subtraction)
Muhammad Umar Farooq
 
PPTX
Lexical Analysis - Compiler Design
Akhil Kaushik
 
PDF
Bottom up parser
Akshaya Arunan
 
PPTX
Yacc
BBDITM LUCKNOW
 
PPT
Booth Multiplier
Sudhir Kumar
 
PPTX
Shift reduce parser
TEJVEER SINGH
 
PPTX
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
 
PPTX
Input buffering
RushikeshKadam23
 
PPTX
AI: Logic in AI
DataminingTools Inc
 
PPTX
A simple approach of lexical analyzers
Archana Gopinath
 
Nonrecursive predictive parsing
alldesign
 
Artificial Intelligence Notes Unit 2
DigiGurukul
 
Realibity design
Abhishek Tiwari
 
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
Parsing in Compiler Design
Akhil Kaushik
 
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
Regular expressions
Ratnakar Mikkili
 
Specification-of-tokens
Dattatray Gandhmal
 
Assembly language (addition and subtraction)
Muhammad Umar Farooq
 
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Bottom up parser
Akshaya Arunan
 
Booth Multiplier
Sudhir Kumar
 
Shift reduce parser
TEJVEER SINGH
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
 
Input buffering
RushikeshKadam23
 
AI: Logic in AI
DataminingTools Inc
 
A simple approach of lexical analyzers
Archana Gopinath
 

Similar to Lexical Analyzer Implementation (20)

PPT
Module4 lex and yacc.ppt
ProddaturNagaVenkata
 
PPTX
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
PPTX
Ch 2.pptx
woldu2
 
PPT
Compier Design_Unit I_SRM.ppt
Apoorv Diwan
 
PPT
LexicalAnalysis in Compiler design .pt
Sannidhanapuharika
 
DOCX
Compiler Design
Anujashejwal
 
PPTX
Language for specifying lexical Analyzer
Archana Gopinath
 
PPTX
CD UNIT-1.3 LEX PPT.pptx
VamsiReddyHere
 
PDF
Lexicalanalyzer
Royalzig Luxury Furniture
 
PDF
Lexicalanalyzer
Royalzig Luxury Furniture
 
PPT
atc 3rd module compiler and automata.ppt
ranjan317165
 
PDF
11700220036.pdf
SouvikRoy149
 
PDF
Lexical analysis - Compiler Design
Kuppusamy P
 
PPTX
Regular Expression to Finite Automata
Archana Gopinath
 
PPTX
A Role of Lexical Analyzer
Archana Gopinath
 
PPT
Compier Design_Unit I.ppt
sivaganesh293
 
PPT
Compier Design_Unit I.ppt
sivaganesh293
 
PDF
Lexical
baran19901990
 
PPTX
Unit2 Toc.pptx
viswanath kani
 
DOC
Pcd question bank
Sumathi Gnanasekaran
 
Module4 lex and yacc.ppt
ProddaturNagaVenkata
 
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Ch 2.pptx
woldu2
 
Compier Design_Unit I_SRM.ppt
Apoorv Diwan
 
LexicalAnalysis in Compiler design .pt
Sannidhanapuharika
 
Compiler Design
Anujashejwal
 
Language for specifying lexical Analyzer
Archana Gopinath
 
CD UNIT-1.3 LEX PPT.pptx
VamsiReddyHere
 
Lexicalanalyzer
Royalzig Luxury Furniture
 
Lexicalanalyzer
Royalzig Luxury Furniture
 
atc 3rd module compiler and automata.ppt
ranjan317165
 
11700220036.pdf
SouvikRoy149
 
Lexical analysis - Compiler Design
Kuppusamy P
 
Regular Expression to Finite Automata
Archana Gopinath
 
A Role of Lexical Analyzer
Archana Gopinath
 
Compier Design_Unit I.ppt
sivaganesh293
 
Compier Design_Unit I.ppt
sivaganesh293
 
Lexical
baran19901990
 
Unit2 Toc.pptx
viswanath kani
 
Pcd question bank
Sumathi Gnanasekaran
 
Ad

More from Akhil Kaushik (19)

PPT
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
PPTX
Code Optimization
Akhil Kaushik
 
PPTX
Context Free Grammar
Akhil Kaushik
 
PPTX
Error Detection & Recovery
Akhil Kaushik
 
PPTX
Symbol Table
Akhil Kaushik
 
PPTX
NFA & DFA
Akhil Kaushik
 
PPTX
File Handling Python
Akhil Kaushik
 
PPTX
Regular Expressions
Akhil Kaushik
 
PPTX
Algorithms & Complexity Calculation
Akhil Kaushik
 
PPTX
Intro to Data Structure & Algorithms
Akhil Kaushik
 
PPTX
Decision Making & Loops
Akhil Kaushik
 
PPTX
Basic programs in Python
Akhil Kaushik
 
PPTX
Python Data-Types
Akhil Kaushik
 
PPTX
Introduction to Python Programming
Akhil Kaushik
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPTX
Bootstrapping in Compiler
Akhil Kaushik
 
PPTX
Compiler construction tools
Akhil Kaushik
 
PPTX
Phases of compiler
Akhil Kaushik
 
PPTX
Introduction to Compilers
Akhil Kaushik
 
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Code Optimization
Akhil Kaushik
 
Context Free Grammar
Akhil Kaushik
 
Error Detection & Recovery
Akhil Kaushik
 
Symbol Table
Akhil Kaushik
 
NFA & DFA
Akhil Kaushik
 
File Handling Python
Akhil Kaushik
 
Regular Expressions
Akhil Kaushik
 
Algorithms & Complexity Calculation
Akhil Kaushik
 
Intro to Data Structure & Algorithms
Akhil Kaushik
 
Decision Making & Loops
Akhil Kaushik
 
Basic programs in Python
Akhil Kaushik
 
Python Data-Types
Akhil Kaushik
 
Introduction to Python Programming
Akhil Kaushik
 
Compiler Design Basics
Akhil Kaushik
 
Bootstrapping in Compiler
Akhil Kaushik
 
Compiler construction tools
Akhil Kaushik
 
Phases of compiler
Akhil Kaushik
 
Introduction to Compilers
Akhil Kaushik
 
Ad

Recently uploaded (20)

PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
community health nursing question paper 2.pdf
Prince kumar
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 

Lexical Analyzer Implementation

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Lexical Analysis - Implementation
  • 5. Tokens -> RE • Specification of Tokens: The Patterns corresponding to a token are generally specified using a compact notation known as regular expression. • Regular expressions of a language are created by combining members of its alphabet. • A regular expression r corresponds to a set of strings L(r) where L(r) is called a regular set or a regular language and may be infinite.
  • 6. RegEx A regular expression is defined as follows:- • A basic regular expression a denotes the set {a} where a ∈Σ; L(a) ={a} • The regular expression ε denotes the set {ε} • If r and s are two regular expressions denoting the sets L(r) and L(s) then; following are some rules for regular expressions
  • 9. Token Recognition • Finite Automata are recognizers that can identify the tokens occurring in input stream. • Finite Automaton (FA) consists of: – A finite set of states – A set of transitions (or moves) between states: – A special start state – A set of final or accepting states
  • 14. Input Buffering • Buffer Pairs: Due to amount of time taken to process characters and the large number of characters that must be processed. • Specialized buffering techniques are developed to reduce the amount of overhead required to process a single input character.
  • 15. Language to Specify LA • Lex, allows one to specify a lexical analyzer by specifying regular expressions to describe patterns for tokens. • The input notation for the Lex tool is referred to as the Lex language and the tool itself is the Lex compiler. • Behind the scenes, the Lex compiler transforms the input patterns into a transition diagram and generates code, in a file called lex.yy.c, that simulates this transition diagram.
  • 16. Language to Specify LA • Creating LA with Lex:-
  • 17. Language to Specify LA In simple words:- • LEX converts Lex source program to Lexical analyzer. • Lexical Analyzer converts input stream into tokens.
  • 18. Lex Source Program - Structure A Lex program has the following form:- • Auxiliary Definitions – it denotes Res of the form D1 = R1 //Di is the shortcut name for RE D2 = R2 //Ri is the RE • Translation Rules – Rules that tell LA which action to take upon encountering these tokens.
  • 19. Lex Source Program - Structure Auxiliary Definitions:- • D1 (letter) = A | B | C…| Z | a | b…| z (R1) • D2 (digit) = 0 | 1| 2….. | 9 (R2) • D3 (identifier) = letter (letter | digit)* • D4 (integer) = digit+ • D5 (sign) = + | - • D6 (signed-integer) = sign integer
  • 20. Lex Source Program - Structure Translation Rules:- • The translation rules each have form: Pi {Actioni} • Each pattern is a regular expression, which may use the regular definitions of the declaration section. • The actions are fragments of code , typically written in C. • Ex: for ‘keyword’-> begin {return 1} • Ex: for ‘variable’-> identifier {install(); return 6}
  • 21. Lex Source Program - Structure
  • 22. Lex Source Program - Structure
  • 23. Lex Source Program - Structure
  • 24. Implementation of LA • Lex generates LA as its o/p by taking Lex program as i/p. • Lex program is collection of patterns (REs) and their corresponding actions. • Patterns represent tokens to be recognized by LA to be generated. • For each pattern, corresponding NFA will be designed.
  • 25. Implementation of LA • There can be ‘n’ no. of NFAs for ‘n’ no. of patterns. • A start state is taken & using ε-transitions, all NFAs are combined. • The final state of each NFA show that it has found its own token Pi. • Convert NFA into DFA. • The final state of DFA shows the token we have found.
  • 26. Implementation of LA • If none of the states of DFA include any final states of NFA, then an error is reported. • If final state of DFA includes more than one final state of NFA, then final state for pattern coming first in transition rule has priority. ************ -----------------************