SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 1
Assignment No.4
AIM:
Implement lexical analyzer for C-language
THEORY:
Role of Lexical Analyzer
The lexical analyzer is the first phase of a compiler. Its main task is to read the
input characters and produce as output a sequence of tokens that the parser uses for
syntax analysis. This interaction, summarized schematically in Figure, is
commonly implemented by making the lexical analyzer be a subroutine or a
coroutine of the parser. Upon receiving a "get next token" command from the
parser, the lexical analyzer reads input characters until it can identify the next
token.
token
get next token
Fig: Interaction of lexical anlyzer with parser
Since the lexical analyzer is the part of the compiler that reads the source text, it
may also perform certain secondary tasks at the user interface. One such task is
stripping out from the source program comments and white space in the form of
blank, cab, and newline characters. Another is correlating error messages from the
Lexical
Analyzer
Symbol
Table
Parser
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 2
compiler with the source program. For example, the lexical analyzer may keep
track of the number of newline characters seen, so that a line number can be
associated with an error message. In some compilers, the lexical analyzer is in
charge of making a copy of the source program with the error messages marked in
it. If the source language supports some macro preprocessor functions, then these
preprocessor functions may also be implemented as lexical analysis takes place,
Issues in lexical analyzer
There are several reasons for separating the analysis phase of compiling into
lexical analysis and parsing.
 Simpler design is perhaps the most important consideration. The separation
of lexical analysis from syntax analysis often allows us to simplify one or
the other of these phases. For example, a parser embodying the conventions
for comments and white space is significantly more complex than one that
can assume comments and white space have already been removed by a
lexical analyzer. If we are designing a new language, separating the lexical
and syntactic conventions can lead to a cleaner overall language design.
 Compiler efficiency is improved. A separate lexical analyzer allows us to
construct a specialized and potentially more efficient processor for the task.
A large amount of time is spent reading the source program and partitioning
it into tokens. Specialized buffering techniques for reading input characters
and processing tokens can significantly speed up the performance of a
compiler,
 Compiler portability is enhanced. Input alphabet peculiarities and other
device-specific anomalies can be restricted to the lexical analyzer. The
representation of special or non-standard symbols, such as in Pascal, can be
isolated in the lexical analyzer.
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 3
Token, patterns & lexemes
TOKEN
• A classification for a common set of strings
• Examples Include <Identifier>, <number>, etc.
PATTERN
• The rules which characterize the set of strings for a token
• File and OS Wildcards ([A-Z]*.*)
LEXEME
• Actual sequence of characters that matches pattern and is classified by a
token
• Identifiers: x, count, name, etc…
Attributes of tokens
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 4
When more than cine pattern matches a lexeme, the lexical analyzer must provide
additional information about the particular lexeme that matched to the subsequent
phases of the compiler. For example, the pattern num matches both the strings 0
and 7, but it is essential for the code generator to know what string was actually
matched.
The lexical analyzer collects information about tokens into their associated
attributes, The tokens influence parsing decisions; the attributes influence the
translation of tokens. As a practical matter, a token has usually only a single
attribute - a pointer to the symbol-table entry in which the information about the
token is kept; the pointer becomes the attribute for the token.
Example: E = M * C ** 2
<id, pointer to symbol-table entry for E>
<assign_op, >
<id, pointer to symbol-table entry for M>
<mult_op, >
<id, pointer to symbol-table entry for C>
<exp_op, >
<num, integer value 2>
Lexical Error
Few errors are discernible at the lexical level alone, because a lexical analyzer has
a very localized view of a source program. If the string f i is encountered in a C
program for the first time in the context
fi((a == f(x))…..
a lexical analyzer cannot tell whether fi is a misspelling of the keyword if or an
undeclared function identifier. Since fi is a valid identifier, the lexical analyzer
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 5
must return the token for an identifier and let some other phase of the compiler
handle any error.
But, suppose a situation does arise in which the lexical analyzer is unable to
proceed because none of the patterns for tokens matches a prefix of the remaining
input. Perhaps the simplest recovery strategy is "panic mode" recovery. We delete
successive characters from the remaining input until the lexical analyzer can find a
well-formed token. This recovery technique may occasionally confuse the parser,
but in an interactive computing environment it may be quite adequate.
Other possible error-recovery actions are:
 Deleting an extraneous character
 Inserting a missing character
 Replacing an incorrect character by a correct character
 Transposing two adjacent characters.
INPUT:
TRIAL.CPP
# include< stdio.h>
# include< conio.h>
void main()
{
int num1= 5 , count= 1 , ab= 10 ;
char ch;
printf ( "This is a trial program" ) ;
while ( count ! = num1 )
{
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 6
ab = ab* count/ 2 ;
if ( count== 3 )
count= count+ 1 ;
printf ( "AB %d" , ab);
}
}
OUTPUT:
Token ID=0 # Special Character
Token ID=1 include Keyword type
Token ID=2 < Special Character
Token ID=3 stdio Keyword type
Token ID=4 . Special Character
Token ID=5 h Identifier type
Token ID=6 > Special Character
Token ID=7 # Special Character
Token ID=8 include Keyword type
Token ID=9 < Special Character
Token ID=10 conio Keyword type
Token ID=11 . Special Character
Token ID=12 h Identifier type
Token ID=13 > Special Character
Token ID=14 void Keyword type
Token ID=15 main Keyword type
Token ID=16 ( Special Character
Token ID=17 ) Special Character
Token ID=18 { Special Character
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 7
Token ID=19 int Keyword type
Token ID=20 num1 Identifier type
Token ID=21 = Operator type
Token ID=22 5 Numeric type
Token ID=23 , Special Character
Token ID=24 count Identifier type
Token ID=25 = Operator type
Token ID=26 1 Numeric type
Token ID=27 , Special Character
Token ID=28 ab Identifier type
Token ID=29 = Operator type
Token ID=30 10 Numeric type
Token ID=31 ; Special Character
Token ID=32 char Keyword type
Token ID=33 ch Identifier type
Token ID=34 ; Special Character
Token ID=35 printf Keyword type
Token ID=36 ( Special Character
Token ID=37 This is a trial program Literal type
Token ID=38 ) Special Character
Token ID=39 ; Special Character
Token ID=40 while Keyword type
Token ID=41 ( Special Character
Token ID=42 count Identifier type
Token ID=43 ! Special Character
Token ID=44 = Operator type
Token ID=45 num1 Identifier type
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 8
Token ID=46 ) Special Character
Token ID=47 { Special Character
Token ID=48 ab Identifier type
Token ID=49 = Operator type
Token ID=50 ab Identifier type
Token ID=51 * Operator type
Token ID=52 count Identifier type
Token ID=53 / Operator type
Token ID=54 2 Numeric type
Token ID=55 ; Special Character
Token ID=56 if Keyword type
Token ID=57 ( Special Character
Token ID=58 count Identifier type
Token ID=59 = Operator type
Token ID=60 = Operator type
Token ID=61 3 Numeric type
Token ID=62 ) Special Character
Token ID=63 count Identifier type
Token ID=64 = Operator type
Token ID=65 count Identifier type
Token ID=66 + Operator type
Token ID=67 1 Numeric type
Token ID=68 ; Special Character
Token ID=69 printf Keyword type
Token ID=70 ( Special Character
Token ID=71 AB %d Literal type
Token ID=72 , Special Character
Compiler Construction Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 9
Token ID=73 ab Identifier type
Token ID=74 ) Special Character
Token ID=75 ; Special Character
Token ID=76 } Special Character
Token ID=77 } Special Character
CONCLUSION:
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)

Weitere ähnliche Inhalte

Was ist angesagt?

Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 

Was ist angesagt? (20)

Handout#11
Handout#11Handout#11
Handout#11
 
Handout#08
Handout#08Handout#08
Handout#08
 
Assignment5
Assignment5Assignment5
Assignment5
 
Handout#04
Handout#04Handout#04
Handout#04
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Handout#06
Handout#06Handout#06
Handout#06
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Lex
LexLex
Lex
 

Ähnlich wie Assignment4

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
kalpana Manudhane
 
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
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptx
YashaswiniYashu9555
 

Ähnlich wie Assignment4 (20)

COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
 
Module 2
Module 2 Module 2
Module 2
 
SS & CD Module 3
SS & CD Module 3 SS & CD Module 3
SS & CD Module 3
 
Cd ch2 - lexical analysis
Cd   ch2 - lexical analysisCd   ch2 - lexical analysis
Cd ch2 - lexical analysis
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
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
 
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
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptx
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
phase of compiler
phase of compilerphase of compiler
phase of compiler
 

Mehr von Sunita Milind Dol

Mehr von Sunita Milind Dol (19)

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
 
Assignment3
Assignment3Assignment3
Assignment3
 
Assignment2
Assignment2Assignment2
Assignment2
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#07
Handout#07Handout#07
Handout#07
 

Kürzlich hochgeladen

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 

Kürzlich hochgeladen (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 

Assignment4

  • 1. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 1 Assignment No.4 AIM: Implement lexical analyzer for C-language THEORY: Role of Lexical Analyzer The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters and produce as output a sequence of tokens that the parser uses for syntax analysis. This interaction, summarized schematically in Figure, is commonly implemented by making the lexical analyzer be a subroutine or a coroutine of the parser. Upon receiving a "get next token" command from the parser, the lexical analyzer reads input characters until it can identify the next token. token get next token Fig: Interaction of lexical anlyzer with parser Since the lexical analyzer is the part of the compiler that reads the source text, it may also perform certain secondary tasks at the user interface. One such task is stripping out from the source program comments and white space in the form of blank, cab, and newline characters. Another is correlating error messages from the Lexical Analyzer Symbol Table Parser
  • 2. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 2 compiler with the source program. For example, the lexical analyzer may keep track of the number of newline characters seen, so that a line number can be associated with an error message. In some compilers, the lexical analyzer is in charge of making a copy of the source program with the error messages marked in it. If the source language supports some macro preprocessor functions, then these preprocessor functions may also be implemented as lexical analysis takes place, Issues in lexical analyzer There are several reasons for separating the analysis phase of compiling into lexical analysis and parsing.  Simpler design is perhaps the most important consideration. The separation of lexical analysis from syntax analysis often allows us to simplify one or the other of these phases. For example, a parser embodying the conventions for comments and white space is significantly more complex than one that can assume comments and white space have already been removed by a lexical analyzer. If we are designing a new language, separating the lexical and syntactic conventions can lead to a cleaner overall language design.  Compiler efficiency is improved. A separate lexical analyzer allows us to construct a specialized and potentially more efficient processor for the task. A large amount of time is spent reading the source program and partitioning it into tokens. Specialized buffering techniques for reading input characters and processing tokens can significantly speed up the performance of a compiler,  Compiler portability is enhanced. Input alphabet peculiarities and other device-specific anomalies can be restricted to the lexical analyzer. The representation of special or non-standard symbols, such as in Pascal, can be isolated in the lexical analyzer.
  • 3. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 3 Token, patterns & lexemes TOKEN • A classification for a common set of strings • Examples Include <Identifier>, <number>, etc. PATTERN • The rules which characterize the set of strings for a token • File and OS Wildcards ([A-Z]*.*) LEXEME • Actual sequence of characters that matches pattern and is classified by a token • Identifiers: x, count, name, etc… Attributes of tokens
  • 4. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 4 When more than cine pattern matches a lexeme, the lexical analyzer must provide additional information about the particular lexeme that matched to the subsequent phases of the compiler. For example, the pattern num matches both the strings 0 and 7, but it is essential for the code generator to know what string was actually matched. The lexical analyzer collects information about tokens into their associated attributes, The tokens influence parsing decisions; the attributes influence the translation of tokens. As a practical matter, a token has usually only a single attribute - a pointer to the symbol-table entry in which the information about the token is kept; the pointer becomes the attribute for the token. Example: E = M * C ** 2 <id, pointer to symbol-table entry for E> <assign_op, > <id, pointer to symbol-table entry for M> <mult_op, > <id, pointer to symbol-table entry for C> <exp_op, > <num, integer value 2> Lexical Error Few errors are discernible at the lexical level alone, because a lexical analyzer has a very localized view of a source program. If the string f i is encountered in a C program for the first time in the context fi((a == f(x))….. a lexical analyzer cannot tell whether fi is a misspelling of the keyword if or an undeclared function identifier. Since fi is a valid identifier, the lexical analyzer
  • 5. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 5 must return the token for an identifier and let some other phase of the compiler handle any error. But, suppose a situation does arise in which the lexical analyzer is unable to proceed because none of the patterns for tokens matches a prefix of the remaining input. Perhaps the simplest recovery strategy is "panic mode" recovery. We delete successive characters from the remaining input until the lexical analyzer can find a well-formed token. This recovery technique may occasionally confuse the parser, but in an interactive computing environment it may be quite adequate. Other possible error-recovery actions are:  Deleting an extraneous character  Inserting a missing character  Replacing an incorrect character by a correct character  Transposing two adjacent characters. INPUT: TRIAL.CPP # include< stdio.h> # include< conio.h> void main() { int num1= 5 , count= 1 , ab= 10 ; char ch; printf ( "This is a trial program" ) ; while ( count ! = num1 ) {
  • 6. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 6 ab = ab* count/ 2 ; if ( count== 3 ) count= count+ 1 ; printf ( "AB %d" , ab); } } OUTPUT: Token ID=0 # Special Character Token ID=1 include Keyword type Token ID=2 < Special Character Token ID=3 stdio Keyword type Token ID=4 . Special Character Token ID=5 h Identifier type Token ID=6 > Special Character Token ID=7 # Special Character Token ID=8 include Keyword type Token ID=9 < Special Character Token ID=10 conio Keyword type Token ID=11 . Special Character Token ID=12 h Identifier type Token ID=13 > Special Character Token ID=14 void Keyword type Token ID=15 main Keyword type Token ID=16 ( Special Character Token ID=17 ) Special Character Token ID=18 { Special Character
  • 7. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 7 Token ID=19 int Keyword type Token ID=20 num1 Identifier type Token ID=21 = Operator type Token ID=22 5 Numeric type Token ID=23 , Special Character Token ID=24 count Identifier type Token ID=25 = Operator type Token ID=26 1 Numeric type Token ID=27 , Special Character Token ID=28 ab Identifier type Token ID=29 = Operator type Token ID=30 10 Numeric type Token ID=31 ; Special Character Token ID=32 char Keyword type Token ID=33 ch Identifier type Token ID=34 ; Special Character Token ID=35 printf Keyword type Token ID=36 ( Special Character Token ID=37 This is a trial program Literal type Token ID=38 ) Special Character Token ID=39 ; Special Character Token ID=40 while Keyword type Token ID=41 ( Special Character Token ID=42 count Identifier type Token ID=43 ! Special Character Token ID=44 = Operator type Token ID=45 num1 Identifier type
  • 8. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 8 Token ID=46 ) Special Character Token ID=47 { Special Character Token ID=48 ab Identifier type Token ID=49 = Operator type Token ID=50 ab Identifier type Token ID=51 * Operator type Token ID=52 count Identifier type Token ID=53 / Operator type Token ID=54 2 Numeric type Token ID=55 ; Special Character Token ID=56 if Keyword type Token ID=57 ( Special Character Token ID=58 count Identifier type Token ID=59 = Operator type Token ID=60 = Operator type Token ID=61 3 Numeric type Token ID=62 ) Special Character Token ID=63 count Identifier type Token ID=64 = Operator type Token ID=65 count Identifier type Token ID=66 + Operator type Token ID=67 1 Numeric type Token ID=68 ; Special Character Token ID=69 printf Keyword type Token ID=70 ( Special Character Token ID=71 AB %d Literal type Token ID=72 , Special Character
  • 9. Compiler Construction Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 9 Token ID=73 ab Identifier type Token ID=74 ) Special Character Token ID=75 ; Special Character Token ID=76 } Special Character Token ID=77 } Special Character CONCLUSION: REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)