SlideShare a Scribd company logo
1 of 28
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

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
 

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

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Recently uploaded (20)

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.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