SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
Compiler Construction Sunita M. Dol, CSE Dept
Page 1
Assignment No.6
AIM:
Implement the top-down parsing using recursive decent parsing technique.
THEORY:
Recursive descent parsing
A recursive-descent parsing program consists of a set of procedures, one for each
non-terminal. Execution begins with the procedure for the start symbol, which
halts and announces success if its procedure body scans the entire input string.
Pseudo code for a typical non-terminal appears in Fig. 1. Note that this pseudo
code is nondeterministic, since it begins by choosing the A-production to apply in
a manner that is not specified.
void A() {
1) Choose an A-production, A ->X1X2 • • Xk
2) for ( i = 1 to k ) {
3) if ( X is a nonterminal )
4) Call procedure Xi;
5) else if ( Xi equals the current input symbol a )
6) Advance the input to the next symbol;
7) else /* an error has occurred */;
}
Figure 1: A typical procedure for a non-terminal in a top-down parser
Compiler Construction Sunita M. Dol, CSE Dept
Page 2
General recursive-descent may require backtracking; that is, it may require
repeated scans over the input. However, backtracking is rarely needed to parse
programming language constructs, so backtracking parsers are not seen frequently.
To allow backtracking, the code of Fig. 1 needs to be modified. First, we cannot
choose a unique A-production at line (1), so we must try each of several
productions in some order. Then, failure at line (7) is not ultimate failure, but
suggests only that we need to return to line (1) and try another A-production. Only
if there are no more A-productions to try do we declare that an input error has been
found. In order to try another A-production, we need to be able to reset the input
pointer to where it was when we first reached line (1). Thus, a local variable is
needed to store this input pointer for future use.
Consider the grammar
S->cAd
A->ab|a
To construct a parse tree top-down for the input string w = cad, begin with a tree
consisting of a single node labeled S, and the input pointer pointing to c, the first
symbol of w. S has only one production, so we use it to expand S and obtain the
tree. The leftmost leaf, labeled c, matches the first symbol of input w, so we
advance the input pointer to a, the second symbol of w, and consider the next leaf,
labeled A. Now, we expand A using the first alternative A -> ab to obtain the tree.
We have a match for the second input symbol, a, so we advance the input pointer
to d, the third input symbol, and compare d against the next leaf, labeled b. Since b
does not match d, we report failure and go back to A to see whether there is
another alternative for A that has not been tried, but that might produce a match.
In going back to A, we must reset the input pointer to position 2, the position it had
when we first came to A, which means that the procedure for A must store the
input pointer in a local variable. The leaf a matches the second symbol of w and
Compiler Construction
the leaf d matches the third symbol.
halt and announce successful
Figure 2: Steps in Top
PROGRAM:
INPUT:
1. Enter string: cad
2. Enter string: cabd
OUTPUT:
1. cabd
Backtracking is required
cad
Accepted
2. cabd
Accepted
CONCLUSION:
Thus the recursive descent parsing which requires backtracking is implemented.
Sunita M. Dol, CSE Dept
the leaf d matches the third symbol. Since we have produced a parse tree for w, we
halt and announce successful completion of parsing.
Figure 2: Steps in Top-down parse
Backtracking is required
Thus the recursive descent parsing which requires backtracking is implemented.
Sunita M. Dol, CSE Dept
Page 3
Since we have produced a parse tree for w, we
Thus the recursive descent parsing which requires backtracking is implemented.
Compiler Construction Sunita M. Dol, CSE Dept
Page 4
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)

Contenu connexe

Tendances

computer notes - Conversion from infix to postfix
computer notes - Conversion from infix to postfixcomputer notes - Conversion from infix to postfix
computer notes - Conversion from infix to postfix
ecomputernotes
 

Tendances (20)

Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Back patching
Back patchingBack patching
Back patching
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Assignment2
Assignment2Assignment2
Assignment2
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
computer notes - Conversion from infix to postfix
computer notes - Conversion from infix to postfixcomputer notes - Conversion from infix to postfix
computer notes - Conversion from infix to postfix
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 

Similaire à Assignment6

CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docxCSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
mydrynan
 
Exposure Interval Initial Risk Assessment (with exist.docx
Exposure Interval    Initial Risk Assessment (with exist.docxExposure Interval    Initial Risk Assessment (with exist.docx
Exposure Interval Initial Risk Assessment (with exist.docx
ssuser454af01
 
Classification of Compiler
Classification of CompilerClassification of Compiler
Classification of Compiler
maharajdey
 
Lecture05 abap on line
Lecture05 abap on lineLecture05 abap on line
Lecture05 abap on line
Milind Patil
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 

Similaire à Assignment6 (20)

CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docxCSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Exposure Interval Initial Risk Assessment (with exist.docx
Exposure Interval    Initial Risk Assessment (with exist.docxExposure Interval    Initial Risk Assessment (with exist.docx
Exposure Interval Initial Risk Assessment (with exist.docx
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
 
Programming Exam Help
 Programming Exam Help Programming Exam Help
Programming Exam Help
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
Classification of Compiler
Classification of CompilerClassification of Compiler
Classification of Compiler
 
Implementation of Low-Complexity Redundant Multiplier Architecture for Finite...
Implementation of Low-Complexity Redundant Multiplier Architecture for Finite...Implementation of Low-Complexity Redundant Multiplier Architecture for Finite...
Implementation of Low-Complexity Redundant Multiplier Architecture for Finite...
 
Ch03
Ch03Ch03
Ch03
 
Data Structure and Algorithm
Data Structure and AlgorithmData Structure and Algorithm
Data Structure and Algorithm
 
Ch5b.pdf
Ch5b.pdfCh5b.pdf
Ch5b.pdf
 
Lecture05 abap on line
Lecture05 abap on lineLecture05 abap on line
Lecture05 abap on line
 
C_Lab Manual_Part A.docx
C_Lab Manual_Part A.docxC_Lab Manual_Part A.docx
C_Lab Manual_Part A.docx
 
TikZ for economists
TikZ for economistsTikZ for economists
TikZ for economists
 
Quiz
QuizQuiz
Quiz
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
Matlab practice
Matlab practiceMatlab practice
Matlab practice
 
C test
C testC test
C test
 

Plus de Sunita Milind Dol

Plus de 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
 
Assignment11
Assignment11Assignment11
Assignment11
 

Dernier

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

Dernier (20)

FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 

Assignment6

  • 1. Compiler Construction Sunita M. Dol, CSE Dept Page 1 Assignment No.6 AIM: Implement the top-down parsing using recursive decent parsing technique. THEORY: Recursive descent parsing A recursive-descent parsing program consists of a set of procedures, one for each non-terminal. Execution begins with the procedure for the start symbol, which halts and announces success if its procedure body scans the entire input string. Pseudo code for a typical non-terminal appears in Fig. 1. Note that this pseudo code is nondeterministic, since it begins by choosing the A-production to apply in a manner that is not specified. void A() { 1) Choose an A-production, A ->X1X2 • • Xk 2) for ( i = 1 to k ) { 3) if ( X is a nonterminal ) 4) Call procedure Xi; 5) else if ( Xi equals the current input symbol a ) 6) Advance the input to the next symbol; 7) else /* an error has occurred */; } Figure 1: A typical procedure for a non-terminal in a top-down parser
  • 2. Compiler Construction Sunita M. Dol, CSE Dept Page 2 General recursive-descent may require backtracking; that is, it may require repeated scans over the input. However, backtracking is rarely needed to parse programming language constructs, so backtracking parsers are not seen frequently. To allow backtracking, the code of Fig. 1 needs to be modified. First, we cannot choose a unique A-production at line (1), so we must try each of several productions in some order. Then, failure at line (7) is not ultimate failure, but suggests only that we need to return to line (1) and try another A-production. Only if there are no more A-productions to try do we declare that an input error has been found. In order to try another A-production, we need to be able to reset the input pointer to where it was when we first reached line (1). Thus, a local variable is needed to store this input pointer for future use. Consider the grammar S->cAd A->ab|a To construct a parse tree top-down for the input string w = cad, begin with a tree consisting of a single node labeled S, and the input pointer pointing to c, the first symbol of w. S has only one production, so we use it to expand S and obtain the tree. The leftmost leaf, labeled c, matches the first symbol of input w, so we advance the input pointer to a, the second symbol of w, and consider the next leaf, labeled A. Now, we expand A using the first alternative A -> ab to obtain the tree. We have a match for the second input symbol, a, so we advance the input pointer to d, the third input symbol, and compare d against the next leaf, labeled b. Since b does not match d, we report failure and go back to A to see whether there is another alternative for A that has not been tried, but that might produce a match. In going back to A, we must reset the input pointer to position 2, the position it had when we first came to A, which means that the procedure for A must store the input pointer in a local variable. The leaf a matches the second symbol of w and
  • 3. Compiler Construction the leaf d matches the third symbol. halt and announce successful Figure 2: Steps in Top PROGRAM: INPUT: 1. Enter string: cad 2. Enter string: cabd OUTPUT: 1. cabd Backtracking is required cad Accepted 2. cabd Accepted CONCLUSION: Thus the recursive descent parsing which requires backtracking is implemented. Sunita M. Dol, CSE Dept the leaf d matches the third symbol. Since we have produced a parse tree for w, we halt and announce successful completion of parsing. Figure 2: Steps in Top-down parse Backtracking is required Thus the recursive descent parsing which requires backtracking is implemented. Sunita M. Dol, CSE Dept Page 3 Since we have produced a parse tree for w, we Thus the recursive descent parsing which requires backtracking is implemented.
  • 4. Compiler Construction Sunita M. Dol, CSE Dept Page 4 REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)