0% found this document useful (0 votes)
43 views

Notes For CFG

Uploaded by

muhazamali0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Notes For CFG

Uploaded by

muhazamali0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Example 1: For the grammar given below, find out the context free language.

The grammar G = ({S}, {a, b }, S, P)


with the productions are;

S → aSb, (Rule: 1)
S → ab (Rule: 2)

Solution:

First compute some strings generated by the production rules of the grammar G in the above;

(i) S ⇒ ab, (Rule: 2)

S ⇒ aSb,
⇒ aabb,
(ii) (Rule: 1)

⇒ aabb ⇒ a2b2
(Rule: 2)
i.e.

S ⇒ aSb,
⇒ aaSbb,
(iii) (Rule: 1)

⇒ aaabbb,
(Rule: 1)

⇒ aaabbb ⇒ a3b3
(Rule: 2)
i.e.

S ⇒ aSb,
⇒ aaSbb,
(iv) (Rule: 1)

⇒ aaaSbbb,
(Rule: 1)

⇒ aaaabbbb, (Rule: 2)
(Rule: 1)

i.e. ⇒ aaaabbbb ⇒ a4b4

S ⇒ aSb,
⇒ aaSbb,
(v) (Rule: 1)

⇒ aaaSbbb,
(Rule: 1)

⇒ aaaaSbbbb,
(Rule: 1)

⇒ aaaaabbbbb,
(Rule: 1)

⇒ aaabbb ⇒ a5b5
(Rule: 2)
i.e.

Hence; Language generated by the above grammar L(G) = {ab, a2b2, a3b3, a4b4, a5b5, a6b6, a7b7,.. .. .. .. }

By analyzing the above generated string form the grammar G, there has a similar pattern in all computed strings, i.e.

 The length of the string is greater than or equal to 2.


 Number of a’s and b’s are equal.
 Presence of a’s followed by b’s.

Thus we can write the language of the grammar L(G) = {anbn : n ≥ 1}

Example 2: For the grammar given below, find out the context free language. The grammar G = ({S}, {a, b}, S, P)
with the productions are;

S → aSa, (Rule: 1)
S → bSb (Rule: 2)
S→ ε (Rule: 3)

Solution:

First compute some strings generated by the production rules of the grammar G in the above;
S ⇒ aSa,
⇒ aεa,
(i) (Rule: 1)

⇒ aa
(Rule: 3)
i.e.

S ⇒ bSb,
⇒ bεb,
(ii) (Rule: 2)

⇒ bb
(Rule: 3)
i.e.

S ⇒ aSa,
⇒ abSba,
(iii) (Rule: 1)

⇒ abεba
(Rule: 2)

⇒ abba
(Rule: 3)
i.e.

S ⇒ bSb,
⇒ baSab,
(iv) (Rule: 2)

⇒ ba ε ab
(Rule: 1)

⇒ baab
(Rule: 3)
i.e.

S ⇒ aSa,
⇒ aaSaa,
(v) (Rule: 1)

⇒ aabSbaa, (Rule: 2)
(Rule: 1)

⇒ aabbSbbaa, (Rule: 2)
⇒ aabb ε bbaa (Rule: 3)
i.e. ⇒ aabbbbaa

Hence; Language generated by the above grammar L(G) = {aa, bb, abba, baab, aabbaa, aabbbbaa,.. .. .. .. }

By analyzing the above generated string form the grammar G, there has a similar pattern in all computed strings, i.e.

 The length of the string is even always i.e. length of the string L(w) ≥ 2 × i | i = 1, 2, 3, 4.. .
 From the half length of the string, string is the reverser of each other side i.e. string is generated by the
language is palindrome.

Thus we can write the language of the grammar L(G) = {wwR : w ∈ {a, b}*};

Example 3: For the grammar given below, find out the context free language. The grammar G = ({S}, {a, b}, S, P)
with the productions are;

S → abB, (Rule: 1)
A → aaBb (Rule: 2)
A→ ε (Rule: 3)
B → bbAa (Rule: 4)

Solution:

First compute some strings generated by the production rules of the grammar G in the above;

S ⇒ abB,
⇒ ab bbAa
(i) (Rule: 1)

⇒ ab bb ε a
(Rule: 4)

⇒ ab bba
(Rule: 3)
i.e.

S ⇒ abB,
⇒ ab bbAa
(ii) (Rule: 1)

⇒ ab bb aaBb a
(Rule: 4)

⇒ ab bb aa bbAa ba
(Rule: 2)
(Rule: 4)
⇒ ab bb aa bb ε a ba
⇒ ab bb aa bba ba
(Rule: 3)
i.e

S ⇒ abB,
⇒ ab bbAa
(iii) (Rule: 1)

⇒ ab bb aaBb a
(Rule: 4)

⇒ ab bb aa bbAa ba
(Rule: 2)

⇒ ab bb aa bb aaBb a ba
(Rule: 4)

⇒ ab bb aa bb aa bbAa b a ba
(Rule: 2)

⇒ ab bb aa bb aa bb ε a b a ba
(Rule: 4)

⇒ ab bb aa bb aa bba ba ba
(Rule: 3)

S ⇒ abB,
i.e

⇒ ab bbAa
(iv) (Rule: 1)

⇒ ab bb aaBb a
(Rule: 4)

⇒ ab bb aa bbAa ba
(Rule: 2)

⇒ ab bb aa bb aaBb a ba
(Rule: 4)

⇒ ab bb aa bb aa bbAa b a ba
(Rule: 2)

⇒ ab bb aa bb aa bb aaBb a ba ba
(Rule: 4)

⇒ ab bb aa bb aa bb aa bbAa ba ba ba
(Rule: 2)

⇒ ab bb aa bb aa bb aa bb ε a ba ba ba
(Rule: 4)

⇒ ab bb aa bb aa bb aa bba ba ba ba
(Rule: 3)
i.e

Hence; Language generated by the above grammar L(G) = { ab bba (minimum string)
ab bb aa bba ba
ab bb aa bb aa bba ba ba


ab bb aa bb aa bb aa bba ba ba ba


}
By analyzing the above generated string form the grammar G, there has a similar pattern in all computed strings, i.e.

 The minimum length of the string consist ab bba always.


 After obtained the minimum string there have some repetition of strings comes i.e. the loop of string (bb aa
and ba) comes finite times. (Note: Highlighted by underlined strings)

Thus we can write the language of the grammar L(G) = {ab (bb aa)n bba (ba)n : n ≥ 0}

Example 4:For the grammar given below, find out the context free language. The grammar G = ({S}, {a, b, c}, S, P)
with the productions are;

S → aSa, (Rule: 1)
S → bSb (Rule: 2)
S→ c (Rule: 3)

Solution:

First compute some strings generated by the production rules of the grammar G in the above;

S ⇒ aSa,
⇒ aca,
(i) (Rule: 1)

⇒ aca
(Rule: 3)
i.e.

S ⇒ bSb,
⇒ bcb,
(ii) (Rule: 2)

⇒ bcb
(Rule: 3)
i.e.
S ⇒ aSa,
⇒ aaSaa,
(iii) (Rule: 1)

⇒ aacaa,
(Rule: 1)

⇒ aacaa
(Rule: 3)
i.e.

S ⇒ aSa,
⇒ abSba,
(iv) (Rule: 1)

⇒ abcba,
(Rule: 2)

⇒ abcba
(Rule: 3)
i.e.

S ⇒ bSb,
⇒ baSab,
(v) (Rule: 2)

⇒ bacab,
(Rule: 1)

⇒ bacab
(Rule: 3)
i.e.

S ⇒ aSa,
⇒ aaSaa,
(vi) (Rule: 1)

⇒ aabSbaa,
(Rule: 1)

⇒ aabcbaa,
(Rule: 2)

⇒ aabcbaa
(Rule: 3)
i.e.

Hence; Language generated by the above grammar L(G) = {c, aca, bcb, aacaa, abcba, aacaa, bbcbb, aabcbaa,.. .. .. .. }

By analyzing the above generated string form the grammar G, there has a similar pattern in all computed strings, i.e.

 The length of the string is odd always i.e. length of the string L(w) ≥ 1, 3, 5, 7, 9 .. .. ..
 From the middle symbol of the string i.e. ‘c’, string is the reverser of each other side i.e. string is generated by
the language is palindrome.

Thus we can write the language of the grammar L(G) = {wcwR : w ∈ {a, b}*}

Example 5: For the grammar given below, find out the context free language. The grammar G = ({S}, {a}, S, P) with
the productions are;

S → SS (Rule: 1)
S→a (Rule: 2)
Solution:

First compute some strings generated by the production rules of the grammar G in the above;

(i) S⇒a (Rule: 2)

S ⇒ SS
⇒ aS,
(ii) (Rule: 1)

⇒ aa,
(Rule: 2)

⇒ aa ⇒ a2
(Rule: 2)
i.e.

S ⇒ SS
⇒ aS
(iii) (Rule: 1)

⇒ aSS
(Rule: 2)

⇒ aaS
(Rule: 1)

⇒ aaa
(Rule: 2)

⇒ aaa ⇒ a3
(Rule: 2)
i.e.

Hence; Language generated by the above grammar L(G) = {a, a2, a3, a4, a5, .. .. .. .. }

By analyzing the above generated string form the grammar G, there has a similar pattern in all computed strings, i.e.
 The minimum length of the string is one.
 The number of occurrence of a’s is increase one by one.

Thus we can write the language of the grammar L(G) = {an : n ≥ 1}

You might also like