0% found this document useful (0 votes)
9 views5 pages

Assignment No 3

Uploaded by

Sufian liaqat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Assignment No 3

Uploaded by

Sufian liaqat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment No.

3
Q.1. For the given grammar, show the canonical collection of LR (1) items, then construct
the CLR (1) parsing table.
Show all the possible sequences of actions on input 'abbcde' for grammar 1.

Grammar 1:
S → aABe
A → Abc
A→b
B→d

Step 1: Augmented Grammar


Add a new starting production:
S' → S
S → aABe
A → Abc
A→b
B→d

Step 2: Canonical Collection of LR(1) Items


1. Closure of I0:
I0:
S' → .S, $
S → .aABe, $
A → .Abc, e
A → .b, e
B → .d, e

2. Goto Operations:
- On `S`, go to I1:
I1:
S' → S., $

- On `a`, go to I2:
I2:
S → a.ABe, $
A → .Abc, e
A → .b, e
B → .d, e
- On `A`, go to I3:
I3:
S → aA.Be, $
B → .d, e

- On `B`, go to I4:
I4:
S → aAB.e, $

- On `e`, go to I5:
I5:
S → aABe., $

- On `b`, go to I6:
I6:
A → b., e

- On `d`, go to I7:
I7:
B → d., e

- On `c`, go to I8:
I8:
A → Ab.c, e

- On `b`, go to I9:
I9:
A → Abc., e

Canonical Collection of Items:


I0, I1, I2, I3, I4, I5, I6, I7, I8, I9

Step 3: CLR(1) Parsing Table


State a b c d e $ S A B

0 S2 1 3

1 acc

2 S6 3 4

3 S7

4 S5
5 acc

6 9

Step 4: Sequence of Actions for `abbcde`


1. Shift `a`: Stack = [0, a, 2]
2. Shift `b`: Stack = [0, a, 2, b, 6]
3. Reduce `A → b`: Stack = [0, a, 2, A, 3]
4. Shift `b`: Stack = [0, a, 2, A, 3, b, 6]
5. Shift `c`: Stack = [0, a, 2, A, 3, b, 6, c, 8]
6. Reduce `A → Abc`: Stack = [0, a, 2, A, 9]
7. Shift `d`: Stack = [0, a, 2, A, 9, d, 7]
8. Reduce `B → d`: Stack = [0, a, 2, A, 9, B, 4]
9. Shift `e`: Stack = [0, a, 2, A, 9, B, 4, e, 5]
10. Accept.

Grammar 2:
S → xAy | xBy | xAz
A → aS | q
B→q

Step 1: Augmented Grammar


Add a new starting production:
S' → S
S → xAy
S → xBy
S → xAz
A → aS
A→q
B→q

Step 2: Canonical Collection of LR(1) Items


1. Closure of I0:
I0:
S' → .S, $
S → .xAy, $
S → .xBy, $
S → .xAz, $
A → .aS, y
A → .q, y
B → .q, y

2. Goto Operations:
- On `S`, go to I1:
I1:
S' → S., $

- On `x`, go to I2:
I2:
S → x.Ay, $
S → x.By, $
S → x.Az, $
A → .aS, y
A → .q, y
B → .q, y

- On `A`, go to I3:
I3:
S → xA.y, $

- On `B`, go to I4:
I4:
S → xB.y, $

- On `y`, go to I5:
I5:
S → xAy., $
S → xBy., $
S → xAz., $

- On `a`, go to I6:
I6:
A → a.S, y
S → .xAy, y
S → .xBy, y
S → .xAz, y

- On `q`, go to I7:
I7:
A → q., y
B → q., y
Canonical Collection of Items:
I0, I1, I2, I3, I4, I5, I6, I7

Step 3: CLR(1) Parsing Table


State x a q y $ S A B

0 S2 1

1 acc

2 S6 S7 3 4

3 S5

4 S5

Step 4: Sequence of Actions for `xay`


1. Shift `x`: Stack = [0, x, 2]
2. Shift `a`: Stack = [0, x, 2, a, 6]
3. Shift `y`: Stack = [0, x, 2, a, 6, y, 5]
4. Reduce A → q: Stack = [0, x, 2, A, 7]
5. Reduce S → xAy: Stack = [0, S, 1] (Accept)

You might also like