SlideShare a Scribd company logo
1 of 39
Download to read offline
JL-Jaladhulagori (via Andul Mouri), Sankrail District : Howrah (West Bengal) Pin : 711302
1
LEXICAL
ANALYSIS
Structure
of a
Compiler
Source Language
Semantic Analyzer
Syntax Analyzer
Lexical Analyzer
Front
End
Code Optimizer
Target Code Generator
Back
End
Int. Code Generator
Intermediate Code
Target Language
4
TODAY!
5
Source Language
Semantic Analyzer
Syntax Analyzer
Lexical Analyzer
Front
End
Code Optimizer
Target Code Generator
Back
End
Int. Code Generator
Intermediate Code
Target Language
The Role of Lexical
Analyzer
The lexical analyzer is the first phase of
a compiler. The main task of lexical
Analyzer(Scanner) is to read a stream
of characters as an input and produce
a sequence of tokens that the
parser(Syntax Analyzer) uses for syntax
analysis.
Read Character Token
Symbol
Table
Parser
Lexical
Analyzer
input
Push back
character
THE ROLE OF LEXICAL
ANALYZER (CONT’D)
7
Get Next Token
When a lexical analyzer is inserted b/w the parser and
input stream ,it interacts with two in the manner shown in
the above fig.It reads characters from the input ,groups
them into lexeme and passes the tokens formed by the
lexemes,together with their attribute value,to the
parser.In some situations ,the lexical analyzer has to
read some characters ahead before it can decide on the
token to be returned to the parser.
THE ROLE OF LEXICAL
ANALYZER (CONT’D)
8
The Role of Lexical Analyzer
(cont’d)
9
For example, a lexical analyzer for Pascal
must read ahead after it sees the character
>.If the next character is = ,then the
character sequence >= is the lexeme forming the
token for the “Greater than or equal to ”
operator. Other wise > is the lexeme forming
“Greater than ” operator ,and the lexical
analyzer has read one character too many.
The Role of Lexical Analyzer
(cont’d)
The extra character has to be pushed back on
to the input,because it can be the
beginning of the next lexeme in the input.The
lexical analyzer and parser form a producer-
Consumer pair. The lexical analyzer
produces tokens and the parser consumes
them.Produced tokens can be held in a token
buffer until they are consumed.
10
THE ROLE OF LEXICAL
ANALYZER (CONT’D)
11
The interaction b/w the two is constrained only by the
size of the buffer,because the lexical analyzer can
not proceed when the buffer is full and the parser
can not proceed when the buffer is empty.Commonly
,the buffer holds just one token.In this case,the
interaction can be implemented simply by making
the lexical analyzer be a procedure called by the
parser,returning tokens on demand.
The Role of Lexical Analyzer
(cont’d)
The implementation of reading and pushing
back character is usually done by setting up
an input buffer.A block of character is read
into the buffer at a time; a pointer keeps track
of the portion of the input that has been
analyzed.Pushing back a character is
implemented by moving the pointer.
12
The Role of Lexical Analyzer
(cont’d)
Some times lexical analyzer are divided into
two phases,the first is called Scanning and
the second is called Lexical Analysis.The
scanning is responsible for doing simple
tasks ,while the lexical analyzer does the
more complex operations.For example, a
Fortran might use a scanner to eliminate
blanks from the input.
13
ISSUES IN LEXICAL
ANALYSIS
14
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 simply one
or the other of these phases.For example,a parser
embodying the conventions for comments and
white space is significantly more complex---
1)
ISSUES IN LEXICAL
ANALYSIS (CONT’D)
15
--then 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
over all language design.
ISSUES IN LEXICAL
ANALYSIS (CONT’D)
2) 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 in put characters and processing
tokens can significantly speed up the
performance of a compiler.
16
ISSUES IN LEXICAL
ANALYSIS (CONT’D)
17
3) 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.
ISSUES IN LEXICAL
ANALYSIS (CONT’D)
18
Specialized tools have been designed to help
automate the construction of lexical
analyzers and parsers when they are
separated.
WHAT EXACTLY IS LEXING?
19
Consider the code:
if (i==j);
z=1;
else;
z=0;
endif;
i f _(i= = j);ntz=1;nels e;ntz=0;nendif;
This is really nothing more than a string of characters:
During our lexical analysis phase we must divide this string into meaningful sub-
strings.
TOKENS
20
● The output of our lexical analysis phase is a streams of tokens.
● A token is a syntactic category.
● In English this would be types of words or punctuation, such as
a “noun”, “verb”, “adjective” or “end-mark”.
● In a program, this could be an “identifier”, a “floating-point
number”, a “math symbol”, a “keyword”, etc…
IDENTIFYING TOKENS
21
● A sub-string that represents an instance of a token is called a
lexeme.
● The class of all possible lexemes in a token is described by the
use of a pattern.
● For example, the pattern to describe an identifier (a variable) is
a string of letters, numbers, or underscores, beginning with a
non-number.
●
Patterns are typically described using regular expressions.
IMPLEMENTATION
22
A lexical analyzer must be able to do three things:
1. Remove all whitespace and comments.
2. Identify tokens within a string.
3.Return the lexeme of a found token, as well as the
line number it was found on.
21
EXAMPLE
if_ ( i = = j);ntz=1;nelse;ntz=0;nendif;
Lexeme
if
(
i
==
j
)
;
z
=
1
;
Line
1
1
1
1
1
1
1
2
2
2
2
3
Token
BLOCK_COMMAND
OPEN_PAREN
ID
OP_RELATION
ID
CLOSE_PAREN
ENDLINE
ID
ASSIGN
NUMBER
ENDLINE
BLOCK_COMMAND else
●
●
●
●
●
●
●
●
●
●
●
●
●
● Etc…
LOOKAHEAD
24
● Lookahead will typically be important to a lexical analyzer.
● Tokens are typically read in from left-to-right, recognized one at
a time from the input string.
● It is not always possible to instantly decide if a token is finished
without looking ahead at the next character. For example…
●
●
Is “i” a variable, or the first character of “if”?
Is “=” an assignment or the beginning of “==”?
TOKENS
25
The output of our lexical analysis phase is a streams of tokens.A
token is a syntactic category.
“A name for a set of input strings with related structure”
Example: “ID,” “NUM”, “RELATION“,”IF”
In English this would be types of words or punctuation, such as
a “noun”, “verb”, “adjective” or “end-mark”.
In a program, this could be an “identifier”, a “floating-point
number”, a “math symbol”, a “keyword”, etc…
TOKENS (CONT’D)
26
As an example,consider the following line of
code,which could be part of a “ C ” program.
a [index] = 4 + 2
TOKENS (CONT’D)
27
a ID
[ Left bracket
index ID
] Right bracket
= Assign
4 Num
+ plus sign
2 Num
TOKENS (CONT’D)
28
characters that are collected into a
Each token consists of one or more
unit
before further processing takes place.
A scanner may perform other operations
along with the recognition of tokens.For
example,it may enter identifiers into the
symbol table.
ATTRIBUTES FOR TOKENS
29
The lexical analyzer collects information
about tokens into their associated
attributes.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.
ATTRIBUTES FOR TOKENS
(CONT’D)
30
Let num be the token representing an integer.
when a sequence of digits appears in the input
stream,the lexical analyzer will pass num to the
parser.The value of the integer will be passed
along as an attribute of the token num.Logically,
the lexical analyzer passes both the token and the
attribute to the parser.
ATTRIBUTES FOR TOKENS
(CONT’D)
31
If we write a token and its attribute as a tuple
enclosed b/w < >, the input
33 + 89 – 60
is transformed into the sequence of tuples
< num, 33 > <+, > <num, 89 > <-, > <num,
60>
ATTRIBUTES FOR TOKENS
(CONT’D)
32
The token “+” has no attribute ,the second
components of the tuples ,the attribute ,play
no role during parsing,but are needed during
translation.
ATTRIBUTES FOR TOKENS
(CONT’D)
33
The token and associated attribute values in the “ C ”
statement.
E = M + C * 2
Tell me the token and their attribute value ?
ATTRIBUTES FOR TOKENS
(CONT’D)
34
< ID , pointer to symbol-table entry for E >
< assign_op , >
< ID , pointer to symbol entry for M >
< add_op , >
< ID , pointer to symbol entry for C >
< mult_op , >
< num ,integer value 2 >
PATTERN
35
“The set of strings described by a rule called a pattern
associated with the token.”
The pattern is said to match each string in the set.
For example, the pattern to describe an identifier (a
variable) is a string of letters, numbers, or underscores,
beginning with a non-number.
The pattern for identifier token, ID, is
ID → letter (letter | digit)*
Patterns are typically described using regular
expressions.(RE)
LEXEME
• “A lexeme is a sequence of characters in the source
program that is matched by the pattern for a token”
•For example, the pattern for the Relation_op token
contains six lexemes ( =, < >, <, < =, >,
• >=) so the lexical analyzer should return a relation_op
token to parser whenever it sees any one of the six.
36
EXAMPLE
37
Input: count = 123
Tokens:
identifier : Rule: “letter followed by …”
Lexeme: count
assg_op : Rule: =
Lexeme: =
integer_const : Rule: “digit followed by …”
Lexeme: 123
TOKENS,PATTERNS.LEXEMES
38
Token Sample Lexemes Informal Description of Pattern
const
if
relation
id
num
const
if
<, <=, =, < >, >, >=
pi, count, D2
3.1416, 0, 6.02E23
const
if
< or <= or = or < > or >= or >
letter followed by letters and digits
any numeric constant
Classifies
Pattern
TOKENS,PATTERNS.LEXEMES
(CONT’D)
39
Example:
Const pi = 3.1416;
In the example above ,when the character
sequence “pi” appears in the source
program,a token representing an identifier is
retuned to the parser(ID).The returning of a
token is often implemented by passing an
integer corresponding to the token.

More Related Content

Similar to COMPILER DESIGN.pdf

A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical AnalyzerArchana Gopinath
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and LexemeA. S. M. Shafi
 
Cd ch2 - lexical analysis
Cd   ch2 - lexical analysisCd   ch2 - lexical analysis
Cd ch2 - lexical analysismengistu23
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxYashaswiniYashu9555
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 
Using Static Analysis in Program Development
Using Static Analysis in Program DevelopmentUsing Static Analysis in Program Development
Using Static Analysis in Program DevelopmentPVS-Studio
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptxwoldu2
 
4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana4 compiler lab - Syntax Ana
4 compiler lab - Syntax AnaMashaelQ
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysisSudhaa Ravi
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORijistjournal
 

Similar to COMPILER DESIGN.pdf (20)

A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Cd ch2 - lexical analysis
Cd   ch2 - lexical analysisCd   ch2 - lexical analysis
Cd ch2 - lexical analysis
 
automata theroy and compiler designc.pptx
automata theroy and compiler designc.pptxautomata theroy and compiler designc.pptx
automata theroy and compiler designc.pptx
 
Module4 lex and yacc.ppt
Module4 lex and yacc.pptModule4 lex and yacc.ppt
Module4 lex and yacc.ppt
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Using Static Analysis in Program Development
Using Static Analysis in Program DevelopmentUsing Static Analysis in Program Development
Using Static Analysis in Program Development
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
Module 2
Module 2 Module 2
Module 2
 
SS & CD Module 3
SS & CD Module 3 SS & CD Module 3
SS & CD Module 3
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
 

Recently uploaded

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 

COMPILER DESIGN.pdf

  • 1.
  • 2. JL-Jaladhulagori (via Andul Mouri), Sankrail District : Howrah (West Bengal) Pin : 711302
  • 4. Structure of a Compiler Source Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code Target Language 4
  • 5. TODAY! 5 Source Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code Target Language
  • 6. The Role of Lexical Analyzer The lexical analyzer is the first phase of a compiler. The main task of lexical Analyzer(Scanner) is to read a stream of characters as an input and produce a sequence of tokens that the parser(Syntax Analyzer) uses for syntax analysis.
  • 7. Read Character Token Symbol Table Parser Lexical Analyzer input Push back character THE ROLE OF LEXICAL ANALYZER (CONT’D) 7 Get Next Token
  • 8. When a lexical analyzer is inserted b/w the parser and input stream ,it interacts with two in the manner shown in the above fig.It reads characters from the input ,groups them into lexeme and passes the tokens formed by the lexemes,together with their attribute value,to the parser.In some situations ,the lexical analyzer has to read some characters ahead before it can decide on the token to be returned to the parser. THE ROLE OF LEXICAL ANALYZER (CONT’D) 8
  • 9. The Role of Lexical Analyzer (cont’d) 9 For example, a lexical analyzer for Pascal must read ahead after it sees the character >.If the next character is = ,then the character sequence >= is the lexeme forming the token for the “Greater than or equal to ” operator. Other wise > is the lexeme forming “Greater than ” operator ,and the lexical analyzer has read one character too many.
  • 10. The Role of Lexical Analyzer (cont’d) The extra character has to be pushed back on to the input,because it can be the beginning of the next lexeme in the input.The lexical analyzer and parser form a producer- Consumer pair. The lexical analyzer produces tokens and the parser consumes them.Produced tokens can be held in a token buffer until they are consumed. 10
  • 11. THE ROLE OF LEXICAL ANALYZER (CONT’D) 11 The interaction b/w the two is constrained only by the size of the buffer,because the lexical analyzer can not proceed when the buffer is full and the parser can not proceed when the buffer is empty.Commonly ,the buffer holds just one token.In this case,the interaction can be implemented simply by making the lexical analyzer be a procedure called by the parser,returning tokens on demand.
  • 12. The Role of Lexical Analyzer (cont’d) The implementation of reading and pushing back character is usually done by setting up an input buffer.A block of character is read into the buffer at a time; a pointer keeps track of the portion of the input that has been analyzed.Pushing back a character is implemented by moving the pointer. 12
  • 13. The Role of Lexical Analyzer (cont’d) Some times lexical analyzer are divided into two phases,the first is called Scanning and the second is called Lexical Analysis.The scanning is responsible for doing simple tasks ,while the lexical analyzer does the more complex operations.For example, a Fortran might use a scanner to eliminate blanks from the input. 13
  • 14. ISSUES IN LEXICAL ANALYSIS 14 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 simply one or the other of these phases.For example,a parser embodying the conventions for comments and white space is significantly more complex--- 1)
  • 15. ISSUES IN LEXICAL ANALYSIS (CONT’D) 15 --then 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 over all language design.
  • 16. ISSUES IN LEXICAL ANALYSIS (CONT’D) 2) 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 in put characters and processing tokens can significantly speed up the performance of a compiler. 16
  • 17. ISSUES IN LEXICAL ANALYSIS (CONT’D) 17 3) 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.
  • 18. ISSUES IN LEXICAL ANALYSIS (CONT’D) 18 Specialized tools have been designed to help automate the construction of lexical analyzers and parsers when they are separated.
  • 19. WHAT EXACTLY IS LEXING? 19 Consider the code: if (i==j); z=1; else; z=0; endif; i f _(i= = j);ntz=1;nels e;ntz=0;nendif; This is really nothing more than a string of characters: During our lexical analysis phase we must divide this string into meaningful sub- strings.
  • 20. TOKENS 20 ● The output of our lexical analysis phase is a streams of tokens. ● A token is a syntactic category. ● In English this would be types of words or punctuation, such as a “noun”, “verb”, “adjective” or “end-mark”. ● In a program, this could be an “identifier”, a “floating-point number”, a “math symbol”, a “keyword”, etc…
  • 21. IDENTIFYING TOKENS 21 ● A sub-string that represents an instance of a token is called a lexeme. ● The class of all possible lexemes in a token is described by the use of a pattern. ● For example, the pattern to describe an identifier (a variable) is a string of letters, numbers, or underscores, beginning with a non-number. ● Patterns are typically described using regular expressions.
  • 22. IMPLEMENTATION 22 A lexical analyzer must be able to do three things: 1. Remove all whitespace and comments. 2. Identify tokens within a string. 3.Return the lexeme of a found token, as well as the line number it was found on.
  • 23. 21 EXAMPLE if_ ( i = = j);ntz=1;nelse;ntz=0;nendif; Lexeme if ( i == j ) ; z = 1 ; Line 1 1 1 1 1 1 1 2 2 2 2 3 Token BLOCK_COMMAND OPEN_PAREN ID OP_RELATION ID CLOSE_PAREN ENDLINE ID ASSIGN NUMBER ENDLINE BLOCK_COMMAND else ● ● ● ● ● ● ● ● ● ● ● ● ● ● Etc…
  • 24. LOOKAHEAD 24 ● Lookahead will typically be important to a lexical analyzer. ● Tokens are typically read in from left-to-right, recognized one at a time from the input string. ● It is not always possible to instantly decide if a token is finished without looking ahead at the next character. For example… ● ● Is “i” a variable, or the first character of “if”? Is “=” an assignment or the beginning of “==”?
  • 25. TOKENS 25 The output of our lexical analysis phase is a streams of tokens.A token is a syntactic category. “A name for a set of input strings with related structure” Example: “ID,” “NUM”, “RELATION“,”IF” In English this would be types of words or punctuation, such as a “noun”, “verb”, “adjective” or “end-mark”. In a program, this could be an “identifier”, a “floating-point number”, a “math symbol”, a “keyword”, etc…
  • 26. TOKENS (CONT’D) 26 As an example,consider the following line of code,which could be part of a “ C ” program. a [index] = 4 + 2
  • 27. TOKENS (CONT’D) 27 a ID [ Left bracket index ID ] Right bracket = Assign 4 Num + plus sign 2 Num
  • 28. TOKENS (CONT’D) 28 characters that are collected into a Each token consists of one or more unit before further processing takes place. A scanner may perform other operations along with the recognition of tokens.For example,it may enter identifiers into the symbol table.
  • 29. ATTRIBUTES FOR TOKENS 29 The lexical analyzer collects information about tokens into their associated attributes.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.
  • 30. ATTRIBUTES FOR TOKENS (CONT’D) 30 Let num be the token representing an integer. when a sequence of digits appears in the input stream,the lexical analyzer will pass num to the parser.The value of the integer will be passed along as an attribute of the token num.Logically, the lexical analyzer passes both the token and the attribute to the parser.
  • 31. ATTRIBUTES FOR TOKENS (CONT’D) 31 If we write a token and its attribute as a tuple enclosed b/w < >, the input 33 + 89 – 60 is transformed into the sequence of tuples < num, 33 > <+, > <num, 89 > <-, > <num, 60>
  • 32. ATTRIBUTES FOR TOKENS (CONT’D) 32 The token “+” has no attribute ,the second components of the tuples ,the attribute ,play no role during parsing,but are needed during translation.
  • 33. ATTRIBUTES FOR TOKENS (CONT’D) 33 The token and associated attribute values in the “ C ” statement. E = M + C * 2 Tell me the token and their attribute value ?
  • 34. ATTRIBUTES FOR TOKENS (CONT’D) 34 < ID , pointer to symbol-table entry for E > < assign_op , > < ID , pointer to symbol entry for M > < add_op , > < ID , pointer to symbol entry for C > < mult_op , > < num ,integer value 2 >
  • 35. PATTERN 35 “The set of strings described by a rule called a pattern associated with the token.” The pattern is said to match each string in the set. For example, the pattern to describe an identifier (a variable) is a string of letters, numbers, or underscores, beginning with a non-number. The pattern for identifier token, ID, is ID → letter (letter | digit)* Patterns are typically described using regular expressions.(RE)
  • 36. LEXEME • “A lexeme is a sequence of characters in the source program that is matched by the pattern for a token” •For example, the pattern for the Relation_op token contains six lexemes ( =, < >, <, < =, >, • >=) so the lexical analyzer should return a relation_op token to parser whenever it sees any one of the six. 36
  • 37. EXAMPLE 37 Input: count = 123 Tokens: identifier : Rule: “letter followed by …” Lexeme: count assg_op : Rule: = Lexeme: = integer_const : Rule: “digit followed by …” Lexeme: 123
  • 38. TOKENS,PATTERNS.LEXEMES 38 Token Sample Lexemes Informal Description of Pattern const if relation id num const if <, <=, =, < >, >, >= pi, count, D2 3.1416, 0, 6.02E23 const if < or <= or = or < > or >= or > letter followed by letters and digits any numeric constant Classifies Pattern
  • 39. TOKENS,PATTERNS.LEXEMES (CONT’D) 39 Example: Const pi = 3.1416; In the example above ,when the character sequence “pi” appears in the source program,a token representing an identifier is retuned to the parser(ID).The returning of a token is often implemented by passing an integer corresponding to the token.