TOC Unit 3 Context Free Grammer
TOC Unit 3 Context Free Grammer
Unit3
Context Free Grammar and
Languages
Context Free Grammar
• We know that every finite automata M accepts a
language L, which is represented by L (M).
• We also know that regular language can be
described by a regular expression .
• We have seen that there are several languages
which are not regular.
1. L1 = {ap | p is prime} is not regular.
2. L2 = {anbn | n>=0} is not regular.
3. L3={ai2 | i>=1} is not regular.
4. L4={ww | w Є {a,b}* } is not regular.
5. L5 = {wwR | w Є {a,b} } is not regular.
• We know two ways for representing a
language:
• Using Finite Automata.
• Using Regular Expression.
• If a language is non-regular, it can not be
represented either using FA or using a regular
expression. Hence a need for representing such
languages.
• Grammar is another approach for representing
a language.
• In this approach, a language is represented
using a set of equations.
• Equations are recursive in nature
• A finite automata has a set of states. A context
free grammar has a set of variables.
• Finite automata is defined over an alphabet.
Similarly, a grammar is defined over a set of
terminals.
• A finite automata has a set of transitions; a
grammar has a set of equations(Productions)
DFA WITH EQUIVALENT PRODUCTIONS
Grammar
• The production X → a should read as X produces
a or X derives a or X gives a.
• The production X → aX is a way of defining a
string containing one or more a’s.
• If X stands for a set of strings containing one or
more a’s then a can be expressed in terms of X.
the concept is as shown
__X_
a a a a a a a or X = aX
X
Every Recursive definition has a base case and a
termination case.
Example
• A language for a DFA is given as a
L = { e, a, aa, aaa, aaaa, …….} X
(c) (d)
(e) (f)
Note that the final parse trees for leftmost and
rightmost derivation are identical.
Q) For the grammar given below S→ 0S1 |01.
give the derivation 000111.
→ Derivation in the sentential form:
S → 0S1 S → 00S11 [using S →0S1]
S → 000111 [using S →01]
Derivation using
Parse tree:
Q) Find whether the string aabbb is in L =L(G),
where G is given by
S→XY, X→YY la, Y→XY|b
→We can make use of
parse tree to generate the
string and check whether
it is in L(G)
Since we have successfully
Generated the tree for the
String aabbb, so it belongs
to L(G).
Q) For the grammar given below
E→E+T|T, T→T*F|F F→(E)|a|b
Give derivation of (a+b) * a+b
→ For the given grammar,
Set of variables V = {E,T,F}
Set of terminals T = { +, *, (, ), a, b}
Set of Productions = {E→E+T|T T→T*F|F
F→(E)|a|b}
Start symbol = E
a) Derivation in sentential form:
E → E+T [using production E → E+T]
→ T+T [using production E → T]
→ T*F+T [using production T → T *F]
→ F*F+T [using production T → F]
→ (E)*F+T [using production F → (E)]
→ (E+T)*F+T [using production E → E+T]
→ (T+T)*F+T [using production E → T]
→ (F+F)*F+F [using production T → F]
→ (a+b)*a+b [using production F → a|b]
a) Derivation in Parse tree form:
Writing grammar for a language
• Productions for an infinite language are
written recursively.
• A production is called recursive if its left side
variable occurs on its right side.
For example:
– S → aS is directly recursive
– S → b|aA
– A → c| bS is indirectly recursive
• As, S→aA → abS [S gives A and A gives S]
A→bS → baA [A gives S and S gives A]
Writing grammar for a language
• Recursive grammar can be written either using
direct recursive productions or using indirectly
recursive productions.
• Grammar for the some basic languages is given
below:
✓ Language L = {ε,a,aa,…}
The productions required to generate the above
language are:
S→ aS, S → ε
The production S → ε is taken as ε is member of L.
The production S → aS can generate one or more
a’s.
Example: Generation of string aaaa
→ a) Using Sentential form:
S→ aS [using S→aS]
→ aaS [using S→aS]
→aaaS [using S→aS]
→ aaaaS [using S→aS]
→aaaa [using S→ ε]
b) Using Parse tree:
✓ Language L={a,aa,aaa,….}
→ The productions required to generate the above
language are: S → aS, S→a
The production, S→aS can generate one or more
a’s. The production, S →a is for termination of
recursion.
Example: Generation of aaaa
→ a) Using Sentential form:
S → aS [using S →aS]
→aaS [using S →aS]
→aaaS [using S →aS]
→aaaa [using S →aS]
R = (a+b)*
The productions required to generate the
above language are:
S → aS, S →bS, S→ ε
The two productions S → aS, S→bS can
generate any number of a’s or b’s with
the next character as either a or b.
The production S → ε is for termination of
recursion. Since ε is also member of L,
S→ε is taken for termination.
Example: Generation of the string abba
→ a) Using Sentential form:
S→ aS [using S→aS]
→abS [using S→bS]
→abbS [using S→bS]
→abbaS [using S→aS]
→abba [using S→ ε]
b) Using Parse tree:
✓Language L ={ε,ab,aabb,….anbn}
→ The above language can be also described as
a language of words in which every word
consists of any number of a’s followed by
equal number of b’s.
• The productions required to generate the
above language are: S→ aSb, S→ ε
• The recursion involved in the above language
is explained below.
|~~~~~ S ~~~~~|
aaaabbbb
• Both the strings a4b4 and a3b3 are generated
by S.
• a4b4 can be written as aa3b3b and thus we
have the recursive relation S → aSb.
• The production, S → aSb can generate any
number of a’s followed by equal number of
b’s.
• The production S→ ε is for termination of
recursion . Since ε is a member of given
language , S→ ε is taken for termination of
recursion.
Example: Generation of string aaabbb.
→ a) Using Sentential form:
S → aSb [using S→aSb]
→ aaSbb [using S→aSb]
→ aaaSbbb [using S→aSb]
→ aaabbb [using S→ ε ]
b) Using Parse tree:
✓ Language L ={ab,aabb,…..anbn}
→ The production required to generate the
above language are: S→aSb, S→ab
• The production, S→aSb can generate a string
of the form anbn . The production S → ab is
for termination of recursion.
• Ex: Generation of string aaabbb
→ a) Using Sentential form
S → aSb [using S→aSb]
→ aaSbb [using S→aSb]
→ aaabbb [using S→ab
b) Using Parse Tree:
✓Language L ={w Є {a,b}* |w is a palindrome of
odd length}
→ String ‘a’ is a palindrome of odd length.
String ‘b’ is a palindrome of odd length.
String ‘abababa’ is a palindrome .
• The palindrome nature of abababa can be
understood as given below.
abababa
babab
aba
b
• Thus a palindrome can be generated
recursively using two productions: S→aSb,
S→bSb
• Since, we are considering odd length
palindromes, S should terminate in either ‘a’
or ‘b’ and the production are: S→a , S→b.
• Thus the production required to generate the
above language are:
S→aSa, S→bSb, S→a, S→b
or in short S→aSa|bSb|a|b
Example: Generation of string abababa
→ a) Using Sentential form:
S→aSa [using S→ aSa]
→abSba [using S→ bSb]
→abaSaba [using S→ aSa]
→abababa [using S→ b]
b) Using Parse Tree:
✓ Language L={w Є{a,b}* | w is an even length
palindrome with |w|>0}
→ The productions required to generate the above
language are:
S→ aSb, S→ bSb, S→ bb, S→ aa
• The production s→aa, S→bb are for the
termination of recursion .
• It may be noted that the smallest string
generated by the above grammar are aa and bb.
• The two productions S→ aSa and S→ bSb can
generate a palindrome of arbitrary length.
Example:Generation of string ababbaba
→ a) Using Sentential form:
S→ aSa [using S→aSa]
→abSba [using S→bSb]
→abasaba [using S→aSa]
→ababbaba [using S→bb]
b) Using Parse Tree:
✓Language L={w Є{a,b}* | w is palindrome with
either even length or odd length with |w|>0}
→The productions required to generate the
above language are:
S→aSa |bSb | aa| bb| a| b
S→aa or S→bb are for termination of
recursion for even length palindromes.
S→a and S→ b are for termination of
recursion for odd length palindromes
S→ aSa or S→bSb can generate a palindrome
of an arbitrary length.
Practice Questions
Q) For the grammar E→E+T|T, T→T*F|F,
F→(E)|a|b Give leftmost derivation
and rightmost derivation for the
expression i) (a+b) * a+b ii) (b*a)+(a+b)
Q) For the grammar S→0S1|01 give
derivation for 00001111
Q) Check whether the string aabbb is in
L(G) where G= S→XY, X→YY|a, Y→XY|b
Thank You