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 (20)

PPT
Ll(1) Parser in Compilers
Mahbubur Rahman
 
PDF
Lecture4 lexical analysis2
Mahesh Kumar Chelimilla
 
PPT
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
PPT
Top down parsing
ASHOK KUMAR REDDY
 
PPTX
First and follow set
Dawood Faheem Abbasi
 
PDF
Topdown parsing
Royalzig Luxury Furniture
 
DOCX
Nonrecursive predictive parsing
alldesign
 
PPTX
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
PDF
Topdown parsing
Antony Alex
 
PPTX
Compiler: Syntax Analysis
Dr. Jaydeep Patil
 
PPTX
Parsing (Automata)
ROOP SAGAR
 
PDF
07 top-down-parsing
Harish Khodke
 
PPTX
Top down parsing
Prankit Mishra
 
PDF
Left factor put
siet_pradeep18
 
PPT
Bottom - Up Parsing
kunj desai
 
PPT
Module 11
bittudavis
 
PDF
Lexicalanalyzer
Royalzig Luxury Furniture
 
PPTX
Predictive parser
Jothi Lakshmi
 
PPTX
Recognition-of-tokens
Dattatray Gandhmal
 
Ll(1) Parser in Compilers
Mahbubur Rahman
 
Lecture4 lexical analysis2
Mahesh Kumar Chelimilla
 
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Top down parsing
ASHOK KUMAR REDDY
 
First and follow set
Dawood Faheem Abbasi
 
Topdown parsing
Royalzig Luxury Furniture
 
Nonrecursive predictive parsing
alldesign
 
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
Topdown parsing
Antony Alex
 
Compiler: Syntax Analysis
Dr. Jaydeep Patil
 
Parsing (Automata)
ROOP SAGAR
 
07 top-down-parsing
Harish Khodke
 
Top down parsing
Prankit Mishra
 
Left factor put
siet_pradeep18
 
Bottom - Up Parsing
kunj desai
 
Module 11
bittudavis
 
Lexicalanalyzer
Royalzig Luxury Furniture
 
Predictive parser
Jothi Lakshmi
 
Recognition-of-tokens
Dattatray Gandhmal
 

Similar to Lecture9 syntax analysis_5 (20)

PPTX
parsing in compiler design presentation .pptx
GeetanjaliSingh82
 
PPTX
Bottom up parsing.pptx
RamBhardwaj11
 
PPT
BUP-1 (1).pptLec 11-BUP-1 (1).pptpptpptppt
Aliza530614
 
PPT
aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbD-lr-parsing.ppt
raosg
 
PPT
Compiler Design_Syntax Analyzer_Bottom Up Parsers.ppt
RushaliDeshmukh2
 
PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
PPT
D-lr-parsing compiler how to compiler com
compengwaelalahmar
 
PPTX
BOTTOM_UP_Parsing techniques_compiler design5.pptx
salaja2
 
PPTX
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
PDF
Assignment7
Sunita Milind Dol
 
PPT
Cd2 [autosaved]
BBDITM LUCKNOW
 
PPTX
Syntax Analysis - LR(0) Parsing in Compiler
RizwanAbro4
 
PPTX
Parsing
Roohaali
 
PDF
Bottom up parser
Akshaya Arunan
 
PDF
Syntactic analysis in NLP
kartikaVashisht
 
PDF
LL(1) and the LR family of parsers used in compilers
MandarMitra1
 
PDF
Syntax analysis
Akshaya Arunan
 
PPT
Parsing
khush_boo31
 
PPTX
Parsing techniques, notations, methods of parsing in compiler design
nithya g
 
PPTX
Compiler Deisgn-Varrious parsing methods
cscprabh
 
parsing in compiler design presentation .pptx
GeetanjaliSingh82
 
Bottom up parsing.pptx
RamBhardwaj11
 
BUP-1 (1).pptLec 11-BUP-1 (1).pptpptpptppt
Aliza530614
 
aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbD-lr-parsing.ppt
raosg
 
Compiler Design_Syntax Analyzer_Bottom Up Parsers.ppt
RushaliDeshmukh2
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
D-lr-parsing compiler how to compiler com
compengwaelalahmar
 
BOTTOM_UP_Parsing techniques_compiler design5.pptx
salaja2
 
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
Assignment7
Sunita Milind Dol
 
Cd2 [autosaved]
BBDITM LUCKNOW
 
Syntax Analysis - LR(0) Parsing in Compiler
RizwanAbro4
 
Parsing
Roohaali
 
Bottom up parser
Akshaya Arunan
 
Syntactic analysis in NLP
kartikaVashisht
 
LL(1) and the LR family of parsers used in compilers
MandarMitra1
 
Syntax analysis
Akshaya Arunan
 
Parsing
khush_boo31
 
Parsing techniques, notations, methods of parsing in compiler design
nithya g
 
Compiler Deisgn-Varrious parsing methods
cscprabh
 
Ad

More from Mahesh Kumar Chelimilla (14)

PDF
Lecture11 syntax analysis_7
Mahesh Kumar Chelimilla
 
PDF
Lecture3 lexical analysis
Mahesh Kumar Chelimilla
 
PDF
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
PDF
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
PPT
Transportlayer tanenbaum
Mahesh Kumar Chelimilla
 
PPT
Network layer tanenbaum
Mahesh Kumar Chelimilla
 
PPT
Forouzan x25
Mahesh Kumar Chelimilla
 
PPT
Forouzan ppp
Mahesh Kumar Chelimilla
 
PPT
Forouzan isdn
Mahesh Kumar Chelimilla
 
PPT
Forouzan frame relay
Mahesh Kumar Chelimilla
 
PPT
Forouzan data link_2
Mahesh Kumar Chelimilla
 
PPT
Forouzan data link_1
Mahesh Kumar Chelimilla
 
PPT
Forouzan atm
Mahesh Kumar Chelimilla
 
PPT
Datalinklayer tanenbaum
Mahesh Kumar Chelimilla
 
Lecture11 syntax analysis_7
Mahesh Kumar Chelimilla
 
Lecture3 lexical analysis
Mahesh Kumar Chelimilla
 
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
Transportlayer tanenbaum
Mahesh Kumar Chelimilla
 
Network layer tanenbaum
Mahesh Kumar Chelimilla
 
Forouzan frame relay
Mahesh Kumar Chelimilla
 
Forouzan data link_2
Mahesh Kumar Chelimilla
 
Forouzan data link_1
Mahesh Kumar Chelimilla
 
Datalinklayer tanenbaum
Mahesh Kumar Chelimilla
 
Ad

Recently uploaded (20)

PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
Hashing Introduction , hash functions and techniques
sailajam21
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 

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!