SlideShare a Scribd company logo
1 of 5
Download to read offline
System Programming
Walchand Institute of Technology
Aim:
Scanner using LEX
Theory:
Language processor development tools (LPDTs) focusing on generation of the
analysis phase of language processors.
Figure 1 shows a schematic
language processor whose source
two inputs:
1. Specification
2. Specification
phase.
Figure 1: A Language Processor Development Tool
It generates programs that perform lexical, syntax and semantic analysis
source program and construct the IR. These programs collectively form the
analysis phase of the language processor.
Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur
HANDOUT#02
guage processor development tools (LPDTs) focusing on generation of the
analysis phase of language processors.
shows a schematic of an LPDT which generates the analysis phase
whose source language is L. The LPDT requires the following
Specification of a grammar of language L
Specification of semantic actions to be performed in the analysis
: A Language Processor Development Tool
It generates programs that perform lexical, syntax and semantic analysis
source program and construct the IR. These programs collectively form the
language processor.
Sunita M. Dol, CSE Dept
Page 1
guage processor development tools (LPDTs) focusing on generation of the
an LPDT which generates the analysis phase of a
is L. The LPDT requires the following
semantic actions to be performed in the analysis
: A Language Processor Development Tool (LPDT)
It generates programs that perform lexical, syntax and semantic analysis of the
source program and construct the IR. These programs collectively form the
System Programming
Walchand Institute of Technology
Two LPDTs are widely used in practice. These are, th
LEX, and the parser generator YACC. The input to these tools is a specification
the lexical and syntactic constructs
on recognizing the constructs. The specification consists
rules of the form
<
where < semantic action >
matching < string specification
erate C programs which contain the code for scanning and parsing, respectively,
and the semantic actions contained in the specification.
A YACC generated parser can use a LEX generated scanner as a routine if the
scanner and parser use same con
Figure 2 shows a schematic for developing the analysis phase
language L using LEX and YACC. The analysis phase processes the source
program to build an intermediate represen
Figure
LEX
LEX accepts an input specification which consists
component is a specification
and constants. This specification is in the form
component is a specification
consists of a set of tables
Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur
Two LPDTs are widely used in practice. These are, the lexical analyzer generator
LEX, and the parser generator YACC. The input to these tools is a specification
the lexical and syntactic constructs of L, and the semantic actions to be performed
on recognizing the constructs. The specification consists of a set
<string specification> {< semantic action>}
> consists of C code. This code is executed when a string
string specification > is encountered in the input. LEX and YACC gen
erate C programs which contain the code for scanning and parsing, respectively,
and the semantic actions contained in the specification.
A YACC generated parser can use a LEX generated scanner as a routine if the
scanner and parser use same conventions concerning the representation
shows a schematic for developing the analysis phase
L using LEX and YACC. The analysis phase processes the source
program to build an intermediate representation.
Figure 2: using LEX and YACC
LEX accepts an input specification which consists of two components. The first
component is a specification of strings representing the lexical units in L, e.g. id’s
and constants. This specification is in the form of regular expres
component is a specification of semantic actions aimed at building an IR. The IR
tables of lexical units and a sequence of tokens for the lexical
Sunita M. Dol, CSE Dept
Page 2
e lexical analyzer generator
LEX, and the parser generator YACC. The input to these tools is a specification of
L, and the semantic actions to be performed
a set of translation
string specification> {< semantic action>}
C code. This code is executed when a string
> is encountered in the input. LEX and YACC gen-
erate C programs which contain the code for scanning and parsing, respectively,
A YACC generated parser can use a LEX generated scanner as a routine if the
oncerning the representation of tokens.
shows a schematic for developing the analysis phase of a compiler for
L using LEX and YACC. The analysis phase processes the source
two components. The first
strings representing the lexical units in L, e.g. id’s
regular expressions. The second
semantic actions aimed at building an IR. The IR
tokens for the lexical
System Programming
Walchand Institute of Technology
units occurring in a source statement. Accordingly, the semantic action
entries in the tables and build tokens for the lexical units.
Example
Figure 3 shows a sample input to LEX. The input consists
three of which are shown here. The first component (enclosed
defines the symbols used in specifying the strings
to stand for any upper or lower case letter, and digit to stand for any digit. The sec
ond component enclosed between %% and %% contains the translation rules. The
third component contains aux
actions.
Figure
The sample input in above
assignment operator), and identifier and constant strings
found, it is entered in the symbol table (if not already present) using the routine
enter.id. The pair (ID, entry #)
convention entry # is put in the global variable yylval, and the cla
returned as the value of the call on scanner. Similar actions are taken on finding a
constant, the keywords begin and end and the assignment operator.
Compiling the Lexical Analyzer
To compile a lex program, do the following:
Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur
units occurring in a source statement. Accordingly, the semantic action
entries in the tables and build tokens for the lexical units.
shows a sample input to LEX. The input consists of
which are shown here. The first component (enclosed
ed in specifying the strings of L. It defines the symbol letter
to stand for any upper or lower case letter, and digit to stand for any digit. The sec
ond component enclosed between %% and %% contains the translation rules. The
third component contains auxiliary routines which can be used in the semantic
Figure 3: A sample LEX specification
above Figure 3 defines the strings begin, end, := (the
assignment operator), and identifier and constant strings of L. When an identifier is
found, it is entered in the symbol table (if not already present) using the routine
entry #) forms the token for the identifier string.
is put in the global variable yylval, and the cla
the call on scanner. Similar actions are taken on finding a
constant, the keywords begin and end and the assignment operator.
Compiling the Lexical Analyzer
program, do the following:
Sunita M. Dol, CSE Dept
Page 3
units occurring in a source statement. Accordingly, the semantic actions make new
of four components,
which are shown here. The first component (enclosed by %{ and %})
L. It defines the symbol letter
to stand for any upper or lower case letter, and digit to stand for any digit. The sec-
ond component enclosed between %% and %% contains the translation rules. The
iliary routines which can be used in the semantic
: A sample LEX specification
defines the strings begin, end, := (the
L. When an identifier is
found, it is entered in the symbol table (if not already present) using the routine
forms the token for the identifier string. By
is put in the global variable yylval, and the class code ID is
the call on scanner. Similar actions are taken on finding a
constant, the keywords begin and end and the assignment operator.
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 4
1. Use the lex program to change the specification file into a C language
program. The resulting program is in the lex.yy.c file.
2. Use the cc command with the -ll flag to compile and link the program with a
library of lex subroutines. The resulting executable program is in the a.out
file.
For example, if the lex specification file is called lextest, enter the following
commands:
lex lextest
cc lex.yy.c -ll
Program:
%{
%}
delim [tn]
ws delim+
letter [A-Za-z]
digit [1-9]
id {letter}({letter}|{digit})*
number {digit}+(.{digit}+)?(E[+/-]?{digit}+)?
%%
{ws} {/*printf("white space");*/}
if {printf("%s : Keywordn",yytext);}
then {printf("%s : Keywordn",yytext);}
else {printf("%s : Keywordn",yytext);}
{id} {printf("Identifier");}
{number} {printf("Number");}
"<"|"<="|"=="|"<>"|">"|">=" {printf("Relational operator");}
%%
main()
{
yylex();
}
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 5
Input:
if
Output:
Keyword
Conclusion:
The lex command generates a C language program that can analyze an input
stream using information in the specification file.
The lex command stores the output program in a lex.yy.c file.
If the output program recognizes a simple, one-word input structure, the
lex.yy.c output file can compile to produce an executable lexical analyzer.
Thus lexical analyzer generator or scanner is implemented using LEX

More Related Content

What's hot

Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question keyArthyR3
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler designJanani Parthiban
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1MashaelQ
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning materialArthyR3
 
Compiler question bank
Compiler question bankCompiler question bank
Compiler question bankArthyR3
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questionsakila viji
 

What's hot (20)

Handout#07
Handout#07Handout#07
Handout#07
 
Handout#09
Handout#09Handout#09
Handout#09
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#06
Handout#06Handout#06
Handout#06
 
Handout#04
Handout#04Handout#04
Handout#04
 
Handout#01
Handout#01Handout#01
Handout#01
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question key
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
Compiler question bank
Compiler question bankCompiler question bank
Compiler question bank
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Language processors
Language processorsLanguage processors
Language processors
 
C language
C languageC language
C language
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Chapter2
Chapter2Chapter2
Chapter2
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 

Similar to Handout#02

Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof Chethan Raj C
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysisSudhaa Ravi
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfkalpana Manudhane
 
Chapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptxChapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptxArebuMaruf
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptxwoldu2
 
Lexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP programLexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP programIOSR Journals
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORijistjournal
 
ProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docxProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docxwkyra78
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationAkhil Kaushik
 

Similar to Handout#02 (20)

module 4.pptx
module 4.pptxmodule 4.pptx
module 4.pptx
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
LANGUAGE PROCESSOR
LANGUAGE PROCESSORLANGUAGE PROCESSOR
LANGUAGE PROCESSOR
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Module4 lex and yacc.ppt
Module4 lex and yacc.pptModule4 lex and yacc.ppt
Module4 lex and yacc.ppt
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
Assignment2
Assignment2Assignment2
Assignment2
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdf
 
Lex Tool
Lex ToolLex Tool
Lex Tool
 
Chapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptxChapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptx
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
Lexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP programLexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP program
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
 
ProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docxProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docx
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 

More from Sunita Milind Dol (17)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment5
Assignment5Assignment5
Assignment5
 
Assignment3
Assignment3Assignment3
Assignment3
 

Recently uploaded

CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Recently uploaded (20)

CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 

Handout#02

  • 1. System Programming Walchand Institute of Technology Aim: Scanner using LEX Theory: Language processor development tools (LPDTs) focusing on generation of the analysis phase of language processors. Figure 1 shows a schematic language processor whose source two inputs: 1. Specification 2. Specification phase. Figure 1: A Language Processor Development Tool It generates programs that perform lexical, syntax and semantic analysis source program and construct the IR. These programs collectively form the analysis phase of the language processor. Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur HANDOUT#02 guage processor development tools (LPDTs) focusing on generation of the analysis phase of language processors. shows a schematic of an LPDT which generates the analysis phase whose source language is L. The LPDT requires the following Specification of a grammar of language L Specification of semantic actions to be performed in the analysis : A Language Processor Development Tool It generates programs that perform lexical, syntax and semantic analysis source program and construct the IR. These programs collectively form the language processor. Sunita M. Dol, CSE Dept Page 1 guage processor development tools (LPDTs) focusing on generation of the an LPDT which generates the analysis phase of a is L. The LPDT requires the following semantic actions to be performed in the analysis : A Language Processor Development Tool (LPDT) It generates programs that perform lexical, syntax and semantic analysis of the source program and construct the IR. These programs collectively form the
  • 2. System Programming Walchand Institute of Technology Two LPDTs are widely used in practice. These are, th LEX, and the parser generator YACC. The input to these tools is a specification the lexical and syntactic constructs on recognizing the constructs. The specification consists rules of the form < where < semantic action > matching < string specification erate C programs which contain the code for scanning and parsing, respectively, and the semantic actions contained in the specification. A YACC generated parser can use a LEX generated scanner as a routine if the scanner and parser use same con Figure 2 shows a schematic for developing the analysis phase language L using LEX and YACC. The analysis phase processes the source program to build an intermediate represen Figure LEX LEX accepts an input specification which consists component is a specification and constants. This specification is in the form component is a specification consists of a set of tables Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Two LPDTs are widely used in practice. These are, the lexical analyzer generator LEX, and the parser generator YACC. The input to these tools is a specification the lexical and syntactic constructs of L, and the semantic actions to be performed on recognizing the constructs. The specification consists of a set <string specification> {< semantic action>} > consists of C code. This code is executed when a string string specification > is encountered in the input. LEX and YACC gen erate C programs which contain the code for scanning and parsing, respectively, and the semantic actions contained in the specification. A YACC generated parser can use a LEX generated scanner as a routine if the scanner and parser use same conventions concerning the representation shows a schematic for developing the analysis phase L using LEX and YACC. The analysis phase processes the source program to build an intermediate representation. Figure 2: using LEX and YACC LEX accepts an input specification which consists of two components. The first component is a specification of strings representing the lexical units in L, e.g. id’s and constants. This specification is in the form of regular expres component is a specification of semantic actions aimed at building an IR. The IR tables of lexical units and a sequence of tokens for the lexical Sunita M. Dol, CSE Dept Page 2 e lexical analyzer generator LEX, and the parser generator YACC. The input to these tools is a specification of L, and the semantic actions to be performed a set of translation string specification> {< semantic action>} C code. This code is executed when a string > is encountered in the input. LEX and YACC gen- erate C programs which contain the code for scanning and parsing, respectively, A YACC generated parser can use a LEX generated scanner as a routine if the oncerning the representation of tokens. shows a schematic for developing the analysis phase of a compiler for L using LEX and YACC. The analysis phase processes the source two components. The first strings representing the lexical units in L, e.g. id’s regular expressions. The second semantic actions aimed at building an IR. The IR tokens for the lexical
  • 3. System Programming Walchand Institute of Technology units occurring in a source statement. Accordingly, the semantic action entries in the tables and build tokens for the lexical units. Example Figure 3 shows a sample input to LEX. The input consists three of which are shown here. The first component (enclosed defines the symbols used in specifying the strings to stand for any upper or lower case letter, and digit to stand for any digit. The sec ond component enclosed between %% and %% contains the translation rules. The third component contains aux actions. Figure The sample input in above assignment operator), and identifier and constant strings found, it is entered in the symbol table (if not already present) using the routine enter.id. The pair (ID, entry #) convention entry # is put in the global variable yylval, and the cla returned as the value of the call on scanner. Similar actions are taken on finding a constant, the keywords begin and end and the assignment operator. Compiling the Lexical Analyzer To compile a lex program, do the following: Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur units occurring in a source statement. Accordingly, the semantic action entries in the tables and build tokens for the lexical units. shows a sample input to LEX. The input consists of which are shown here. The first component (enclosed ed in specifying the strings of L. It defines the symbol letter to stand for any upper or lower case letter, and digit to stand for any digit. The sec ond component enclosed between %% and %% contains the translation rules. The third component contains auxiliary routines which can be used in the semantic Figure 3: A sample LEX specification above Figure 3 defines the strings begin, end, := (the assignment operator), and identifier and constant strings of L. When an identifier is found, it is entered in the symbol table (if not already present) using the routine entry #) forms the token for the identifier string. is put in the global variable yylval, and the cla the call on scanner. Similar actions are taken on finding a constant, the keywords begin and end and the assignment operator. Compiling the Lexical Analyzer program, do the following: Sunita M. Dol, CSE Dept Page 3 units occurring in a source statement. Accordingly, the semantic actions make new of four components, which are shown here. The first component (enclosed by %{ and %}) L. It defines the symbol letter to stand for any upper or lower case letter, and digit to stand for any digit. The sec- ond component enclosed between %% and %% contains the translation rules. The iliary routines which can be used in the semantic : A sample LEX specification defines the strings begin, end, := (the L. When an identifier is found, it is entered in the symbol table (if not already present) using the routine forms the token for the identifier string. By is put in the global variable yylval, and the class code ID is the call on scanner. Similar actions are taken on finding a constant, the keywords begin and end and the assignment operator.
  • 4. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 4 1. Use the lex program to change the specification file into a C language program. The resulting program is in the lex.yy.c file. 2. Use the cc command with the -ll flag to compile and link the program with a library of lex subroutines. The resulting executable program is in the a.out file. For example, if the lex specification file is called lextest, enter the following commands: lex lextest cc lex.yy.c -ll Program: %{ %} delim [tn] ws delim+ letter [A-Za-z] digit [1-9] id {letter}({letter}|{digit})* number {digit}+(.{digit}+)?(E[+/-]?{digit}+)? %% {ws} {/*printf("white space");*/} if {printf("%s : Keywordn",yytext);} then {printf("%s : Keywordn",yytext);} else {printf("%s : Keywordn",yytext);} {id} {printf("Identifier");} {number} {printf("Number");} "<"|"<="|"=="|"<>"|">"|">=" {printf("Relational operator");} %% main() { yylex(); }
  • 5. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 5 Input: if Output: Keyword Conclusion: The lex command generates a C language program that can analyze an input stream using information in the specification file. The lex command stores the output program in a lex.yy.c file. If the output program recognizes a simple, one-word input structure, the lex.yy.c output file can compile to produce an executable lexical analyzer. Thus lexical analyzer generator or scanner is implemented using LEX