SlideShare a Scribd company logo
G R O U P M E M B E R S :
H I R A S H A H Z A D
J A V E R I A K H A L I D
T A N Z E E L A H U S S A I N
P R E S E N T E D T O :
M S . S A N I A B A T O O L
COMPILER
CONSTRUCTION
PARSING
• The term parsing comes from Latin pars meaning “part”.
• Parsing is a process that constructs a syntactic structure (i.e. parse tree) from the
stream of tokens.
• Parsing is the process of determining if a string of tokens can be generated by a
grammar.
• For any context-free grammar there is a parser that takes at most Ο(n3) time to parse a
string of n tokens.
• Parsing a string with a CFG:
– Finding a derivation of the string consistent with the grammar
– The derivation gives us a PARSE TREE
PARSING TECHNIQUES
• Syntax analyzers follow production rules defined by means of context-free
grammar. The way the production rules are implemented (derivation) divides
parsing into two types :
Top-down parsing and
Bottom-up parsing.
TYPES OF PARSING
Parsing
Bottom-up
parsing
Top-Down
Parsing
Predictive
parsing
Recursive decent
parsing
Recursive
predictive parsing
Non-Recursive
predictive
parsing
Shift reduce
parsing
LALR parsing
Canonical
parsing
SLR parsing
Operator
Precedence parsing
TOP DOWN PARSING
• Top-down parsers build parse trees from the top (root) to the bottom (leaves).
• A top-down parse corresponds to a preorder traversal of the parse tree
• A leftmost derivation is applied at each derivation step
• Top-Down Parsing may need to backtracking
• Two top-down parsing are further sub-divided into the following categories:
 Predictive Parsing.
 Recursive Descent Parsing
EXAMPLE:
Consider the following Grammar:
<program>  begin <stmts> end $
<stmts>  SimpleStmt ; <stmts>
<stmts>  begin <stmts> end ; <stmts>
<stmts>  €
Input: begin SimpleStmt; SimpleStmt; end $
RECURSIVE DECENT PARSER
• This parsing technique recursively parses the input to make a parse tree
• A recursive-descent parser consists of several small functions, one for each
nonterminal in the grammar
• A procedure is associated with each nonterminal of a grammar..
• Recursive descent parsing involves backtracking.
• For an input string: read
S → rXd
X → oa
X → ea
PREDICTIVE PARSING
• Predictive parser, has the capability to predict which production is to be used to
replace the input string.
• The predictive parser does not suffer from backtracking.
• The predictive parser uses a look-ahead pointer, which points to the next input
symbols.
• To make the parser back-tracking free, the predictive parser puts some constraints on
the grammar.
• It accepts only a class of grammar known as LL(k) grammar.
• Hence, Predictive Parser is also known as LL(1) Parser.
LL(1) PARSER
• LL(1) Parser accepts LL(1) grammar.
• LL(1) grammar is a subset of context-free grammar but with some restrictions to get
the simplified version
• In LL(1) parser, the first L in LL(1) is parsing the input from left to right, the second L
in LL(1) stands for left-most derivation and the 1 means one input symbol of look
ahead.
CONSTRUCTING PREDICTIVE PARSER
Following are the steps for constructing predictive parser.
o Removing unreachable productions.
o Removing ambiguity from the Grammar.
o Eliminating left recursion.
o Left Factoring of a grammar.
o First and Follow
o Constructing a parse table
REMOVING UNREACHABLE PRODUCTIONS
An unreachable production is one that cannot possibly appear in the parse tree rooted at
the start symbol.
For example, in the following grammar :
S A (1)
A a (2)
B b (3)
Production (3) is unreachable because the non-terminal B does not appear on the right
side of any production.
A non-terminal can be unreachable either it appears on the right side of any production.
if it is on the right side of unreachable non-terminal.
Data Structures:
• A stack
• A List for Reachable Non-Terminals
Method:
Initially both the stack and list are Empty.
Step 1:
Start symbol to the list of reachable non-
terminal also push onto the stack.
Step 2:
While (The stack is not Empty)
{
P= POP one Item of the stack
for (Each non-terminal X on right hand side
are P)
{
If (X is not in the list of reachable non -
terminals)
{
Push X;
Add X to the list of Reachable non-
terminal;
}
}
}
Step 3:
Remove all the productions from the
grammar where L-H-S is not in the list of
reachable non –terminals.
Algorithmto remove unreachable production
• Grammer:
S aB | bA
A a | bAA | aS
B b | aBB | bS
C aD | bS | €
D bD | €
After Removing Unreachable Productions we have :
S aB | bA
A a | bAA | aS
B b | aBB | bS
ELIMINATING AMBIGUITY
A grammar that produces more than one parse tree for some sentence (input string) is
said to be ambiguous.
Ambiguity can be remove only by constructing a new grammar.
Note:
 For left associative, replace right non-terminal.
 For right associative, replace left no-terminal.
 If a grammar contains more than one operators, ambiguity will be removed first from
the production involving the operator having the lowest precedence.
EXAMPLE:
• S  S+S | S-S | S*S | S/S | NUM
The operators with lower precedence will deal first:
S  S + S ’ | S - S ’ | S ’
S ’  S | S * S | S / S | N U M
R e p l a c e S b y S ’ f r o m R - H - S
S ’  S ’ | S ’ * S ’ | S ’ / S ’ | N U M
After Eliminating the redundant productions i-e S’  S’ , We will get :
S ’  S ’ * S ’ | S ’ / S ’ | N U M
 S ’  S ’ * S ’ ’ | S ’ / S ’ ’ | S ’ ’
S ’ ’  S ’ | N U M
Replace again S’ by S’’ from R-H-S
S ’ ’  S ’ ’ | N U M
After Eliminating the redundant productions i-e S’’  S’’ , We will get :
S’ ’  N U M
Unambiguous Grammar:
S  S +S ’ | S - S ’ | S ’
S ’  S ’* S ’’ | S ’/S ’’ | S ’’
S ’’ NUM
TRANSITION DIAGRAMS
• Transition diagrams can describe predictive parsers, just like they can describe lexical
analyzers, but the diagrams are slightly different.
For Predictive Parser For Lexical Analyzer
There is one diagram foe Every
Non-terminal
There is one Diagram for the
Entire language construct.
The labels of edge was terminals
and Non-terminals.
The label of edges are only
terminals.
CONSTRUCTION
1. Eliminate left recursion from G
2. Left factor G
3. For each non-terminal A, do
Create an initial and final (return) state
For each production A -> X1 X2 … Xn, create a path from the initial to the final
state with edges X1 X2 … Xn.
4. Simplify the transition Diagram, if possible.
EXAMPLE OF TRANSITION DIAGRAMS
• An expression grammar with left
recursion and ambiguity removed:
• E -> T E’
• E’ -> + T E’ | ε
• T -> F T’
• T’ -> * F T’ | ε
• F -> ( E ) | id
• Corresponding transition diagrams
TYPES OF PREDICTIVE PARSING
• Following are the two types of Predictive Parsing:
 Recursive Predictive Parsing
 Non-Recursive Predictive Parsing
Here, we discuss in Detail Non-Recursive Predictive Parsing Technique.
NON-RECURSIVE PREDICTIVE PARSING
• A non-recursive predictive parser is an efficient way of implementing by handling
the stack of activation records explicitly.
CONT..
• The predictive parser has an input, a stack, a parsing table, and an output.
• The input contains the string to be parsed, followed by $, the right end marker.
• The stack contains a sequence of grammar symbols, preceded by $, the bottom-of-
stack marker.
• Initially the stack contains the start symbol of the grammar preceded by $.
• The parsing table is a two dimensional array M[A ,a], where A is a nonterminal, and
a is a terminal or the symbol $.
• The parser is controlled by a program that behaves as follows:
The program determines X, the symbol on top of the stack, and a, the current input
symbol.
These two symbols determine the action of the parser.
CONT..
There are three possibilities:
o If X = a = $, the parser halts and announces successful completion of parsing.
o If X = a ≠ $, the parser pops X off the stack and advances the input pointer to
the next input symbol.
o If X is a nonterminal, the program consults entry M[X, a] of the parsing table
M. This entry will be either an X-production of the grammar or an error entry.
• § If M[X, a] = {X → UVW}, the parser replaces X on top of the stack
by WVU (with U on top).
• § If M[X, a] = error, the parser calls an error recovery routine.
PREDICTIVE PARSING ALGORITHM
repeat
begin
let X be the top stack symbol and a
the next input symbol;
if X is a terminal or $ then
if X = a then
pop X from the stack and
remove a from the input
else
ERROR( )
else /* X is a nonterminal */
if M[X, a] = X → Y1, Y2, … , Yk then
begin
pop X from the stack;
push Yk, Yk-1, … ,Y1 onto
the stack, Y1 on top
end
else
ERROR( )
end
until
X = $ /* stack has emptied */
EXAMPLE:
• Use the table-driven predictive parser to parse
id + id * id
• Assuming parsing table
• Initial stack is $E
• Initial input is id + id * id $
Grammar:
E  TE’
E’  +TE’ | €
T  FT’
T’  *FT’ | €
F  (E) | id
ERROR RECOVERY IN PREDICTIVE
PARSING
An error is detected during predictive parsing when the terminal on top of the stack does
not match input symbol or when nonterminal A is on top of the stack, a is the next input
symbol, and the parsing table entry M[A, a] is empty.
Following two Error Recovery Routines are handled in predictive parser.
Panic-mode error recovery:
It is based on the idea of skipping symbols on the input until a token in a selected
set of synchronizing tokens appears.
Its effectiveness depends on the choice of synchronizing set.
The sets should chosen so that the parser recovers quickly from errors that are
likely to occur in practice.
CONT..
Phrase=level recovery:
§ It is implemented by filling in the blank entries in the predictive parsing table with
pointers to error routines.
§ These routines may change, insert, or delete symbols on the input and issue
appropriate error messages.
§ They may also pop from the stack.
§ In any event, sure that there is no possibility of an infinite loop.
§ Checking that any recovery action eventually results in an input symbol being
consumed is a good way to protect against such loops.
DIFFERENCE BETWEEN PREDICTIVE PARSER AND
RECURSIVE DECENT PARSER
Predictive Parsing Recursive-Descent Parsing
Its Non-Recursive (Table Driven)
Predictive Parser, which is also known
as LL(1) parser
Its has a set of recursive procedures to
process the input.
No backtracking is Needed. Backtracking is needed.
Needs a special form of grammars
(LL(1) grammars) and Widely used
It is a general parsing technique, but
not widely used.
Its an efficient technique. Its not an efficient Technique.
Predictive parsers operate in linear
time
It operates in exponential time
BOTTOM-UP PARSING
Bottom-up parsing starts with the input symbols and tries to construct the parse
tree up to the start symbol.
Example:
Input string : a + b * c
Production rules:
S → E
E → E + T
E → E * T
E → T
T → id
EXAMPLE
Let us start bottom-up parsing
a + b * c
Read the input and check if any production matches with the input:
a + b * c
T + b * c
E + b * c
E + T * c
E * c
E * T
E
S
Top Down Parsing, Predictive Parsing

More Related Content

What's hot

Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
LakshmiSamivel
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
Akshaya Arunan
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
Nishant Joshi
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
Dattatray Gandhmal
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
R Islam
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
Richa Sharma
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
Karthi Keyan
 

What's hot (20)

Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Hashing
HashingHashing
Hashing
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
stack & queue
stack & queuestack & queue
stack & queue
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 

Viewers also liked

LIDO勉強会#1
LIDO勉強会#1LIDO勉強会#1
LIDO勉強会#1
Masaharu Hayashi
 
11 technical descriptions
11 technical descriptions11 technical descriptions
11 technical descriptions
Nikhil Joshi
 
How to describe the object
How to describe the objectHow to describe the object
How to describe the object
Dr Abhijeet Dawle
 
Describing Technical Object
Describing Technical ObjectDescribing Technical Object
Describing Technical ObjectNurul Shanty
 
Describing objects in English
Describing objects in EnglishDescribing objects in English
Describing objects in EnglishDiana Alpizar
 
Describing objects
Describing objectsDescribing objects
Describing objects
paulaazaharareyes
 

Viewers also liked (8)

LIDO勉強会#1
LIDO勉強会#1LIDO勉強会#1
LIDO勉強会#1
 
describing objects
describing objectsdescribing objects
describing objects
 
11 technical descriptions
11 technical descriptions11 technical descriptions
11 technical descriptions
 
How to describe the object
How to describe the objectHow to describe the object
How to describe the object
 
Describing Technical Object
Describing Technical ObjectDescribing Technical Object
Describing Technical Object
 
Describing objects in English
Describing objects in EnglishDescribing objects in English
Describing objects in English
 
DESCRIBING OBJECTS
DESCRIBING OBJECTSDESCRIBING OBJECTS
DESCRIBING OBJECTS
 
Describing objects
Describing objectsDescribing objects
Describing objects
 

Similar to Top Down Parsing, Predictive Parsing

Parsing
ParsingParsing
Parsing
khush_boo31
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
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
Nitesh Dubey
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
Mattupallipardhu
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
BBDITM LUCKNOW
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
Jothi Lakshmi
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
Assignment10
Assignment10Assignment10
Assignment10
Sunita Milind Dol
 
Ch06
Ch06Ch06
Ch06
Hankyo
 
Parsing
ParsingParsing
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
JinTaek Seo
 
Non- Recursive Predictive Parsing.pptx
Non- Recursive Predictive Parsing.pptxNon- Recursive Predictive Parsing.pptx
Non- Recursive Predictive Parsing.pptx
sampathkumar912515
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
Mahesh Kumar Chelimilla
 
LECTURE-1 (1).pptx
LECTURE-1 (1).pptxLECTURE-1 (1).pptx
LECTURE-1 (1).pptx
MRKUsafzai0607
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
gadisaAdamu
 

Similar to Top Down Parsing, Predictive Parsing (20)

Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Parsing
ParsingParsing
Parsing
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
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
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
Assignment10
Assignment10Assignment10
Assignment10
 
Ch06
Ch06Ch06
Ch06
 
Parsing
ParsingParsing
Parsing
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Non- Recursive Predictive Parsing.pptx
Non- Recursive Predictive Parsing.pptxNon- Recursive Predictive Parsing.pptx
Non- Recursive Predictive Parsing.pptx
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
LECTURE-1 (1).pptx
LECTURE-1 (1).pptxLECTURE-1 (1).pptx
LECTURE-1 (1).pptx
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 

Recently uploaded

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 

Recently uploaded (20)

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 

Top Down Parsing, Predictive Parsing

  • 1. G R O U P M E M B E R S : H I R A S H A H Z A D J A V E R I A K H A L I D T A N Z E E L A H U S S A I N P R E S E N T E D T O : M S . S A N I A B A T O O L
  • 3. PARSING • The term parsing comes from Latin pars meaning “part”. • Parsing is a process that constructs a syntactic structure (i.e. parse tree) from the stream of tokens. • Parsing is the process of determining if a string of tokens can be generated by a grammar. • For any context-free grammar there is a parser that takes at most Ο(n3) time to parse a string of n tokens. • Parsing a string with a CFG: – Finding a derivation of the string consistent with the grammar – The derivation gives us a PARSE TREE
  • 4. PARSING TECHNIQUES • Syntax analyzers follow production rules defined by means of context-free grammar. The way the production rules are implemented (derivation) divides parsing into two types : Top-down parsing and Bottom-up parsing.
  • 5. TYPES OF PARSING Parsing Bottom-up parsing Top-Down Parsing Predictive parsing Recursive decent parsing Recursive predictive parsing Non-Recursive predictive parsing Shift reduce parsing LALR parsing Canonical parsing SLR parsing Operator Precedence parsing
  • 6. TOP DOWN PARSING • Top-down parsers build parse trees from the top (root) to the bottom (leaves). • A top-down parse corresponds to a preorder traversal of the parse tree • A leftmost derivation is applied at each derivation step • Top-Down Parsing may need to backtracking • Two top-down parsing are further sub-divided into the following categories:  Predictive Parsing.  Recursive Descent Parsing
  • 7. EXAMPLE: Consider the following Grammar: <program>  begin <stmts> end $ <stmts>  SimpleStmt ; <stmts> <stmts>  begin <stmts> end ; <stmts> <stmts>  € Input: begin SimpleStmt; SimpleStmt; end $
  • 8.
  • 9.
  • 10. RECURSIVE DECENT PARSER • This parsing technique recursively parses the input to make a parse tree • A recursive-descent parser consists of several small functions, one for each nonterminal in the grammar • A procedure is associated with each nonterminal of a grammar.. • Recursive descent parsing involves backtracking. • For an input string: read S → rXd X → oa X → ea
  • 11. PREDICTIVE PARSING • Predictive parser, has the capability to predict which production is to be used to replace the input string. • The predictive parser does not suffer from backtracking. • The predictive parser uses a look-ahead pointer, which points to the next input symbols. • To make the parser back-tracking free, the predictive parser puts some constraints on the grammar. • It accepts only a class of grammar known as LL(k) grammar. • Hence, Predictive Parser is also known as LL(1) Parser.
  • 12. LL(1) PARSER • LL(1) Parser accepts LL(1) grammar. • LL(1) grammar is a subset of context-free grammar but with some restrictions to get the simplified version • In LL(1) parser, the first L in LL(1) is parsing the input from left to right, the second L in LL(1) stands for left-most derivation and the 1 means one input symbol of look ahead.
  • 13. CONSTRUCTING PREDICTIVE PARSER Following are the steps for constructing predictive parser. o Removing unreachable productions. o Removing ambiguity from the Grammar. o Eliminating left recursion. o Left Factoring of a grammar. o First and Follow o Constructing a parse table
  • 14. REMOVING UNREACHABLE PRODUCTIONS An unreachable production is one that cannot possibly appear in the parse tree rooted at the start symbol. For example, in the following grammar : S A (1) A a (2) B b (3) Production (3) is unreachable because the non-terminal B does not appear on the right side of any production. A non-terminal can be unreachable either it appears on the right side of any production. if it is on the right side of unreachable non-terminal.
  • 15. Data Structures: • A stack • A List for Reachable Non-Terminals Method: Initially both the stack and list are Empty. Step 1: Start symbol to the list of reachable non- terminal also push onto the stack. Step 2: While (The stack is not Empty) { P= POP one Item of the stack for (Each non-terminal X on right hand side are P) { If (X is not in the list of reachable non - terminals) { Push X; Add X to the list of Reachable non- terminal; } } } Step 3: Remove all the productions from the grammar where L-H-S is not in the list of reachable non –terminals. Algorithmto remove unreachable production
  • 16. • Grammer: S aB | bA A a | bAA | aS B b | aBB | bS C aD | bS | € D bD | € After Removing Unreachable Productions we have : S aB | bA A a | bAA | aS B b | aBB | bS
  • 17. ELIMINATING AMBIGUITY A grammar that produces more than one parse tree for some sentence (input string) is said to be ambiguous. Ambiguity can be remove only by constructing a new grammar. Note:  For left associative, replace right non-terminal.  For right associative, replace left no-terminal.  If a grammar contains more than one operators, ambiguity will be removed first from the production involving the operator having the lowest precedence.
  • 18. EXAMPLE: • S  S+S | S-S | S*S | S/S | NUM The operators with lower precedence will deal first: S  S + S ’ | S - S ’ | S ’ S ’  S | S * S | S / S | N U M R e p l a c e S b y S ’ f r o m R - H - S S ’  S ’ | S ’ * S ’ | S ’ / S ’ | N U M After Eliminating the redundant productions i-e S’  S’ , We will get : S ’  S ’ * S ’ | S ’ / S ’ | N U M  S ’  S ’ * S ’ ’ | S ’ / S ’ ’ | S ’ ’ S ’ ’  S ’ | N U M Replace again S’ by S’’ from R-H-S S ’ ’  S ’ ’ | N U M After Eliminating the redundant productions i-e S’’  S’’ , We will get : S’ ’  N U M
  • 19. Unambiguous Grammar: S  S +S ’ | S - S ’ | S ’ S ’  S ’* S ’’ | S ’/S ’’ | S ’’ S ’’ NUM
  • 20. TRANSITION DIAGRAMS • Transition diagrams can describe predictive parsers, just like they can describe lexical analyzers, but the diagrams are slightly different. For Predictive Parser For Lexical Analyzer There is one diagram foe Every Non-terminal There is one Diagram for the Entire language construct. The labels of edge was terminals and Non-terminals. The label of edges are only terminals.
  • 21. CONSTRUCTION 1. Eliminate left recursion from G 2. Left factor G 3. For each non-terminal A, do Create an initial and final (return) state For each production A -> X1 X2 … Xn, create a path from the initial to the final state with edges X1 X2 … Xn. 4. Simplify the transition Diagram, if possible.
  • 22. EXAMPLE OF TRANSITION DIAGRAMS • An expression grammar with left recursion and ambiguity removed: • E -> T E’ • E’ -> + T E’ | ε • T -> F T’ • T’ -> * F T’ | ε • F -> ( E ) | id • Corresponding transition diagrams
  • 23. TYPES OF PREDICTIVE PARSING • Following are the two types of Predictive Parsing:  Recursive Predictive Parsing  Non-Recursive Predictive Parsing Here, we discuss in Detail Non-Recursive Predictive Parsing Technique.
  • 24. NON-RECURSIVE PREDICTIVE PARSING • A non-recursive predictive parser is an efficient way of implementing by handling the stack of activation records explicitly.
  • 25. CONT.. • The predictive parser has an input, a stack, a parsing table, and an output. • The input contains the string to be parsed, followed by $, the right end marker. • The stack contains a sequence of grammar symbols, preceded by $, the bottom-of- stack marker. • Initially the stack contains the start symbol of the grammar preceded by $. • The parsing table is a two dimensional array M[A ,a], where A is a nonterminal, and a is a terminal or the symbol $. • The parser is controlled by a program that behaves as follows: The program determines X, the symbol on top of the stack, and a, the current input symbol. These two symbols determine the action of the parser.
  • 26. CONT.. There are three possibilities: o If X = a = $, the parser halts and announces successful completion of parsing. o If X = a ≠ $, the parser pops X off the stack and advances the input pointer to the next input symbol. o If X is a nonterminal, the program consults entry M[X, a] of the parsing table M. This entry will be either an X-production of the grammar or an error entry. • § If M[X, a] = {X → UVW}, the parser replaces X on top of the stack by WVU (with U on top). • § If M[X, a] = error, the parser calls an error recovery routine.
  • 27. PREDICTIVE PARSING ALGORITHM repeat begin let X be the top stack symbol and a the next input symbol; if X is a terminal or $ then if X = a then pop X from the stack and remove a from the input else ERROR( ) else /* X is a nonterminal */ if M[X, a] = X → Y1, Y2, … , Yk then begin pop X from the stack; push Yk, Yk-1, … ,Y1 onto the stack, Y1 on top end else ERROR( ) end until X = $ /* stack has emptied */
  • 28. EXAMPLE: • Use the table-driven predictive parser to parse id + id * id • Assuming parsing table • Initial stack is $E • Initial input is id + id * id $ Grammar: E  TE’ E’  +TE’ | € T  FT’ T’  *FT’ | € F  (E) | id
  • 29.
  • 30. ERROR RECOVERY IN PREDICTIVE PARSING An error is detected during predictive parsing when the terminal on top of the stack does not match input symbol or when nonterminal A is on top of the stack, a is the next input symbol, and the parsing table entry M[A, a] is empty. Following two Error Recovery Routines are handled in predictive parser. Panic-mode error recovery: It is based on the idea of skipping symbols on the input until a token in a selected set of synchronizing tokens appears. Its effectiveness depends on the choice of synchronizing set. The sets should chosen so that the parser recovers quickly from errors that are likely to occur in practice.
  • 31. CONT.. Phrase=level recovery: § It is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. § These routines may change, insert, or delete symbols on the input and issue appropriate error messages. § They may also pop from the stack. § In any event, sure that there is no possibility of an infinite loop. § Checking that any recovery action eventually results in an input symbol being consumed is a good way to protect against such loops.
  • 32. DIFFERENCE BETWEEN PREDICTIVE PARSER AND RECURSIVE DECENT PARSER Predictive Parsing Recursive-Descent Parsing Its Non-Recursive (Table Driven) Predictive Parser, which is also known as LL(1) parser Its has a set of recursive procedures to process the input. No backtracking is Needed. Backtracking is needed. Needs a special form of grammars (LL(1) grammars) and Widely used It is a general parsing technique, but not widely used. Its an efficient technique. Its not an efficient Technique. Predictive parsers operate in linear time It operates in exponential time
  • 33. BOTTOM-UP PARSING Bottom-up parsing starts with the input symbols and tries to construct the parse tree up to the start symbol. Example: Input string : a + b * c Production rules: S → E E → E + T E → E * T E → T T → id
  • 34. EXAMPLE Let us start bottom-up parsing a + b * c Read the input and check if any production matches with the input: a + b * c T + b * c E + b * c E + T * c E * c E * T E S