University of Kansas Drew Davidson 13 SLR Parser
- Slides: 37
University of Kansas | Drew Davidson 13 – SLR Parser Construction
Checkin LR Parsers Why can’t LL(1) parsers handle left recursion but LR can? 2
Last Time LR Parsers • Concept • Theory • Operation 2 Amazin’ facts: • All viable prefixes for an LR grammar can be captured by a Finite State Automaton! • A stack of states can track our position Parsing 3
Today’s Outline SLR Parsing Parser Construction • Build parser automaton • Build LR parser table Parser Operation • Running from Table Parsing 4
Today’s Outline SLR Parsing There are several types of “classic” LR Parsers LR(1), aka “Canonical LR” LALR SLR LR(0) All have the same automaton edge types and run exactly the same way Only differ in what the states represent 5
A Quick Piece of Vocab: Items SLR Parsing An item represents progress through a production “Basic” item form Z � α ● X β • What constitutes an item depends on LR parser type • FSM states are sets of items In a Z production Already seen symbol(s) α About to produce symbol X After X, will produce symbols(s) β Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id G Parser Automaton (item sets shown) G Parser Automaton P I 0 I 1 R ❶ ( I 2 I 0 I 5 R ❷ - id I 4 R ❸ L I 3 ) - id I 6 R ❹ I 2 S’ → ● P P → ● ( L ) ( P → (● L ) L → ● id L → ● L id id I 4 L → id ● I 1 P S’ → P ● I 5 P → ( L ) ● ) I L 3 P → ( L ● ) L → L ● id id I 6 L → L Id ● 6
A Quick Piece of Vocab: Items SLR Parsing An item represents progress through a production • What constitutes an item depends on LR parser type • FSM states are sets of items The heavyweight form seems more powerful What could possibly go wrong? “Basic” item form Z � α ● X β In a Z production Already seen symbol(s) α About to produce symbol X After X, will produce symbols(s) β “Heavyweight” item form Z � α ● X β, t/u/v As above, but lookahead token could be t or u or v 7
FSM State Space Explosion LR Parser Construction Bad news: The FSM can become impractically large 8
Today’s Outline SLR Parsing Parser Operation • Quick review of parser operation Parser Construction • Introduce Closure and Go. To • Build parser automaton • Build parser table Parsing 9
SLR Building Workflow Input Grammar Parser Automaton Go. To Closure Sets State Diagram Transition Operations Go. To Action Table Define the edges of the automaton Define the states of the automaton 10
SLR Building: Closure From Grammar Input Grammar Parser Automaton Go. To Closure Sets State Diagram Transition Operations Go. To Action Table “What other items look like I to the parser? ” Building Closure(I) Add I and repeat until saturation: if X � α ● Z β is in Closure(I): for all Z : : = γ productions: add Z � ● γ 11
SLR Building: Go. To From Closure Input Grammar Parser Automaton Go. To Closure Sets State Diagram Transition Operations Go. To Action Table If we were in a state I item, where might we be after parsing X? 12
SLR Building: FSM from Go. To + Closure Input Grammar Parser Automaton Go. To Closure Sets I 0 I 2 I 4 S’ → ● P P → ● ( L ) ( P → (● L ) L → ● id L → ● L id id L → id ● Transition Operations Go. To Action Table State Diagram I 1 P S’ → P ● I 5 P → ( L ) ● ) L I 3 P → ( L ● ) L → L ● id id I 6 L → L Id ● 13
SLR Building: FSM from Go. To + Closure Input Parser Automaton Go. To Closure Sets Grammar State Diagram Transition Operations Go. To Action Table 14
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id Start with the I 0 state closure { S’ → ● P } 15
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) ( P Start with the I 0 state closure { S’ → P ● } closure { P → ( ● L ) Add Edges: Go. To(I 0, P) = closure({ S’ → P ● }) Go. To(I 0, ( ) = closure({P → ( ● L ) }) 16
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) ( P closure { S’ → P ● } Identify state Closure({ S’ → P ● }) = { S’ → P ● } (nothing else) closure { P → ( ● L ) 17
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) ( P I 1 S’ → P ● Identify state Closure({ S’ → P ● }) = { S’ → P ● } (nothing else) closure { P → ( ● L ) Identify edges from I 1 (none) 18
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) ( closure P I 1 S’ → P ● Identify state Closure({ P → ( ● L ) }) = P → ( ● L ) L → ● id L → ● L id { P → ( ● L ) 19
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 S’ → P ● ( I 2 P → (● L ) L → ● id L → ● L id id closure L Identify state Closure({ P → ( ● L ) }) = P → ( ● L ) L → ● id L → ● L id closure { L → id ● } 20
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 S’ → P ● ( I 2 P → (● L ) L → ● id L → ● L id id closure L closure { L → id ● } 21
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 L I 3 S’ → P ● ( I 2 P → (● L ) L → ● id L → ● L id id P → ( L ● ) L → L ● id closure { L → id ● } 22
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 P → (● L ) L → ● id L → ● L id id closure { L → id ● } S’ → P ● closure ( I 2 { P → ( L ) ● L I 3 ) P → ( L ● ) L → L ● id id closure { L → id ● ) Edges out of I 3 Go. To(I 3, ) ) = closure({P → ( L ) ● }) Go. To(I 3, id) = closure({L → L id ● }) 23
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 closure ( I 2 P → (● L ) L → ● id L → ● L id id closure { L → id ● } S’ → P ● { P → ( L ) ● L I 3 Identify state Closure({ L → id ● }) = L → id ● ) P → ( L ● ) L → L ● id id closure { L → id ● ) 24
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 closure ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● S’ → P ● { P → ( L ) ● L I 3 ) P → ( L ● ) L → L ● id Identify state Closure({ L → id ● }) = L → id ● No edges out of I 4 id closure { L → id ● } 25
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 1 closure ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● S’ → P ● { P → ( L ) ● L I 3 ) P → ( L ● ) L → L ● id Identify state Closure({ P → ( L ) ● }) = P → ( L ) ● (nothing else) id closure { L → id ● } 26
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 5 ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● I 1 L I 3 S’ → P ● P → ( L ) ● ) P → ( L ● ) L → L ● id Identify state Closure({ P → ( L ) ● }) = P → ( L ) ● (nothing else) No edges out of I 5 id closure { L → id ● } 27
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 5 ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● I 1 L I 3 S’ → P ● P → ( L ) ● ) P → ( L ● ) L → L ● id Identify state Closure({ L → id ● }) = L → id ● (nothing else) id closure { L → id ● } 28
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 5 ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● I 1 L I 3 S’ → P ● P → ( L ) ● ) P → ( L ● ) L → L ● id Identify state Closure({ L → id ● }) = L → id ● (nothing else) No edges out of I 6 id I 6 L → L id ● 29
Building an SLR Parser: Example SLR Parsing Parse Table Construction 1: Add new 1 st production S’ : : = S to G 2: Build State I 0 for Closure({S’ → ● S}) 3: Saturate FSM: Add edges according to Go. To Add nodes according to Closure Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 5 ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● I 1 L I 3 S’ → P ● P → ( L ) ● ) P → ( L ● ) L → L ● id Automaton Complete! id I 6 L → L id ● 30
Time to Convert FSM to a Table LR Parser Construction Input Parser Automaton Go. To Closure Sets Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id I 0 S’ → ● P P → ● ( L ) P I 5 ( I 2 I 4 P → (● L ) L → ● id L → ● L id id L → id ● I 1 L I 3 State Diagram Transition Operations Go. To Action Table S’ → P ● P → ( L ) ● ) P → ( L ● ) L → L ● id id I 6 L → L id ● 31
Basic LRTable Structure Parser Construction Row: Item Column: Symbol Go. To Table Action Table I 0 I 2 I 4 S’ → ● P P → ● ( L ) ( P → (● L ) L → ● id L → ● L id id L → id ● I 1 P ( S’ → P ● I 5 P → ( L ) ● ) L I 3 P → ( L ● ) L → L ● id id I 6 L → L Id ● ) id eof P L I 0 I 1 I 2 I 3 I 4 I 5 I 6 32
What Should. LR Parser the. Construction Table Look Like? Types of Actions (hence, table cell entries): • Shift (terminal edge), push 1 state, advance lookahead • Reduce (reduce state), pop RHS stack items • Go. To (nonterminal edge), push LHS item following reduce • (Special actions: accept / reject) Accepting state Pop 2 items I 6 I 3 I 3 I 2 I 2 I 0 Item Stack T 3 id Next Token I 0 Item Stack ) Next Token I 0 Item Stack R❹ to L New top ) Next Token I 5 I 3 I 2 I 0 Item Stack I 2 ) Next Token I 0 Item Stack eof Next Token 33
Time to Convert FSM to a Table LR Parser Construction Input Parser Automaton Go. To Closure Sets Grammar State Diagram Transition Operations Go. To Action Table shift go to accept reduce 34
Time to Convert FSM to a Table LR Parser Construction 4 Types of Table entries Shift, Go. To, Accept, Reduce Standard DFA Translation Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id Go. To Table Action Table I 0 I 2 I 4 S’ → ● P P → ● ( L ) ( P → (● L ) L → ● id L → ● L id id L → id ● I 1 P S’ → P ● I 5 P → ( L ) ● ) I L 3 P → ( L ● ) L → L ● id id I 6 L → L Id ● ( ) id eof I 0 S I 2 P L I 1 S I I 2 I 3 S I 5 4 I 3 S I 6 I 4 I 5 I 6 35
Time to Convert FSM to a Table LR Parser Construction 4 Types of Table entries Shift, Go. To, Accept, Reduce Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id Mark state(s) with S’ → S ● as accept I 0 I 2 I 4 S’ → ● P P → ● ( L ) ( P → (● L ) L → ● id L → ● L id id L → id ● Go. To Table Action Table I 1 P S’ → P ● I 5 P → ( L ) ● ) I L 3 P → ( L ● ) L → L ● id id I 6 L → L Id ● ( ) id eof I 0 S I 2 L I 1 S I I 2 I 3 P S I 5 4 I 3 S I 6 I 4 I 5 I 6 36
Time to Convert FSM to a Table LR Parser Construction 4 Types of Table entries Shift, Go. To, Accept, Reduce FOLLOW(L) = { ), id } FOLLOW(P) = { eof } Grammar G ❶ S’ : : = P ❷ P : : = ( L ) ❸ L : : = id ❹ L : : = L id Go. To Table Action Table I 0 I 2 I 4 S’ → ● P P → ● ( L ) ( P → (● L ) L → ● id L → ● L id id L → id ● I 1 P S’ → P ● I 5 P → ( L ) ● ) I L 3 P → ( L ● ) L → L ● id id I 6 L → L Id ● ( ) id eof I 0 S I 2 L I 1 S I I 2 4 I 3 S I 6 I 3 S I 5 I 4 R❸ R❸ R❷ I 5 I 6 P R❹ R❹ 37
- Conflicts in slr parser
- Drew davidson ku
- Slr parser
- Drew davidson ku
- Kansas kansas state fight
- Richard davidson andrews university
- Map of drew university
- Slr
- Crr for cooperative banks
- View camera parts
- Slr vs lr
- Slr
- Sicard test
- Credit control measures of rbi
- Slr test
- Ilrs
- Slr parsing table
- Slr
- University of kansas premed
- Kstate computer science
- Ku writing center
- Ctpps
- Sim writing strategies
- Ku premed
- K state counseling
- Pi davidson
- Davidson county drug court
- Beta club shirt ideas
- Missionaries of the most holy eucharist
- Arthur davidson legal
- Clad
- Davidson county mental health court
- Davidson police department
- Webphysics.davidson.edu physlet_resources
- Davidson county co-op
- Harley davidson value proposition
- Dnet davidson
- Scotson v pegg (1861)