Download presentation
Presentation is loading. Please wait.
Published byGeorge Poole Modified over 9 years ago
1
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead SLR Simple LR: like LR(0), but uses FOLLOW sets to build more “precise” parsing tables LR(0) is a toy, so we focus on SLR Reading: Section 4.7
2
Problem: when to shift, when to reduce? Recall our favorite grammar: E T + E | T T int * T | int | (E) The step T * int + int int * int + int is not part of any rightmost derivation Hence, reducing first int to T was a mistake How to know when to reduce and when to shift?
3
What we need for LR parsing LR(0) states describe states in which the parser can be Note: LR(0) states are used by both LR(0) and SLR parsers Parsing tables transitions between LR(0) states, actions to take when transiting: shift, reduce, accept, error How to construct LR(0) states? How to construct parsing tables? How to drive the parser?
4
LR(0) state = set of LR(0) items An LR(0) item [X ] says that the parser is looking for an X it has an on top of the stack expects to find input string derived from Notes: [X . a ] means that if a is on the input, it can be shifted (resulting in a. . That is: a is a correct token to see on the input, and shifting a would not “over-shift” (still a viable prefix). [X .] means that we could reduce to X
5
T int. * T T int. 5 LR(0) states E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
6
Naïve SLR Parsing Algorithm 1. Let M be LR(0) state machine for G each state contains a set I of LR(0) items 2. Let |x 1 …x n $ be initial configuration 3. Repeat until configuration is S’|$ Let | be current configuration Run M on current stack If M rejects , report parsing error If M accepts , let a be next input Shift if [X . a ] Items Reduce if [X Items and a Follow( ) a X a Report parsing error if neither applies
7
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
8
T int. * T T int. 5 LR(0) states E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
9
SLR Example ConfigurationDFA Halt StateAction | int * int $1
10
T int. * T T int. 5 Configuration | int * int $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
11
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5
12
T int. * T T int. 5 Configuration int | * int $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
13
T int. * T T int. 5 Configuration int | * int $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3 T (. E) E .T E .T + E T .(E) T .int * T T .int 4 E T +. E E .T E .T + E T .(E) T .int * T T .int 6 7T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E. T (E).10 T int * T.11 T
14
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8
15
T int. * T T int. 5 Configuration int * | int $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
16
T int. * T T int. 5 Configuration int * | int $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3 T (. E) E .T E .T + E T .(E) T .int * T T .int 4 E T +. E E .T E .T + E T .(E) T .int * T T .int 6 7T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E. T (E).10 T int * T.11 T
17
T int. * T T int. 5 Configuration int * | int $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
18
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $5
19
T int. * T T int. 5 Configuration int * int | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
20
T int. * T T int. 5 Configuration int * int | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
21
T int. * T T int. 5 Configuration int * int | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
22
T int. * T T int. 5 Configuration int * int | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
23
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $ 5 $ Follow(T)reduce T int int * | T $8
24
T int. * T T int. 5 Configuration int * | T $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
25
T int. * T T int. 5 Configuration int * | T $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
26
T int. * T T int. 5 Configuration int * | T $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
27
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $ 5 $ Follow(T)reduce T int int * | T $8shift int * T | $11
28
T int. * T T int. 5 Configuration int * T | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10 T int * T.11 T
29
T int. * T T int. 5 Configuration int * T | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10 T int * T.11 T
30
T int. * T T int. 5 Configuration int * T | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10 T int * T.11 T
31
T int. * T T int. 5 Configuration int * T | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10 T int * T.11 T
32
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $ 5 $ Follow(T)reduce T int int * | T $8shift int * T | $ 11 $ Follow(T)reduce T int * T | T $1
33
T int. * T T int. 5 Configuration | T $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
34
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $ 5 $ Follow(T)reduce T int int * | T $8shift int * T | $ 11 $ Follow(T)reduce T int * T | T $1shift T | $3
35
T int. * T T int. 5 Configuration T | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2 E T. E T. + E 3 T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
36
T int. * T T int. 5 Configuration T | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2 E T. E T. + E 3 T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
37
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $ 5 $ Follow(T)reduce T int int * | T $8shift int * T | $ 11 $ Follow(T)reduce T int * T | T $1shift T | $ 3 $ Follow(E)reduce E T | E $1
38
T int. * T T int. 5 Configuration | E $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1S’ E.2E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
39
SLR Example ConfigurationDFA Halt StateAction | int * int $1shift int | * int $5 * not in Follow(T)shift int * | int $8shift int * int | $ 5 $ Follow(T)reduce T int int * | T $8shift int * T | $ 11 $ Follow(T)reduce T int * T | T $1shift T | $ 3 $ Follow(E)reduce E T | E $1shift E | $2ACCEPTt
40
T int. * T T int. 5 Configuration E | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2 E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
41
T int. * T T int. 5 Configuration E | $ E T ( int * ) E E T ( ( T ( S’ . E E . T E .T + E T .(E) T .int * T T .int 1 S’ E.2 E T. E T. + E 3T (. E) E .T E .T + E T .(E) T .int * T T .int 4E T +. E E .T E .T + E T .(E) T .int * T T .int 67T (E.) 8 T int *.T T .(E) T .int * T T .int 9E T + E.T (E).10T int * T.11 T
42
Notes Can also use one more state: it accepts in state “S’ E $. ” i.e., it accepts in configuration E$|, not in E|$. Rerunning the automaton at each step is wasteful Most of the work is repeated
43
An Improvement Remember the state of the automaton on each prefix of the stack Change stack to contain pairs DFA State, Symbol
44
An Improvement (Cont.) For a stack state 1, sym 1 ... state n, sym n state n is the final state of the DFA on sym 1 … sym n Detail: bottom of stack is start,any where any is any dummy input start is the start state of the DFA
45
Goto Table Define Goto[i,A] = j if state i A state j Goto is just the transition function of the DFA One of two parsing tables
46
Refined Parser Moves Shift x Push a, x on the stack a is current input x is a DFA state Reduce X As before Accept Error
47
Action Table For each state s i and terminal a If s i has item X .a and Goto[i,a] = j then Action[i,a] = shift j If s i has item X . and a Follow(X) and X S’ then Action[i,a] = reduce X If s i has item S’ S. then action[i,$] = accept Otherwise, action[i,a] = error
48
SLR Parsing Algorithm Let I = w$ be initial input Let J = 1 Let DFA state 1 have item S’ .S Let stack = 1, dummy repeat case action[top_state(stack),I J ] of shift k: push k, I J J++ reduce X A: pop |A| pairs, replace I J-|A| to I J-1 with X J = J - |A| accept: halt normally error: halt and report error
49
Notes on SLR Parsing Algorithm Note that the algorithm uses only the DFA states and the input The stack symbols are never used! However, we still need the symbols for semantic actions
50
Constructing SLR states LR(0) state machine encodes all strings that are valid on the stack each valid string is a configuration, and hence corresponds to a state of the LR(0) state machine each state tells us what to do (shift or reduce?)
51
Example SLR Parse Table int*+()$ET 1s5s4s2s3 2acc 3s6r2 4s5s4s7s3 5s8r4 6s5s4s9s3 7s10 8s5s4s11 9r1 10r5 11r3 1: E T + E 2: E T 3: T int * T 4: T int 5: T (E)
52
Example SLR Parse StackIJAct <1,?><1,?>int * int $1s5 2s8 3s5 4r4 int * T $3s11 4r3 <1,?><1,?>T $1s3 2r2 <1,?><1,?>E $1s2 2acc
53
Another Example int * (int + int) * int $
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.