Download presentation
Presentation is loading. Please wait.
Published byKateřina Libuše Jandová Modified over 5 years ago
1
Building Readback FSMs for Readahead FSMs
Building Readback FSMs for Readahead FSMs
2
Goals for This Section
3
To understand what a readback FSM does To understand how to build one.
Goals To understand what a readback FSM does To understand how to build one. and what do we need to build one. A previously built readahead FSM If doing it by hand Additional information that simulates the opposite of right and down; i.e., left and up If doing it algorithmically
4
What’s a Readback FSM?
5
How does a Parser Work Again!
Find the right end of a handle (while munching inputs, stacking stuff (3 stacks that grow on the right), and moving R (indicator for the right end of a handle)) Find the left end of a handle (while traversing the stack from right to left and moving L) Reduce to a nonterminal A (using the stack contents between L and R, build a new tree and replace everything by an A-token) and repeat until no more input (EndOfFile encountered); equivalent to reaching an accept table.
6
How does a Parser Work Again!
Find the right end of a handle. Find the left end of a handle. Reduce to a nonterminal A. A readahead FSM is used to guide this process A readback FSM is used to guide this process Grammar '|-' G {EndOfFile} G' -> 1 2 3 4 G -> 5 A Readahead FSM a Right part states 6 7 A -> Ra '|-' Ra @G 1 2 Ra Ra @G' G {EndOfFile} 3 6 A Ra 4 A A readahead FSM processed from left to right moving R (the right pointer) a @G A readback FSM processes information in the stack from right to left in order to move L (the left pointer) Ra a @A 5
7
Recall: What Precisely Is The Stack Information
Consider this readahead FSM: Readahead FSM Ra '|-' Ra @G 1 2 Ra Ra @G' G {EndOfFile} 3 6 A Ra 4 A The table number below the token is the goto for that token; e.g., from 1, '|-’ goes to 2, so 2 is in the corresponding table number stack. a @G Ra a @A 5 Suppose it was working on: |- A A EndOfFile Consider 2 parallel stacks. What do we expect to see Token stack - '|-' A A Table number stack 1 2 4 4
8
Recall: This is How We Showed the Pair Diagrammatically
2 parallel stacks. Table number stack 1 2 4 4 Token stack - '|-' A A A more compact way to show it. -1 '|-'2 A4 A4 Technically, it’s a special kind of TransitionName; namely a ReadbackName with 2 parts: a token name and a readahead state (although what we are showing here is the state number for the readahead state). We could have implemented it just as 2-element array. During implementation, it nicer to store a readahead state instead of a state number since it allows the states to be renumbered without having to fix up the readback names.
9
Let’s See the Readback FSM
Grammar transitions in the Readahead FSM connect to the initial states of the Readback FSM '|-' G {EndOfFile} G' -> 1 2 3 4 The final states correspond to being at the left end of the handle (the look transition is what’s to the left) G -> 5 A a Right part states 6 7 A -> '|-' G’ is special To Accept @G 2 @G' Readahead FSM G {EndOfFile} Readback FSM 3 6 Ra '|-' Ra A @G 1 2 Rb 4 A Ra Ra @G' A4 G {EndOfFile} 1 6 a 3 { '|-'2 } @G Ra A To Reduce to G 4 A a @A a 5 @G { , '|-’2 } a5 A4 Rb Rb Ra @A 2 3 a To Reduce to A 5
10
Using the RaFSM to find the right end of a handle?
Augmented Grammar Readahead FSM Ra Ra '|-' A G’ -> '|-’ G {EndOfFile} G -> A *. A -> a. 1 2 @G Ra Ra {EndOfFile} @G' G 3 4 Ra a Ra @A 5 Showing R only At each step, restart from the beginning, go as far as you can |- a a EndOfFile Reached state 5 |- A a EndOfFile Reached state 5 again RECALL THIS |- A A EndOfFile Reached state 2 |- G EndOfFile Reached state 4 This means STOP
11
Let’s Use Both FSMs (Version That Restarts)
To Accept Ra '|-' Ra @G Readahead FSM Readback FSM Rb 1 2 Ra Ra A4 A 4 @G' G 1 {EndOfFile} 1 3 6 { '|-'2 } { '|-' 2 } Ra A To Reduce to G To Reduce to G 4 A a Rb { , } @G '|-’2 a5 A4 Rb a 5 { '|-' 2 , } A 4 Ra 2 3 To Reduce to A @A 2 3 a To Reduce to A 5 Using RaFSM Using RbFSM |-2 a5 a EndOfFile Reached state 5 |-2 a5 a EndOfFile R L L tags along to the right of R L R Moved left 1 |-2 A4 a5 EndOfFile Reached state 5 |-2 A4 a5 EndOfFile R L L tags along to the right of R L R Moved left 1 |-2 A4 A4 EndOfFile Reached state 2 |-2 A4 A4 EndOfFile L tags along to the right of R L R Moved left 2 R L This will STOP |-2 G3 EndOfFile Reached state 6
12
Building Readback FSMs
Building Readback FSMs
13
Building Readback FSMs
By hand via tracing By hand via relations. Algorithmically
14
Building Readback FSMs
Building Readback FSMs By Hand Via Tracing
15
Building Readback FSMs By Hand Tracing
You need both the grammar and the readahead FSM. Readahead FSM Grammar Ra '|-' Ra @G '|-' G {EndOfFile} 1 2 G' -> 1 2 3 4 Ra Ra @G' G {EndOfFile} 3 6 G -> 5 A A Ra 4 A a Right part states a 6 7 A -> @G Ra a @A 5 For symbol start reverse tracing using the grammar as a guide. Don’t go outside of your production. Stuff not part of the production should be drawn as a LOOK; i.e. lookback.
16
Reverse Tracing from FIRST @G
You need both the grammar and the readahead FSM. Stopped showing Ra (less clutter) Readahead FSM Grammar '|-' @G '|-' G {EndOfFile} 1 2 G' -> 1 2 3 4 @G' G {EndOfFile} 3 6 G -> 5 A A 4 A a Right part states a 6 7 A -> @G a @A 5 For For A* in G-production, how many A’s got us to state 2 0 A’s what’s to the left is NOT part of G production; i.e., it lookback. Draw readback FSM for {'|-’2} going to “reduce to G”
17
Reverse Tracing from SECOND @G
You need both the grammar and the readahead FSM. Readahead FSM Grammar '|-' @G '|-' G {EndOfFile} 1 2 G' -> 1 2 3 4 @G' G {EndOfFile} 3 6 G -> 5 A A 4 A a Right part states a 6 7 A -> @G a @A 5 For For A* in G-production, how many A’s got us to state 4 A from 2, and A from 4 both correspond to A4 (both goto 4) Draw readback FSM for A4+ {'|-’2} going to “reduce to G” So there is at least one A4 but there could be more. What’s to the left when done? It’s '|-’2 1 or more A4‘s
18
Reverse Tracing from @A
You need both the grammar and the readahead FSM. Readahead FSM Grammar '|-' @G '|-' G {EndOfFile} 1 2 G' -> 1 2 3 4 @G' G {EndOfFile} 3 6 G -> 5 A A 4 A a Right part states a 6 7 A -> @G a @A 5 For a in A-production, how many ways of getting to state 5 a from 2, and a from 4 both correspond to a5 (both goto 5) So there is exactly one a5. What’s to the left when done? Either, '|-’2 if it came from 2, or A4 if it came from 4 Draw readback FSM for a5 {'|-’2 | A4} going to “reduce to A” Or
19
Summarizing The Result
For Draw readback FSM for {'|-’2} going to “reduce to G” { '|-' 2 } 1 To Reduce to G For Draw readback FSM for A4+ {'|-'2} going to “reduce to G” { '|-'2 } A4 Combine it; its e | A4+ A4 2 3 { '|-'2 } A4 To Reduce to G Draw readback FSM for a5 {'|-'2 | A4} going to “reduce to A” a5 a5 { '|-'2 } 4 5 To Reduce to A { A4 }
20
Comparing New Derivation With OLD
{ '|-'2 } For 1 To Reduce to G A4 A4 Combine it; its e | A4+ = A4 * For 2 3 { '|-'2 } To Reduce to G a5 { '|-'2 } 4 5 To Reduce to A { A4 } '|-' @G Readahead FSM Readback FSM 1 2 A4 @G' G {EndOfFile} 1 3 6 { '|-'2 A } 4 A To Reduce to G a @G a5 { '|-'2 , } A4 @A 2 3 a To Reduce to A 5
21
A More Complicated example
Building BOTH readahead + readback FSMs by hand. Grammar Lisp with the addition operator Note from wilf: This had been intended to be just a little more complicated. It turned out to be a lot more complicated. SHOULD I REMOVE THE EXAMPLE OR USE IT TO EXPLAIN SOMETHING COMPLICATED |- G {EndOfFile} G' -> 1 2 3 4 E Build tree semantic action; just a transition with an action #buildTree and parameters +. ( ) G -> 5 6 7 E + a => + 8 9 10 11 12 E -> a 13 Step 1: Expand G' Ra Ra Ra Ra No readback for G’ since it will lead to Accept (in future, will replace state 4 by the accept state) |- G {EndOfFile} @G' 1 2 3 4
22
A More Complicated example
Grammar |- G {EndOfFile} ( ) G' -> 1 2 3 4 G -> 5 6 7 E + a => + 8 9 10 11 12 E -> a 13 Step 2: Expand G in Ra Add the reverse in Rb Ra Ra Ra Ra |- G {EndOfFile} @G' Lookahead; i.e., Follow (G) 1 2 3 4 lookback E E5 Red G ( Ra Ra @G Rb ) )6 Rb (5 Rb {|-2} 5 6 1 2 3 1
23
A More Complicated example
Grammar |- G {EndOfFile} ( ) G' -> 1 2 3 4 G -> 5 6 7 E + a => + 8 9 10 11 12 E -> a 13 Step 3: Expand E in Ra i.e., expand E in RED by the right side of E-production in BLUE Ra Ra Ra Ra |- G {EndOfFile} @G' Lookahead; i.e., Follow (G) 1 2 3 4 lookback E E5 Ra ( Ra @G Rb )6 Rb Red G ) (5 Rb {|-2} Rb 5 6 1 2 3 4 1 NEXT SLIDE SHOWS RESULT
24
A More Complicated example
Grammar E |- G {EndOfFile} ( G' -> 1 2 3 4 ) G -> 5 6 7 E + a => + 8 9 10 11 12 E -> a 13 Step 3: Expand E in Ra i.e., expand E in RED by the right side of E-production BLUE Ra Ra Ra Ra |- G {EndOfFile} @G' Lookahead; i.e., Follow (G) 1 2 3 4 lookback E E5 Ra Ra ( @G Rb )6 Rb Rb Rb Red G ) (5 {|-2} 5 6 1 2 3 4 1 + a => + Result MUST be deterministic 6 8 9 Ignore adding readback for now Can’t add a new E-transition to a state with +-transition. a So far so good. Looks OK but is it? 10
25
A More Complicated example
Grammar E |- G {EndOfFile} ( G' -> 1 2 3 4 ) G -> 5 6 7 E + a => + 8 9 10 11 12 Here’s the problem E -> An E could be a | a+a | a+a+a … a 13 It can’t be +a It can’t be +a Step 3: Expand E in Ra i.e., expand E in RED by the right side of E-production BLUE Ra Ra Ra Ra |- G {EndOfFile} @G' Lookahead; i.e., Follow (G) 1 2 3 4 lookback E E5 Ra Ra @G Rb ( )6 Rb (5 Rb {|-2} Rb Red G ) 5 6 1 2 3 4 1 + a => + 6 8 9 a The readahead FSM allows |- (+a… This is WRONG 10
26
Trying to Explain What is Wrong
Grammar E E + a => + 8 9 10 11 12 E -> ( ) 5 G -> 6 7 a An E could be a | a+a | a+a+a … 13 It can’t be +a We know this is true (from the grammar) What’s the problem: We are trying to merge E* from and E+a from E E {6} {6} E E + a {8} {9} i.e. merge with 8 9 10 11 6 E {9} {} Let’s MERGE wih the algorithm that converts to a deterministic FSM E E E E So {6,8} {6, 9} {6} {6} + The rest of this is simple DID NOT EXPECT SUCH A COMPLICATED RESULT
27
Trying Again With the Dot Pushing Algorithm
Grammar E E + a => + 8 9 10 11 12 E -> ( ) 5 G -> 6 7 a An E could be a | a+a | a+a+a … 13 It can’t be +a What’s the problem: We are trying to merge E* from and E+a from (from dot pushing) (from the grammar) E E {6} 6 E 8 {6} E + a i.e. merge with 8 9 10 11 E 6 {8} {9} E {9} {} Let’s use the dot pushing algorithm E E E 6,8 6,9 6,9 It’s converting the loop from E* to E+ 8 8 + The rest of this is simple OK. MUCH simpler than the previous approach
28
A More Complicated example
What we USED to have Ra Ra Ra Ra |- G {EndOfFile} @G' Lookahead; i.e., Follow (G) 1 2 3 4 lookback E E5 Ra Rb Red G ( Ra ) @G )6 Rb (5 Rb {|-2} Rb 5 6 1 2 3 4 1 + a => + 6 8 9 + a before an E should NOT have happened (not in the language) a Changed E5 to E7. 10 Everything else in the old RbFSM is the same What we have now Ra Ra Ra Ra |- G {EndOfFile} @G' Lookahead; i.e., Follow (G) 1 2 3 4 lookback E7 Ra Ra Rb Rb Red G ( ) @G )6 (5 Rb {|-2} 5 6 1 2 3 1 E Lookahead; i.e., Follow (A) The original E that was expanded was replaced by 2 new E’s. Ra ) E Ra 7 Ra Ra @A + a => + … SLIDE TOO BUSY. Continued on next slide Now we can continue Ra 9 10 11 a 8
29
Continuing E Grammar E + a => + ( ) 8 9 10 11 12 G -> 5 6 7
13 Step 5: Expand the 2 E’s going to state 7 Ra Ra Ra Ra @G' Lookahead for 2 i.e., Follow (E) |- G {EndOfFile} 1 2 3 4 E7 Ra Ra @G Rb ( )6 Rb ) (5 Rb {|-2} Red G 5 6 1 2 3 1 2 new lookbacks These 2 E’s have NOT YET BEEN EXPANDED E The semantic action is INVISIBLE to the readback FSM E ) 7 Rb a9 Rb +8 Rb E7 Rb {(5} Red E + a => + @E 8 9 10 4 5 6 7 2 Rb a11 a a @E {E7} 11 8
30
Last Step No more nonterminals to expand, so we’re done E Grammar E +
=> + ( ) 8 9 10 11 12 G -> 5 6 7 E -> a 1. Expand E in state 5. E+a… is already there. Also, a is already there 13 2. Expand E in state 7. E+a… is already there. Also, a is already there Step 5: Expand the 2 E’s going to state 7 No more nonterminals to expand, so we’re done Ra Ra Ra Ra Lookahead for 2 i.e., Follow (E) |- G {EndOfFile} @G' 1 2 3 4 E7 Ra Ra Rb ( ) @G )6 Rb (5 Rb {|-2} Red G 5 6 1 2 3 1 2 new lookbacks E The semantic action is INVISIBLE to the readback FSM E ) 7 Rb + a => + @E a9 Rb +8 Rb E7 Rb {(5} Red E 8 9 10 4 5 6 7 2 Rb a11 a a @E {E7} 11 8
31
What’s Wrong With This Technique
Easy if there are no loops BUT can be very hard with loops. Solution: In readahead FSMs, don’t loop back as soon as you can Impossible to automate Deciding where a pair came from is error-prone (easy to forget some). Also difficult to find the lookbacks for when you’re at the left end.. OK, doing it by hand is not recommended but can be done
32
Building Readahead FSMs
Building Readahead FSMs Via Relations
33
Just like we implemented readahead states as sets of something
Basic Idea Just like we implemented readahead states as sets of something i.e., right part states We can implement readback states as sets of something too i.e., <right part state, readahead state> pairs Using short form 110 to represent readback item <1, 10> See next slide for why
34
Recall This Readahead FSM
Ra State 1 '|-' G {EndOfFile} G' -> 1 2 3 4 1 '|-' G -> 5 A a Right part states 6 7 Ra State 2 A -> Ra State 3 2 3 Ra State 6 {EndOfFile} @G' 5, 6 4 Ra State 4 If I say “look at right part state 5” in the readahead FSM, you say which one. 5 @G @G G A 6 a A 2 5’s, 2 6’s a <right part state, readahead state> pairs Ra State 5 7 @A 52, 54 62, 64
35
Readback states are sets of readback items;
Basic Idea Readback states are sets of readback items; i.e., <right part state, readahead state> pairs Now we need a way of being able to compute successors! Basic idea: Track all the relations; i.e., right and down when computing readahead FSM and use these somehow to compute the readback FSM.
36
where r is an initial state
Recall: 2 Relations The right relation X = {(p,q) such that there is a production right part of the form } to go right... A -> … X … p q X p q a A Still using and We will now call them relations. The down relation A = {(p,r) such that there is a production right part of the form and } ? -> … A … p q to go down... A A -> … … r p r where r is an initial state
37
So, for example, consider the grammar
Recording Relations So, for example, consider the grammar Grammar c a No augmented grammar for simplicity A b G -> 1 2 A -> 3 4 A and We will now call them relations. Examples of recording relations A c Things we used to do in our heads when building readahead FSMs A 1 2 2 2 1 3
38
Generalizing Relations
Just like the > relation might originally be defined, say, just for integers. 10 > 1 We might like to generalize it to floats as in 10.2 > 1.2 Going further, we can also allow mixed types as in 1.01 > 1
39
Generalizing Right, and Down
c a Grammar Using short form 110 A b G -> 1 2 A -> 3 4 to represent readback item <1, 10> Suppose you built a number of readahead states like Ra State 20 Original Relations Generalizations 2 A A Ra State 10 1 2 110 220 A 1 A 3 generalizations A20 1 3 b 3 110 220 Ra State 30 Same idea for downs A 4 110 2
40
Before We Start When do we apply downs. Before a state’s successors are to be computed. At this point in time, we know what state it’s in. A This is the kind of relation that can be used. 111 311 When do we apply rights. To compute successors but at that point in time, we don’t know if the successor is a NEW or OLD state. A This is the best we can do. 111 2 Once we know which successor state it is; e.g., 20. We can convert the above to A20 This is the best we can do. 111 220
41
Are Inverses Better For Computing Readback?
For readback, we’ll ultimately need up and left (inverse of down and right) A 311 111 A 111 311 rather than a20 110 220 a20 220 110 rather than No harder to record one way or the other (just flips the sides) But when first computing successors, we might as well use right (instead of left), converting later directly to left later converting to a 110 2 a20 220 110 once we know the successor state is 20
42
Summary When applying downs, record “complete ups”
31 111 When applying rights, record “not quite complete right” a 110 2 states may be NEW or OLD After successor states are known, record “complete left” a20 220 110 convert above right to left
43
Now Let’s Build Readahead/Readback FSMs
Grammar '|-' G {EndOfFile} G' -> 1 2 3 4 c a We’ll start numbering Rastates at 10 so you won’t confuse them with the right part states. A b G -> 5 6 A -> 7 8 Ra 10 Initial Ra State Note that we know what Ra state this is. 1 before applying downs Temporary Rights Ups Lefts “none” “none” “none” Apply downs and recording ups Ra 10 Yes, it’s the same because there were no downs 1
44
Compute Succesors of Ra 10 (Before Seeing if they Already Exist)
Grammar '|-' G {EndOfFile} G' -> 1 2 3 4 Why is it that we don’t know the successor state numbers? a c A b 5 6 7 They might be PRE-EXISTING states or NEW ones. G -> A -> 8 Temporary Rights Ups Lefts Computing Successors (State Numbers Unknown) “none” “none” '|-' 110 2 Ra 10 '|-' 1 Ra ? 2
45
Compute Succesors of Ra 10 (After looking them up)
Grammar '|-' G {EndOfFile} G' -> 1 2 3 4 c a A b We now know the successor Ra state number G -> 5 6 A -> 7 8 Temporary Rights Ups Lefts Computing Successors 11 “none” '|-' '|-'11 11 Ra 10 '|-' 111 2 211 111 1 Ra 11 Now convert rights to lefts 2
46
Apply Downs on Ra 11 2 1 2 5 7 G '|-'11 511 211 211 111 A 711
Grammar '|-' G {EndOfFile} G' -> 1 2 3 4 c a A b G -> 5 6 A -> 7 8 Computing Successors Before applying downs Temporary Rights Ups Lefts Ra 10 '|-' Ra 11 G '|-'11 511 211 211 111 1 2 A 711 511 Apply downs and recording ups Ra 11 2 5 7
47
Computing Successors of Ra 11 (Before Knowing Succesor Numbers)
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 G G '|-'11 G a c 211 3 511 211 211 111 511 211 A A b A G -> 5 A 6 A -> 7 8 711 511 511 711 511 6 Ra ? a Computing Successors 3 711 7 b Ra ? 711 8 G 6 Ra 11 Ra 10 A '|-' 2 Ra ? We don’t know the successor Ra state number. Record temporary rights. 1 a 7 5 b 7 Ra ? 8
48
Computing Successors of Ra 11 (After Knowing Successor Numbers)
Grammar '|-' G {EndOfFile} G' -> 1 2 3 4 c a A b G -> 5 6 A -> 7 8 Temporary Rights Ups Lefts Ra 12 12 G '|-'11 G 12 Computing Successors 211 3 511 211 211 111 3 A 13 A 711 511 G12 Ra 13 13 511 6 312 211 G 6 14 Ra 11 a A13 Ra 10 14 711 A 7 613 511 '|-' 2 Ra 14 1 15 b 15 a14 a 711 7 8 5 714 711 b b15 We now know the successor Ra state numbers. Record lefts from augmented rights Ra 15 815 711 8
49
Apply Downs to Ra 12. None, so Compute Successors
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 a G '|-'11 c {EndOfFile} 511 211 211 111 312 4 A A b G -> 5 6 A -> 7 8 711 511 G12 Ra ? 312 211 Ra 12 {EndOfFile} Computing Successors 4 A13 3 613 511 Ra 13 a14 G 714 711 6 Ra 11 Ra 10 A b15 815 711 '|-' 2 Ra 14 1 We don’t know the successor Ra state number. Record temporary rights. a 7 5 b Ra 15 8
50
We Now Know the Successors of Ra 12 (Ra 16)
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 a 16 G '|-'11 c {EndOfFile} 511 211 16 211 111 312 4 A b A G -> 5 6 A -> 7 8 711 511 G12 Ra 16 312 211 Ra 12 {EndOfFile} Computing Successors 4 A13 3 613 511 G Ra 13 a14 714 711 6 Ra 11 A Ra 10 b15 815 711 '|-' 2 Ra 14 1 a We now know the successor Ra state number. Record extended temporary rights as lefts {EndOfFile}16 7 5 b 416 312 Ra 15 8
51
Compute Downs to Ra 16 (None) So Compute Successors (Also None) Record @G'
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 a G '|-'11 c 511 211 211 111 A b A G -> 5 6 A -> 7 8 711 511 G12 Ra 16 312 211 Ra 12 {EndOfFile} Computing Successors 4 @G' A13 3 613 511 Ra 13 a14 G @G' = Follow (G') = {EndOfFile} 714 711 6 Ra 11 Ra 10 A b15 815 711 '|-' 2 Ra 14 1 a {EndOfFile}16 7 5 416 312 b Ra 15 8
52
Compute Downs to Ra 13 (None) So Compute Successors (State Number Unknown) Record @G
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 a 13 G '|-'11 c c 13 511 211 211 111 613 6 A A b G -> 5 6 A -> 7 8 711 511 G12 Ra 16 312 211 Ra 12 {EndOfFile} Computing Successors 4 A13 3 613 511 Ra 13 a14 G c 714 711 6 Ra 11 @G Ra 10 b15 A 815 711 '|-' 2 @G = Follow (G) = {EndOfFile} Ra 14 1 a {EndOfFile}16 7 By eyeballing the grammar 5 416 312 b Follow (G) = {EndOfFile] Follow (A) = {EndOfFile, c} Ra 15 c13 613 613 8
53
Finish Compute Successors to Ra 13 (State Number Now Known)
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 a G '|-'11 c c 511 211 211 111 613 6 A A b G -> 5 6 A -> 7 8 711 511 G12 Ra 16 312 211 Ra 12 {EndOfFile} Computing Successors 4 A13 3 613 511 Ra 13 a14 G c 714 711 6 Ra 11 @G Ra 10 b15 A 815 711 '|-' 2 @G = Follow (G) = {EndOfFile} Ra 14 1 a {EndOfFile}16 7 5 416 312 b Ra 15 c13 Running out of space. Stop drawing states < 14 613 613 8
54
Compute Downs to Ra 14. (None) so Compute Successors (Unknown State Numbers)
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 a G '|-'11 c a 511 211 211 111 714 7 A b A G -> 5 6 A -> 7 8 711 511 b G12 714 312 211 8 Computing Successors Ra ? A13 613 511 7 Ra 14 a We don’t know the successor Ra state number. Record temporary rights. a14 7 714 711 Ra ? b b15 8 Ra 15 815 711 8 {EndOfFile}16 416 312 c13 613 613
55
Finish Computing Successors of Ra 14 (State Numbers Now Known)
Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 '|-'11 a G 14 c a 211 111 511 211 14 714 7 A b A G12 G -> 5 6 A -> 7 8 15 711 511 312 211 b 15 714 A13 8 613 511 Computing Successors Ra 14 a14 714 711 7 Ra 14 a We now know the successor Ra state numbers. Turns out that THEY ALREADY EXIST Record temporary rights. b15 7 815 711 Ra 15 b {EndOfFile}16 8 416 312 Ra 15 8 c13 613 613 a14 714 714 b15 815 714
56
Finally, Compute Downs to Ra 15 (none). Compute Successors (None)
Finally, Compute Downs to Ra 15 (none). Compute Successors (None). Grammar Temporary Rights Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 '|-'11 a G c 211 111 511 211 A A b G12 G -> 5 6 A -> 7 8 711 511 312 211 A13 613 511 Computing Successors By eyeballing the grammar a14 Follow (G) = {EndOfFile] Follow (A) = {EndOfFile, c} 714 711 Ra 14 a b15 7 815 711 b {EndOfFile}16 416 312 Ra 15 @A 8 c13 @A = Follow (A) = {EndOfFile, c} 613 613 a14 714 714 b15 815 714
57
Summarizing Ups Lefts Ra 12 G '|-'11 Ra 16 211 111 211 3 511 {EndOfFile} A The Readahead FSM 4 @G' G12 711 511 312 211 To Accept Ra 13 G c The goal is special A13 6 613 @G = Follow (G) = {EndOfFile} 511 Ra 11 Ra 10 A To Readback a14 '|-' 2 714 711 1 Ra 14 a b15 7 815 711 5 a b b {EndOfFile}16 312 Ra 15 416 8 c13 @A = Follow (A) = {EndOfFile, c} To Readback 613 613 a14 714 714 CLAIM: The readahead FSM is done and we have the relations for computing the readback FSM. b15 815 714
58
Building a Readback FSM?
How do we start Building a Readback FSM?
59
But Before We Proceed To Building a Readback FSM
How do we tell if a Readahead State needs a bridge to readback! Pretend we have a large grammar (these are a few of the productions) b a, b Grammar d 3 b 6 d d b c d b A -> 1 2 4 B -> 5 7 Ra 30 Which of the right parts states inside of Ra 30 suggests you need a bridge to readback? Final right part states indicate you’re at the end of readahead. 1, 2, 4, 5, 7 but not 3, 6 Intuitively, each nonterminal with final right part states ends the readahead
60
But Before We Proceed To Building a Readback FSM
How do we tell if a Readahead State needs a bridge to a readback state! Pretend we have a large grammar (these are a few of the productions) Grammar b a, b d 3 b 6 d d b c d b A -> 1 2 4 B -> 5 7 How many bridges do we need, 1, 2, 3, …? Each bridge goes to a separate initial readback state. Ra 30 Intuitively, each nonterminal with final right part states ends the readahead 1 2 4 are for A, so at the end you need to reduce to A 5 7 are for B, so at the end you need to reduce to B
61
But Before We Proceed To Building a Readback FSM
How do we tell if a Readahead State needs a bridge to a readback state! Pretend we have a large grammar (these are a few of the productions) Grammar b a, b d 3 b 6 d d b c d b A -> 1 2 4 B -> 5 7 You need 2 bridges, one per nonterminal Ra 30 Intuitively, each nonterminal with final right part states ends the readahead Ultimately, the end of readback will be reached when initial right part states are encountered. One leading to reduce to A and the other leading to reduce to B This means you want to build 2 completely different readback FSMs.
62
Bridging: One for each @ (@A and @B here)
How do we tell if a Readahead State needs a bridge to a readback state! Pretend we have a large grammar (these are a few of the productions) b a, b Grammar d 3 b 6 d d b c d b A -> 1 2 4 B -> 5 7 Initial Rb 100 Ra 30 @A = Follow (A) @A The end of readahead must be replaced i.e., the appropriate lookahead Follow (A) and Follow (B) What if these two sets have something in common? @B Initial Rb 200 Not answered in the notes @B = Follow (B)
63
Back to the Example
64
Back to the Example: Create 2 Initial Readback States
Grammar c a Ups Lefts G A '|-'11 b G -> 5 6 7 8 511 211 A -> 211 111 A G12 Ignoring G’, 6 and 9 above are final. So Ra 13 and Ra 15 need lookahead to initial readback states 711 511 312 211 Ra 12 A13 Ra 16 613 511 3 {EndOfFile} @G' a14 4 To Accept 714 711 G Ra 13 b15 Rb 1 815 711 6 @G = {EndOfFile] Ra 11 Ra 10 A 613 {EndOfFile}16 416 312 '|-' 2 1 Ra 14 a c13 7 5 613 a 613 b b Rb 2 a14 714 714 Ra 15 815 = {EndOfFile, c} b15 8 @A 815 714
65
Computing Rb-Successors
Ups Lefts G '|-'11 Rb 1 Rb 3 511 211 211 111 A 613 A13 511 G12 711 511 312 211 A13 Rb 3 613 511 c13 a14 613 714 711 b15 815 711 {EndOfFile}16 Rb 2 Rb 4 Rb ? 416 312 815 b15 a14 c13 613 613 a14 714 714 There won’t be any items added as a result of closure… b15 815 714
66
Does It Match The Grammar?
Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 G '|-'11 a c 511 211 211 111 A A b G12 711 511 G -> 5 6 A -> 7 8 312 211 A13 613 511 Readback FSM a14 Rb 1 Rb 3 714 711 613 A13 511 b15 815 711 {EndOfFile}16 416 312 Rb 3 YES c13 c13 613 613 Rb 2 Rb 4 a14 815 b15 a14 714 714 b15 815 714
67
Does It Match The Readahead FSM?
Ra 12 Ups Lefts Ra 16 3 {EndOfFile} G '|-'11 4 @G' 511 211 211 111 The Readahead FSM To Accept A G Ra 13 c G12 711 511 312 211 6 @G = Follow (G) = {EndOfFile} Ra 11 Ra 10 A To Rb1 A13 613 '|-' 2 511 1 Ra 14 a a14 7 714 711 5 a b b15 b 815 711 Ra 15 {EndOfFile}16 8 @A = Follow (A) = {EndOfFile, c} 416 312 To Rb2 Readback FSM c13 613 613 Rb 1 Rb 3 Rb 2 Rb 4 a14 A13 815 b15 a14 YES 613 511 714 714 b15 c13 815 714
68
Adding Lookback Grammar Ups Lefts '|-' G {EndOfFile} G' -> 1 2 3 4 G '|-'11 a c 511 211 211 111 A A b G12 711 511 G -> 5 6 A -> 7 8 312 211 A13 613 511 Anything with an Up is a final readback state and needs lookback. Readback FSM a14 714 711 Rb 1 Rb 3 b15 A13 511 815 613 711 {EndOfFile}16 416 312 Rb 3 c13 c13 613 613 Rb 2 Rb 4 a14 714 714 815 b15 a14 b15 815 714
69
Intuitive Summary So Far
Readahead FSMs are sets of right part states (or call them readahead items). They can be built entirely using down and right (on right part states). However, if you generalize down and right to work on (right part state / readahead state) pairs, call them readback items, then its easy to flip these to their inverses up and left. Readback FSMs are sets of readback items and they can be built entirely using left and up (that you built during the readahead process).
70
Detailed Summary So Far: Building Readahead States
While building readaheads states which are collections of right parts states, you need to create the up and left relations you will need later. right part state When applying downs, record ups (you know the states they’re in at the time) A 31 111 When applying rights when we don’t yet know whether the state is OLD or NEW. a 110 2 After successor states are known (say it’s 20) , we convert the above right to left 20 20 a 110 2 a20 220 110 Next, you build readback states 20
71
Detailed Summary So Far: Building Readback States
Readback states are collections of readback items, pairs containing right part state × readahead state Building initial readback states: If a readahead state, say Ra 10, has a set of final right part states f1, f2, … associated with nonterminal A (and there may be more than one such nonterminal) If A is G', buld a lookahead bridge to the Accept state. Otherwise, build a lookahead bridge to an initial readback state, say Rb 100, as follows. Ra 10 Accept Follow (A) where A = G' Ra 10 Rb 100 Follow (A) {f110, f210, …} Building successor readback states: We compute the ap-successor of a readback state, say Rb 200, as follows: ap We’ll have more so say about the readbacks after we discuss invisibles Rb 200 (assuming this set is not empty)
72
Detailed Summary So Far : Building Readback States
Determining which Readback states are final: Recall: Readback states are collections of readback items, pairs containing right part state × readahead state If such a state, say readback state Rb 400, has initial right part states f1, f2, … associated with nonterminal A, then you need lookback from this state to reduce state A; i.e., We compute this lookback from f1, f2, … Rb 400 Reduce to A lookback We’ll have more so say about the lookback after we discuss invisibles
73
What’s an Invisible?
74
Consider the Following Grammar
Terminal (implied read) Grammar Terminal (implied look) NON-TREE Building Semantic Action Nonterminal TREE Building Semantic Action b {c} #indent B => ‘+’ A -> 1 2 3 4 5 6 Suppose This is The Corresponding Readahead FSM fragment Ra Ra Ra Ra Ra Ra a b {c} #indent B => ‘+’ 10 20 30 40 50 60 If you were able to run to Ra 60, what would be on the stack? Just for context
75
Consider the Following Grammar
Readahead FSM fragment Ra Ra Ra Ra Ra Ra a b {c} #indent B => ‘+’ 10 20 30 40 50 60 If you were able to run to Ra 60, what would be on the stack? a10 b20 B50 that’s it An invisible is something that can’t ever be on the stack. So you CAN’T HAVE AN INVISIBLE transition in a readback state. If you had only reached Ra 40, what would you see in the stack? Not answered in the notes
76
Summary aq ? Bq ? {a}q ? {@A}q ? ? ?
Which ones should shortly lead to a Reduce Table? Which ones could ever be on the STACK? Ra Ra a aq … … ? terminal p q YES Those for which q is a final state Ra Ra B Bq ? nonterminal … p q … YES Ra Ra {a} {a}q ? look … p q … NO Ra Ra @A Unknown look … p q … ? NO Ra 'list' Ra Tree Building Semantic action … … 'list'q p q ? NO Ra #indent Ra Other Semantic action #indentq ? NO … p q … Nonterminals and terminals with the read attribute are visible; i.e., stackable. All others are invisible.
77
Lookback?
78
Lookback Lookback is what you would see TO THE LEFT when you reach a final readback state. Readahead FSM Fragment Grammar Fragment Some Relations C Ra 10 Ra 20 420 220 a B a G -> 1 2 3 1 2 B C 620 420 C C 4 B -> 4 5 D D 6 820 620 Ra 30 D C -> 6 7 8 s a20 9 s 220 110 D -> 8 9 s30 Rb 50 Rb 40 930 820 {a20} s30 820 930 Follow(D) How did we get {a20} from 820?
79
Lookback Lookback is what you would see TO THE LEFT when you reach a final readback state. Readahead FSM Fragment Some Relations Grammar Fragment C Ra 10 Ra 20 420 220 4 a B a G -> 1 2 3 1 2 B C 620 420 3 C C 4 B -> 4 5 D D 6 820 620 2 Ra 30 D C -> 6 7 8 s a20 9 220 110 s 5 D -> 8 9 Rb 50 s30 Rb 40 930 820 {a20} If you go left after you go up at least once, any matching VISIBLE left’s transition name is a look s30 820 930 1 How did we get {a20} from 820?
80
In general, Lookback is the following
F -> a #indent C C -> B B -> A A -> w R * invisibles Mp ( | ) Example grammar What does it mean? In readback state R, you are at the left end of the handle say w to be reduced to say A Then you are where the dot is… A -> w Up 1 B -> A Up C -> B Up F -> a #indent C Invisible Left F -> a #indent C Visible a is lookback; but as a pair with the associated Ra state
81
The COMPLETE Process for Computing Readback FSM with lookback Bridge
The COMPLETE Process for Computing Readback FSM with lookback Bridge
82
The Table Building Algorithm
Let the initial state of the extra production that was added to augment your grammar be IG’. The initial readahead state before closure: {IG’} * The initial readahead state after closure: {IG’} Let the R be any readahead state after closure; i.e., a set of dotted productions (or equivalent right part states) M The M-successor of R before closure: R Provided this set has something in it; i.e., something in it has an M-successor M * The M-successor of R after closure: R R M Provided has something in it
83
The Table Building Algorithm
Let F be the set of final right part states in readahead state Ra associated with nonterminal A There can be more than one such nonterminal The initial readback state before closure: F * The initial readback state after closure: F invisibles Let R be any readback state after closure; i.e., a set of readback items. Mp The Mp-successor of R before closure: R Provided this set has something in it; i.e., something in it has an Mp-successor Mp The Mp-successor of R after closure: R invisibles * R Mp Provided has something in it
84
The Table Building Algorithm
Let R be the set of readback items in a readback state Rb associated with nonterminal A where the right part state is an initial state. All items in a readback state have to be associated with the same nonterminal. The lookback for R includes Mp if * Mp R ( | ) invisibles In words, if you go up at least once, and then go up and left over invisibles as far as you can and then you go left over the visible Mp, then Mp is a valid look.
85
What’s Wrong With This Technique
A lot of steps. Just building readback states from up and left relations is relatively easy. Building the left and up relations while building readahead states is a little more complicated. Building lookback appears the most complicated to describe although it’s not actually that much harder than the previous step. You should be able to do it for a grammar with a handful of productions; but any more than that, you’ll run out of paper.
86
End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.