0% found this document useful (0 votes)
85 views58 pages

TOC Unit 3 Context Free Grammer

The document discusses context free grammars and how they can be used to represent languages, both regular and non-regular. It provides examples of context free grammars and how they generate languages through derivations. Derivations can be represented using sentential forms or parse trees. The key aspects are that a grammar defines a language through a set of productions from a start symbol and terminals, and that derivations recursively apply the productions to generate strings in the language.

Uploaded by

karan Dabhade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views58 pages

TOC Unit 3 Context Free Grammer

The document discusses context free grammars and how they can be used to represent languages, both regular and non-regular. It provides examples of context free grammars and how they generate languages through derivations. Derivations can be represented using sentential forms or parse trees. The key aspects are that a grammar defines a language through a set of productions from a start symbol and terminals, and that derivations recursively apply the productions to generate strings in the language.

Uploaded by

karan Dabhade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Theory of Computation

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

• Now let us consider the grammar given below


and see if it can generate the language generated
by the DFA given above
X → aX
X→Є
• The empty string Є can be generated by the
production X → ε
• A string “a” can be generated as shown below.
First by writing X as aX and then by replacing X on
the right side with an ε
• A string ‘aa’ can be generated as shown below
• First by writing X as aX and then by replacing X
on the right side with an aX and finally
replacing X on the right side with an ε.
• Thus the language generated by the set of
productions { X→aX, X→ ε} is L = {ε,a,aa,….}
• The production X→ ε, is for termination of
recursion.
Representing Non Regular Language
• A non regular language can be expressed
using a set of productions
• Let us consider a language L ={0n1n | n>=1}
• We know from pumping lemma that the
above language is not regular. But this
language can be represented using a set of
production.
• It will be worthwhile to understand that the
recursive nature of above language.
Representing Non Regular Language
• A string 0,1 Є L [ base case or termination
case]
• 0n1n can be written as 0(0n-11n-1)1
• if 0n1n is generated by X then 0n-11n-1 can also
be generated by X.
[0 (0n-1 1n-1 ) 1]
• Now, we can say that, X→ 0X1
i.e if we have string of the form 0n1n , after
removing the first 0 and the last 1, the string
remains in the form 0n1n.
• Thus the language L = {0n1n | n>=1} can be
represented using
• X → 0X1
• X→01
• The production X → 0X1 can recursively
generate a string 0n1n and the second
production X → 01 is used for termination of
recursion.
• Grammar can be used to represent both
– Regular languages
– Non regular languages
• Let us try to generate a string a3b3 using the
two productions
Context free Grammar
• A context free grammar G is a quadruple
(V,T,P,S), where:
• V is a set of variables .
• T is set of terminals.
• P is set of productions.
• S is a special variable called the start symbol
SЄV
• A production is of the form
Vi → αi where Vi Є V and
αi is a string of terminals and variables.
Notations
✓Terminals are denoted by lower case
letters a,b,c….or digits 0,1,2….etc .
✓Non terminals (variables) are denoted by
Upper case letters A,B,C……Z.
✓A string of terminals or a word w Є L is
represented using u,v,w,z,y,z.
✓A sentential form is a string of terminals
and variables and it is denoted by α, β, γ
etc.
CFG example
• Let us consider English sentences of the form:
1. manish reads
2. sachin sleeps
3. rohan plays
• The first word of in the above sentences is a
noun and the second word is a verb .
• A sentences of the above form can be written as
<sentence> → <noun><verb>
• Here , noun can be replaced with Manish, Sachin
or Rohan and <verb> can be replaced with
reads, sleeps, or plays.
CFG example
• We can write:
– <noun> → manish <verb> →reads
– <noun> → sachin <verb> →sleeps
– <noun> → rohan <verb> →plays
• In the above example:
1. <sentence>,<noun> and <verb> are variables or
non-terminals.
2. manish, sachin, rohan, sleeps, plays and reads
are terminals.
3. The variable <sentences> is the start symbol as a
sentence will be formed using the start symbol
<sentence>.
Formally, the grammar can be written as:
• G =(V,T,P,S), where
• V ={<sentence>,<noun>,<verb>}
• T ={ manish, sachin, rohan, sleeps, plays,
reads}
• P = {<sentence>→<noun><verb>,
<noun> → manish, <noun> → sachin,
<noun> → rohan, <verb> → sleeps,
<verb> → plays, <verb> → reads }
• S = <sentence>
• Several productions of <noun> and <verb> can
be merged together and set of productions
can be rewritten as:
• P = {<sentences> → <noun><verb>,
<noun> →manish|sachin|rohan,
<verb> → sleeps|plays|reads }
Language of a grammar
• Every grammar generates a language .
• A word of a language is generated by applying
productions a finite number of times.
• Derivation of a string should start from the start
symbol and the final string should consist of terminals .
• If G is a grammar with start symbol S and set of
terminals T, then the language of G is the set

• If the productions rule is applied once, then we write


• When it is applied a number of times then we write
Derivations
• Derivations can be represented using
– Sentential Form
– Parse Tree form
• In the sentential form, derivation starts from
the start symbol through a finite application of
productions.
• A set of derivations applied to generate a
word can be represented using a tree. Such a
tree is known as a parse tree.
Sentential Form
• Consider the grammar given below :
➢S → A1B (production 1)
➢A → 0A| ε (production 2)
➢B → 0B|1B| ε (production 3)
• Where, G is given by (V,T,P,S) with
➢V = {S,A,B}
➢T = {0,1}
➢P = {productions 1, 2, and 3}
➢S = start symbol
Sentential Form
• Let us try to generate the string 00101 from the
given grammar.
• S → A1B [starting production]
→ 0A1B [using the production A → 0A]
→ 00A1B [using the production A → 0A]
→ 001B [using the production A → ε]
→ 0010B [using the production B → 0B]
→ 00101B [using the production B → 1B]
→ 00101 [using the production B → ε ]
• Thus the string 00101 Є L(G)
• In the sentential form, derivation starts from
the start symbol through a finite application of
productions.
• A string α derived so far consists of terminals
and non – terminals.
• S *→ α |α Є (V ∪ T)*
• A final string consists of terminals.
• In the left sentential form, leftmost symbol is
picked up for expansion.
• In the right sentential form, rightmost symbol
is picked up for expansion.
• A string can be derived in many ways, but we
restrict our selves to :
• Leftmost derivation
• Rightmost derivation
• In the leftmost derivation the leftmost
variable of α (sentential form) is picked for
expansion
• In the rightmost derivation the rightmost
variable of α (sentential form) is picked for
expansion.
Example
• For the grammar given below :
S → A1B (production 1)
A → 0A| ε (production 2)
B → 0B|1B| ε (production 3)
Give Leftmost and Rightmost derivation of the
string 1001
→Leftmost derivation by picking up leftmost
variable fro expansion will be as
Leftmost Derivation
S → A1B [starting production]
→ 1B [using the production A → ε]
→ 10B [using the production B → 0B]
→ 100B [using the production B → 0B]
→ 1001B [using the production B → 1B]
→ 1001 [using the production B → ε]
Rightmost Derivation
S → A1B [starting production]
→ A10B [using the production B → 0B]
→ A100B [using the production B → 0B]
→ A1001B [using the production B → 1B]
→ A1001 [using the production B → ε ]
→ 1001 [using the production A → ε]
Parse tree
• A set of derivations applied to generate a word
can be represented using a tree. Such a tree is
known as a parse tree. This representation gives
us a better understanding of:
– Recursion
– Grouping of symbols
• A parse tree is constructed with the following
condition:
– Root of the tree is represented by start symbol.
– Each interior node is represented by a terminal or ε.
– A string generated by a parse tree is seen from left to
right.
Examples
Q) For the grammar given below
S→ A1B A→ 0A | ε B→0B|1B| ε
Give parse tree for the leftmost and rightmost
derivation of the string 1001. (d)
→Parse tree for leftmost derivation
is constructed stepwise
(a) (b) (c)
Similarly rightmost derivation can be shown
stepwise as:
(a) (b)

(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]

b) Using Parse tree:


✓ Language L= {b,ab,aab,aab}
→ The productions required to generate the
above language are: S→aS S→b
The production, S →aS can generate one or
more a’s .
The production S→b is for termination of
recursion
Thus generating zero or more a’s followed by a
‘b’.
Example: Generation of string aaab
→ a) Using Sequential form:
S → aS [using S →aS]
→aaS [using S →aS]
→aaaS [using S →aS]
→aaaa [using S →b]
b) Using Parse tree:
✓Language L={w Є {a,b}*}
→ The above language can also be written
as L={ε,a,b,aa,ab,ba,bb,….}
Thus any string that can be derived from
the alphabet {a,b} belongs to L.
This language can also be generate by:
The DFA M = a,b

or by the Regular Expression q0

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy