SlideShare a Scribd company logo
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)

More Related Content

What's hot

Handout#11
Handout#11Handout#11
Handout#11
Sunita Milind Dol
 
Handout#08
Handout#08Handout#08
Handout#08
Sunita Milind Dol
 
Assignment5
Assignment5Assignment5
Assignment5
Sunita Milind Dol
 
Handout#04
Handout#04Handout#04
Handout#04
Sunita Milind Dol
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
Dattatray Gandhmal
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
Munni28
 
Handout#06
Handout#06Handout#06
Handout#06
Sunita Milind Dol
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
abhishek gupta
 
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
MashaelQ
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
A. S. M. Shafi
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Farzana Aktar
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
Heshan Suriyaarachchi
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
Sudhaa Ravi
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
MashaelQ
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
Akshaya Arunan
 
Lex
LexLex

What's hot (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
 

Similar to Assignment4

COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
ManishBej3
 
Module 2
Module 2 Module 2
Module 2
ShwetaNirmanik
 
SS & CD Module 3
SS & CD Module 3 SS & CD Module 3
SS & CD Module 3
ShwetaNirmanik
 
Cd ch2 - lexical analysis
Cd   ch2 - lexical analysisCd   ch2 - lexical analysis
Cd ch2 - lexical analysis
mengistu23
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
TANZINTANZINA
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
ijistjournal
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Dr. Jaydeep Patil
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
Sumathi Gnanasekaran
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
Avinash Kapse
 
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
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Anujashejwal
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptx
YashaswiniYashu9555
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
Dr. C.V. Suresh Babu
 
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
IJCI JOURNAL
 
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
IJCI JOURNAL
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
SouvikRoy149
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
Sudhaa Ravi
 
phase of compiler
phase of compilerphase of compiler
phase of compiler
TECHNICALGYANIBABUAA
 

Similar to 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
 

More from Sunita Milind Dol

Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
Sunita Milind Dol
 
Unit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writingUnit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writing
Sunita Milind Dol
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
Sunita Milind Dol
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
Sunita Milind Dol
 
Unit Number 1 - Introduction to Research
Unit Number 1 - Introduction to ResearchUnit Number 1 - Introduction to Research
Unit Number 1 - Introduction to Research
Sunita Milind Dol
 
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
Sunita Milind Dol
 
Unit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writingUnit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writing
Sunita Milind Dol
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
Sunita Milind Dol
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
Sunita Milind Dol
 
Unit Number 1 : Introduction to Research
Unit Number 1 : Introduction to ResearchUnit Number 1 : Introduction to Research
Unit Number 1 : Introduction to Research
Sunita Milind Dol
 
9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
Sunita Milind Dol
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
Sunita Milind Dol
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
Sunita Milind Dol
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
Sunita Milind Dol
 
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
Sunita Milind Dol
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
Sunita Milind Dol
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
Sunita Milind Dol
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
Sunita Milind Dol
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
Sunita Milind Dol
 
Assignment12
Assignment12Assignment12
Assignment12
Sunita Milind Dol
 

More from Sunita Milind Dol (20)

Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
 
Unit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writingUnit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writing
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
 
Unit Number 1 - Introduction to Research
Unit Number 1 - Introduction to ResearchUnit Number 1 - Introduction to Research
Unit Number 1 - Introduction to Research
 
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
 
Unit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writingUnit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writing
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
 
Unit Number 1 : Introduction to Research
Unit Number 1 : Introduction to ResearchUnit Number 1 : Introduction to Research
Unit Number 1 : Introduction to Research
 
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
 

Recently uploaded

BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 

Recently uploaded (20)

BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 

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)