SlideShare une entreprise Scribd logo
1  sur  5
Télécharger pour lire hors ligne
Compiler Construction Sunita M. Dol, CSE Dept
Page 1
Assignment No.5
AIM:
Implement the recognizer for given transition diagram.
THEORY:
Recognition of token
Consider the following grammar fragment:
stmt → if expr then stmt
| if expr then stmt else stmt
| Ɛ
expr → term relop term
| term
term → num
| id
where the terminals if, then, else, relop, id, and num generate sets of strings given
by the following regular definitions:
if → if
then → then
else → else
relop→ < | <= | < > | = | > | >=
id → letter ( letter | digit )*
num → digit+ ( . digit+ )? (E ( + | - )? digit + ) ?
For this language fragment, the lexical analyzer will recognize the keywords if,
then, else, as well as the lexeme denoted by relop, id, and mum. To simplify
matters, we assume keywords are reserved; that is, they cannot be used as
identifiers.
In addition, we assume lexemes are separated by white space, consisting of nonnull
sequences of blanks, tabs, and newlines. Our lexical analyzer will strip out white
space. It will do so by comparing a string against the regular definition ws, below.
delim → blank | tab | newline
Compiler Construction Sunita M. Dol, CSE Dept
Page 2
ws → delim+
If a match for ws is found, the lexical analyzer does not return a token to the
parser. Rather, it proceeds to find a token following the white space and returns
that to the parser. Our goal is to construct a lexical analyzer that will isolate the
lexeme for the next token in the input buffer and produce as output a pair
consisting of the appropriate token and attribute-value, using the translation table
given in Fig. The attribute-values for the relational operators are given by the
symblic constants LT, LE, EQ, NE, GT, GE.
Regular
expression
Token Attribute-value
ws - -
if if -
then then -
else else -
num num Pointer to table entry
id id Pointer to table entry
< relop LT
<= relop LE
= relop EQ
< > relop NE
> relop GT
>= relop GE
1. Transition diagram
Transition diagrams depict the actions that take place when a lexical analyzer is
called by the parser to get the next token. Positions in a transition diagram are
drawn as circles and are called states. The states are connected by arrows, called
edges. Edges leaving state s have labels indicating the input characters that can
next appear after the transition diagram has reached state S. The label other refers
to any character that is not indicated by any of the other edges leaving s.
One state is labeled the start state; it is the initial state of the transition diagram
where control resides when we begin to recognize a token. Certain states may have
actions that are executed when the flow of control reaches that state. On entering a
state we read the next input character. If there is an edge from the current state
Compiler Construction Sunita M. Dol, CSE Dept
Page 3
whose label matches this input character, we then go to the state pointed to by the
edge. Otherwise, we indicate failure.
2. Implementing the transition diagram
A sequence of transition diagrams can be converted into a program to look for
the tokens specified by the diagrams. We adopt a systematic approach that
works for all transition diagrams and constructs programs whose size is
proportional to the number of states and edges in the diagrams.
Each state gets a segment of code. If there are edges leaving a state, then its
code reads a character and selects an edge to follow, if possible. A function
nextchar() is used to read the next character from the input buffer, advance the
forward pointer at each call, and return the character read. If there is an edge
labelled by the character read, or labelled by a character class containing the
character read, then control is transferred to the code for the state pointed to by
that edge. If there is no such edge, and the current state is not one that indicates
a token has been found, then a routine fail() is invoked to retract the forward
pointer to the position of the beginning pointer and to initiate a search for a
token specified by the next transition diagram. If there are no other transition
diagrams to try, fail() calls an errorrecovery routine.
Compiler Construction Sunita M. Dol, CSE Dept
Page 4
To return tokens we use a global variable lexical_value, which is assigned the
pointers returned by functions install_id() and install_num() when an identifier
or number, respectively, is found. The token class is returned by the main
procedure of the lexical analyzer, called nexttoken().
PROGRAM:
Compiler Construction Sunita M. Dol, CSE Dept
Page 5
INPUT AND OUTPUT:
Enter input string :< =
return (RELOP,LE)
Do you wish to continue?(y/n):y
Enter input string: +
Invalid Input
Do you wish to continue?(y/n):y
Enter input string: abc
return(abc,1)
Do you wish to continue?(y/n):y
Enter input string:123
return(123,0)
Do you wish to continue?(y/n):n
CONCLUSION:
Thus the recognizer for given transition diagram is implemented.
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)

Contenu connexe

Tendances

C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
Andrew Raj
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
C++
C++C++
C++
k v
 

Tendances (20)

Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
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
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Cnotes
CnotesCnotes
Cnotes
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Learn C
Learn CLearn C
Learn C
 
C language basics
C language basicsC language basics
C language basics
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Introduction to c language
Introduction to c languageIntroduction to c language
Introduction to c language
 
C++
C++C++
C++
 
Handout#04
Handout#04Handout#04
Handout#04
 
C program
C programC program
C program
 

Similaire à Assignment5

02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
raosir123
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
vip_du
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
8neutron8
 

Similaire à Assignment5 (20)

Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design File
 
3. Lexical analysis
3. Lexical analysis3. Lexical analysis
3. Lexical analysis
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
Ch3
Ch3Ch3
Ch3
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designs
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
Handout#02
Handout#02Handout#02
Handout#02
 
Assignment8
Assignment8Assignment8
Assignment8
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
Introduction
IntroductionIntroduction
Introduction
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 

Plus de Sunita Milind Dol

Plus de Sunita Milind Dol (20)

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
 
Assignment11
Assignment11Assignment11
Assignment11
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment3
Assignment3Assignment3
Assignment3
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#11
Handout#11Handout#11
Handout#11
 
Handout#10
Handout#10Handout#10
Handout#10
 
Handout#08
Handout#08Handout#08
Handout#08
 

Dernier

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
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
 

Dernier (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
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 ...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
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...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
(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
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
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
 

Assignment5

  • 1. Compiler Construction Sunita M. Dol, CSE Dept Page 1 Assignment No.5 AIM: Implement the recognizer for given transition diagram. THEORY: Recognition of token Consider the following grammar fragment: stmt → if expr then stmt | if expr then stmt else stmt | Ɛ expr → term relop term | term term → num | id where the terminals if, then, else, relop, id, and num generate sets of strings given by the following regular definitions: if → if then → then else → else relop→ < | <= | < > | = | > | >= id → letter ( letter | digit )* num → digit+ ( . digit+ )? (E ( + | - )? digit + ) ? For this language fragment, the lexical analyzer will recognize the keywords if, then, else, as well as the lexeme denoted by relop, id, and mum. To simplify matters, we assume keywords are reserved; that is, they cannot be used as identifiers. In addition, we assume lexemes are separated by white space, consisting of nonnull sequences of blanks, tabs, and newlines. Our lexical analyzer will strip out white space. It will do so by comparing a string against the regular definition ws, below. delim → blank | tab | newline
  • 2. Compiler Construction Sunita M. Dol, CSE Dept Page 2 ws → delim+ If a match for ws is found, the lexical analyzer does not return a token to the parser. Rather, it proceeds to find a token following the white space and returns that to the parser. Our goal is to construct a lexical analyzer that will isolate the lexeme for the next token in the input buffer and produce as output a pair consisting of the appropriate token and attribute-value, using the translation table given in Fig. The attribute-values for the relational operators are given by the symblic constants LT, LE, EQ, NE, GT, GE. Regular expression Token Attribute-value ws - - if if - then then - else else - num num Pointer to table entry id id Pointer to table entry < relop LT <= relop LE = relop EQ < > relop NE > relop GT >= relop GE 1. Transition diagram Transition diagrams depict the actions that take place when a lexical analyzer is called by the parser to get the next token. Positions in a transition diagram are drawn as circles and are called states. The states are connected by arrows, called edges. Edges leaving state s have labels indicating the input characters that can next appear after the transition diagram has reached state S. The label other refers to any character that is not indicated by any of the other edges leaving s. One state is labeled the start state; it is the initial state of the transition diagram where control resides when we begin to recognize a token. Certain states may have actions that are executed when the flow of control reaches that state. On entering a state we read the next input character. If there is an edge from the current state
  • 3. Compiler Construction Sunita M. Dol, CSE Dept Page 3 whose label matches this input character, we then go to the state pointed to by the edge. Otherwise, we indicate failure. 2. Implementing the transition diagram A sequence of transition diagrams can be converted into a program to look for the tokens specified by the diagrams. We adopt a systematic approach that works for all transition diagrams and constructs programs whose size is proportional to the number of states and edges in the diagrams. Each state gets a segment of code. If there are edges leaving a state, then its code reads a character and selects an edge to follow, if possible. A function nextchar() is used to read the next character from the input buffer, advance the forward pointer at each call, and return the character read. If there is an edge labelled by the character read, or labelled by a character class containing the character read, then control is transferred to the code for the state pointed to by that edge. If there is no such edge, and the current state is not one that indicates a token has been found, then a routine fail() is invoked to retract the forward pointer to the position of the beginning pointer and to initiate a search for a token specified by the next transition diagram. If there are no other transition diagrams to try, fail() calls an errorrecovery routine.
  • 4. Compiler Construction Sunita M. Dol, CSE Dept Page 4 To return tokens we use a global variable lexical_value, which is assigned the pointers returned by functions install_id() and install_num() when an identifier or number, respectively, is found. The token class is returned by the main procedure of the lexical analyzer, called nexttoken(). PROGRAM:
  • 5. Compiler Construction Sunita M. Dol, CSE Dept Page 5 INPUT AND OUTPUT: Enter input string :< = return (RELOP,LE) Do you wish to continue?(y/n):y Enter input string: + Invalid Input Do you wish to continue?(y/n):y Enter input string: abc return(abc,1) Do you wish to continue?(y/n):y Enter input string:123 return(123,0) Do you wish to continue?(y/n):n CONCLUSION: Thus the recognizer for given transition diagram is implemented. REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)