Presentation is loading. Please wait.

Presentation is loading. Please wait.

Readahead FSMs, Readback FSMs, and Reduce States

Similar presentations


Presentation on theme: "Readahead FSMs, Readback FSMs, and Reduce States"— Presentation transcript:

1 Readahead FSMs, Readback FSMs, and Reduce States
Readahead FSMs, Readback FSMs, and Reduce States

2 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.

3 The Process ReadaheadFSM ReadbackFSM Reduce to A
{lookahead} Computed from Follow (A); i.e., what comes after ReadaheadFSM Build and stacks input from left to right Stops when it reaches the right end of a unique handle with R pointing at the rightmost stack entry {lookback} ReadbackFSM Computed from ReadaheadFSM; i.e., what comes before Scans stack from right to left Stops when it reaches the left end of a unique handle with L pointing at the leftmost stack entry Reduce to A Build a tree from information between L and R, pops it off, and replaces it by A and the new tree The lookahead and lookback is used as a bridge to interconnect different parts The process keeps repeating

4 Example -1 -1 G {EndOfFile}-> a b c d Acc grammar 12 G Ra Ra Ra Ra
Readahead FSM 1 2 3 4 5 Rb Rb Rb Rb Rb {-1} a2 b3 c4 d5 Readback FSM 10 9 8 7 6 Red G 11 bridging lookahead bridging lookback Some initial entry -1 How the stack grows during readahead a2 c4 Left initially past right b3 d5 R L How left moves during readback L -1 How stack (generally) shrinks by the reduce G12

5 Parsing Versus Derivations
grammar G {EndOfFile}-> a b c d Input a b c d EndOfFile As we saw on the previous slide, the stack changes as follows This corresponds to the derivation -1 G => abcd a2 b3 c4 d5 -1 G2 So the parser works in the reverse order of a derivation. More specifically, from left-to-right and bottom-up, simulating a specific type of derivation (but we can’t show that here since the example is too simple)

6 What Precisely Is The Stack Information
-1 Stack as shown on previous slide a2 b3 c4 d5 In reality, these are 2 parallel stacks. Token stack - a b c d Table number stack 1 2 3 4 5 They grow on the right Also, there exists a 3rd parallel stack. Tree stack Each entry is an entire tree (not a node in the tree), This is easy to do with object-oriented programming

7 Bridging G {EndOfFile}-> a b c d Acc grammar 12 G Ra Ra Ra Ra Ra a
Readahead FSM 1 2 3 4 5 Rb Rb Rb Rb Rb {-1} a2 b3 c4 d5 Readback FSM 10 9 8 7 6 Red A 11 bridging lookahead bridging lookback Computing lookahead: For each nonterminal A, we need to compute Follow (A); i.e., what can come after “according to the grammar”. Computing lookback: Computed from the information in the readahead FSM.

8 What Order Do We Implement Things
Standard order Compute Follow Sets 1 week to do Build readahead and readback FSMs 2 week sto do What we might want to do instead Switch the order So we have to pretend we have the follows sets What if we don’t Takes a long time to explain latter, could waste a week of assignment time.


Download ppt "Readahead FSMs, Readback FSMs, and Reduce States"

Similar presentations


Ads by Google