Context-Free Grammar (CFG) : Example 1
Context-Free Grammar (CFG) : Example 1
Context-Free Grammar (CFG) : Example 1
CFG stands for context-free grammar. It is is a formal grammar which is used to generate
all possible patterns of strings in a given formal language. Context-free grammar G can be
defined by four tuples as:
1. G = (V, T, P, S)
Where,
G is the grammar, which consists of a set of the production rule. It is used to generate the
string of a language.
P is a set of production rules, which is used for replacing non-terminals symbols(on the left
side of the production) in a string with other terminal or non-terminal symbols(on the right
side of the production).
S is the start symbol which is used to derive the string. We can derive the string by
repeatedly replacing a non-terminal by the right-hand side of the production until all non-
terminal have been replaced by terminal symbols.
Example 1:
Construct the CFG for the language having any number of a's over the set ∑= {a}.
Solution:
1. r.e. = a*
Production rule for the Regular expression is as follows:
1. S → aS rule 1
2. S → ε rule 2
Now if we want to derive a string "aaaaaa", we can start with start symbols.
1. S
2. aS
3. aaS rule 1
4. aaaS rule 1
5. aaaaS rule 1
6. aaaaaS rule 1
7. aaaaaaS rule 1
8. aaaaaaε rule 2
9. aaaaaa
The r.e. = a* can generate a set of string {ε, a, aa, aaa,.....}. We can have a null string
because S is a start symbol and rule 2 gives S → ε.
Example 2:
Construct a CFG for the regular expression (0+1)*
Solution:
Example 3:
Construct a CFG for a language L = {wcwR | where w € (a, b)*}.
Solution:
The string that can be generated for a given language is {aacaa, bcb, abcba, bacab,
abbcbba, ....}
1. S → aSa rule 1
2. S → bSb rule 2
3. S → c rule 3
Now if we want to derive a string "abbcbba", we can start with start symbols.
1. S → aSa
2. S → abSba from rule 2
3. S → abbSbba from rule 2
4. S → abbcbba from rule 3
Thus any of this kind of string can be derived from the given production rules.
Example 4:
Construct a CFG for the language L = anb2n where n>=1.
Solution:
The string that can be generated for a given language is {abb, aabbbb, aaabbbbbb....}.
1. S → aSbb | abb
Now if we want to derive a string "aabbbb", we can start with start symbols.
1. S → aSbb
2. S → aabbbb
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: 1)
⇒ aabbSbbaa, (Rule: 2)
(Rule: 2)
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
Thus we can write the language of the grammar L(G) = {ww R : w ∈ {a, b}*};
side i.e. string is generated by the language is a palindrome.
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 → 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
(ii) (Rule: 1)
⇒ ab
bbAa (Rule: 4)
⇒ ab
bb aaBb a (Rule: 2)
⇒ ab
bb aa bbAa ba (Rule: 4)
⇒ ab bb aa bba ba
bb aa bb ε a 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)
i.e
S ⇒ abB,
⇒ 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
⋮
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.
Example 3:
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
Thus we can write the language of the grammar L(G) = {wcw R : w ∈ {a, b}*}
each other side i.e. string is generated by the language is palindrome.
Example 4: 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;
S ⇒ SS
⇒ aS,
(ii) (Rule: 1)
⇒ aa,
(Rule: 2)
⇒ aa ⇒ a2
(Rule: 2)
i.e.
S ⇒ SS
⇒ aS
(iii) (Rule: 1)
(Rule: 2)
⇒ aSS
⇒ aaS
(Rule: 1)
⇒ aaa
(Rule: 2)
⇒ aaa ⇒ a3
(Rule: 2)
i.e.
Hence; Language generated by the above grammar L(G) = {a, a 2, a3, a4, a5, .. .. ..
.. }
By analyzing the above generated string form the grammar G, there has a
similar pattern in all computed strings, i.e.
Example 5: 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;
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)
(Rule: 1)
⇒ aaaabbbb, (Rule: 2)
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, a 2b2, 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.