You are on page 1of 18

WORLD UNIVERSITY OF BANGLADESH

Term Paper On
Course Title: Formal System
Course Code: CSE 904

Submitted To Submitted By
Supriya Sarker Sanzida Islam
Lecturer REG: WUB03/18/42/2610
Department of CSE Batch: 42/A
World University of Bangladesh Department of CSE
World University of Bangladesh
Answer to the question no: 01(a)
i.
Input alphabet, Σ={a, b}
L = {ab, ba, aabb,baaa,………….}
The required DFA is-

a b

a q2
q0 q1

b b
a

a
q3 q2

b
Answer to the question no: 01(b)

ii.
Input alphabet, Σ={a, b}
Language L = {ε, ab, abab, bbbb, ...}
The required DFA is-

b b

a b
q0 q0 q0

a
a

q0
Answer to the question no: 01(a)
iii.

L = {000, 0001, 1000, 11000, 00100011,001100001, .........}


The required DFA is-

1 0,1
1

0 0 0
q0 q1 q2 q3

Answer the the question no: 01(b)


A context free grammar G = (N, Σ, R, S) where:
 N is a set of non-terminal symbols

 Σ is a set of terminal symbols

 R is a set of rules of the form X →Y1Y2 ...Yn for n ≥0, Xϵ N, Yi ϵ (N ∪ Σ)

 S ϵ N is a distinguished start symbol.


Given,
N = {S, NP, VP, PP, DT, Vi, Vt, N, IN}
S=S
∑ = {sleep, saw, man, women, telescope, the, with, in}
R=

Here,
S = sentence,
VP = verb phrase,
NP = noun phrase,
PP = prepositional phrase,
DT=determiner,
Vi=intransitive verb,
Vt=transitive verb,
NN=noun,
IN=preposition
Parse tree

NP VP

DT NN Vi

the man sleeps


Figure: Parse tree for ‘the man sleeps’

A parse of the sentence " the man sleeps" is: S =>NP VP => DT N VP=> the N VP => the man
VP => the man Vi => the man sleeps.

Answer to the question no: 2(a)


Arden's Theorem:
In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem along
with the properties of regular expressions.
Statement −
Let P and Q be two regular expressions.
If P does not contain null string, then R = Q + RP has a unique solution that is R = QP*
Here the initial state and final state is q1.
The equations for the three states q1, q2, and q3 are as follows −
q1 = q1a + q3a + ε (ε move is because q1 is the initial state0)
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these three equations −
q2 = q1b + q2b + q3b
= q1b + q2b + (q2a) b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab) * (Applying Arden’s Theorem)
q1 = q1a + q3a + ε
= q1a + q2aa + ε (Substituting value of q3)
= q1a + q1b (b + ab*) aa + ε (Substituting value of q2)
= q1(a + b (b + ab) *aa) + ε
= ε (a+ b (b + ab) *aa) *
= (a + b (b + ab) *aa) *
Hence, the regular expression is (a + b (b + ab) *aa) *.
Answer to the question no: 2(b)
Ambiguity:
A Grammar is said to be ambiguous if its language contains some string with more than one
parse tree, or leftmost derivation or rightmost derivation or syntax tree or derivation tree.
If the grammar has ambiguity, then it isn't useful for compiler construction. No method can
automatically detect and eliminate the ambiguity, but we can remove ambiguity by re-composing
the entire grammar without ambiguity.

i.
P = {S → AB | C, A → aAb | ab, B → cBd | cd, C → aCd | aD, D → bDc | bc}
A grammar for P is
S → AB | C
A → aAb | ab
B → cBd | cd
C → aCd | aDd
D → bDc | bc
The string aa bb cc dd has two left most derivation and and the two parse tree:
Left most derivation
S ⇒ AB
→ aAbB
→ aabbB
→ aabb CBd
→ aabb CCdd
Parse tree 01

A B

S S S S S S

S S S S

And another left most derivation is –


S⇒C
L.M.D
→ aCd
→ aaDdd
→ aab Dcdd
→ aabbcdd
Parse tree 02

a d

a d

b c

b c
Right most derivation:
S ⇒ AB
R.M.D
→ AcBd
→ Accdd
→ aAb ccdd
→ aabb ccdd
Yes, the grammar is ambiguous.

ii.
S → aSbS|bSaS|∈
For grammar to be ambiguous, there should be more than one parse tree for same string. Above
grammar can be written as
S → aSbS
S → bSaS
S→∈
Let’s generate a string 'abab'. So, now parse tree for 'abab'.
Left most derivative parse tree 01
S → aSbS
S → a∈bS
S → a∈baSbS
S → a∈ba∈b∈
S → abab
S

a S b S

∈ a S b S

∈ ∈

Parse Tree 01
Left most derivative parse tree 02
S → aSbS
S → abSaSbS
S → ab∈aSbS
S → ab∈a∈bS
S → ab∈a∈b∈
S → abab
S

a S b S

b S a S ∈

∈ ∈

Parse Tree 02

So there are more than 1 parse tree for same string, that means grammar is ambiguous.

Answer to the question no: 3(a)


Pushdown Automata:
A pushdown automaton is a way to implement a context-free grammar in a similar way we
design DFA for a regular grammar. A DFA can remember a finite amount of information, but a
PDA can remember an infinite amount of information.
Basically a pushdown automaton is −
"Finite state machine" + "a stack"
A pushdown automaton has three components −

 an input tape,
 a control unit, and
 a stack with infinite size.
Answer to the question no: 3(b)

A PDA that accepts L = {0n 1n | n ≥ 0}


This language accepts L = {ε, 01, 0011, 000111, .............................}

0, ∈→0 1, 0→∈

∈, ∈→$ 1, 0→∈ ∈, $→∈


q0 q1 q2 q3

PDA for L = {0n 1n | n ≥ 0}


Here,
 Initially we put a special symbol ‘$’ into the empty stack.
 Then at state q1, if we encounter input 0 and top is Null, we push 0 into stack. This may
iterate. And if we encounter input 1 and top is 0, we pop this 0.
 Then at state q2, if we encounter input 1 and top is 0, we pop this 0. This may also iterate.
And if we encounter input 1 and top is 0, we pop the top element.
 If the special symbol ‘$’ is encountered at top of the stack, it is popped out and it finally
goes to the accepting state q3.

Answer to the question no 3(c)


Using pumping lemma prove that L = {yy | y ϵ {0, 1}*} is not regular.

Proof:
Assume A is regular and pumping length = P
S = 0p10p1 [Let S = 0000000100000001 where P=7]
S = xyz and |y| ≥ 1 and |xy| ≤ p, so y contains only 0s.
xyi z ∉ A. Here, we choose i = 2. xy2 z = xyyz = 0p+|y|10p, but |y|≠ 0
so this string is not in A, contradicting the pumping lemma. Thus, A is not regular.
Answer to the question no: 4(a)
Convert the given NFA to DFA.

0 0,1 0 0,1

q0 1 q1 q2

1
For the given transition diagram we will first construct the transition table.
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
*q2 q2 {q1, q2}

Now we will obtain δ' transition for state q0.


δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]
The δ' transition for state q1 is obtained as:
δ'([q1], 0) = [q1, q2] (new state generated)
δ'([q1], 1) = [q1]
The δ' transition for state q2 is obtained as:
δ'([q2], 0) = [q2]
δ'([q2], 1) = [q1, q2]
Now we will obtain δ' transition on [q1, q2].
δ'([q1, q2], 0) = δ (q1, 0) ∪ δ (q2, 0)
= {q1, q2} ∪ {q2}
= [q1, q2]
δ'([q1, q2], 1) = δ (q1, 1) ∪ δ(q2, 1)
= {q1} ∪ {q1, q2}
= {q1, q2}
= [q1, q2]
The state [q1, q2] is the final state as well because it contains a final state q2. The transition table
for the constructed DFA will be:
State 0 1
→[q0] [q0] [q1]
[q1] [q1, q2] [q1]
*[q2] [q2] [q1, q2]
*[q1, q2] [q1, q2] [q1, q2]

The Transition diagram will be:

0 1 0

[q0]
1 [q1] [q2]

0 1

[q1, q2]
0,1

The state q2 can be eliminated because q2 is an unreachable state.


Answer to the question no: 4(b)
Step 1:
Consider every final-nonfinal state pair and tick it working only on the lower triangular part of
the table
State u p t q s r

p 

t 

q  

s  

r   

Step 2:
Consider all the un-ticked areas of step1
For an input(either a or b) for each un-ticked state, see the intermediate state For the area (r,t):
(r,a) => {r} and (t,a) =>s
So, here the intermediate state is ‘s’
Now check if {r,s} is ticked in step1.
If yes, tick {r,t} as well.
Similarly, {q,u} and {r,q} are also ticked
Step3:
Continue step2 until all states have been processed. Once no more can be ticked, algorithm
terminates.
Hence, here {s,u} is also ticked.
Final table now becomes
State u p t q s r

p 

t 

q   

s   

r     

Step 4:
Check the spaces which are still un-ticked and such states can be merged together.
In the final minimized DFA, q-s are the new states and p-t are the new states

You might also like