SlideShare a Scribd company logo
Syntax Analysis V
Lecture 10
Syntax Analysis V
Bottom-Up Parsing
Hierarchy of Grammar Class
Viable Prefixes
• α is a viable prefix of a given grammar if:
– There is a such that αw is a right sentential form
• α | ω is a state of the shift-reduce parser
• As long as the parser has viable prefixes on the stack no• As long as the parser has viable prefixes on the stack no
parser error has been seen
• The set of viable prefixes is a regular language (not
obvious)
• Construct an automaton that accepts viable prefixes
LR(0) Items
• An LR(0) item of a grammar G is a production of G with a
special symbol “.” at some position of the right side
• • Thus production AXYZ gives four LR(0) items
A .XYZ
A X.YZ
A XY.ZA XY.Z
A XYZ.
• An item indicates how much of a production has been seen at
a point in the process of parsing
– Symbols on the left of “.” are already on the stack s
– Symbols on the right of “.” are expected in the input
• The only item for X ɛ is X .
Viable Prefixes and LR(0) Items
• Consider the input: (int)
E T + E | T
T int * T | int | (E)
• Then (E | ) is a state of a shift-reduce parse
• (E is a prefix of the rhs of T (E)
– Will be reduced after the next shift
• Item T (E.) says that so far we have seen (E of
this production and hope to see )
Viable Prefixes and LR(0) Items
• The stack may have many prefixes of rhs’s
– Prefix1 Prefix2 . . . Prefixn-1Prefixn
• • Let Prefixi be a prefix of the rhs of Xi αi
– Prefixi will eventually reduce to Xi– Prefixi will eventually reduce to Xi
– The missing part of αi-1 starts with Xi
– i.e. there is a Xi-1 Prefixi-1 Xi β for some β
• Recursively, Prefixk+1…Prefixn eventually
reduces to the missing part of αk
Viable Prefixes and LR(0) Items
• Consider the string (int * int):
– (int *|int) is a state of a shift-reduce parse
• “(” is a prefix of the rhs of T (E)
• “ɛ” is a prefix of the rhs of E T
• “int *” is a prefix of the rhs of T int * T
• The “stack of items”
– T (.E) says, we have seen “(” of T (E)
– E .T says, we have seen of E T
– T int * .T says, we have seen int * of T int * T
Recognizing Viable Prefixes
• Therefore, we have to build the finite automata that
recognizes this sequence of partial rhs’s of productions
• Algorithm:
1. Add a dummy production S’ S to G
2. The NFA states are the items of G2. The NFA states are the items of G
– Including the extra production
3. For item E α.Xβ add transition
– E α.X β X E α X. β
4. For item E α.Xβ and production X γ add
– E α.Xβ ɛ X .γ
5. Every state is an accepting state
6. Start state is S’ .S
Recognizing Viable Prefixes
• Given the grammar:
S’ E
E T + E | T
T int * T | int | (E)
Create an NFA to recognize viable prefixes
Recognizing Viable Prefix
S' .E
S' E.
E
E .T+Eɛ
T
T .(E)
ɛ
E T.+E E T+.E
+
ɛ
ɛ
T (.E)
(
T (E.)
E
ɛ
ɛ
T (E).
)
E
ɛ
T int.
ɛ
S' .E
E .T
E T.
T .int
ɛ
T .int*T
ɛ
ɛ
ɛ E T+E.
ɛ
T int.
int
T int.*Tint
T int*.T
*
T int*T.T
ɛ ɛ
ɛ
T
NFA to DFA – Subset Construction
S .E
E .T
S E.
E
E T.
E T.+ET
T int.*Tint
E T+.E
E .T
E .T+E
T .(E)
T .int*T
T .int
+
T
int
E T+E.
E
T (.E)
E .T
E .T+E
(
T
(
E .T
E .T+E
T .(E)
T .int*T
T .int
T int.*T
T int.
int
T int*.T
T .(E)
T .int*T
T .int
*
int
E .T+E
T .(E)
T .int*T
T .int
T
T int*T.
T
T (E.)
T (E).
E
)
((
Valid Items
• The states of the DFA are:
– canonical collections of LR(0) items
• Item X β.γ is valid for a viable prefix αβ if
– S’ *αXω αβγω by a right-most derivation
• After parsing αβ, the valid items are the possible tops of
the stack of items
• An item I is valid for a viable prefix α if the DFA
recognizing viable prefixes terminates on input α in a
state S containing I
• The items in S describe what the top of the
item stack might be after reading input α
• An item is often valid for many prefixes
Valid Items
• An item is often valid for many prefixes
– Example: The item T (.E) is valid for prefixes
• (, ((, (((, ((((, . . .
NFA to DFA – Subset Construction
S .E
E .T
S E.
E
E T.
E T.+ET
T int.*Tint
E T+.E
E .T
E .T+E
T .(E)
T .int*T
T .int
+
T
int
E T+E.
E
T (.E)
E .T
E .T+E
(
T
(
Start
state
E .T
E .T+E
T .(E)
T .int*T
T .int
T int.*T
T int.
int
T int*.T
T .(E)
T .int*T
T .int
*
int
E .T+E
T .(E)
T .int*T
T .int
T
T int*T.
T
T (E.)
T (E).
E
)
((
LR(0) Parsing
• Assume
– stack contains α
– next input token is t
– DFA on input α terminates in state s
• Reduce by X β if• Reduce by X β if
– s contains item X β.
• Shift if
– s contains item X β.tω
– Equivalent to saying s has a transition labeled t
LR(0) Parsing - Conflicts
• LR(0) has a reduce/reduce conflict if:
– Any state has two reduce items:
– X β. and Y ω.
• LR(0) has a shift/reduce conflict if:• LR(0) has a shift/reduce conflict if:
– Any state has a reduce item and a shift item:
– X β. and Y ω.tδ
• SLR improves on LR(0) shift/reduce heuristics
– Fewer states have conflicts
Shift Reduce Conflicts
S .E
E .T
S E.
E
E T.
E T.+ET
T int.*Tint
E T+.E
E .T
E .T+E
T .(E)
T .int*T
T .int
+
T
int
E T+E.
E
T (.E)
E .T
E .T+E
(
T
Shift-Reduce conflicts
Either Can be reduced by E T
Or can be shifted for i/p : +
E .T
E .T+E
T .(E)
T .int*T
T .int
T int.*T
T int.
int
T int*.T
T .(E)
T .int*T
T .int
*
int
E .T+E
T .(E)
T .int*T
T .int
T
T int*T.
T
T (E.)
T (E).
E
)
((
Countermeasure
• SLR = Simple LR
• Improve on LR(0) shift/reduce heuristics
– Fewer states have conflicts
SLR Parsing
• Assume
– stack contains α
– next input token is t
– DFA on input α terminates in state s
• Reduce by X β if• Reduce by X β if
– s contains item X β.
– t ϵ Follow (X)
• Shift if
– s contains item X β.tω
– Equivalent to saying s has a transition labeled t
• If there are conflicts under these rules, the
grammar is not SLR
• The rules amount to a heuristic for detecting
SLR Parsing
• The rules amount to a heuristic for detecting
handle
– The SLR grammars are those where the heuristics
detect exactly the handle
Shift Reduce Conflicts
S .E
E .T
S E.
E
E T.
E T.+ET
T int.*Tint
E T+.E
E .T
E .T+E
T .(E)
T .int*T
T .int
+
T
int
E T+E.
E
T (.E)
E .T
E .T+E
(
T
Follow(E) = {$,)}
Follow(T) = {$,),+}
E .T
E .T+E
T .(E)
T .int*T
T .int
T int.*T
T int.
int
T int*.T
T .(E)
T .int*T
T .int
*
int
E .T+E
T .(E)
T .int*T
T .int
T
T int*T.
T
T (E.)
T (E).
E
)
((
SLR Parsing Algorithm
1. Let M be DFA for viable prefixes of G
2. Let |x1…xn$ be initial configuration
3. Repeat until configuration is S|$
1. Let α|ω be current configuration
2. Run M on current stack α2. Run M on current stack α
3. If M rejects α, report parsing error
1. Stack α is not a viable prefix
4. If M accepts α with items I, let u be next input
1. Shift if X β.uγ ϵ I
2. Reduce if X β. ϵ I and u ϵ Follow(X)
3. Report parsing error if neither applies
SLR Parsing - Improvements
• Note that Step 3.3 is redundant
• If there is a conflict in the last step, grammar is not
SLR(k)
• Lots of grammars are not SLR• Lots of grammars are not SLR
– Including all ambiguous grammars
• We can parse more grammars by using precedence
declarations
– Instructions for resolving conflicts
SLR Parsing
• Consider the ambiguous grammar:
– E E + E | E * E | (E) | int
• The DFA for this grammar contains a state
with the following items:with the following items:
– E E * E. and E E. + E
– There is a shift/reduce conflict
• Declaring “* has higher precedence than +”
resolves this conflict in favor of reducing
SLR Parsing Example
S .E
E .T
S E.
E
E T.
E T.+ET
T int.*Tint
E T+.E
E .T
E .T+E
T .(E)
T .int*T
T .int
+
T
int
E T+E.
E
T (.E)
E .T
E .T+E
(
T
1
2 5
7
6
8
E .T
E .T+E
T .(E)
T .int*T
T .int
T int.*T
T int.
int
T int*.T
T .(E)
T .int*T
T .int
*
int
E .T+E
T .(E)
T .int*T
T .int
T
T int*T.
T
T (E.)
T (E).
E
)
((
1
3
4
10
911
• Parse the token stream: int * int$
Configuration DFA Halt State Action
|int * int$ 1 Shift
int | * int$ 3 * ϵ Follow(T) Shift
SLR Parsing Example
int | * int$ 3 * ϵ Follow(T) Shift
int * | int$ 11 Shift
int * int |$ 3 $ ϵ Follow(T) Reduce. T int
int * T |$ 4 $ ϵ Follow(T) Reduce. T int * T
T |$ 5 $ ϵ Follow(T) Reduce. E T
E |$ Accept
Lecture10 syntax analysis_6

More Related Content

PDF
Lecture9 syntax analysis_5
PDF
Lecture7 syntax analysis_3
PDF
Lecture8 syntax analysis_4
PDF
Lecture6 syntax analysis_2
PDF
Lecture5 syntax analysis_1
PPTX
LL(1) parsing
PPT
Lecture 05 syntax analysis 2
PPT
Ll(1) Parser in Compilers
Lecture9 syntax analysis_5
Lecture7 syntax analysis_3
Lecture8 syntax analysis_4
Lecture6 syntax analysis_2
Lecture5 syntax analysis_1
LL(1) parsing
Lecture 05 syntax analysis 2
Ll(1) Parser in Compilers

What's hot (20)

PPTX
Lecture 07 08 syntax analysis-4
PPT
Chapter 5 Syntax Directed Translation
PPTX
First and follow set
PPT
Top down parsing
PPTX
Parsing LL(1), SLR, LR(1)
PDF
Lecture11 syntax analysis_7
PPT
Bottom - Up Parsing
PDF
Topdown parsing
PPTX
Compiler: Syntax Analysis
PDF
Topdown parsing
PPTX
Parsing (Automata)
PPT
Module 11
DOCX
Nonrecursive predictive parsing
PPT
Chapter Five(2)
PPTX
LR(1) and SLR(1) parsing
PDF
Left factor put
DOC
Pcd(Mca)
PPTX
Top down parsing
PDF
Lecture 07 08 syntax analysis-4
Chapter 5 Syntax Directed Translation
First and follow set
Top down parsing
Parsing LL(1), SLR, LR(1)
Lecture11 syntax analysis_7
Bottom - Up Parsing
Topdown parsing
Compiler: Syntax Analysis
Topdown parsing
Parsing (Automata)
Module 11
Nonrecursive predictive parsing
Chapter Five(2)
LR(1) and SLR(1) parsing
Left factor put
Pcd(Mca)
Top down parsing
Ad

Viewers also liked (20)

PPT
Forouzan frame relay
PPTX
Broadband ca
PPTX
Prefixes gr. 2 reading
PPT
Prefixes
PPT
Frame Relay
PDF
Cisco all book command show
DOCX
Computer Networks Front Page
PPT
Prefixes & Suffixes
 
PDF
Operator precedence
PPT
Lecture 06 syntax analysis 3
DOCX
Router vs switch
PPTX
Top Down Parsing, Predictive Parsing
PDF
Bottom up parser
PPTX
Parsing
PPTX
Top down and botttom up Parsing
PDF
Eigrp
PPT
Prefixes and-suffixes
PDF
PPT
Frame Relay
PPT
CCNA PPP and Frame Relay
Forouzan frame relay
Broadband ca
Prefixes gr. 2 reading
Prefixes
Frame Relay
Cisco all book command show
Computer Networks Front Page
Prefixes & Suffixes
 
Operator precedence
Lecture 06 syntax analysis 3
Router vs switch
Top Down Parsing, Predictive Parsing
Bottom up parser
Parsing
Top down and botttom up Parsing
Eigrp
Prefixes and-suffixes
Frame Relay
CCNA PPP and Frame Relay
Ad

Similar to Lecture10 syntax analysis_6 (20)

PPTX
Compiler Deisgn-Varrious parsing methods
PDF
Syntax Analysis (Bottom-Up Parser) PPTs for Third Year Computer Science Engin...
PDF
Syntax Analysis ( Bottom up Parser) PPTs
PPTX
Parsing techniques, notations, methods of parsing in compiler design
PPT
Compiler Design_Syntax Analyzer_Bottom Up Parsers.ppt
PPT
aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbD-lr-parsing.ppt
PPT
Presentation mam saima kanwal
PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
PPTX
BOTTOM_UP_Parsing techniques_compiler design5.pptx
PPT
D-lr-parsing compiler how to compiler com
PDF
lalr. fo engineering student those who to
PPTX
This is a presentation on the parsing.pptx
PPT
LR-Parsing.ppt
PPTX
Bottom-up parsingn_CompilationTechnics.pptx
PPT
Predicting Stock Market Trends Using Machine Learning and Deep Learning Algor...
PPT
Lecture 15 16
PPTX
Compiler Design LR parsing SLR ,LALR CLR
PPTX
sameermlr0parser-200701133032.pptx
DOCX
How do you learn Bottom-up Parsing Algorithm.docx
Compiler Deisgn-Varrious parsing methods
Syntax Analysis (Bottom-Up Parser) PPTs for Third Year Computer Science Engin...
Syntax Analysis ( Bottom up Parser) PPTs
Parsing techniques, notations, methods of parsing in compiler design
Compiler Design_Syntax Analyzer_Bottom Up Parsers.ppt
aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbD-lr-parsing.ppt
Presentation mam saima kanwal
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
BOTTOM_UP_Parsing techniques_compiler design5.pptx
D-lr-parsing compiler how to compiler com
lalr. fo engineering student those who to
This is a presentation on the parsing.pptx
LR-Parsing.ppt
Bottom-up parsingn_CompilationTechnics.pptx
Predicting Stock Market Trends Using Machine Learning and Deep Learning Algor...
Lecture 15 16
Compiler Design LR parsing SLR ,LALR CLR
sameermlr0parser-200701133032.pptx
How do you learn Bottom-up Parsing Algorithm.docx

More from Mahesh Kumar Chelimilla (13)

PDF
Lecture4 lexical analysis2
PDF
Lecture3 lexical analysis
PDF
Lecture2 general structure of a compiler
PDF
Lecture1 introduction compilers
PPT
Transportlayer tanenbaum
PPT
Network layer tanenbaum
PPT
PPT
PPT
PPT
Forouzan data link_2
PPT
Forouzan data link_1
PPT
PPT
Datalinklayer tanenbaum
Lecture4 lexical analysis2
Lecture3 lexical analysis
Lecture2 general structure of a compiler
Lecture1 introduction compilers
Transportlayer tanenbaum
Network layer tanenbaum
Forouzan data link_2
Forouzan data link_1
Datalinklayer tanenbaum

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
OOP with Java - Java Introduction (Basics)
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
introduction to datamining and warehousing
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Sustainable Sites - Green Building Construction
DOCX
573137875-Attendance-Management-System-original
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PPTX
Foundation to blockchain - A guide to Blockchain Tech
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Embodied AI: Ushering in the Next Era of Intelligent Systems
R24 SURVEYING LAB MANUAL for civil enggi
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CH1 Production IntroductoryConcepts.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
OOP with Java - Java Introduction (Basics)
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
introduction to datamining and warehousing
Model Code of Practice - Construction Work - 21102022 .pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Sustainable Sites - Green Building Construction
573137875-Attendance-Management-System-original
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
Foundation to blockchain - A guide to Blockchain Tech

Lecture10 syntax analysis_6

  • 1. Syntax Analysis V Lecture 10 Syntax Analysis V Bottom-Up Parsing
  • 3. Viable Prefixes • α is a viable prefix of a given grammar if: – There is a such that αw is a right sentential form • α | ω is a state of the shift-reduce parser • As long as the parser has viable prefixes on the stack no• As long as the parser has viable prefixes on the stack no parser error has been seen • The set of viable prefixes is a regular language (not obvious) • Construct an automaton that accepts viable prefixes
  • 4. LR(0) Items • An LR(0) item of a grammar G is a production of G with a special symbol “.” at some position of the right side • • Thus production AXYZ gives four LR(0) items A .XYZ A X.YZ A XY.ZA XY.Z A XYZ. • An item indicates how much of a production has been seen at a point in the process of parsing – Symbols on the left of “.” are already on the stack s – Symbols on the right of “.” are expected in the input • The only item for X ɛ is X .
  • 5. Viable Prefixes and LR(0) Items • Consider the input: (int) E T + E | T T int * T | int | (E) • Then (E | ) is a state of a shift-reduce parse • (E is a prefix of the rhs of T (E) – Will be reduced after the next shift • Item T (E.) says that so far we have seen (E of this production and hope to see )
  • 6. Viable Prefixes and LR(0) Items • The stack may have many prefixes of rhs’s – Prefix1 Prefix2 . . . Prefixn-1Prefixn • • Let Prefixi be a prefix of the rhs of Xi αi – Prefixi will eventually reduce to Xi– Prefixi will eventually reduce to Xi – The missing part of αi-1 starts with Xi – i.e. there is a Xi-1 Prefixi-1 Xi β for some β • Recursively, Prefixk+1…Prefixn eventually reduces to the missing part of αk
  • 7. Viable Prefixes and LR(0) Items • Consider the string (int * int): – (int *|int) is a state of a shift-reduce parse • “(” is a prefix of the rhs of T (E) • “ɛ” is a prefix of the rhs of E T • “int *” is a prefix of the rhs of T int * T • The “stack of items” – T (.E) says, we have seen “(” of T (E) – E .T says, we have seen of E T – T int * .T says, we have seen int * of T int * T
  • 8. Recognizing Viable Prefixes • Therefore, we have to build the finite automata that recognizes this sequence of partial rhs’s of productions • Algorithm: 1. Add a dummy production S’ S to G 2. The NFA states are the items of G2. The NFA states are the items of G – Including the extra production 3. For item E α.Xβ add transition – E α.X β X E α X. β 4. For item E α.Xβ and production X γ add – E α.Xβ ɛ X .γ 5. Every state is an accepting state 6. Start state is S’ .S
  • 9. Recognizing Viable Prefixes • Given the grammar: S’ E E T + E | T T int * T | int | (E) Create an NFA to recognize viable prefixes
  • 10. Recognizing Viable Prefix S' .E S' E. E E .T+Eɛ T T .(E) ɛ E T.+E E T+.E + ɛ ɛ T (.E) ( T (E.) E ɛ ɛ T (E). ) E ɛ T int. ɛ S' .E E .T E T. T .int ɛ T .int*T ɛ ɛ ɛ E T+E. ɛ T int. int T int.*Tint T int*.T * T int*T.T ɛ ɛ ɛ T
  • 11. NFA to DFA – Subset Construction S .E E .T S E. E E T. E T.+ET T int.*Tint E T+.E E .T E .T+E T .(E) T .int*T T .int + T int E T+E. E T (.E) E .T E .T+E ( T ( E .T E .T+E T .(E) T .int*T T .int T int.*T T int. int T int*.T T .(E) T .int*T T .int * int E .T+E T .(E) T .int*T T .int T T int*T. T T (E.) T (E). E ) ((
  • 12. Valid Items • The states of the DFA are: – canonical collections of LR(0) items • Item X β.γ is valid for a viable prefix αβ if – S’ *αXω αβγω by a right-most derivation • After parsing αβ, the valid items are the possible tops of the stack of items • An item I is valid for a viable prefix α if the DFA recognizing viable prefixes terminates on input α in a state S containing I
  • 13. • The items in S describe what the top of the item stack might be after reading input α • An item is often valid for many prefixes Valid Items • An item is often valid for many prefixes – Example: The item T (.E) is valid for prefixes • (, ((, (((, ((((, . . .
  • 14. NFA to DFA – Subset Construction S .E E .T S E. E E T. E T.+ET T int.*Tint E T+.E E .T E .T+E T .(E) T .int*T T .int + T int E T+E. E T (.E) E .T E .T+E ( T ( Start state E .T E .T+E T .(E) T .int*T T .int T int.*T T int. int T int*.T T .(E) T .int*T T .int * int E .T+E T .(E) T .int*T T .int T T int*T. T T (E.) T (E). E ) ((
  • 15. LR(0) Parsing • Assume – stack contains α – next input token is t – DFA on input α terminates in state s • Reduce by X β if• Reduce by X β if – s contains item X β. • Shift if – s contains item X β.tω – Equivalent to saying s has a transition labeled t
  • 16. LR(0) Parsing - Conflicts • LR(0) has a reduce/reduce conflict if: – Any state has two reduce items: – X β. and Y ω. • LR(0) has a shift/reduce conflict if:• LR(0) has a shift/reduce conflict if: – Any state has a reduce item and a shift item: – X β. and Y ω.tδ • SLR improves on LR(0) shift/reduce heuristics – Fewer states have conflicts
  • 17. Shift Reduce Conflicts S .E E .T S E. E E T. E T.+ET T int.*Tint E T+.E E .T E .T+E T .(E) T .int*T T .int + T int E T+E. E T (.E) E .T E .T+E ( T Shift-Reduce conflicts Either Can be reduced by E T Or can be shifted for i/p : + E .T E .T+E T .(E) T .int*T T .int T int.*T T int. int T int*.T T .(E) T .int*T T .int * int E .T+E T .(E) T .int*T T .int T T int*T. T T (E.) T (E). E ) ((
  • 18. Countermeasure • SLR = Simple LR • Improve on LR(0) shift/reduce heuristics – Fewer states have conflicts
  • 19. SLR Parsing • Assume – stack contains α – next input token is t – DFA on input α terminates in state s • Reduce by X β if• Reduce by X β if – s contains item X β. – t ϵ Follow (X) • Shift if – s contains item X β.tω – Equivalent to saying s has a transition labeled t
  • 20. • If there are conflicts under these rules, the grammar is not SLR • The rules amount to a heuristic for detecting SLR Parsing • The rules amount to a heuristic for detecting handle – The SLR grammars are those where the heuristics detect exactly the handle
  • 21. Shift Reduce Conflicts S .E E .T S E. E E T. E T.+ET T int.*Tint E T+.E E .T E .T+E T .(E) T .int*T T .int + T int E T+E. E T (.E) E .T E .T+E ( T Follow(E) = {$,)} Follow(T) = {$,),+} E .T E .T+E T .(E) T .int*T T .int T int.*T T int. int T int*.T T .(E) T .int*T T .int * int E .T+E T .(E) T .int*T T .int T T int*T. T T (E.) T (E). E ) ((
  • 22. SLR Parsing Algorithm 1. Let M be DFA for viable prefixes of G 2. Let |x1…xn$ be initial configuration 3. Repeat until configuration is S|$ 1. Let α|ω be current configuration 2. Run M on current stack α2. Run M on current stack α 3. If M rejects α, report parsing error 1. Stack α is not a viable prefix 4. If M accepts α with items I, let u be next input 1. Shift if X β.uγ ϵ I 2. Reduce if X β. ϵ I and u ϵ Follow(X) 3. Report parsing error if neither applies
  • 23. SLR Parsing - Improvements • Note that Step 3.3 is redundant • If there is a conflict in the last step, grammar is not SLR(k) • Lots of grammars are not SLR• Lots of grammars are not SLR – Including all ambiguous grammars • We can parse more grammars by using precedence declarations – Instructions for resolving conflicts
  • 24. SLR Parsing • Consider the ambiguous grammar: – E E + E | E * E | (E) | int • The DFA for this grammar contains a state with the following items:with the following items: – E E * E. and E E. + E – There is a shift/reduce conflict • Declaring “* has higher precedence than +” resolves this conflict in favor of reducing
  • 25. SLR Parsing Example S .E E .T S E. E E T. E T.+ET T int.*Tint E T+.E E .T E .T+E T .(E) T .int*T T .int + T int E T+E. E T (.E) E .T E .T+E ( T 1 2 5 7 6 8 E .T E .T+E T .(E) T .int*T T .int T int.*T T int. int T int*.T T .(E) T .int*T T .int * int E .T+E T .(E) T .int*T T .int T T int*T. T T (E.) T (E). E ) (( 1 3 4 10 911
  • 26. • Parse the token stream: int * int$ Configuration DFA Halt State Action |int * int$ 1 Shift int | * int$ 3 * ϵ Follow(T) Shift SLR Parsing Example int | * int$ 3 * ϵ Follow(T) Shift int * | int$ 11 Shift int * int |$ 3 $ ϵ Follow(T) Reduce. T int int * T |$ 4 $ ϵ Follow(T) Reduce. T int * T T |$ 5 $ ϵ Follow(T) Reduce. E T E |$ Accept