0% found this document useful (0 votes)
25 views110 pages

Chapter - 3 - Context Free Language - Part - 1

The document discusses Context-Free Grammars (CFG) and Push-Down Automata (PDA), highlighting their roles in recognizing Context-Free Languages (CFL). It explains the structure of CFG, including its components and production rules, and provides examples of CFGs for various languages. Additionally, it covers derivation processes, including leftmost and rightmost derivations, and introduces the concept of ambiguous grammars.

Uploaded by

ishangautam099
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)
25 views110 pages

Chapter - 3 - Context Free Language - Part - 1

The document discusses Context-Free Grammars (CFG) and Push-Down Automata (PDA), highlighting their roles in recognizing Context-Free Languages (CFL). It explains the structure of CFG, including its components and production rules, and provides examples of CFGs for various languages. Additionally, it covers derivation processes, including leftmost and rightmost derivations, and introduces the concept of ambiguous grammars.

Uploaded by

ishangautam099
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/ 110

Course Instructor

ANUJ GHIMIRE
DISCLAIMER
 This document does not claim any originality and
cannot be used as a substitute for prescribed textbooks.
 The information presented here is merely a collection
from various sources as well as freely available material
from internet and textbooks.
 The ownership of the information lies with the
respective authors or institutions.
Chapter 3

Context Free Language and


Push Down Automata
Context Free Grammar
 We have introduced two different but equivalent,
methods of describing languages i.e. Finite
Automata and Regular Expressions.
 Expressions used to represent regular languages are
called regular expressions and the model that
recognizes the regular expression is finite automata.
 However, there is a class of languages, i.e. Non Regular
Languages, which are not processed by an NFA/DFA.
 In order to recognize these languages, there is a need
to come up with a new kind of automata.
Context Free Grammar
 This is what brings us to the concept of Push-Down
Automata (PDA).
 A PDA is a more powerful version of automata which
can recognize certain languages that are not
recognized by an NFA/DFA.
 Languages recognized by PDA are generated by a set of
rules called Context-Free Grammar(CFG) and are
known as Context-Free Languages (CFL).
 Context Free Languages are a larger class of languages
that encompasses all regular languages and some non
regular languages.
Context Free Grammar
 Context Free Grammar are the set of rules that are
used to generate the string that belongs to Context
Free Language
 Context Free Grammars are more expressive it is
because if a language L is accepted by a finite automata
then L can be generated by a context-free grammar as
well. Beware: The converse is NOT true
 CFG can describe certain features that have a recursive
structure, which makes them useful in a variety of
applications.
Context Free Grammar
 CFG are studied in fields of theoretical computer
science compiler design, and linguistics.
 Designers of compilers and interpreters for
programming languages often start by obtaining a
grammar for the language.
 Most compilers and interpreters contain a component
called a parser that extracts the meaning of a program
prior to generating the compiled code or performing
the interpreted execution.
 So construction of parser will be facilitated after we
describe the CFG.
Context Free Grammar
 Formally Context Free Grammar G can be defined by
quadruple:
G=(V, ∑, R, S)
Where, V= Set of variables / set of non-terminals (upper
case letter) and set of terminals (lower case
letter)
∑=Set of terminals (lower case letter)
S= Start symbol
R =Set of production rules
R is of the form:
P→α, where α={V, ∑}* and P Є V {single non terminals)
Context Free Grammar
 Let us consider a CFG as
G=(V, ∑, R, S)
where, V= {A,B,S,a,b}
∑= {a,b}
R consists of:
S → AbB
A → aA
A→ε
B → bB
B→ε
S= S is a start symbol
Context Free Grammar
 Above CFG can be used to generate the string that
belongs to the language L={ anbbm for m,n>=0}
 For example the string w=aaabbbb can be derived from
the given grammer by using the production rule.
Context Free Grammar
The process of string w=aaabbbb generation can be shown as:
S → AbB using (i)
→ aAbA using (ii)
→ aaAbA using (ii)
→ aaaAbA using (ii)
→ aaaεbA using (iii)
→ aaabbB using (iv)
→ aaabbbB using (iv)
→ aaabbbbB using (iv)
→ aaabbbbε using (v)
→ aaabbbb
which is the required string generated by the given grammar.
Context Free Grammar
 Rules of grammar for the regular expression are:
 a*
S → aS------------- (i)
S →ε ------------------(ii)
 a+b
S → a------------- (i)
S → b------------- (ii)
 (a+b)*
S → aS------------- (i)
S → bS------------- (ii)
S →ε ------------------(iii)
Context Free Grammar
 a*.b*
S → AB------------- (i)
S →ε ------------------(ii)
A→ aA------------- (iii)
A →ε ------------------(iv)
B→ bB------------- (v)
B→ε ------------------(vi)
 a*+b*
S → A------------- (i)
S → B------------- (ii)
S →ε ------------------(iii)
A→ aA------------- (iv)
A →ε ------------------(v)
B→ bB------------- (vi)
B→ε ------------------(vii)
CFG_Example_1
Write the CFG for the language that generates the string of at
least 2 length over ∑ {0,1}.
Solution,
The regular expression for the language that generates the string of at
least 2 length is:
R.E= (0+1)(0+1)(0+1)*
Its rule for the grammar can be written as:
S → AAB------------- (i)
A→ 1 ------------- (ii)
A → 0 ------------------(iii)
B→ 1B------------- (iv)
B →0B ------------------(v)
B→ε ------------------(vi)
CFG_Example_1
Let G be the CFG for the given language which is given by
quadruple as:
G=(V, ∑, R, S)
where, V= {A, B, S, 0, 1} are set of variables
∑= {0, 1} are set of terminals
R consists of:
S → AAB------------- (i)
A→ 1 ------------- (ii)
A → 0 ------------------(iii)
B→ 1B------------- (iv)
B →0B ------------------(v)
B→ε ------------------(vi)
S= S is a start symbol
CFG_Example_2
 Write the CFG for the language that generates the
palindrome string over ∑ {a,b}.

Solution,
The set of palindrome string over {a,b} are:
{a, b, aa, bb,aba, baab, bbbaabbb, aaabbaaa, ababbaba………}

The properties of palindrome string is that the first symbol


must match with last symbol, second last symbol must match
with second last symbol and so on.
CFG_Example_2
 Let us consider a CFG as
G=(V, ∑, R, S)
where, V= {A, a, b} are set of variables
∑= {a,b} set of terminals
R consists of:
A→ aAa
A→ bAb
A→a
A→b
A→ε
S = A is the start symbol
CFG_Example_3
 Construct CFG for language: {ambn : m>n, n≥0} .
Solution,
The set of string over {a,b} are:
{a, aa, aaa, aaab, aaaabbb, aaaaabbbb, aaaabbb, aaaaab………}

The properties of string generated by above CFG is that the


occurrence of a is always greater than b, also the number of b
can be zero i.e. no occurrence of b.
CFG_Example_3
 Let us consider a CFG as
G=(V, ∑, R, S)
where, V= {S, A, a, b}
∑= {a,b}
R consists of:
S→ SA
S→ aS | a
A →aAb | ε
S = S is the start symbol
CFG_Example_4
 Construct CFG for language: {ambn : m, n>0, m≥n} .
Solution,
The set of string over {a,b} are:
{ab, aabb, aab, aaabb, aaaaabbbb, aaaabbb, aaaaab………}

The properties of string generated by above CFG is that the


occurrence of a and b can be same as well as the number of a
can be more than number of b.
CFG_Example_4
 Let us consider a CFG as
G=(V, ∑, R, S)
where, V= {S, a, b}
∑= {a,b}
R consists of:
S→ AB
A→ aA| a
B→ aBb| b
S = S is the start symbol
Derivation of String from CFG
 Let G = (V, ∑, R, S) be a context free grammar. If w1 w2
w3….…wn are strings over variable V such that:
w1 → w2 → w3 ……….→ wn then we can say wn is
derivable.
 The sequence of substitution to obtain a string is
called derivation.
 Say wi derives wn i.e. w1 →* wn. Then, the sequence of
steps to obtain wn from wi is called derivation.
 A derivation of a string w in a given grammar G is a
sequence of substitutions starting with the start
symbol and resulting in w.
Derivation of String from CFG
 Language of CFG (L(G)):
 If G = (V, ∑, R, S) be a CFG, then the language of G
denoted by L(G) is the set of terminal strings that have
derivations from start symbol.
i.e. L(G) = { w ϵ ∑* : S →* w }
Derivation of String from CFG
 There are two types of derivation:
 Left Most Derivation (LMD)
 Right Most Derivation (RMD)
 In the leftmost derivation (LMD), at each step the
production rule for the leftmost non-terminal is used
whereas in rightmost derivation (RMD) the
production rule for the rightmost non-terminal is
used.
Derivation of String from CFG
Consider a grammar G as:
G=(V, ∑, R, S)
where, V= {S, A, B, a, b} are set of terminals
∑= {a, b} are set of terminals
R consists of:
S → aAS------------- (i)
S→ aS ------------- (ii)
S → b ------------------(iii)
A→ bB------------- (iv)
A →a ------------------(v)
B→ aA ------------------(vi)
B→ b ------------------(vii)
S= S is a start symbol
Derivation of String from CFG
Let us derive the string w=abaaab using different
derivation.
Left Most Derivation (LMD)
S → aAS------------rule (i)
→ abBS------------rule (iv)
→ abaAS------------rule (vi)
→ abaaS------------rule (v)
→ abaaaS------------rule (ii)
→ abaaab------------rule (iii)
Derivation of String from CFG
Right Most Derivation (RMD)
S → aAS------------rule (i)
→ aAaS------------rule (ii)
→ aAab------------rule (iii)
→ abBab------------rule (iv)
→ abaAab------------rule (vi)
→ abaaab------------rule (v)
Derivation Tree/ Parse Tree
 Derivation tree is the pictorial description of the
derivation of the string using the production rule
defined by the grammar.
 It is a hierarchical representation of derivation that
shows how the start symbol of a grammar derives a
strings in language.
 For a CFG, G = (V, ∑, R, S) , a parse tree has following
properties:
 The root is labeled by start symbol (S).
 Each interior nodes of parse tree are variables (V).
Derivation Tree/ Parse Tree
 Each leaf node is labeled by terminal symbol ( ∑ ) or ϵ .
 If an interior node is labeled with non-terminal A and its
children are x1 ,x2 ,….xn from left to right then there is a
production P as:
A → x1 x2 ………xn for each xi ϵ ∑

 Left hand side of the production rule is the root node at


each level and the right hand side of the production rule
is divided into multiple branches
Derivation Tree/ Parse Tree
Consider a production rule
S → X1 X2 X3
X2→ aX3
Its derivation tree can be drawn as:
S

X1 X2 X3

a X3
Derivation Tree/ Parse Tree
Consider a grammar G as:
G=(V, ∑, R, S)
where, V= {S, A, B, a, b} are set of terminals
∑= {a, b} are set of terminals
R consists of:
S → aAS------------- (i)
S→ aS ------------- (ii)
S → b ------------------(iii)
A→ bB------------- (iv)
A →a ------------------(v)
B→ aA ------------------(vi)
B→ b ------------------(vii)
S= S is a start symbol
Derive a string w= abaaab using LMD draw parse tree
Derivation Tree/ Parse Tree
Left Most Derivation (LMD)
S → aAS------------rule (i)
→ abBS------------rule (iv)
→ abaAS------------rule (vi)
→ abaaS------------rule (v)
→ abaaaS------------rule (ii)
→ abaaab------------rule (iii)
Derivation Tree/ Parse Tree
Parse Tree for derivation
S

a S
A a
S

b B

A b
a

a
Derivation Tree/ Parse Tree
Consider a grammar G as:
G=(V, ∑, R, S)
where, V= {S, A, B, a, b} are set of terminals
∑= {a, b} are set of terminals
R consists of:
S → aA | bB
A→ b | bS | aAA
B → a | aS | bBB
S= S is a start symbol
For the string bbaababa find its LMD, RMD and draw
parse tree.
Derivation Tree/ Parse Tree
Left Most Derivation (LMD)
S → bB------------[ using S → bB ]
→ bbBB------------[ using B → bBB ]
→ bbaB------------[ using B → a]
→ bbaaS------------[ using B → aS ]
→ bbaabB------------[ using S → bB ]
→ bbaabaS------------[ using B → aS ]
→ bbaababB------------[ using S → bB ]
→ bbaababa------------[ using B → a ]
Derivation Tree/ Parse Tree
Right Most Derivation (RMD)
S → bB------------[ using S → bB ]
→ bbBB------------[ using B → bBB ]
→ bbBaS------------[ using B → aS]
→ bbBabB------------[ using S → bB ]
→ bbBabaS------------[ using B → aS ]
→ bbBababB------------[ using S → bB ]
→ bbBababa------------[ using B → a ]
→ bbaababa------------[ using B → a ]
Derivation Tree/ Parse Tree
S

b B

b B
B

a S
a
b B

a S

b B
Parse Tree of Derivation
a
Ambiguous Grammar
 A grammar G = (V, ∑, R, S) is said to be ambiguous if
there is a string w Є L(G) for which we can derive two
or more distinct derivation tree rooted at S and
resulting the string w.
 In other words, if there exist multiple leftmost
derivation or multiple rightmost derivation for the
same string for any grammar then it is ambiguous.
Ambiguous Grammar
 Let G = (V, ∑, R, S) be a CFG with the production rule of
the form:
S → AB | aaB
A → a | Aa
B→b
Now, for string w=aab , we have two left most derivation
as:
S → AB
S → aaB
→ AaB
→ aab
→ aaB
→ aab
Ambiguous Grammar
 The derivation tree can be:
S S

A B a a B

A a
b b

a
 Since there are two parse tree for the same string w=aab,
this grammar is ambiguous.
Inherently Ambiguous
 Sometimes when we have an ambiguous grammar we
can find an unambiguous grammar that generates the
same language.
 Some context-free languages, however, can be
generated only by ambiguous grammars.
 Such languages are called inherently ambiguous.
 Ambiguity is a property of a grammar, and it is usually,
but not always possible to find an equivalent
unambiguous grammar.
 An “inherently ambiguous language” is a language for
which no unambiguous grammar exists.
Inherently Ambiguous
Assignment……

Explore about the example of


Inherently Ambiguous grammar.
Simplification of CFG
 In CFG sometimes all the production rules and symbols
used are not needed for the derivation of strings.
 Besides this, there may also be some NULL productions,
UNIT productions and USELESS symbols.
 Elimination of these productions and symbols that are not
used while derivation is called simplification of CFG.
 Simplification consists of:
 Removal of Null Productions
 Removal of Unit Productions
 Removal of Useless symbol
 Non generating symbol
 Not reachable symbol
Removal of Null Production
 The production rule of the form
non-terminal → empty (ε) string
is called as null production.
 To eliminate ε -production from a CFG, we first find
out null production rule of CFG and then remove then
from the grammar.
 A non terminal is said to produce null, if it derives an
epsilon(ε) in zero or more steps.
i.e if A →* ε then A is null production.
Removal of Null Production
 Steps:
 Find all the non terminal that contains null production
rule in the grammar.
 If such production rule is found then we replace that
non terminal with the empty string and add the
resulting production rule to the grammar.
 After adding new production to the grammar, we discard
the null production from the grammar.
 The process is continued until all the null productions
are eliminated.
Removal of Null Production_Example(1)
Consider a grammar:
S → AB
A → aAA | ε
B → bBB | ε
Remove the null productions
Solution,
The null production in the grammar is:
A→ε
B→ε
Now we need to find all the non-terminal A and B that exists
in the production rules.
Removal of Null Production_Example(1)
Here, the occurrence of A and B exists in the rule:
S → AB
A → aAA
B → bBB
So, we replace A and B by ε by all possible ways and add
the resulting production rule in the grammar
Let us take the production rule: S → AB
S → A [when B →ε]
S → B [when A →ε]
Removal of Null Production_Example(1)
So the production rule for S can be:
S → AB | A | B
Again, take the production rule: A → aAA
A → aA [when one A is replaced by A →ε]
A → a [when both A is replaced by A →ε]
So the production rule for A can be:
A → aAA |aA | a
Similarly, take the production rule: B → bBB
B → bB [when one B is replaced by B →ε]
B → b [when both B is replaced by B →ε]
So the production rule for A can be:
B → bBB |bB | b
Removal of Null Production_Example(1)
So the final production rule after removing the null
productions are:
S → AB | A | B
A → aAA |aA | a
B → bBB |bB | b
Removal of Null Production_Example(2)
 Consider a grammar:
S → ABC
A → BB | ε
B → CC | a
C → AA | b
Remove all the null productions.
Solution,
The null production in the grammar is:
A→ε
C → AA results in C → ε
B → CC results in B → ε
Removal of Null Production_Example(2)
So, we replace A, B and C by ε by all possible ways and add
the resulting production rule in the grammar

For S → ABC
S →AB [when C → ε ]
S → AC [when B → ε ]
S → BC [when A → ε ]
S→A [when B → ε and C → ε ]
S→B [when A→ ε and C → ε ]
S→C [when A→ ε and B → ε ]
After the removal of null production the resulting rules for S
are:
S → ABC | AB | AC | BC | A | B | C
Removal of Null Production_Example(2)
For A → BB
A →B [when B → ε ]
So, after the removal of null production the resulting rules
for A are:
A → BB | B
For B → CC
B →C [when C → ε ]
So, after the removal of null production the resulting rules
for B are:
B → CC | C | a
Removal of Null Production_Example(2)
For C → AA
C →A [when A → ε ]
So, after the removal of null production the resulting rules
for C are:
C → AA | A | b
So the final production rule after removing the null
productions are:
S → ABC | AB | AC | BC | A | B | C
A → BB | B
B → CC | C | a
C → AA | A | b
Removal of Unit Production
 A production rule of the form:
Non Terminal → one Non-Terminal
i.e A → B (where A and B, both are non-
terminals) is called unit production.
 Procedure to remove unit production:
 To remove the production rule of form A→ B, such that
there exists a production B→ α, where α is a terminal
add A→ α to the grammar
 Remove A→ B from the grammar.
 Repeat until all unit production are removed.
Removal of Unit Production_Example(1)
Consider a grammar
S → 0A | 1B | C
A → 0S | 00
B→1|A
C → 01
Remove the unit production
Solution,
The unit production in the grammar is:
S→C
B→A
Removal of Unit Production_Example(1)
To remove these unit production we have to search for a
production rule for C and A and add these rule to the
grammar.
Here, C → 01
Now replace the production of S → C by C → 01 and
remove S → C from the grammar.
S → 0A | 1B | 01
Again A → 0S | 00
Replace the production rule B → A by all the rule of
what A gives.
Removal of Unit Production_Example(1)
Replace the production rule B → A by all the rule of
what A gives and remove B → A from grammar .
i.e. B → 1 | A
B → 1 | 0S | 00
Thus final CFG after removal of unit production is:
S → 0A | 1B | 01
A → 0S | 00
B → 1 | 0S | 00
C → 01
Removal of Unit Production_Example(2)
Consider a grammar
S → AB
A→a
B→ C | b
C→ D
D→E
E→a
Remove the unit production
Removal of Unit Production_Example(2)
Solution,
The unit production in the grammar is:
B→C
C→D
D→E
To remove these unit production we have to search for a
production rule that contains the terminal symbol.
Here, such production rule is E → a
Now replace the production of D → E by E → a and remove
D → E from the grammar.
D→a
E→a
Removal of Unit Production_Example(2)
Again for C → D
Replace the production of C → D by D → a and remove C → D
from the grammar.
C→a
D→a
E→a
Lastly for unit production: B → C
Replace the production of B → C by C → a and remove B → C
from the grammar.
B→a|b
C→a
D→a
E→a
Removal of Unit Production_Example(2)
Hence all the unit production has been removed so the final
grammar is:
S→ AB
A→a
B→a|b
C→a
D→a
E→a
Removal of Useless Symbol
 Useless symbols are those variables or terminals used
in grammar that can never take part in derivation of
any string from the start symbol.
 Formally, we say a symbol X is useful for a grammar
G = (V, ∑, R, S) if there is some derivation of the form:
S →* α X β →* w, where w is in ∑*.
 Here, X may be either variable or terminal and the
sentential form α X β might be the first or last in the
derivation.
 If X is not useful, we say it is useless.
Removal of Useless Symbol
 Eliminating a useless symbol includes identifying
whether or not the symbol is “generating” and
“reachable”.
 Removal of useless symbol from CFG includes:
 Removal of non generating symbol
 Removal of not reachable symbol
 A symbol X is generating if X →* w for some terminal
string w.
 A symbol y is reachable if there is derivation S →* XyZ
for some X and Z.
 Thus we eliminate non-generating and then non
reachable symbols.
Removal of Useless Symbol
 For eliminating non generating symbol, identify the
non-generating symbols in CFG and remove those
from productions rules.
 For eliminating non-reachable symbols from CFG,
first identify non-reachable symbols and eliminate
symbols non-reachable symbols and production rules.
Removal of Useless Symbol_Example(1)
Consider the grammar G
S→ aB | bX
A→ Bad | bSX | a
B→ aSB | bBX
X→ SBd | aBX | ad
Let, S is start symbol.
Remove all useless symbol
Removal of Useless Symbol_Example(1)
Solution,
First identify all the non-generating symbol in the
grammar.
Here S, A and X are generating symbols but B is non-
generating because there is no way from B you can
reach to any terminal symbol.
So, remove all the rules with B. The new grammar is
then:
S→ bX
A→ bSx | a
X→ ad
Removal of Useless Symbol_Example(1)
Now remove non-reachable symbol if any from the
grammar
Here clearly you can see that we cannot reach to A from
start symbol S so, A is non-reachable.
So, remove A from the grammar and the simplified
grammar will be:
S→ bX
X→ ad
Removal of Useless Symbol_Example(2)
Consider the grammar G
S→ AB | CA
A→ a
B→ BC | AB
C→ aB | b
Let, S is start symbol.
Remove all useless symbol
Removal of Useless Symbol_Example(2)
Solution,
First identify all the non-generating symbol in the grammar.
Here A, C are generating symbols as they can generate
terminals. S can also generate terminal since S→ AC but B is
non-generating because there is no way from B you can
reach to any terminal symbol.
So, remove all the rules with B. The new grammar is then:
S→ CA
A→ a
C→ b
Removal of Useless Symbol_Example(2)
Now remove non-reachable symbol if any from the grammar
Here clearly you can see that we can reach to A and C from
start symbol S so, there is no any non-reachable.
So, the required simplified grammar is:
S→ CA
A→ a
C→ b
Chomsky Normal Form (CNF)
 A context free grammar G (V, ∑, R, S) is said to be in
Chomsky's Normal Form (CNF) if every production in
the grammar are in one of the two forms:
A→BC &
A→a
where A, B, and C are the non-terminals and a is
terminal
 A grammar in CNF is one which should not have:
 ε-production
 Unit production
 Useless symbols
Chomsky Normal Form (CNF)
 Procedure to find Equivalent Grammar in CNF
 Step 1: If the start symbol S occurs on same right side
then create a new start symbol S' and a new production
S'→ S.
 Step 2: Eliminate all the ε-productions, unit productions
and useless symbols from grammar if any.
 Step 3: If the right side of any production is in of the
form A→ aB where 'a' is a terminal and A and B are
non-terminal then the production 1s replaced by A→XB
and X → a. Repeat this step for every Production which
1s of the form A→ aB.
Chomsky Normal Form (CNF)
 Step 4: Replace each Production A → B₁..... Bn where
n>2, with A→B₁C where C → B2.... Bn. Repeat this step
for all productions having two or more symbols on the
right side.
Chomsky Normal Form(CNF)_Example(1)
Convert the following CFG into CNF G = (V, ∑, R, S)
where
V = { S, A , B}
∑ = {a, b}
R consists of:
S → aAB | AaB | B
A → aA | ε
B → ab | bA
Solution,
To convert CFG into CNF , we have to simplify the given CFG.
Chomsky Normal Form(CNF)_Example(1)
In given grammar there are ε -productions so we remove those
productions
Here, A → ε is the null production so we replace A with ε by
all possible ways and add the resulting production rule in the
grammar
S → aAB | AaB | aB | B
A → aA | a
B → ab | bA | b
Chomsky Normal Form(CNF)_Example(1)
In given grammar there are unit productions also so we
remove those productions from grammar
Here, S → B is the unit production.
To remove these unit production we have to search for a
production rule for B and add these rule to the grammar.
Here, B → ab | bA | b
Now replace the production of S → B by production rules of
B and remove S → B from the grammar.
S → aAB | AaB | aB | ab | bA | b
A → aA | a
B → ab | bA | b
Chomsky Normal Form(CNF)_Example(1)
S → aAB | AaB | aB | ab | bA | b
A → aA | a
B → ab | bA | b
There is no any useless symbol so this is the final simplified
grammar
In this simplified grammar all the production rules are not in the
form of:
A→ BC & A → a
Now, Converting this simplified form to get CNF:
S → CAB | ACB | CB | CD | DA | b
C→a
D→b
A → CA | a
B → CD | DA | b We also convert ab to CD as ab is not of the form
for CNF
Chomsky Normal Form(CNF)_Example(1)
Here , again the production rules S → CAB and S → ACB are
not in required form of CNF
we can perform like:
S → EB | FB | CB | CD | DA | b
E → CA
F → AC
C→a
D→b
A → CA | a
B → CD | DA | b
This the required CNF of the given CFG
Chomsky Normal Form(CNF)_Example(2)
Convert the following CFG into CNF G = (V, ∑, R, S)
where
V = { S, A , C}
∑ = {a, b}
R consists of:
S → AAC
A → aAb | ε
C → aC | a
Solution,
To convert CFG into CNF , we have to simplify the given CFG.
In given grammar there are ε -productions so we remove those
productions
Chomsky Normal Form(CNF)_Example(2)
Here, A → ε is the null production so we replace A with ε by
all possible ways and add the resulting production rule in the
grammar
S → AAC | AC | C
A → aAb | ab
C → aC | a
In given grammar there are unit productions also so we
remove those productions from grammar
Here, S → C is the unit production.
To remove these unit production we have to search for a
production rule for C and add these rule to the grammar.
Here, C → aC | a
Chomsky Normal Form(CNF)_Example(2)
Now replace the production of S → C by production rules of
C and remove S → C from the grammar.
S → AAC | AC | aC | a
A → aAb | ab
C → aC | a
There is no any useless symbol so this is the final simplified
grammar
In this simplified grammar all the production rules are not in
the form of:
A→ BC & A → a
Chomsky Normal Form(CNF)_Example(2)
Now, Converting this simplified form to get CNF:
S → AAC | AC | aC | a
A → aAb | ab
C → aC | a
These can be written as:
S → AAC | AC | DC | a
D→a
A → DAE | DE
E→b
C → DC | a
Chomsky Normal Form(CNF)_Example(2)
Here , again the production rules S → AAC and A → DAE are
not in required form of CNF
We can perform like:
These can be written as:
S → AF | AC | DC | a
F → AC
D→a
A → DG | DE
G → AE
E→b
C → DC | a
Greibach Normal Form (GNF)
 A context free grammar G=(V, ∑, R, S) is said to be in
Greibach Normal Form (GNF) if every production in
the grammar is in the form:
α→aβ*
where α and β Є V and a Є ∑ .
i.e. Non-Terminal →exactly one Terminal
OR
Non-Terminal → exactly one Terminal followed by
any number of Non-Terminal
Regular Grammar
 A grammar is said to be regular if it has the production rule
of the form:
A → aB
A→a
A→ε
where A,B Є V and a Є ∑ .
i.e. Non-Terminal →exactly one Terminal
OR
Non-Terminal → exactly one Terminal followed by
exactly one Non-Terminal

 A regular grammar may be left linear or right linear.


Linear Grammar
 A linear grammar is also a context-free grammar that
has at most one non-terminal symbol on the right
hand side of each grammar rule.
 A rule may have just one terminal symbols on the right
hand side (zero non-terminals) .
E.g.
S → aA
A → aBb | a | ε
B → Bb
Left and Right Linear Grammar
 A left linear grammar is a linear grammar in which the
non-terminal symbol always occurs on the left side.
E.g.
S → Aa
A → ab
 A right linear grammar is a linear grammar in which
the non-terminal symbol always occurs on the right
side.
E.g.
S → abaA
A→ε
Meaning of Context Free
 Let us consider a CFG as
G=(V, ∑, R, S)
where, V= {P, M, A, B, a, b}
∑= {a,b}
R consists of:
P→ aMb
M→ A
M→ B
A→ aA | ε
B→ bB | ε
S = P is the start symbol
Meaning of Context Free
 Now, Consider a string aaAb, which is an intermediate
stage in the generating of aaab.
 It is natural to call the strings “aa" and "b" that
surrounds the symbol A, the "context" of A in this
particular string.
 Now, the rule A → aA says that we can replace A by the
string aA no matter what the surrounding strings are;
in other words, independently of the context of A.
Meaning of Context Free
 When there is a production of form:
aaAb → aaBb (but not of the form A→ B),
 This grammar is context sensitive since A can be
replaced by B only when it is surrounded by the strings
“aa" and “b“.
 Such grammar i.e. Context Sensitive and is even
powerful than the CFG in term of generating wide
variety of string.
Pumping Lemma for CFG
 Theorem: Let A be a context free language. Then
there exists an integer n, called the pumping length,
such that any string w in A, with |w| ≥ n, can be
decomposed into five parts as w = uvxyz, such that:
 for all i ≥ 0, uvixyi z ∈ A
 |vy|≥ 1, y∉ ε
 |vxy|≤ n
 In words, the pumping lemma states that by replacing
the portion v and y in w by zero or more copies of it,
the resulting string is still in the language A.
Pumping Lemma for CFG_Example(1)
 Show that L = {anbncn : n>=1} is not CFL using
pumping lemma.
Solution,
Let L be a CFL and w be any string in L, where w ϵ L and
p be pumping constant with |w| >=p.
Now w can be written as w = uvxyz = apbpcp such that:
 for all i ≥ 0, uvixyiz ∈ L
 |vxy| <= p and
 |vy| > 0
Pumping Lemma for CFG_Example(1)
We prove this by contradiction,
Let us assume that given language L is context free.
If it is context free then we will have pumping length
(say p). Then our string s =apbpcp.
Let we take p=4 then, s=a4b4c4. Now we divide s into five
parts uvxyz.
Dividing the string into uvxyz can be done in two ways:
 v and y contains only one type of symbol.
 either v or y have more than one type of symbol
Pumping Lemma for CFG_Example(1)
Case-I : v and y contains only one type of symbol.
i.e. s=aaaabbbbcccc [ u=a, v=b, x=bb, y=b, z=cc]
For this let us check the condition :
for all i ≥ 0, uvixyiz ∈ L, with i=2
s=aaaabbbbbbbbcccc
We get s=a4b8c4 ∉ L.

Case-II : either v or y have more than one type of symbol


i.e. s=aaaabbbbcccc [ u=aa, v=aa, x=b, y=b, z=bbcccc]
Pumping Lemma for CFG_Example(1)
for all i ≥ 0, uvixyiz ∈ L, with i=2
s=aaaaaabbbbbcccc
We get s=a6b5c4 ∉ L.
So, s=aaaabbaabbbbccccc ∉ L
This concludes that s cannot be pumped which leads to the
contradiction.
Hence the given language is not CFL.
Pumping Lemma for CFG_Example(2)
 Show that L = {ww | w ε {0, 1}*} is not CFL.
Solution,
Let L be a CFL and w be any string in L, where w ϵ L and
p be pumping constant with |w| >=p.
Now w can be written as w = uvxyz = apbpcp such that:
 for all i ≥ 0, uvixyiz ∈ L
 |vxy| <= p and
 |vy| > 0
Pumping Lemma for CFG_Example(2)
We prove this by contradiction,
Let us assume that given language L is context free.
If it is context free then we will have pumping length
(say p). Then our string s =apbpapbp.
Let we take p=5 then, s=a5b5a5b5. Now we divide s into
five parts uvxyz.
Dividing the string into uvxyz can be done in two ways:
 vxy does not straddle a boundary.
Straddle means it does not lie on
 vxy straddles the first boundary
the either side of boundary but
lies in one part only without
crossing boundary
Pumping Lemma for CFG_Example(2)
Case-I : vxy does not straddle a boundary.
i.e. s=aaaaabbbbbaaaaabbbbb [i.e. uvxyz]
For this let us check the condition :
for all i ≥ 0, uvixyiz ∈ L, with i=2
s=aaaaabbbbbbbaaaaabbbbb
We get s=a5b7a5b5 ∉ L.
Pumping Lemma for CFG_Example(2)
Case-II : vxy straddles the first boundary
i.e. s=aaaaabbbbbaaaaabbbbb [i.e. uvxyz]
For this let us check the condition :
for all i ≥ 0, uvixyiz ∈ L, with i=2
i.e. s=aaaaaaabbbbbbbaaaaabbbbb [i.e. uvxyz]
We get s=a7b7a5b5 ∉ L.
Pumping Lemma for CFG_Example(2)
Case-III : vxy straddles the third boundary
i.e. s=aaaaabbbbbaaaaabbbbb [i.e. uvxyz]
For this let us check the condition :
for all i ≥ 0, uvixyiz ∈ L, with i=2
i.e. s= s=aaaaabbbbbaaaaaaabbbbbbb [i.e. uvxyz]
We get s=a5b5a7b7 ∉ L.
Since any of of the the cases does not satisfy our
conditions, it leads to contradiction for our assumption.
Hence, the given language is not CFL.
Closure Properties of CFG
 Context Free Languages are closed under
 Union
 Concatenation
 Kleene Star
 That is we can establish a new rule in the existing
grammar which can generate the string similar to the
existing grammar.
 To prove the closure properties we need to redefine the
four tuples of the grammar
Closure Properties of CFG (Union)
 Let G1 = (V1, ∑1 , R1, S1) and G2 = (V2, ∑2 , R2, S2) be two
context free grammars.
 Let us consider that they have disjoint set of non terminals.
 To show the union of two CFG is also a CFG we need to
define the new start symbol say S which is not in G1 and G2
 Then construct a new grammar G = (V, ∑ , R, S) where ,
V = V1 ∪ V2 ∪ {S}
∑ = ∑1 ∪ ∑2
R = R1 ∪ R2 ∪ {S →S1 | S2}
 Here G is clearly a context free grammar because two new
rules added are also of correct form.
Closure Properties of CFG (Union)
 Now , we can claim that L(G) = L(G1) ∪ L(G2)
 It is because of the rule S →S1 | S2.
 If we want to generate the string as per the rule of G1
we can use the rule S→S1 and using the production
rule of R1 it can generate the string that belongs to
L(G1).
 Again if we want to generate the string as per the rule
of G2 we can use the rule S→S2 and using the
production rule of R2 it can generate the string that
belongs to L(G2).
 So, context free languages are closed under union.
Closure Properties of CFG (Concatenation)
 Let G1 = (V1, ∑1 , R1, S1) and G2 = (V2, ∑2 , R2, S2) be two
context free grammars.
 Let us consider that they have disjoint set of non terminals.
 To show the concatenation of two CFG is also a CFG we
need to define the new start symbol say S which is not in G1
and G2
 Then construct a new grammar G = (V, ∑ , R, S) where ,
V = V1 ∪ V2 ∪ {S}
∑ = ∑1 ∪ ∑2
R = R1 ∪ R2 ∪ {S →S1S2}
 Here G is clearly a context free grammar because new rule
added is also of correct form.
Closure Properties of CFG (Concatenation)
 Here, if S1→*w1 and S2→*w2 then we can claim
S →* w1w2 as S →S1S2
 Now , we can claim that L(G) = L(G1).L(G2)
 If we want to generate the string w1w2 it is possible
because of rule S →S1S2 which can generate the string
that belongs to L(G1) and L(G2).
 The symbol S1 generates the string that belongs to
L(G1) and S2 generates the string that belongs to
L(G2).
 So, context free languages are closed under
concatenation.
Closure Properties of CFG (Kleene Star)
 Let G1 = (V1, ∑1 , R1, S1) be a context free grammar.
 To show the kleen star of CFG is also a CFG we need to
define the new start symbol say S which is not in G1.
 Then construct a new grammar G = (V, ∑ , R, S) where,
V = V1 ∪ {S}
∑ = ∑1
R = R1 ∪ {S→SS1 and S→ ε}
 Here G is clearly a context free grammar because new
rule added is also of correct form.
Closure Properties of CFG (Kleene Star)
 Here, if S1→*w1 and we can claim S →*w1 as S→SS1
and S→ ε
 Now , we can claim that L(G) = L(G1)
 If we want to generate the string w1 we can repeatedly
use S→SS1 as per our need and after that we can use
the rule R1 to generate w1.
 The symbol S1 generates the string that belongs to
L(G1).
 So, context free languages are closed under kleen star.
Closure Properties of CFG
 The Context Free Language are not closed under
Intersection and Complement.
 This property of CFL can be shown by using the
Pumping Lemma.
 We know that L1 = {0n1n2n | n >= 1} is not a CFL (use
the Pumping Lemma).
 However, L2 = {0n1n2i | n >= 1, i >= 1} is a CFL, and its
CFG is:
S → AB
A → 0A1 | 01
B → 2B | 2
Closure Properties of CFG
 So is L3 = {0i1n2n | n >= 1, i >= 1} is a CFL, and its CFG is:
S → AB
A → 0A | 0
B → 1B2 | 12
But L1 = L2  L3 is NOT a CFL.
So, context free languages are not closed under
intersection.
Closure Properties of CFG
 CFLs are NOT closed under Complement.
 If L is a CFL, its complement ഥ
𝑳
We know L1 L𝟐 = L𝟏 + L𝟐
Also CFLs are closed under union, it would follow that
the CFLs are closed under intersection.
However we know CFL are not closed under intersection.
Hence CFL are not closed under complement.

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