Chapter_2_Finite State Automata_Part_3
Chapter_2_Finite State Automata_Part_3
Solution,
The set of palindrome string over {a,b} are:
{a, b, aa, bb,aba, baab, bbbaabbb, aaabbaaa, ababbaba………}
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.
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.
Chomsky Hierarchy of Grammar
Chomsky Hierarchy of Grammar
Assignment:
• Go through the production rule of each grammar
End of Chapter 2
Thank You !!!!