Structure of a Compiler, Compiler and Interpreter, Lexical Analysis: Role of the lexical analyzer

GunjalSanjay 161 views 22 slides Mar 06, 2025
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Structure of a Compiler, Compiler and Interpreter, Lexical Analysis: Role of the lexical analyzer


Slide Content

Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Dr. S. N. Gunjal
Assistant Professor
E-mail : [email protected]
Contact No: 91301 91301 Ext :145, 9503916876
Course-System Software
(CO313)
Structure of a Compiler, Compiler and Interpreter, Lexical Analysis: Role of the lexical
analyzer
Dr. S.N Gunjal

Compiler
Compiler is a program that can read a program in one language -the source language -and translate
it into an equivalent program in another language -the target language.
An important role of the compiler is to report any errors in the source program that it detects during
the translation process.
Compiler
Source
Program
Target
Program
Errors
Figure 1.1 : A compiler

Compiler
If the target program is an executable machine-language program, it can then be called by the user to
process inputs and produce outputs;
Target Program
Input
Output
Figure 1.2: Running the target program

Interpreter
Aninterpreterisanothercommonkindoflanguageprocessor.Insteadofproducinga
targetprogramasatranslation,aninterpreterappearstodirectlyexecutetheoperations
specifiedinthesourceprogramoninputssuppliedbytheuser.
Interpreter
Input
Output
Source
Program
Figure 1.3: An interpreter

The machine-language target program produced by a compiler is usually much faster than an
interpreter at mapping inputs to outputs .
An interpreter, however, can usually give better error diagnostics than a compiler, because it
executes the source program statement by statement.
Interpreter

5
Structure of
Compiler

Structure of Compiler

The analysis part breaks up the source program into constituent pieces and imposes a
grammatical structure on them. It then uses this structure to create an intermediate representation of
the source program.
The analysis part also collects information about the source program and stores it in a data
structure called a symbol table, which is passed along with the intermediate representation to the
synthesis part.
Analysis and Synthesis Phase of Compiler
Analysis Phase :

Synthesis Phase
The synthesis part constructs the desired target program from the interme-diate representation and the
information in the symbol table.
The analysis part is often called the front end of the compiler; the synthesis part is the back end.

The first phase of a compiler is called lexical analysis or scanning.
Thelexicalanalyzerreadsthestreamofcharactersmakingupthesourceprogram.and
groupsthecharactersintomeaningfulsequencescalledlexemes.
Foreachlexeme,thelexicalanalyzerproducesasoutputatokenoftheform
(token-name,attribute-value)
position=initial+rate*60;
sequenceoftokens: <id,1><=><id,2><+><id,3><*><60><;>
Compiler Phases:
Lexical Analysis Phase :

Thesecondphaseofthecompilerissyntaxanalysisorparsing.
Theparserusesthefirstcomponentsofthetokensproducedbythelexical
analyzertocreateatree-likeintermediaterepresentationthatdepictsthe
grammaticalstructureofthetokenstream.
Atypicalrepresentationisasyntaxtreeinwhicheachinteriornode
representsanoperationandthechildrenofthenoderepresentthearguments
oftheoperation.
Compiler Phases:
Syntax Analysis :

Thesemanticanalyzerusesthesyntaxtreeandtheinformationinthe
symboltabletocheckthesourceprogramforsemanticconsistencywiththe
languagedefinition.
Italsogatherstypeinformationandsavesitineitherthesyntaxtreeorthe
symboltable,forsubsequentuseduringintermediate-codegeneration.
Compiler Phases:
Sematic Analysis :

Intheprocessoftranslatingasourceprogramintotargetcode,acompiler
mayconstructoneormoreintermediaterepresentations,whichcanhavea
varietyofforms.
Anintermediateformcalledthree-addresscode,whichconsistsofa
sequenceofassembly-likeinstructionswiththreeoperandsperinstruction.
tl = inttofloat(60)
t2 = id3 * tl
t3 = id2 + t2
id1 = t3
Compiler Phases:
Intermediate Code Generation:

Themachine-independentcode-optimizationphaseattemptstoimprovethe
intermediatecodesothatbettertargetcodewillresult.
Compiler Phases:
Code Optimization:

Thecodegeneratortakesasinputanintermediaterepresentationofthe
sourceprogramandmapsitintothetargetlanguage.
Theintermediateinstructionsaretranslatedintosequencesofmachine
instructionsthatperformthesametask.
Compiler Phases:
Code Generation:

THE ROLE OF THE LEXICAL ANALYZER
Thefirstphaseofacompileriscalledlexicalanalysisorscanning.
Thelexicalanalyzerreadsthestreamofcharactersmakingupthesource
program.andgroupsthecharactersintomeaningfulsequencescalled
lexemes.
Foreachlexeme,thelexicalanalyzerproducesasoutputatokenofthe
form (token-name,attribute-value)
position=initial+rate*60;
sequenceoftokens:<id,1><=><id,2><+><id,3><*><60><;>
Thelexicalanalyzerisresponsibleforremovingthewhitespacesand
commentsfromthesourceprogram.

15/06/20 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
THE ROLE OF THE LEXICAL ANALYZER

THE ROLE OF THE LEXICAL ANALYZER
Token:Atokenisapair–atokennameandanoptional
tokenvalue.
Apatternisadescriptionoftheformthatthelexemesofatokenmaytake.
Alexemeisasequenceofcharactersinthesourceprogramthatmatchesthe
patternforatoken
Alexicalerroroccurswhenasequenceofcharactersdoesnotmatch
thepatternofanytoken.

15/06/20 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
Attributes for tokens
• 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>
–<number, integer value 2>

References
1.Alfred V.Aho,MonicaS.Lam,RaviSethi, Jeffrey D. Ullman, “Compilers-
Principles,Techniquesand Tools”, Pearson,ISBN:978-81-317-2101-8