Outline: - Viable Prefixes
Outline: - Viable Prefixes
Review
is a handle of in a rightmost derivation if S =>* X Handles always appear at the top of the stack
To the right of the rightmost non-terminal. Thus shift and reduce moves are sufficient.
To implement shift-reduce parsing, we must detect handles. But even if there are symbols on the stack that can be reduced, that doesnt mean there is a handle.
CS780(Prasad) L134ALG 3
Notes on Handles Every handle has some production rhs on top of the stack. But a production rhs on top of the stack is not necessarily a handle. In other words, whether a given stack has handle can depend on the contents of the entire stack. Unique Handles: If a grammar is unambiguous, then every step of a rightmost derivation has a unique handle.
CS780(Prasad) L134ALG 5
Viable Prefixes It is not obvious how to detect handles. At each step the parser sees only the stack, not the entire input. is a viable prefix if there is an such that | is a state of a shift-reduce parser.
CS780(Prasad)
L134ALG
Important Fact #3 Important Fact #3 about bottom-up parsing: For any grammar, the set of viable prefixes is a regular language. This fact is non-obvious. We show how to compute automata that accept viable prefixes.
CS780(Prasad) L134ALG 8
Items An item is a production with a . somewhere on the rhs. The items for T (E) are
T T T T .(E) (.E) (E.) (E).
Intuition The problem in recognizing viable prefixes is that the stack has only bits and pieces of the rhs of productions.
If it had a complete rhs, we could reduce.
The only item for X is X . Items are often called LR(0) items.
CS780(Prasad) L134ALG 9 CS780(Prasad) L134ALG 10
Generalization The stack may have many prefixes of rhss Prefix1 Prefix2 . . . Prefixn-1 Prefixn Let Prefixi be a prefix of rhs of Xi i
Now Prefixn is a prefix of n Which eventually reduces to Xn Which should be a prefix of the missing part of n-1
Item T (E.) says that so far we have seen (E of this production and hope to see )
CS780(Prasad) L134ALG 11
An Example
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
An Example (Cont.) The stack of items T (.E) E .T T int * .T Says Weve seen ( of T (E) Weve seen of E T Weve seen int * of T int * T
13 CS780(Prasad) L134ALG 14
CS780(Prasad)
L134ALG
An NFA Recognizing Viable Prefixes 1. Add a dummy production S S to G 2. The NFA states are the items of G 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
CS780(Prasad) L134ALG 16
CS780(Prasad)
L134ALG
15
E T+.E
E T + E.
T (.E)
E T
T (E.) E T.+E
int +
T (E). E T+.E
E
E . T+E
S . E
T int.
T .int
E T+E. T int.* T
T int * T. T T (E.) )
E . T
T
T .int * T E T.
int
* T
T int *.T
T int * T.
17
T (E).
L134ALG
(
31
CS780(Prasad)
Valid Items Item X . is valid for a viable prefix if S =>* X by a right-most derivation. Intuition: The valid items are the prefixes of productions we might see after Items Valid for a Prefix An item I is valid for a viable prefix if the DFA recognizing viable prefixes terminates on input in a state containing I.
CS780(Prasad) L134ALG 32
Valid Items Example An item is often valid for many prefixes. Example: The item T (.E) is valid for prefixes ( (( ((( (((( ...
CS780(Prasad) L134ALG 33
E T+.E
E T + E.
Lingo
(
The states of the DFA are canonical collections of items or canonical collections of LR(0) items
T (E).
34
SLR Moves
Reduce by X if Shift if
s contains item X . t Follow(X) 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 handles.
The SLR grammars are those where the heuristics detect exactly the handles.
CS780(Prasad)
L134ALG
36
CS780(Prasad)
L134ALG
37
Nave SLR Parsing Algorithm 1. Let M be DFA for viable prefixes of G. 2. Let |x1xn$ be the initial configuration. 3. Repeat until configuration is S|$
Let | be current configuration. Run M on current stack If M rejects , report parsing error
Stack is not a viable prefix Shift if X . a I Reduce if X . I and a Follow() Report parsing error if neither applies
L134ALG
Notes If there is a conflict in the last step, grammar is not SLR(k). k is the amount of lookahead
In practice, k = 1.
CS780(Prasad)
38
CS780(Prasad)
L134ALG
39
E T+.E
7
E E T + E.
SLR Example Configuration DFA Halt State |int * int$ 1 Action shift
2
+ int
6
( T (. E) E .T E .T + E E T .(E) T .int * T T .int
T .int * T
T int. * T
4
T
T int * T.
11
T (E.) )
T .int * T
T (E).
9
(
41
10
CS780(Prasad)
L134ALG
40
E T+.E
7
E E T + E.
SLR Example Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift
2
+ int
6
( T (. E) ( E .T E .T + E E T .(E) T .int * T T .int
T .int * T
T int. * T
4
T
T int * T.
11
T (E.) )
T .int * T
T (E).
9
(
44
10
CS780(Prasad)
L134ALG
42
E T+.E
7
E E T + E.
SLR Example Configuration |int * int$ int | * int$ int * | int$ DFA Halt State 1 3 * not in Follow(T) 11 Action shift shift shift
2
+ int
6
( T (. E) ( E .T E .T + E E T .(E) T .int * T T .int
T .int * T
T int. * T
4
T
T int * T.
11
T (E.) )
T .int * T
T (E).
9
(
48
10
CS780(Prasad)
L134ALG
45
E T+.E
7
E E T + E.
SLR Example Configuration |int * int$ int | * int$ int * | int$ int * int |$ DFA Halt State 1 3 * not in Follow(T) 11 3 $ Follow(T) Action shift shift shift red. T int
2
+ int
6
( T (. E) E .T E .T + E E T .(E) T .int * T T .int
T .int * T
T int. * T
4
T
T int * T.
11
T (E.) )
T .int * T
T (E).
9
(
53
10
CS780(Prasad)
L134ALG
49
SLR Example Configuration |int * int$ int | * int$ int * | int$ int * int |$ int * T |$ DFA Halt State 1 3 * not in Follow(T) 11 3 $ Follow(T) 4 $ Follow(T) Action shift shift shift red. T int red. T int*T
SLR Example Configuration |int * int$ int | * int$ int * | int$ int * int |$ int * T |$ T |$ DFA Halt State 1 3 * not in Follow(T) 11 3 $ Follow(T) 4 $ Follow(T) 5 $ Follow(T) Action shift shift shift red. T int red. T int*T red. ET
CS780(Prasad)
L134ALG
54
CS780(Prasad)
L134ALG
59
SLR Example Configuration |int * int$ int | * int$ int * | int$ int * int |$ int * T |$ T |$ E |$ DFA Halt State 1 3 * not in Follow(T) 11 3 $ Follow(T) 4 $ Follow(T) 5 $ Follow(T) Action shift shift shift red. T int red. T int*T red. ET accept
62
Notes
Skipped using extra start state S in this example to save space on slides. Rerunning the automaton at each step is wasteful
Most of the work is repeated.
An Improvement Remember the state of the automaton on each prefix of the stack. Change stack to contain pairs Symbol, DFA State
CS780(Prasad) L134ALG 63
Goto Table Define Goto[i,A] = j if statei A statej Goto is just the transition function of the DFA
One of two parsing tables.
CS780(Prasad)
L134ALG
64
CS780(Prasad)
L134ALG
65
If si has item X .a and Goto[i,a] = j then action[i,a] = shift j If si has item X . and a Follow(X) and X S then action[i,a] = reduce X If si has item S S. then action[i,$] = accept Otherwise, action[i,a] = error
Reduce X
As before
Accept Error
CS780(Prasad) L134ALG 66
CS780(Prasad)
L134ALG
67
Notes on SLR Parsing Algorithm Note that the algorithm uses only the DFA states and the input
The stack symbols are never used!
CS780(Prasad)
L134ALG
69