SlideShare a Scribd company logo
1 of 8
Download to read offline
Compiler Construction
AIM:
Implement the non-recursive
THEORY:
Top down parsing
Top-down parsing can be viewed as an attempt to find a leftmost derivation for an
input string. Equivalently, it can be viewed as an attempt to
for the input starting from the root and creating the nodes of the parse tree in
preorder.
Following are the examples of top
• Recursive decent parser
• Predictive parser
• Non-recursive predictive parser
Non- Recursive Predictive
Figure 1: Model of non
It is possible to build a non
explicitly. The key problem during predictive parsing is that of determining the
production to be applied for a nonterminal. The non
production to be applied in a parsing table.
Sunita M. Dol, CSE Dept
Assignment No.10
recursive predictive parser for the language.
down parsing can be viewed as an attempt to find a leftmost derivation for an
input string. Equivalently, it can be viewed as an attempt to construct a parse tree
for the input starting from the root and creating the nodes of the parse tree in
Following are the examples of top-down parser
Recursive decent parser
Predictive parser
recursive predictive parser
Recursive Predictive parser
: Model of non-recursive predictive parser
It is possible to build a non-recursive predictive parser by maintaining a stack
explicitly. The key problem during predictive parsing is that of determining the
production to be applied for a nonterminal. The non-recursive parser looks up the
production to be applied in a parsing table.
Sunita M. Dol, CSE Dept
Page 1
down parsing can be viewed as an attempt to find a leftmost derivation for an
construct a parse tree
for the input starting from the root and creating the nodes of the parse tree in
recursive predictive parser by maintaining a stack
explicitly. The key problem during predictive parsing is that of determining the
recursive parser looks up the
Compiler Construction Sunita M. Dol, CSE Dept
Page 2
A table-driven predictive parser has an input buffer, a stack, a parsing table, and an
output stream. The input buffer contains the string to be parsed, followed by $, a
symbol used as a right end-marker to indicate the end of the input string. The stack
contains a sequence of grammar symbols with $ on the bottom, indicating the
bottom of the stack. Initially, the stack contains the start symbol of the grammar on
top of $. The parsing table is a two dimensional array M[A, a], where capital A is a
nonterminal, and small a is a terminal or the symbol $.
In parsing table, each row is a non-terminal symbol, each column is a terminal
symbol or the special symbol $ and each entry holds a production rule.
The parser is controlled by a program that behaves as follows. The program
considers X, the symbol on top of the stack, and a, the current input symbol. These
two symbols determine the action of the parser. There are four possible parser
actions.
1. If X = a = $ then the parser halts and announces successful completion of
parsing
2. If X and a are the same terminal symbol (different from $) that is X = a
which is not equal to $ then parser pops X from the stack, and advances the
input pointer to the next input symbol,
3. If X is a non-terminal, the program consults the parsing table entry M[X,a].
If M[X,a] holds a production rule X→Y1Y2...Yk, it pops X from the stack
and pushes Yk,Yk-1,...,Y1 into the stack. The parser also outputs the
production rule X→Y1Y2...Yk to represent a step of the derivation.
4. If none of the above then error. All empty entries in the parsing table are
errors and the parser calls an error recovery routine. If X is a terminal
symbol different from a, this is also an error case.
Non- Recursive Predictive parser Algorithm
INPUT: A string w and a parsing table M for grammar G.
OUTPUT: If w is in L(G), a leftmost derivation of w; otherwise, an error
indication.
METHOD: Initially, the parser is in a configuration with w$ in the input buffer
and the start symbol S of grammar G on top of the stack, above $. The flowchart
shown below uses the predictive parsing table M to produce a predictive parse for
the input.
set ip to point to the first symbol of w;
Compiler Construction Sunita M. Dol, CSE Dept
Page 3
repeat
Let X be the top stack symbol and a the symbol pointed by ip;
if X is a terminal or $ then
if X=a then
pop X from the stack and advance ip
else error()
else /* X is a non-terminal */
if M[X,a] = X -> Y1Y2 •••Yk then
begin
pop X from the stack
push Yk, Yk-i,... ,Y1 onto the stack, with Y1 on top;
output the production X -> Y1 Y2 • • • Yk;
end
else error()
until X=$ /* stack is empty */
Consider the grammar given below. A predictive parsing table for this grammar is
shown in Fig. In parsing table, Blanks are error entries while non-blanks indicate a
production with which to expand the top nonterminal on the stack. The input string
is id+id.
With input id + id; the predictive parser makes the sequence of moves shown
below. Initially, the parser is in a configuration with w$ that is id+id$ in the input
buffer and the start symbol E of grammar G on top of the stack, above $. The input
pointer points to the leftmost symbol of the string in the INPUT column. The
parser traces out a Leftmost derivation for the Input, that is, the productions output
are those of a leftmost derivation. The input symbols that have already been
scanned, followed by the grammar symbol on the stack (from the top to bottom),
make up the left-sentential forms in the derivation.
Finally stack as well as input contains the $ hence the parser halts and announces
successful completion of parsing
Compiler Construction Sunita M. Dol, CSE Dept
Page 4
Example 1:
E → T E’
E’ → +T E’ | ε
T → F T’
T’ → *F T’ | ε
F → id | (E)
Input String : id + id
stack input output
$E id+id$ E → TE’
$E’T id+id$ T → FT’
$E’ T’F id+id$ F → id
$ E’ T’id id+id$
$ E’ T’ +id$ T’ → ε
$ E’ +id$ E’ → +TE’
$ E’ T+ +id$
$ E’ T id$ T → FT’
$ E’ T’ F id$ F → id
$ E’ T’id id$
$ E’ T’ $ T’ → ε
$ E’ $ E’ → ε
$ $ accept
Compiler Construction Sunita M. Dol, CSE Dept
Page 5
PROGRAM:
INPUT:
This parser has been made for the grammar given by
E --> Te
e --> +Te | N
T --> Ft
t --> *Ft | N
F --> (E) | i
E , F , T , t , e , N are nonterminals
* , + , i , ( , ) are terminals
OUTPUT:
Top down parsing using the TABLE DRIVEN/PREDICTIVE/LL(1) PARSER
============================================
First of E is given by (i
First of e is given by +N
First of T is given by (i
First of t is given by *N
First of F is given by (i
First of i is given by i
First of + is given by +
First of * is given by *
First of ( is given by (
First of ) is given by )
First of $ is given by $
Follow of E is given by )$
Follow of e is given by )$
Follow of T is given by +)$
Follow of t is given by +)$
Follow of F is given by *+)$
Compiler Construction Sunita M. Dol, CSE Dept
Page 6
Enter the string(Use i for an identifier) (i+i)*i
$E (i+i)*i$
$eT (i+i)*i$
$etF (i+i)*i$
$et)E( (i+i)*i$
$et)E i+i)*i$
$et)eT i+i)*i$
$et)etF i+i)*i$
$et)eti i+i)*i$
$et)et +i)*i$
$et)e +i)*i$
$et)eT+ +i)*i$
$et)eT i)*i$
$et)etF i)*i$
$et)eti i)*i$
$et)et )*i$
$et)e )*i$
$et) )*i$
$et *i$
$etF* *i$
$etF i$
$eti i$
$et $
$E (i+i)*i$
$eT (i+i)*i$
$etF (i+i)*i$
$et)E( (i+i)*i$
$et)E i+i)*i$
$et)eT i+i)*i$
$et)etF i+i)*i$
$et)eti i+i)*i$
$et)et +i)*i$
$et)e +i)*i$
$et)eT+ +i)*i$
Compiler Construction Sunita M. Dol, CSE Dept
Page 7
$et)eT i)*i$
$et)etF i)*i$
$et)eti i)*i$
$et)et )*i$
$et)e )*i$
$et) )*i$
$et *i$
$etF* *i$
$etF i$
$eti i$
$et $
$E (i+i)*i$
$eT (i+i)*i$
$etF (i+i)*i$
$et)E( (i+i)*i$
$et)E i+i)*i$
$et)eT i+i)*i$
$et)etF i+i)*i$
$et)eti i+i)*i$
$et)et +i)*i$
$et)e +i)*i$
$et)eT+ +i)*i$
$et)eT i)*i$
$et)etF i)*i$
$et)eti i)*i$
$et)et )*i$
$et)e )*i$
$et) )*i$
$et *i$
$etF* *i$
$etF i$
$eti i$
$et $
$e $
Accepted
Compiler Construction Sunita M. Dol, CSE Dept
Page 8
CONCLUSION:
Thus the non-recursive predictive parser for given grammar is implemented.
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)

More Related Content

What's hot (20)

Parsing
ParsingParsing
Parsing
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Parsing
ParsingParsing
Parsing
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
C Programming
C ProgrammingC Programming
C Programming
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
What is c
What is cWhat is c
What is c
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 

Similar to Assignment10

Similar to Assignment10 (20)

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
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
11CS10033.pptx
11CS10033.pptx11CS10033.pptx
11CS10033.pptx
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Non- recusive
Non- recusiveNon- recusive
Non- recusive
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Ch06
Ch06Ch06
Ch06
 
Parsing
ParsingParsing
Parsing
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
3b. LMD & RMD.pdf
3b. LMD & RMD.pdf3b. LMD & RMD.pdf
3b. LMD & RMD.pdf
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 

More from 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
 
Assignment11
Assignment11Assignment11
Assignment11
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment4
Assignment4Assignment4
Assignment4
 
Assignment3
Assignment3Assignment3
Assignment3
 
Assignment2
Assignment2Assignment2
Assignment2
 
Assignment1
Assignment1Assignment1
Assignment1
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#11
Handout#11Handout#11
Handout#11
 
Handout#10
Handout#10Handout#10
Handout#10
 
Handout#09
Handout#09Handout#09
Handout#09
 
Handout#08
Handout#08Handout#08
Handout#08
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
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
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
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
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
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
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
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
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
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
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
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
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

Assignment10

  • 1. Compiler Construction AIM: Implement the non-recursive THEORY: Top down parsing Top-down parsing can be viewed as an attempt to find a leftmost derivation for an input string. Equivalently, it can be viewed as an attempt to for the input starting from the root and creating the nodes of the parse tree in preorder. Following are the examples of top • Recursive decent parser • Predictive parser • Non-recursive predictive parser Non- Recursive Predictive Figure 1: Model of non It is possible to build a non explicitly. The key problem during predictive parsing is that of determining the production to be applied for a nonterminal. The non production to be applied in a parsing table. Sunita M. Dol, CSE Dept Assignment No.10 recursive predictive parser for the language. down parsing can be viewed as an attempt to find a leftmost derivation for an input string. Equivalently, it can be viewed as an attempt to construct a parse tree for the input starting from the root and creating the nodes of the parse tree in Following are the examples of top-down parser Recursive decent parser Predictive parser recursive predictive parser Recursive Predictive parser : Model of non-recursive predictive parser It is possible to build a non-recursive predictive parser by maintaining a stack explicitly. The key problem during predictive parsing is that of determining the production to be applied for a nonterminal. The non-recursive parser looks up the production to be applied in a parsing table. Sunita M. Dol, CSE Dept Page 1 down parsing can be viewed as an attempt to find a leftmost derivation for an construct a parse tree for the input starting from the root and creating the nodes of the parse tree in recursive predictive parser by maintaining a stack explicitly. The key problem during predictive parsing is that of determining the recursive parser looks up the
  • 2. Compiler Construction Sunita M. Dol, CSE Dept Page 2 A table-driven predictive parser has an input buffer, a stack, a parsing table, and an output stream. The input buffer contains the string to be parsed, followed by $, a symbol used as a right end-marker to indicate the end of the input string. The stack contains a sequence of grammar symbols with $ on the bottom, indicating the bottom of the stack. Initially, the stack contains the start symbol of the grammar on top of $. The parsing table is a two dimensional array M[A, a], where capital A is a nonterminal, and small a is a terminal or the symbol $. In parsing table, each row is a non-terminal symbol, each column is a terminal symbol or the special symbol $ and each entry holds a production rule. The parser is controlled by a program that behaves as follows. The program considers X, the symbol on top of the stack, and a, the current input symbol. These two symbols determine the action of the parser. There are four possible parser actions. 1. If X = a = $ then the parser halts and announces successful completion of parsing 2. If X and a are the same terminal symbol (different from $) that is X = a which is not equal to $ then parser pops X from the stack, and advances the input pointer to the next input symbol, 3. If X is a non-terminal, the program consults the parsing table entry M[X,a]. If M[X,a] holds a production rule X→Y1Y2...Yk, it pops X from the stack and pushes Yk,Yk-1,...,Y1 into the stack. The parser also outputs the production rule X→Y1Y2...Yk to represent a step of the derivation. 4. If none of the above then error. All empty entries in the parsing table are errors and the parser calls an error recovery routine. If X is a terminal symbol different from a, this is also an error case. Non- Recursive Predictive parser Algorithm INPUT: A string w and a parsing table M for grammar G. OUTPUT: If w is in L(G), a leftmost derivation of w; otherwise, an error indication. METHOD: Initially, the parser is in a configuration with w$ in the input buffer and the start symbol S of grammar G on top of the stack, above $. The flowchart shown below uses the predictive parsing table M to produce a predictive parse for the input. set ip to point to the first symbol of w;
  • 3. Compiler Construction Sunita M. Dol, CSE Dept Page 3 repeat Let X be the top stack symbol and a the symbol pointed by ip; if X is a terminal or $ then if X=a then pop X from the stack and advance ip else error() else /* X is a non-terminal */ if M[X,a] = X -> Y1Y2 •••Yk then begin pop X from the stack push Yk, Yk-i,... ,Y1 onto the stack, with Y1 on top; output the production X -> Y1 Y2 • • • Yk; end else error() until X=$ /* stack is empty */ Consider the grammar given below. A predictive parsing table for this grammar is shown in Fig. In parsing table, Blanks are error entries while non-blanks indicate a production with which to expand the top nonterminal on the stack. The input string is id+id. With input id + id; the predictive parser makes the sequence of moves shown below. Initially, the parser is in a configuration with w$ that is id+id$ in the input buffer and the start symbol E of grammar G on top of the stack, above $. The input pointer points to the leftmost symbol of the string in the INPUT column. The parser traces out a Leftmost derivation for the Input, that is, the productions output are those of a leftmost derivation. The input symbols that have already been scanned, followed by the grammar symbol on the stack (from the top to bottom), make up the left-sentential forms in the derivation. Finally stack as well as input contains the $ hence the parser halts and announces successful completion of parsing
  • 4. Compiler Construction Sunita M. Dol, CSE Dept Page 4 Example 1: E → T E’ E’ → +T E’ | ε T → F T’ T’ → *F T’ | ε F → id | (E) Input String : id + id stack input output $E id+id$ E → TE’ $E’T id+id$ T → FT’ $E’ T’F id+id$ F → id $ E’ T’id id+id$ $ E’ T’ +id$ T’ → ε $ E’ +id$ E’ → +TE’ $ E’ T+ +id$ $ E’ T id$ T → FT’ $ E’ T’ F id$ F → id $ E’ T’id id$ $ E’ T’ $ T’ → ε $ E’ $ E’ → ε $ $ accept
  • 5. Compiler Construction Sunita M. Dol, CSE Dept Page 5 PROGRAM: INPUT: This parser has been made for the grammar given by E --> Te e --> +Te | N T --> Ft t --> *Ft | N F --> (E) | i E , F , T , t , e , N are nonterminals * , + , i , ( , ) are terminals OUTPUT: Top down parsing using the TABLE DRIVEN/PREDICTIVE/LL(1) PARSER ============================================ First of E is given by (i First of e is given by +N First of T is given by (i First of t is given by *N First of F is given by (i First of i is given by i First of + is given by + First of * is given by * First of ( is given by ( First of ) is given by ) First of $ is given by $ Follow of E is given by )$ Follow of e is given by )$ Follow of T is given by +)$ Follow of t is given by +)$ Follow of F is given by *+)$
  • 6. Compiler Construction Sunita M. Dol, CSE Dept Page 6 Enter the string(Use i for an identifier) (i+i)*i $E (i+i)*i$ $eT (i+i)*i$ $etF (i+i)*i$ $et)E( (i+i)*i$ $et)E i+i)*i$ $et)eT i+i)*i$ $et)etF i+i)*i$ $et)eti i+i)*i$ $et)et +i)*i$ $et)e +i)*i$ $et)eT+ +i)*i$ $et)eT i)*i$ $et)etF i)*i$ $et)eti i)*i$ $et)et )*i$ $et)e )*i$ $et) )*i$ $et *i$ $etF* *i$ $etF i$ $eti i$ $et $ $E (i+i)*i$ $eT (i+i)*i$ $etF (i+i)*i$ $et)E( (i+i)*i$ $et)E i+i)*i$ $et)eT i+i)*i$ $et)etF i+i)*i$ $et)eti i+i)*i$ $et)et +i)*i$ $et)e +i)*i$ $et)eT+ +i)*i$
  • 7. Compiler Construction Sunita M. Dol, CSE Dept Page 7 $et)eT i)*i$ $et)etF i)*i$ $et)eti i)*i$ $et)et )*i$ $et)e )*i$ $et) )*i$ $et *i$ $etF* *i$ $etF i$ $eti i$ $et $ $E (i+i)*i$ $eT (i+i)*i$ $etF (i+i)*i$ $et)E( (i+i)*i$ $et)E i+i)*i$ $et)eT i+i)*i$ $et)etF i+i)*i$ $et)eti i+i)*i$ $et)et +i)*i$ $et)e +i)*i$ $et)eT+ +i)*i$ $et)eT i)*i$ $et)etF i)*i$ $et)eti i)*i$ $et)et )*i$ $et)e )*i$ $et) )*i$ $et *i$ $etF* *i$ $etF i$ $eti i$ $et $ $e $ Accepted
  • 8. Compiler Construction Sunita M. Dol, CSE Dept Page 8 CONCLUSION: Thus the non-recursive predictive parser for given grammar is implemented. REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)