SlideShare a Scribd company logo
Syntax Analysis IV
Lecture 9
Syntax Analysis IV
Bottom-Up Parsing
Bottom-Up Parsing
• Given a grammar G, a parse tree for a given string is constructed by
starting at the leaves (terminals of the string) and working to the root
(the start symbol S).
• They are able to accept a more general class of grammars compared to
top-down predictive parsers.
• It builds on the concepts developed in top-down parsing.
• Preferred method for most of the parser generators including bison
• They don’t need left-factored grammars
– So, its valid to use the following grammar
E T + E | T
T int * T | int | (E)
• A parse for a string generates a sequence of derivations of the
form:
S δ0 δ1 δ2 … … δn-1 sentence
• Bottom-up parsing reduces a string to the start symbol by
inverting productions
Bottom-Up Parsing
• Let A b be a production and δi-1 and δi be two consecutive
derivations with sentential forms: αAβ and αbβ
– δi-1 is derived from δi by match the RHS b in δi, and then replacing b with
its corresponding LHS, A. This is called a reduction
• The parse tree is the result of the tokens and the reductions.
• Consider the parse for the input string: int * int + int
E T + E | T
T int * T | int | (E)
• When we reduce, we only have
terminals to the right.
Sentential Form Productions
int * int + int
int * T + int T int
T + int T int * T
T + T T int
Bottom-Up Parsing
terminals to the right.
– In the reduction: αAβ αbβ
– If b to A is a step of a bottom-up
parsing (i.e. A b is a reduction)
– Then β is a string of terminals
• In other words, a bottom-up parser traces a rightmost derivation
in reverse
T + E E T
E E T+E
Shift-Reduce Parsing
• Idea: Split string being parsed into two parts:
– Two parts are separated by a special character ‘|’
– Left part is a string of terminals and non terminals
– Right part is a string of terminals
• Still to be examined
• Bottom up parsing has two actions• Bottom up parsing has two actions
– Shift: Move terminal symbol from right string to left string
• ABC | xyz ABCx | yz
– Reduce: Apply an inverse production at the right end of the
left string
– If A xy is a production, then
• Cbxy | ijk CbA | ijk
Sentential Form Productions
|int * int + int Shift
int | * int + int Shift
int * | int + int Shift
int * int | + int Reduce T int
int * T | + int Reduce T int * T
Shift-Reduce Example
int * T | + int Reduce T int * T
T | + int Shift
T + | int Shift
T + int | Reduce T int
T + T | Reduce E T
T + E | Reduce E T+E
E | Accept
To Shift or Reduce
• Symbols on the left of “|” are kept on a stack
– Top of the stack is at “|”
– Shift pushes a terminal on the stack
– Reduce pops symbols (RHS of production) and pushes a
non terminal (LHS of production) onto the stack
• The most important issues are:
– When to shift and when to reduce!
– Which production to use for reduction?
– Sometimes parser can reduce but it should not!
– X Є can always be reduced!
– Sometimes parser can reduce in different ways!
• In a given state, more than one action (shift or
reduce) may lead to a valid parse
– If it is legal to shift or reduce:
• Shift-reduce conflict
– If it is legal to reduce by two different productions:
To Shift or Reduce ? - Conflicts
– If it is legal to reduce by two different productions:
• Reduce-reduce conflict
• Reduce action should be taken only if the result
can be reduced to the start symbol
Shift-Reduce Parsing - Handles
• A substring that matches the right-side of a production
that occurs as one step in the rightmost derivation. This
substring is called a handle.
• Because d is a right-sentential form, the substring to the
right of a handle contains only terminal symbols.
Therefore, the parser doesn’t need to scan past theTherefore, the parser doesn’t need to scan past the
handle.
• If a grammar is unambiguous, then every right
sentential form has a unique handle
• If we can find those handles, we can build a derivation
Recognizing Handles
• Given the grammar: E T + E | T
T int * T | int | (E)
• Consider step : int | * int + int
• We could reduce by T int giving T | * int + int
– But this is incorrect because:
• No way to reduce to the start symbol E
• So, a handle is a reduction that also allows further
reductions back to the start symbol
• In shift-reduce parsing, handles appear only at the top
of the stack, never inside
• Handles always appear only at stack top:
– Immediately after reducing a handle
– Right-most non-terminal on top of the stack
– Next handle must be to right of right-most non-
terminal, because this is a right-most derivation
– Sequence of shift moves reaches next handle
Recognizing Handles
– Sequence of shift moves reaches next handle
• It is not obvious how to detect handles
• At each step the parser sees only the stack, not the
entire input; start with that . . .
• α is a viable prefix if there is a β such that α|β is a
state of a shift-reduce parser
Viable Prefixes
• A viable prefix does not extend past the right end
of the handle
• It’s a viable prefix because it is a prefix of the
handle
• As long as a parser has viable prefixes on the
stack no parsing error has been detectedstack no parsing error has been detected
• For any grammar, the set of viable prefixes is a
regular language
• So, we can generate an automata to recognize
viable prefixes!
Hierarchy of Grammar Class
Lecture9 syntax analysis_5

More Related Content

What's hot

Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
Mahesh Kumar Chelimilla
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
First and follow set
First and follow setFirst and follow set
First and follow set
Dawood Faheem Abbasi
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
alldesign
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
Antony Alex
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
Dr. Jaydeep Patil
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
ROOP SAGAR
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
Prankit Mishra
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Module 11
Module 11Module 11
Module 11
bittudavis
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
Jothi Lakshmi
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 

What's hot (20)

Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Left factor put
Left factor putLeft factor put
Left factor put
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Module 11
Module 11Module 11
Module 11
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 

Similar to Lecture9 syntax analysis_5

Parsing
ParsingParsing
Parsing
khush_boo31
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
Mattupallipardhu
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
RamBhardwaj11
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
BBDITM LUCKNOW
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
GeetanjaliSingh82
 
Floating point arithmetic operations (1)
Floating point arithmetic operations (1)Floating point arithmetic operations (1)
Floating point arithmetic operations (1)
cs19club
 
UNIT-3 Complete PPT.pptx
UNIT-3 Complete PPT.pptxUNIT-3 Complete PPT.pptx
UNIT-3 Complete PPT.pptx
Medicaps University
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
ShasidharaniD
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Polygon Fill
Polygon FillPolygon Fill
Polygon Fillwahab13
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
Shab Bi
 

Similar to Lecture9 syntax analysis_5 (20)

Parsing
ParsingParsing
Parsing
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
 
Floating point arithmetic operations (1)
Floating point arithmetic operations (1)Floating point arithmetic operations (1)
Floating point arithmetic operations (1)
 
UNIT-3 Complete PPT.pptx
UNIT-3 Complete PPT.pptxUNIT-3 Complete PPT.pptx
UNIT-3 Complete PPT.pptx
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Polygon Fill
Polygon FillPolygon Fill
Polygon Fill
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 
Cs1311lecture23wdl
Cs1311lecture23wdlCs1311lecture23wdl
Cs1311lecture23wdl
 

More from Mahesh Kumar Chelimilla

Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
Mahesh Kumar Chelimilla
 
Lecture3 lexical analysis
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysis
Mahesh Kumar Chelimilla
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
Transportlayer tanenbaum
Transportlayer tanenbaumTransportlayer tanenbaum
Transportlayer tanenbaum
Mahesh Kumar Chelimilla
 
Network layer tanenbaum
Network layer tanenbaumNetwork layer tanenbaum
Network layer tanenbaum
Mahesh Kumar Chelimilla
 
Forouzan frame relay
Forouzan frame relayForouzan frame relay
Forouzan frame relay
Mahesh Kumar Chelimilla
 
Forouzan data link_2
Forouzan data link_2Forouzan data link_2
Forouzan data link_2
Mahesh Kumar Chelimilla
 
Forouzan data link_1
Forouzan data link_1Forouzan data link_1
Forouzan data link_1
Mahesh Kumar Chelimilla
 
Datalinklayer tanenbaum
Datalinklayer tanenbaumDatalinklayer tanenbaum
Datalinklayer tanenbaum
Mahesh Kumar Chelimilla
 

More from Mahesh Kumar Chelimilla (14)

Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Lecture3 lexical analysis
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysis
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
 
Transportlayer tanenbaum
Transportlayer tanenbaumTransportlayer tanenbaum
Transportlayer tanenbaum
 
Network layer tanenbaum
Network layer tanenbaumNetwork layer tanenbaum
Network layer tanenbaum
 
Forouzan x25
Forouzan x25Forouzan x25
Forouzan x25
 
Forouzan ppp
Forouzan pppForouzan ppp
Forouzan ppp
 
Forouzan isdn
Forouzan isdnForouzan isdn
Forouzan isdn
 
Forouzan frame relay
Forouzan frame relayForouzan frame relay
Forouzan frame relay
 
Forouzan data link_2
Forouzan data link_2Forouzan data link_2
Forouzan data link_2
 
Forouzan data link_1
Forouzan data link_1Forouzan data link_1
Forouzan data link_1
 
Forouzan atm
Forouzan atmForouzan atm
Forouzan atm
 
Datalinklayer tanenbaum
Datalinklayer tanenbaumDatalinklayer tanenbaum
Datalinklayer tanenbaum
 

Recently uploaded

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
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
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
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
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
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
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
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
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
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
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
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
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
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
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
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
 

Lecture9 syntax analysis_5

  • 1. Syntax Analysis IV Lecture 9 Syntax Analysis IV Bottom-Up Parsing
  • 2. Bottom-Up Parsing • Given a grammar G, a parse tree for a given string is constructed by starting at the leaves (terminals of the string) and working to the root (the start symbol S). • They are able to accept a more general class of grammars compared to top-down predictive parsers. • It builds on the concepts developed in top-down parsing. • Preferred method for most of the parser generators including bison • They don’t need left-factored grammars – So, its valid to use the following grammar E T + E | T T int * T | int | (E)
  • 3. • A parse for a string generates a sequence of derivations of the form: S δ0 δ1 δ2 … … δn-1 sentence • Bottom-up parsing reduces a string to the start symbol by inverting productions Bottom-Up Parsing • Let A b be a production and δi-1 and δi be two consecutive derivations with sentential forms: αAβ and αbβ – δi-1 is derived from δi by match the RHS b in δi, and then replacing b with its corresponding LHS, A. This is called a reduction • The parse tree is the result of the tokens and the reductions.
  • 4. • Consider the parse for the input string: int * int + int E T + E | T T int * T | int | (E) • When we reduce, we only have terminals to the right. Sentential Form Productions int * int + int int * T + int T int T + int T int * T T + T T int Bottom-Up Parsing terminals to the right. – In the reduction: αAβ αbβ – If b to A is a step of a bottom-up parsing (i.e. A b is a reduction) – Then β is a string of terminals • In other words, a bottom-up parser traces a rightmost derivation in reverse T + E E T E E T+E
  • 5. Shift-Reduce Parsing • Idea: Split string being parsed into two parts: – Two parts are separated by a special character ‘|’ – Left part is a string of terminals and non terminals – Right part is a string of terminals • Still to be examined • Bottom up parsing has two actions• Bottom up parsing has two actions – Shift: Move terminal symbol from right string to left string • ABC | xyz ABCx | yz – Reduce: Apply an inverse production at the right end of the left string – If A xy is a production, then • Cbxy | ijk CbA | ijk
  • 6. Sentential Form Productions |int * int + int Shift int | * int + int Shift int * | int + int Shift int * int | + int Reduce T int int * T | + int Reduce T int * T Shift-Reduce Example int * T | + int Reduce T int * T T | + int Shift T + | int Shift T + int | Reduce T int T + T | Reduce E T T + E | Reduce E T+E E | Accept
  • 7. To Shift or Reduce • Symbols on the left of “|” are kept on a stack – Top of the stack is at “|” – Shift pushes a terminal on the stack – Reduce pops symbols (RHS of production) and pushes a non terminal (LHS of production) onto the stack • The most important issues are: – When to shift and when to reduce! – Which production to use for reduction? – Sometimes parser can reduce but it should not! – X Є can always be reduced! – Sometimes parser can reduce in different ways!
  • 8. • In a given state, more than one action (shift or reduce) may lead to a valid parse – If it is legal to shift or reduce: • Shift-reduce conflict – If it is legal to reduce by two different productions: To Shift or Reduce ? - Conflicts – If it is legal to reduce by two different productions: • Reduce-reduce conflict • Reduce action should be taken only if the result can be reduced to the start symbol
  • 9. Shift-Reduce Parsing - Handles • A substring that matches the right-side of a production that occurs as one step in the rightmost derivation. This substring is called a handle. • Because d is a right-sentential form, the substring to the right of a handle contains only terminal symbols. Therefore, the parser doesn’t need to scan past theTherefore, the parser doesn’t need to scan past the handle. • If a grammar is unambiguous, then every right sentential form has a unique handle • If we can find those handles, we can build a derivation
  • 10. Recognizing Handles • Given the grammar: E T + E | T T int * T | int | (E) • Consider step : int | * int + int • We could reduce by T int giving T | * int + int – But this is incorrect because: • No way to reduce to the start symbol E • So, a handle is a reduction that also allows further reductions back to the start symbol • In shift-reduce parsing, handles appear only at the top of the stack, never inside
  • 11. • Handles always appear only at stack top: – Immediately after reducing a handle – Right-most non-terminal on top of the stack – Next handle must be to right of right-most non- terminal, because this is a right-most derivation – Sequence of shift moves reaches next handle Recognizing Handles – Sequence of shift moves reaches next handle • It is not obvious how to detect handles • At each step the parser sees only the stack, not the entire input; start with that . . . • α is a viable prefix if there is a β such that α|β is a state of a shift-reduce parser
  • 12. Viable Prefixes • A viable prefix does not extend past the right end of the handle • It’s a viable prefix because it is a prefix of the handle • As long as a parser has viable prefixes on the stack no parsing error has been detectedstack no parsing error has been detected • For any grammar, the set of viable prefixes is a regular language • So, we can generate an automata to recognize viable prefixes!