SlideShare a Scribd company logo
CS416 Compiler Design 1
Unit – I Syllabus
 Introduction: Language processors,
The Structure of a Compiler,
The science of building a complier
 Lexical Analysis: The Role of the lexical analyzer,
Input buffering,
Specification of tokens,
Recognition of tokens,
The lexical analyzer generator: Lex,
Design of a Lexical Analyzer generator
Compiler Construction Tools
Writing a Compiler is difficult and time consuming task. There are some specialized tools
for helping implementation of various phases of compiler. These Tools are called Compiler
Construction Tools.
Some of the Tools are shown below:
1)Scanner: generates lexical analyzers based on the given regular expressions.
Example: LEX tool
2)Parse Generator: produces Syntax analyzer where specification must be given in CFG.
Example: YACC tool
3)Syntax-Directed Translation Engines: It helps us to produce intermediate code generator
based on the given parse tree Notations in the form of SDD.
4)Data Flow Engine: It helps us to produce code optimizer.
5)Automatic Code Generator: It helps us to produce code generator, that takes intermediate
code and converts into equivalent machine code.
Lexical Analyzer Generator - Lex
Introduction:-
Lex is a Unix utility which generates the Lexical analyzer.
Lex allows us to specify a lexical analyzer by regular expressions to
describe patterns for tokens.
The input notation for the Lex tool is referred to as the Lex language
program and the tool itself is the Lex compiler.
Behind the scenes, the Lex compiler transforms the input pattern regular
expressions into a transition diagram and generates c-language code, in a
file called lex.yy.c, that simulates this transition diagram.
Use of 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
Here, the input file, which we call lex.1, is written in the Lex language
and describes the lexical analyzer to be generated in terms of regular
expressions.
The Lex compiler transforms lex.1 to a C program, in a file that is named
lex.yy.c that simulates this transition diagram.
There after, lex.yy.c file is compiled by the C compiler into a file called
a. out.
The C-Compiler outcome file (a.out) is a working lexical analyzer, when
it is executed it take a stream of input characters and produce a stream of
tokens.
Structure of Lex Program:-
Declarations
% {
….
%}
Translation rules
% %
Pattern {Action}
……
% %
Auxiliary functions
The Declarations section includes declarations of variables,
manifest constants used in regular Expressions.
The Translation rules section consists rules of the form
Pattern { Action }
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.
The Auxiliary Section holds whatever additional functions
are used in the actions.
Example1:
Write a simple Lex source program that recognizes Noun and Verb from the
given set of Strings.
Sol:-
Example2:
%{
/* 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); }
{number} {yylval = (int) installNum(); return(NUMBER);}
…
CS416 Compiler Design 10
Unit – I Syllabus
 Introduction: Language processors,
The Structure of a Compiler,
The science of building a complier
 Lexical Analysis: The Role of the lexical analyzer,
Input buffering,
Specification of tokens,
Recognition of tokens,
The lexical analyzer generator Lex,
Design of a Lexical Analyzer generator
11
Design of a Lexical Analyzer generator
Introduction:-
 lexical-analyzer generator is architected by Finite State Machine
with two approaches namely based on NFA and DFA;
 Lex tool uses the DFA implementation internally.
12
Structure of the Generated Analyzer:-
 The program that serves as the lexical analyzer includes a fixed
program that implements an automaton for the given LEX
program.
 Below diagram shows the architecture of a lexical analyzer
generated by Lex.
13
14
Here, the Lex program is turned into a transition table and actions
by the Lex Compiler, which are used by the Finite Automaton
Simulator.
 Automaton is constructed by taking each regular-expression
pattern in the Lex program and converting it into an NFA, after
that it is converted into DFA and corresponding Transition table
is constructed.
When Transition Table is executed it creates Automaton that reads
the input buffer strings of source program and returns all
accepted lexemes and tokens
CS416 Compiler Design 15
Unit – I Syllabus
 Introduction: Language processors,
The Structure of a Compiler,
The science of building a complier
 Lexical Analysis: The Role of the lexical analyzer,
Input buffering,
Specification of tokens,
Recognition of tokens,
The lexical analyzer generator Lex,
Design of a Lexical Analyzer generator
16
Finite Automata
Introduction:-
 Regular expression is used as specification for Lexical Analyzer.
 Finite automata is implementation of Lexical Analyzer
 A finite automaton consists of
 A set of states Q
 An input alphabet 
 A start state q0
 A set of transitions : state input state
 A set of accepting states F  Q
17
 Transition
s1 a s2
It is read as:
State s1 on input “a” go to state s2
 After reading input
 If it is in Final state => accept,
 otherwise => reject
 If no transition possible => reject
18
Finite Automata States:-
 A state
• The start state
• An accepting state
• A transition
a
19
Example1
 A finite automaton that accepts only “1”
20
Example1
 A finite automaton that accepts only “1”
 A finite automaton accepts a string if it reaches Final
State after reading the input string.
1
21
Example2
 A finite automaton accepting any number of 1’s followed
by a single 0
 Where Alphabet: {0,1}
22
Example2
 A finite automaton accepting any number of 1’s followed
by a single 0
 Where Alphabet: {0,1}
 Check that “1110” is accepted but “110…” is not
0
1
23
Example3
 What language does this recognize?
0
1
0
1
0
1
24
Example4:
 What language does this recognize?
1
1
25
Epsilon Moves
 Another kind of transition: -moves

• Machine can move from state A to state B
without reading input
A B
26
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
27
28

More Related Content

Similar to CD UNIT-1.3 LEX PPT.pptx

1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
Anbarasan Radhakrishnan R
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
abhishek gupta
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
woldu2
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
Rossy719186
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
vijaya603274
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
venkatapranaykumarGa
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
ssuser3b4934
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
Tirumala Rao
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
sivaganesh293
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
sivaganesh293
 
Unit1 cd
Unit1 cdUnit1 cd
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
SouvikRoy149
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
Mahbubur Rahman
 
Compiler design
Compiler designCompiler design
Compiler design
sanchi29
 
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
DrIsikoIsaac
 
Handout#02
Handout#02Handout#02
Handout#02
Sunita Milind Dol
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
guest91cc85
 

Similar to CD UNIT-1.3 LEX PPT.pptx (20)

LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Unit1 cd
Unit1 cdUnit1 cd
Unit1 cd
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 
Compiler design
Compiler designCompiler design
Compiler design
 
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
 
Handout#02
Handout#02Handout#02
Handout#02
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 

Recently uploaded

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 

Recently uploaded (20)

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 

CD UNIT-1.3 LEX PPT.pptx

  • 1. CS416 Compiler Design 1 Unit – I Syllabus  Introduction: Language processors, The Structure of a Compiler, The science of building a complier  Lexical Analysis: The Role of the lexical analyzer, Input buffering, Specification of tokens, Recognition of tokens, The lexical analyzer generator: Lex, Design of a Lexical Analyzer generator
  • 2. Compiler Construction Tools Writing a Compiler is difficult and time consuming task. There are some specialized tools for helping implementation of various phases of compiler. These Tools are called Compiler Construction Tools. Some of the Tools are shown below: 1)Scanner: generates lexical analyzers based on the given regular expressions. Example: LEX tool 2)Parse Generator: produces Syntax analyzer where specification must be given in CFG. Example: YACC tool 3)Syntax-Directed Translation Engines: It helps us to produce intermediate code generator based on the given parse tree Notations in the form of SDD. 4)Data Flow Engine: It helps us to produce code optimizer. 5)Automatic Code Generator: It helps us to produce code generator, that takes intermediate code and converts into equivalent machine code.
  • 3. Lexical Analyzer Generator - Lex Introduction:- Lex is a Unix utility which generates the Lexical analyzer. Lex allows us to specify a lexical analyzer by regular expressions to describe patterns for tokens. The input notation for the Lex tool is referred to as the Lex language program and the tool itself is the Lex compiler. Behind the scenes, the Lex compiler transforms the input pattern regular expressions into a transition diagram and generates c-language code, in a file called lex.yy.c, that simulates this transition diagram.
  • 4. Use of 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
  • 5. Here, the input file, which we call lex.1, is written in the Lex language and describes the lexical analyzer to be generated in terms of regular expressions. The Lex compiler transforms lex.1 to a C program, in a file that is named lex.yy.c that simulates this transition diagram. There after, lex.yy.c file is compiled by the C compiler into a file called a. out. The C-Compiler outcome file (a.out) is a working lexical analyzer, when it is executed it take a stream of input characters and produce a stream of tokens.
  • 6. Structure of Lex Program:- Declarations % { …. %} Translation rules % % Pattern {Action} …… % % Auxiliary functions
  • 7. The Declarations section includes declarations of variables, manifest constants used in regular Expressions. The Translation rules section consists rules of the form Pattern { Action } 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. The Auxiliary Section holds whatever additional functions are used in the actions.
  • 8. Example1: Write a simple Lex source program that recognizes Noun and Verb from the given set of Strings. Sol:-
  • 9. Example2: %{ /* 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); } {number} {yylval = (int) installNum(); return(NUMBER);} …
  • 10. CS416 Compiler Design 10 Unit – I Syllabus  Introduction: Language processors, The Structure of a Compiler, The science of building a complier  Lexical Analysis: The Role of the lexical analyzer, Input buffering, Specification of tokens, Recognition of tokens, The lexical analyzer generator Lex, Design of a Lexical Analyzer generator
  • 11. 11 Design of a Lexical Analyzer generator Introduction:-  lexical-analyzer generator is architected by Finite State Machine with two approaches namely based on NFA and DFA;  Lex tool uses the DFA implementation internally.
  • 12. 12 Structure of the Generated Analyzer:-  The program that serves as the lexical analyzer includes a fixed program that implements an automaton for the given LEX program.  Below diagram shows the architecture of a lexical analyzer generated by Lex.
  • 13. 13
  • 14. 14 Here, the Lex program is turned into a transition table and actions by the Lex Compiler, which are used by the Finite Automaton Simulator.  Automaton is constructed by taking each regular-expression pattern in the Lex program and converting it into an NFA, after that it is converted into DFA and corresponding Transition table is constructed. When Transition Table is executed it creates Automaton that reads the input buffer strings of source program and returns all accepted lexemes and tokens
  • 15. CS416 Compiler Design 15 Unit – I Syllabus  Introduction: Language processors, The Structure of a Compiler, The science of building a complier  Lexical Analysis: The Role of the lexical analyzer, Input buffering, Specification of tokens, Recognition of tokens, The lexical analyzer generator Lex, Design of a Lexical Analyzer generator
  • 16. 16 Finite Automata Introduction:-  Regular expression is used as specification for Lexical Analyzer.  Finite automata is implementation of Lexical Analyzer  A finite automaton consists of  A set of states Q  An input alphabet   A start state q0  A set of transitions : state input state  A set of accepting states F  Q
  • 17. 17  Transition s1 a s2 It is read as: State s1 on input “a” go to state s2  After reading input  If it is in Final state => accept,  otherwise => reject  If no transition possible => reject
  • 18. 18 Finite Automata States:-  A state • The start state • An accepting state • A transition a
  • 19. 19 Example1  A finite automaton that accepts only “1”
  • 20. 20 Example1  A finite automaton that accepts only “1”  A finite automaton accepts a string if it reaches Final State after reading the input string. 1
  • 21. 21 Example2  A finite automaton accepting any number of 1’s followed by a single 0  Where Alphabet: {0,1}
  • 22. 22 Example2  A finite automaton accepting any number of 1’s followed by a single 0  Where Alphabet: {0,1}  Check that “1110” is accepted but “110…” is not 0 1
  • 23. 23 Example3  What language does this recognize? 0 1 0 1 0 1
  • 24. 24 Example4:  What language does this recognize? 1 1
  • 25. 25 Epsilon Moves  Another kind of transition: -moves  • Machine can move from state A to state B without reading input A B
  • 26. 26 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
  • 27. 27
  • 28. 28