Handout#02

S
Sunita Milind DolAssistant Professor em Walchand Institute of Technology, Solapur

Scanner using LEX Programming Language

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

Recomendados

Assignment11 por
Assignment11Assignment11
Assignment11Sunita Milind Dol
89 visualizações6 slides
Handout#03 por
Handout#03Handout#03
Handout#03Sunita Milind Dol
144 visualizações10 slides
Handout#10 por
Handout#10Handout#10
Handout#10Sunita Milind Dol
129 visualizações8 slides
Handout#11 por
Handout#11Handout#11
Handout#11Sunita Milind Dol
145 visualizações8 slides
Assignment1 por
Assignment1Assignment1
Assignment1Sunita Milind Dol
81 visualizações9 slides
Handout#08 por
Handout#08Handout#08
Handout#08Sunita Milind Dol
136 visualizações5 slides

Mais conteúdo relacionado

Mais procurados

Handout#07 por
Handout#07Handout#07
Handout#07Sunita Milind Dol
136 visualizações6 slides
Handout#09 por
Handout#09Handout#09
Handout#09Sunita Milind Dol
128 visualizações8 slides
Handout#12 por
Handout#12Handout#12
Handout#12Sunita Milind Dol
185 visualizações13 slides
Handout#06 por
Handout#06Handout#06
Handout#06Sunita Milind Dol
138 visualizações7 slides
Handout#04 por
Handout#04Handout#04
Handout#04Sunita Milind Dol
146 visualizações5 slides
Handout#01 por
Handout#01Handout#01
Handout#01Sunita Milind Dol
133 visualizações8 slides

Mais procurados(20)

Compiler gate question key por ArthyR3
Compiler gate question keyCompiler gate question key
Compiler gate question key
ArthyR3139 visualizações
Principles of compiler design por Janani Parthiban
Principles of compiler designPrinciples of compiler design
Principles of compiler design
Janani Parthiban7.6K visualizações
Compiler Engineering Lab#1 por MashaelQ
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1
MashaelQ1.6K visualizações
Java conceptual learning material por ArthyR3
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
ArthyR3284 visualizações
Compiler question bank por ArthyR3
Compiler question bankCompiler question bank
Compiler question bank
ArthyR3421 visualizações
Lexical analyzer por Ashwini Sonawane
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane48.3K visualizações
Language processors por Ganesh Wedpathak
Language processorsLanguage processors
Language processors
Ganesh Wedpathak14.1K visualizações
C language por spatidar0
C languageC language
C language
spatidar0859 visualizações
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str... por Bhavin Darji
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 Darji1.2K visualizações
Programming in c por ankitjain851
Programming in cProgramming in c
Programming in c
ankitjain8515.3K visualizações
Chapter2 por Anees999
Chapter2Chapter2
Chapter2
Anees999657 visualizações
Structure of the compiler por Sudhaa Ravi
Structure of the compilerStructure of the compiler
Structure of the compiler
Sudhaa Ravi1.7K visualizações
Solutions manual for c++ programming from problem analysis to program design ... por Warren0989
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 ...
Warren098918.4K visualizações
Compiler design important questions por akila viji
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
akila viji370 visualizações

Similar a Handout#02

module 4.pptx por
module 4.pptxmodule 4.pptx
module 4.pptxShwetaNirmanik
11 visualizações47 slides
Language for specifying lexical Analyzer por
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
1.2K visualizações16 slides
LANGUAGE PROCESSOR por
LANGUAGE PROCESSORLANGUAGE PROCESSOR
LANGUAGE PROCESSOREZIOAUDITORE15070
52 visualizações8 slides
Compiler Design por
Compiler DesignCompiler Design
Compiler DesignAnujashejwal
63 visualizações34 slides
Module4 lex and yacc.ppt por
Module4 lex and yacc.pptModule4 lex and yacc.ppt
Module4 lex and yacc.pptProddaturNagaVenkata
50 visualizações234 slides
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat... por
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
594 visualizações88 slides

Similar a Handout#02(20)

module 4.pptx por ShwetaNirmanik
module 4.pptxmodule 4.pptx
module 4.pptx
ShwetaNirmanik11 visualizações
Language for specifying lexical Analyzer por Archana Gopinath
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
Archana Gopinath1.2K visualizações
LANGUAGE PROCESSOR por EZIOAUDITORE15070
LANGUAGE PROCESSORLANGUAGE PROCESSOR
LANGUAGE PROCESSOR
EZIOAUDITORE1507052 visualizações
Compiler Design por Anujashejwal
Compiler DesignCompiler Design
Compiler Design
Anujashejwal63 visualizações
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat... por Prof Chethan Raj C
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 C594 visualizações
role of lexical anaysis por Sudhaa Ravi
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
Sudhaa Ravi66 visualizações
11700220036.pdf por SouvikRoy149
11700220036.pdf11700220036.pdf
11700220036.pdf
SouvikRoy1495 visualizações
Compiler Design lab manual for Computer Engineering .pdf por kalpana Manudhane
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdf
kalpana Manudhane37 visualizações
Lex Tool por Rajan Shah
Lex ToolLex Tool
Lex Tool
Rajan Shah1.5K visualizações
Chapter _4_Semantic Analysis .pptx por ArebuMaruf
Chapter _4_Semantic Analysis .pptxChapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptx
ArebuMaruf18 visualizações
Ch 2.pptx por woldu2
Ch 2.pptxCh 2.pptx
Ch 2.pptx
woldu216 visualizações
Lexical and Parser tool for CBOOP program por IOSR Journals
Lexical and Parser tool for CBOOP programLexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP program
IOSR Journals293 visualizações
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR por ijistjournal
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
ijistjournal20 visualizações
Compiler Design por Dr. Jaydeep Patil
Compiler DesignCompiler Design
Compiler Design
Dr. Jaydeep Patil805 visualizações
Lexical Analysis por Munni28
Lexical AnalysisLexical Analysis
Lexical Analysis
Munni2810.4K visualizações
Symbol Table, Error Handler & Code Generation por Akhil Kaushik
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
Akhil Kaushik257 visualizações
unit 1 cpds.pptx por madhurij54
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
madhurij5420 visualizações

Mais de Sunita Milind Dol

9.Joins.pdf por
9.Joins.pdf9.Joins.pdf
9.Joins.pdfSunita Milind Dol
1.7K visualizações9 slides
8.Views.pdf por
8.Views.pdf8.Views.pdf
8.Views.pdfSunita Milind Dol
1.7K visualizações5 slides
7. Nested Subqueries.pdf por
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdfSunita Milind Dol
1.7K visualizações9 slides
6. Aggregate Functions.pdf por
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdfSunita Milind Dol
1.7K visualizações12 slides
5. Basic Structure of SQL Queries.pdf por
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdfSunita Milind Dol
1.7K visualizações64 slides
4. DML.pdf por
4. DML.pdf4. DML.pdf
4. DML.pdfSunita Milind Dol
1.7K visualizações17 slides

Mais de Sunita Milind Dol(17)

9.Joins.pdf por Sunita Milind Dol
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
Sunita Milind Dol1.7K visualizações
8.Views.pdf por Sunita Milind Dol
8.Views.pdf8.Views.pdf
8.Views.pdf
Sunita Milind Dol1.7K visualizações
7. Nested Subqueries.pdf por Sunita Milind Dol
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
Sunita Milind Dol1.7K visualizações
6. Aggregate Functions.pdf por Sunita Milind Dol
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
Sunita Milind Dol1.7K visualizações
5. Basic Structure of SQL Queries.pdf por Sunita Milind Dol
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
Sunita Milind Dol1.7K visualizações
4. DML.pdf por Sunita Milind Dol
4. DML.pdf4. DML.pdf
4. DML.pdf
Sunita Milind Dol1.7K visualizações
3. DDL.pdf por Sunita Milind Dol
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
Sunita Milind Dol1.7K visualizações
2. SQL Introduction.pdf por Sunita Milind Dol
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
Sunita Milind Dol1.7K visualizações
1. University Example.pdf por Sunita Milind Dol
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
Sunita Milind Dol1.7K visualizações

Último

13_DVD_Latch-up_prevention.pdf por
13_DVD_Latch-up_prevention.pdf13_DVD_Latch-up_prevention.pdf
13_DVD_Latch-up_prevention.pdfUsha Mehta
9 visualizações16 slides
Pull down shoulder press final report docx (1).pdf por
Pull down shoulder press final report docx (1).pdfPull down shoulder press final report docx (1).pdf
Pull down shoulder press final report docx (1).pdfComsat Universal Islamabad Wah Campus
8 visualizações25 slides
FLOW IN PIPES NOTES.pdf por
FLOW IN PIPES NOTES.pdfFLOW IN PIPES NOTES.pdf
FLOW IN PIPES NOTES.pdfDearest Arhelo
90 visualizações10 slides
What is Unit Testing por
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
23 visualizações25 slides
SNMPx por
SNMPxSNMPx
SNMPxAmatullahbutt
12 visualizações12 slides
SPICE PARK DEC2023 (6,625 SPICE Models) por
SPICE PARK DEC2023 (6,625 SPICE Models) SPICE PARK DEC2023 (6,625 SPICE Models)
SPICE PARK DEC2023 (6,625 SPICE Models) Tsuyoshi Horigome
14 visualizações218 slides

Último(20)

13_DVD_Latch-up_prevention.pdf por Usha Mehta
13_DVD_Latch-up_prevention.pdf13_DVD_Latch-up_prevention.pdf
13_DVD_Latch-up_prevention.pdf
Usha Mehta9 visualizações
FLOW IN PIPES NOTES.pdf por Dearest Arhelo
FLOW IN PIPES NOTES.pdfFLOW IN PIPES NOTES.pdf
FLOW IN PIPES NOTES.pdf
Dearest Arhelo90 visualizações
What is Unit Testing por Sadaaki Emura
What is Unit TestingWhat is Unit Testing
What is Unit Testing
Sadaaki Emura23 visualizações
SNMPx por Amatullahbutt
SNMPxSNMPx
SNMPx
Amatullahbutt12 visualizações
SPICE PARK DEC2023 (6,625 SPICE Models) por Tsuyoshi Horigome
SPICE PARK DEC2023 (6,625 SPICE Models) SPICE PARK DEC2023 (6,625 SPICE Models)
SPICE PARK DEC2023 (6,625 SPICE Models)
Tsuyoshi Horigome14 visualizações
Digital Watermarking Of Audio Signals.pptx por AyushJaiswal781174
Digital Watermarking Of Audio Signals.pptxDigital Watermarking Of Audio Signals.pptx
Digital Watermarking Of Audio Signals.pptx
AyushJaiswal7811748 visualizações
What is Whirling Hygrometer.pdf por IIT KHARAGPUR
What is Whirling Hygrometer.pdfWhat is Whirling Hygrometer.pdf
What is Whirling Hygrometer.pdf
IIT KHARAGPUR 11 visualizações
NEW SUPPLIERS SUPPLIES (copie).pdf por georgesradjou
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdf
georgesradjou7 visualizações
STUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptx por AnnieRachelJohn
STUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptxSTUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptx
STUDY OF SMART MATERIALS USED IN CONSTRUCTION-1.pptx
AnnieRachelJohn25 visualizações
Multi-objective distributed generation integration in radial distribution sy... por IJECEIAES
Multi-objective distributed generation integration in radial  distribution sy...Multi-objective distributed generation integration in radial  distribution sy...
Multi-objective distributed generation integration in radial distribution sy...
IJECEIAES15 visualizações
performance uploading.pptx por SanthiS10
performance uploading.pptxperformance uploading.pptx
performance uploading.pptx
SanthiS107 visualizações
An approach of ontology and knowledge base for railway maintenance por IJECEIAES
An approach of ontology and knowledge base for railway maintenanceAn approach of ontology and knowledge base for railway maintenance
An approach of ontology and knowledge base for railway maintenance
IJECEIAES12 visualizações
Dynamics of Hard-Magnetic Soft Materials por Shivendra Nandan
Dynamics of Hard-Magnetic Soft MaterialsDynamics of Hard-Magnetic Soft Materials
Dynamics of Hard-Magnetic Soft Materials
Shivendra Nandan13 visualizações
LFA-NPG-Paper.pdf por harinsrikanth
LFA-NPG-Paper.pdfLFA-NPG-Paper.pdf
LFA-NPG-Paper.pdf
harinsrikanth40 visualizações
Thermal aware task assignment for multicore processors using genetic algorithm por IJECEIAES
Thermal aware task assignment for multicore processors using genetic algorithm Thermal aware task assignment for multicore processors using genetic algorithm
Thermal aware task assignment for multicore processors using genetic algorithm
IJECEIAES29 visualizações
Saikat Chakraborty Java Oracle Certificate.pdf por SaikatChakraborty787148
Saikat Chakraborty Java Oracle Certificate.pdfSaikat Chakraborty Java Oracle Certificate.pdf
Saikat Chakraborty Java Oracle Certificate.pdf
SaikatChakraborty78714813 visualizações
A multi-microcontroller-based hardware for deploying Tiny machine learning mo... por IJECEIAES
A multi-microcontroller-based hardware for deploying Tiny machine learning mo...A multi-microcontroller-based hardware for deploying Tiny machine learning mo...
A multi-microcontroller-based hardware for deploying Tiny machine learning mo...
IJECEIAES10 visualizações

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