SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
Compiler Construction Sunita M. Dol, CSE Dept
Page 1
Assignment No.8
AIM:
Implement the top-down parsing using predictive parsing technique.
THEORY:
Predictive parser
In many cases, by carefully writing a grammar, eliminating left recursion from it,
and left factoring the resulting grammar, we can obtain a grammar that can be
parsed by a recursive-dcscent parser that needs no backtracking, i.e., a predictive
parser.
To construct a predictive parser, we must know, given the current input symbol a
and the nonterminal A to be expanded, which one of the alternatives of production
A → α1| α2|…| αk is the unique alternative that derives a string beginning with a.
That is, the proper alternative must be detectable by looking at only the first
symbol it derives. Flow-of-control constructs in most programming languages,
with their distinguishing keywords, are usually detectable in this way.
For example, if we have the following productions
stmt → if expr then stmt else stmt
| while expr do stmt
| begin stmt_list end
then the keywords if, while, and begin tell us which alternative is the only one that
could possibly succeed if we are to find a statement.
Compiler Construction Sunita M. Dol, CSE Dept
Page 2
Transition diagram for predictive parser:
In parser, there is one diagram for each nonterminal. The labels of edges are tokens
and non-terminals. A transition on a token (terminal) means we should take that
transition if that token is the next input symbol. A transition on a non-terminal A is
a call of the procedure for A.
To construct the transition diagram of a predictive parser from a grammar, first
eliminate left recursion from the grammar and then left factor the grammar. Then
for each nonterminal A do the following
• Create an initial and final state
• For each production A → X1X2...Xn, create a path from initial top the
final state with edges labeled X1, X2, ... Nn.
Predictive parser behaves as follows:
• It begins in the start state from the start symbol. If after some action, it
is in state s with an edge labeled by terminal a to state t and if the next
input symbol is a then the parser moves the input cursor one position
right and goes to state t.
• If edge is labeled by a nonterminal A then parser goes to the start state
for A without moving the input cursor. If it ever reaches the final state
for A, it immediately goes to state t, in effect having "read" A from the
input during the time it moved from state s to t.
• If there is an edge from s to t labeled Ɛ, then from state s the parser
immediately goes to state without advancing the input.
• A predictive parsing program based on a transition diagram attempts
to match terminal symbols against the input, and makes a potentially
recursive procedure call whenever it has to follow an edge labelled by
a nonterminal.
Compiler Construction
Figure contains a collection of transition diagram for given grammar. The only
ambiguities concern whether or not to take an epsilon edge. If we interpret the
edges out of the initial state for E' as saying take the transition on +
is the next input and take the transition on epsilon otherwise, and make the
analogous assumption for T', then the ambiguity is removed, and we can write a
predictive parsing program for grammar
Example:
E → T E’
E’ → +T E’ | ε
T → F T’
T’ → *F T’ | ε
F → id | (E)
Sunita M. Dol, CSE Dept
Figure contains a collection of transition diagram for given grammar. The only
ambiguities concern whether or not to take an epsilon edge. If we interpret the
edges out of the initial state for E' as saying take the transition on +
is the next input and take the transition on epsilon otherwise, and make the
analogous assumption for T', then the ambiguity is removed, and we can write a
predictive parsing program for grammar
Sunita M. Dol, CSE Dept
Page 3
Figure contains a collection of transition diagram for given grammar. The only
ambiguities concern whether or not to take an epsilon edge. If we interpret the
edges out of the initial state for E' as saying take the transition on + whenever that
is the next input and take the transition on epsilon otherwise, and make the
analogous assumption for T', then the ambiguity is removed, and we can write a
Compiler Construction
Transition diagrams can be simplified by substituting diagrams in one another. For
example, in Fig. a, the call of E' on itself has been replaced by a jump to the
beginning of the diagram for E' .
Figure b shows an equivalent transition diagram for E'. We may then substitute the
diagram of Fig. b for the transition on E' in the transition diagram for E yielding
the diagram of Fig.c Observe that the first and third nodes in Fig.c arc equivalent
and we merge them resulting in fig d. The same techniques apply to the diagrams
for T and T'.
Now how to parse the input string using predictive parser:
id+id
• Step 1: First input symbol is id, hence input cursor points to id. So consider
the first transition diagram that is for non
from state 0 to 3 on non
Sunita M. Dol, CSE Dept
Transition diagrams can be simplified by substituting diagrams in one another. For
example, in Fig. a, the call of E' on itself has been replaced by a jump to the
beginning of the diagram for E' .
shows an equivalent transition diagram for E'. We may then substitute the
diagram of Fig. b for the transition on E' in the transition diagram for E yielding
the diagram of Fig.c Observe that the first and third nodes in Fig.c arc equivalent
them resulting in fig d. The same techniques apply to the diagrams
Now how to parse the input string using predictive parser: Here input string is
Step 1: First input symbol is id, hence input cursor points to id. So consider
the first transition diagram that is for non-terminal E. There is transition
from state 0 to 3 on non-terminal T, so parser goes to start state for T
Sunita M. Dol, CSE Dept
Page 4
Transition diagrams can be simplified by substituting diagrams in one another. For
example, in Fig. a, the call of E' on itself has been replaced by a jump to the
shows an equivalent transition diagram for E'. We may then substitute the
diagram of Fig. b for the transition on E' in the transition diagram for E yielding
the diagram of Fig.c Observe that the first and third nodes in Fig.c arc equivalent
them resulting in fig d. The same techniques apply to the diagrams
Here input string is
Step 1: First input symbol is id, hence input cursor points to id. So consider
terminal E. There is transition
terminal T, so parser goes to start state for T
Compiler Construction Sunita M. Dol, CSE Dept
Page 5
without moving input cursor. In transition diagram for T, there is transition
from state 7 to 8 on non-terminal F, so parser goes to start state for F without
moving input cursor. In transition diagram for F, there is state 14 with an
edge labeled by terminal id to state 17 and the input symbol is also id so the
parser moves the input cursor to next input symbol + and goes to final state
17. Now parser goes back from state 17 to 8 and from 8 to final state 13 on
symbol Ɛ. From state 13, parser goes back to state 3.
• Step 2: Now input cursor is pointing to +, hence parser goes from state 3 to 0
on input symbol +. Now the parser moves the input cursor to the next input
symbol id.
• Step 3: Parser repeat the step 1 for input symbol id and finally goes to final
state 6 from state 3 on Ɛ.
PROGRAM:
INPUT:
1. Enter string: i+i*i+i
2. Enter string: i+i*
Compiler Construction Sunita M. Dol, CSE Dept
Page 6
OUTPUT:
1. Output is
Call E
Call T
Call F
Terminal i
Call t
Call e
Terminal +
Call T
Call F
Terminal i
Call t
Terminal *
Call F
Terminal i
Call t
Call e
Terminal +
Call T
Call F
Terminal i
Call t
Call e
Valid input
2. Output is
Compiler Construction Sunita M. Dol, CSE Dept
Page 7
Call E
Call T
Call F
Terminal i
Call t
Call e
Terminal +
Call T
Call F
Terminal i
Call t
Terminal *
Call F
Invalid input
CONCLUSION:
Thus the recursive descent parsing which requires backtracking is implemented.
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)

Contenu connexe

Tendances

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
ecko_disasterz
 

Tendances (20)

Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Assignment2
Assignment2Assignment2
Assignment2
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
C# operators
C# operatorsC# operators
C# operators
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
C Token’s
C Token’sC Token’s
C Token’s
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
C programming part4
C programming part4C programming part4
C programming part4
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
 

Similaire à Assignment8

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
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
GeetanjaliSingh82
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
raosir123
 
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
ganeshkarthy
 

Similaire à Assignment8 (20)

Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Introduction to Digital Signal Processing (DSP) - Course Notes
Introduction to Digital Signal Processing (DSP) - Course NotesIntroduction to Digital Signal Processing (DSP) - Course Notes
Introduction to Digital Signal Processing (DSP) - Course Notes
 
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,
 
Introduction
IntroductionIntroduction
Introduction
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
 
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
 
Ch03
Ch03Ch03
Ch03
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
C language basics
C language basicsC language basics
C language basics
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Parsing
ParsingParsing
Parsing
 
03-FiniteAutomata.pptx
03-FiniteAutomata.pptx03-FiniteAutomata.pptx
03-FiniteAutomata.pptx
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
3Arrays, Declarations, Expressions, Statements, Symbolic constant.pptx
 

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

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
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
 

Dernier (20)

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
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
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
 
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
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
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
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
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
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
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
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
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
 
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
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
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
 

Assignment8

  • 1. Compiler Construction Sunita M. Dol, CSE Dept Page 1 Assignment No.8 AIM: Implement the top-down parsing using predictive parsing technique. THEORY: Predictive parser In many cases, by carefully writing a grammar, eliminating left recursion from it, and left factoring the resulting grammar, we can obtain a grammar that can be parsed by a recursive-dcscent parser that needs no backtracking, i.e., a predictive parser. To construct a predictive parser, we must know, given the current input symbol a and the nonterminal A to be expanded, which one of the alternatives of production A → α1| α2|…| αk is the unique alternative that derives a string beginning with a. That is, the proper alternative must be detectable by looking at only the first symbol it derives. Flow-of-control constructs in most programming languages, with their distinguishing keywords, are usually detectable in this way. For example, if we have the following productions stmt → if expr then stmt else stmt | while expr do stmt | begin stmt_list end then the keywords if, while, and begin tell us which alternative is the only one that could possibly succeed if we are to find a statement.
  • 2. Compiler Construction Sunita M. Dol, CSE Dept Page 2 Transition diagram for predictive parser: In parser, there is one diagram for each nonterminal. The labels of edges are tokens and non-terminals. A transition on a token (terminal) means we should take that transition if that token is the next input symbol. A transition on a non-terminal A is a call of the procedure for A. To construct the transition diagram of a predictive parser from a grammar, first eliminate left recursion from the grammar and then left factor the grammar. Then for each nonterminal A do the following • Create an initial and final state • For each production A → X1X2...Xn, create a path from initial top the final state with edges labeled X1, X2, ... Nn. Predictive parser behaves as follows: • It begins in the start state from the start symbol. If after some action, it is in state s with an edge labeled by terminal a to state t and if the next input symbol is a then the parser moves the input cursor one position right and goes to state t. • If edge is labeled by a nonterminal A then parser goes to the start state for A without moving the input cursor. If it ever reaches the final state for A, it immediately goes to state t, in effect having "read" A from the input during the time it moved from state s to t. • If there is an edge from s to t labeled Ɛ, then from state s the parser immediately goes to state without advancing the input. • A predictive parsing program based on a transition diagram attempts to match terminal symbols against the input, and makes a potentially recursive procedure call whenever it has to follow an edge labelled by a nonterminal.
  • 3. Compiler Construction Figure contains a collection of transition diagram for given grammar. The only ambiguities concern whether or not to take an epsilon edge. If we interpret the edges out of the initial state for E' as saying take the transition on + is the next input and take the transition on epsilon otherwise, and make the analogous assumption for T', then the ambiguity is removed, and we can write a predictive parsing program for grammar Example: E → T E’ E’ → +T E’ | ε T → F T’ T’ → *F T’ | ε F → id | (E) Sunita M. Dol, CSE Dept Figure contains a collection of transition diagram for given grammar. The only ambiguities concern whether or not to take an epsilon edge. If we interpret the edges out of the initial state for E' as saying take the transition on + is the next input and take the transition on epsilon otherwise, and make the analogous assumption for T', then the ambiguity is removed, and we can write a predictive parsing program for grammar Sunita M. Dol, CSE Dept Page 3 Figure contains a collection of transition diagram for given grammar. The only ambiguities concern whether or not to take an epsilon edge. If we interpret the edges out of the initial state for E' as saying take the transition on + whenever that is the next input and take the transition on epsilon otherwise, and make the analogous assumption for T', then the ambiguity is removed, and we can write a
  • 4. Compiler Construction Transition diagrams can be simplified by substituting diagrams in one another. For example, in Fig. a, the call of E' on itself has been replaced by a jump to the beginning of the diagram for E' . Figure b shows an equivalent transition diagram for E'. We may then substitute the diagram of Fig. b for the transition on E' in the transition diagram for E yielding the diagram of Fig.c Observe that the first and third nodes in Fig.c arc equivalent and we merge them resulting in fig d. The same techniques apply to the diagrams for T and T'. Now how to parse the input string using predictive parser: id+id • Step 1: First input symbol is id, hence input cursor points to id. So consider the first transition diagram that is for non from state 0 to 3 on non Sunita M. Dol, CSE Dept Transition diagrams can be simplified by substituting diagrams in one another. For example, in Fig. a, the call of E' on itself has been replaced by a jump to the beginning of the diagram for E' . shows an equivalent transition diagram for E'. We may then substitute the diagram of Fig. b for the transition on E' in the transition diagram for E yielding the diagram of Fig.c Observe that the first and third nodes in Fig.c arc equivalent them resulting in fig d. The same techniques apply to the diagrams Now how to parse the input string using predictive parser: Here input string is Step 1: First input symbol is id, hence input cursor points to id. So consider the first transition diagram that is for non-terminal E. There is transition from state 0 to 3 on non-terminal T, so parser goes to start state for T Sunita M. Dol, CSE Dept Page 4 Transition diagrams can be simplified by substituting diagrams in one another. For example, in Fig. a, the call of E' on itself has been replaced by a jump to the shows an equivalent transition diagram for E'. We may then substitute the diagram of Fig. b for the transition on E' in the transition diagram for E yielding the diagram of Fig.c Observe that the first and third nodes in Fig.c arc equivalent them resulting in fig d. The same techniques apply to the diagrams Here input string is Step 1: First input symbol is id, hence input cursor points to id. So consider terminal E. There is transition terminal T, so parser goes to start state for T
  • 5. Compiler Construction Sunita M. Dol, CSE Dept Page 5 without moving input cursor. In transition diagram for T, there is transition from state 7 to 8 on non-terminal F, so parser goes to start state for F without moving input cursor. In transition diagram for F, there is state 14 with an edge labeled by terminal id to state 17 and the input symbol is also id so the parser moves the input cursor to next input symbol + and goes to final state 17. Now parser goes back from state 17 to 8 and from 8 to final state 13 on symbol Ɛ. From state 13, parser goes back to state 3. • Step 2: Now input cursor is pointing to +, hence parser goes from state 3 to 0 on input symbol +. Now the parser moves the input cursor to the next input symbol id. • Step 3: Parser repeat the step 1 for input symbol id and finally goes to final state 6 from state 3 on Ɛ. PROGRAM: INPUT: 1. Enter string: i+i*i+i 2. Enter string: i+i*
  • 6. Compiler Construction Sunita M. Dol, CSE Dept Page 6 OUTPUT: 1. Output is Call E Call T Call F Terminal i Call t Call e Terminal + Call T Call F Terminal i Call t Terminal * Call F Terminal i Call t Call e Terminal + Call T Call F Terminal i Call t Call e Valid input 2. Output is
  • 7. Compiler Construction Sunita M. Dol, CSE Dept Page 7 Call E Call T Call F Terminal i Call t Call e Terminal + Call T Call F Terminal i Call t Terminal * Call F Invalid input CONCLUSION: Thus the recursive descent parsing which requires backtracking is implemented. REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)