You are on page 1of 7

Computer Science & Engineering Discipline

Khulna University,Khulna,Bangladesh

An assignment on Compiler Design


CSE 3205: Compiler Design

Submitted To:
Aysha Akther
Submitted by :
Lecturer
Md. Azizul Haque
Computer science & Engineering
Discipline. ID:180235

Khulna University, Khulna, 3rd year 1st term


Bangladesh. Computer science &
Engineering Discipline.
Khulna University,Khulna

Date of submission: 4 December,2020


1.Consider the following grammar, where A is the start symbol:
A → BwA
A→Ɛ
B → CxB
B → yC
C→z

In this grammar A, B and C are non-terminals and w, x, y, and z are


terminals.
(a). Calculate first and follow of each non-terminal of the grammar.
(b). Prove or disprove: This grammar is LL(1).

1.(a).Answer:
A → BwA First(A)=First(B)=First(C) U First(y)={z,y}
A→Ɛ First(A)= {Ɛ};
B → CxB First(B)=First(C)={z}
B → yC First(B)={y}
C→z First(C)={z}

First Functions-
• First(A) = { y, z, Ɛ }
• First(B) = { y,z }
• First(C) = { z }

Follow Functions-
• Follow(A) = { $ }
• Follow(B) = { w }
• Follow(C) = { w,x }

(b).Answer:
By obtaining First and Follow form 1.(a) answer ,generating a parsing table:
Non- Terminal
terminal/Variable w x y z $
A A → BwA A → BwA A→Ɛ
B B → yC B → CxB
C C→z
In order to check if a grammar is LL(1) -
i.) The grammar is not an ambiguous grammar
ii.)The grammar should not be left recursive
iii.)The grammar should be deterministic.
For LL(1) parsing table, no cell should have more than one entry. that means Parsing
table have no FIRST/FIRST conflicts and no FIRST/FOLLOW conflicts.

From the above parsing table, as there are no conflicts, so the given grammar
is LL(1).

2. Show that the following grammar


S → Aa | bAc | Bc | bBa
A→d
B→d
is LR(1) but not LALR(1).

Answer: Given grammar:


S → Aa | bAc | Bc | bBa
A→d
B→d

Add Augment Production, insert '•' symbol at the first position for every production
in G and also add the look ahead.

S` → •S, $ [for augment production, look ahead is always $]


S → •Aa, $
S → •bAc, $
S → •Bc, $
S → •bBa, $
A → •d, a
B → •d, c

LR (1) item is a collection of LR (0) items and a look ahead symbol.

LR (1) item = LR (0) item + look ahead

The look ahead is used to determine that where we place the final item.

The look ahead always add $ symbol for the argument production.
S` → •S, $ [for augment production, look ahead is always $]
S → •Aa, $
A → •d, a [Look ahead of A→•d is First{a,$}=a]
S → •bAc, $
S → •Bc, $
B → •d, c [Look ahead of B → •d is First{c,$}=c]
S → •bBa, $

Add all productions starting with S in modified I0 State because "•" is followed by
the non-terminal. So, the I0 State becomes.

I0= S` → •S, $ [for augment production, look ahead is always $]


S → •Aa, $
A → •d, a [Look ahead of A→•d is First{a,$}=a]

When changing transition state look ahead remain unchanged.

Following the procedures for constructing the LR(1) parser, here is the resulting
State Diagram / Canonical collection of LR(1) items:

Fig: State Diagram / Canonical collection of LR(1) items:


Marking state- I0 as a production number:
(0) S` → •S, $ [for augment production, look ahead is always $]
(1) S → •Aa, $
(2) S → •bAc, $
(3) S → •Bc, $
(4) S → •bBa, $
(5) A → •d, a
(6) B → •d, c
Based on the state diagram, we derive the LR(1) parsing table as follows:

DFA:

I1
I6

S
A c
I2 I7 I11
S A

b B a
I0 I3 l8 I12

B
I9
d I4

I10
I5

Fig: DFA
Parsing Table:
State Action (terminal) Go to (Variable)
a b c d $ S A B
I0 S3 S5 1 2 4
I1 accept
I2 S6
I3 S9 7 8
I4 S10
I5 r5 r6
I6 r1
I7 S11
I8 S12
I9 r6 r5
I10 r3
I11 r2
I12 r4
Since there are no mutiple actions in any entry, the given grammar is LR(1).

However, when obtaining the LALR(1) parsing table by merging states, we will
merge states I5 and I9 , and the resulting state will be as follows:
I5, I9 [production same, but different look ahead]
I59: A → d•, a/c
B →d•, c/a

State Action (terminal) Go to (Variable)


a b c d $ S A B
I0 S3 S59 1 2 4
I1 accept
I2 S6
I3 S59 7 8
I4 S10
I59 r5 r6
I6 r1
I7 S11
I8 S12
I59 r6 r5
I10 r3
I11 r2
I12 r4
LALR(1) Parsing table:
State Action (terminal) Go to (Variable)
a b c d $ S A B
I0 S3 S59 1 2 4
I1 accept
I2 S6
I3 S59 7 8
I4 S10
I59 r5,r6 r6,r5
I6 r1
I7 S11
I8 S12
I10 r3
I11 r2
I12 r4

It is basically a reduce-reduce conflict. So, the given grammar is not LALR(1).


Now, it is proved that the given is LR(1) but not LALR(1).

You might also like