Compiler Design and Construction - Old Questions
5. Construct LR(1) parse table for
The augmented grammar of given grammar is:
S' → S
S → XX
X → pX | q
Now, Canonical LR(1) item sets are:
I0 = closure([S' →•S, $]) = {[S' →•S, $], [S →•XX, $], [X → •pX, p/q], [X → •q, p/q]}
goto (I0, S) = closure([S' →•S, $]) = {[S'→ S•, $]} = I1
goto(I0, X) = closure([S → X•X, $] = {[S→X•X, $], [X→•pX, $], [X→•q, $]} = I2
goto(I0, p) = closure([X →p•X, p/q]) = {[X →p•X, p/q], [X →•pX, p/q], [X →•q, p/q]} = I3
goto(I0, q) = closure([X→q•, p/q]) = {[X→q•, p/q]} = I4
goto(I2, X) = closure ([S→XX•, $]) = {[S → XX •, $]} = I5
goto(I2, p) = closure([X→p•X, $]) = {[X → p•X, $], [X → • pX, $], [X → • q, $]} = I6
goto (I2, q) = closure ([X → q•, $]) = {[X → q•, $]} = I7
goto (I3, X) = closure ([X → pX•, p/q]) = {[X → pX•, p/q]} = I8
goto(I3, p) = closure([X →p•X, p/q]) = I3
goto(I3, q) = closure([X→q•, p/q]) = I4
goto(I6, X) = closure([X → pX•, $]) = {[X →pX•, $]} = I9
goto (I6, p) = closure([X→p•X, $]) = I6
goto(I6, q) = closure ([X → q•, $] = I7
Now, the LR(1) parsing table is,
1. S → XX
2. X → pX
3. X → q
States/ Terminals |
Action Table |
Goto Table |
|||
p |
q |
$ |
S |
X |
|
0 |
S3 |
S4 |
|
1 |
2 |
1 |
|
|
accept |
|
|
2 |
S6 |
S7 |
|
|
5 |
3 |
S3 |
S4 |
|
|
8 |
4 |
R4 |
R3 |
|
|
|
5 |
|
|
R1 |
|
|
6 |
S6 |
S7 |
|
|
9 |
7 |
|
|
R3 |
|
|
8 |
R2 |
R2 |
|
|
|
9 |
|
|
R2 |
|
|