0% found this document useful (0 votes)
14 views36 pages

Toc 3

Uploaded by

rahulcswe
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)
14 views36 pages

Toc 3

Uploaded by

rahulcswe
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/ 36

UNIT – 3 : PUSHDOWN AUTOMATON

Basic Structure of PDA:


A pushdown automaton is a way to implement a context-free grammar in a similar
way we design DFA for a regular grammar. A DFA can remember a finite amount
of information, but a PDA can remember an infinite amount of information.
Basically a pushdown automaton is −
"Finite state machine" + "a stack"
A pushdown automaton has three components −
 an input tape,
 a control unit, and
 a stack with infinite size.
The stack head scans the top symbol of the stack.
A stack does two operations −
 Push − a new symbol is added at the top.
 Pop − the top symbol is read and removed.
A PDA may or may not read an input symbol, but it has to read the top of the
stack in every transition.
A PDA can be formally described as a 7-tuple (Q, ∑, S, δ, q 0, I, F) −
 Q is the finite number of states
 ∑ is input alphabet
 S is stack symbols
 δ is the transition function: Q × (∑ ∪ {ε}) × S × Q × S*
 q0 is the initial state (q0 ∈ Q)
 I is the initial stack top symbol (I ∈ S)
 F is a set of accepting states (F ∈ Q)
The following diagram shows a transition in a PDA from a state q1 to state q2,
labelled as a,b → c −

This means at state q1, if we encounter an input string ‘a’ and top symbol of the
stack is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.

Elements of Push-down Automata:


Now, we will discuss below in detail these seven elements of Push-down
automata –
1. Set of Input Alphabets – The input alphabet can be any numeric value and
alphabet. It is represented by using the' ∑ ‘ symbol. The input alphabets are
passed through the states, represented by the arrows in the transition diagram.
For example, ∑ = { a, b, c, d },
∑ = { 0, 1 } , and so many.
2. Initial State – The ‘q0' symbol represents it. The initial state is the starting
state of any machine. It is present uniquely in the machine. The machine has only
one initial state. The beginning of any transition begins itself from the initial state.
3. Final State – The ‘F' symbol denotes it. The final state is the reaching or
ending state of any machine. Whenever any transition is reached to the final state,
that string is always accepted by the machine. The machine has zero or more final
states in it. F belongs to the set of final states, which means the machine has more
than one reaching state.
4. Transition state – The ‘ Q ' symbol represents it. It is a set of all transition
states. In this set, the initial state, the final state, and all other intermediate
states are present. It consists of all transition states which takes participate in the
transition diagram.
For example – Q = { q1 , q2 } ,
Q = { A , B , C } , and so on.
5. Set of Stack Alphabet – The ‘ τ ' symbol represents it. It is a set of all Stack
alphabets; the stack alphabet symbolizes that whenever we perform push, pop and
bypass operations, we will directly apply it on any transition state and perform
simultaneously on the stack. It contains all those symbols which are pushes into
the stack.
6. Initial stack symbol – It is always represented by using the ' z0 ' symbol. The
initial stack symbol, as a name suggests it is always present in the stack while we
are constructing any push-down Automata; if we find only z0 is present, then it
means that the stack is empty.
7. Transition Function – The ' δ' symbol represents it. Transition Function is
used to identify the transition from one state to another state when any input
symbol pushes in the stack, popped out from the stack, or bypasses. It is denoted
as a cross product of transition state with input alphabet including epsilon (Є)
string and Stack Alphabet, as a result, produces a cross product of another
transition state (it can be finial state, initial state, or any intermediate state) with
any configuration of stack alphabets.
δ = Q x ∑ ⋃ { Є } x τ → Q x τ*
For example –
δ ( q0 , a , z0 ) → ( q0 , az0 ) ,
δ ( q0 , a , a ) → ( q0 , aa ) , and so on.
Terminologies Related to PDA:
Instantaneous Description:
The instantaneous description (ID) of a PDA is represented by a triplet (q, w, s)
where,
 q is the state
 w is unconsumed input
 s is the stack contents

Turnstile Notation:
The "turnstile" notation is used for connecting pairs of ID's that represent one or
many moves of a PDA. The process of transition is denoted by the turnstile
symbol "⊢".
Consider a PDA (Q, ∑, S, δ, q0, I, F). A transition can be mathematically
represented by the following turnstile notation −
(p, aw, Tβ) ⊢ (q, w, αb)
This implies that while taking a transition from state p to state q, the input
symbol ‘a’ is consumed, and the top of the stack ‘T’ is replaced by a new
string ‘α’.
Note − If we want zero or more moves of a PDA, we have to use the symbol (⊢*)
for it.

Operations performed on Push-down Automata:


Three operations are performed on a stack of push-down Automata for accepting
any string –
1. Push operation – Push operation is performed on the stack to push the input
alphabets into the stack whenever required. While performing this operation
transition state may change or remain the same. It depends on the criteria for
constructing the Push-down Automata Transition diagram.
For example –
δ ( q0 , a , z0 ) → ( q0 , az0 ) ,
Here in the above example, before pushing, the symbol that is present on the top
of the stack is ‘z0 ', i.e., initial stack symbol and represents the stack is empty, now
when we are applying pushing operation by pushing symbol ‘a’ into the stack
from transition state q0 then after pushing state remains same, i.e., q0 and symbol
that is present on top of the stack are ' a '.

2. Pop operation – Pop operation is performed on the stack to pop out the input
alphabets whenever required. While performing this operation transition state
may change or remain the same; it depends on constructing the Push-down
Automata Transition diagram.
For example –
δ (q0, b, a ) → ( q1, Є ) ,
In the above example, before popping, the symbol that is present on the top of the
stack is ‘a', we need to pop out this symbol and apply pop operation by using
symbol 'b' to pop out symbol 'a', the state is changing from q0 to q1 and on the
right-hand side symbol ‘Є’ represents the pop operation.

3. Bypass operation – Bypass operation is performed on the stack to bypass the


symbol or alphabet; basically, it means to skip the current alphabet. It is neither
used to push the symbol into the stack nor pop out the symbol. While performing
this operation transition state may change or remain the same; it depends on
constructing the Push-down Automata Transition diagram.
For example –
δ (q0 , b , a ) → ( q1 , a ) ,
Here, before bypass operation, the symbol that is present on the top of the stack
is ‘ a', we need to bypass this symbol and apply bypass operation by using symbol
' b ' to bypass the symbol ' a ', the state is changing from q0 to q1, and now the
symbol present on the top of the stack is ' a ', it is not changed; we have
successfully bypassed the symbol ‘ a ’.
Pushdown Automata Acceptance:
There are two different ways to define PDA acceptability –
Final State Acceptability:
In final state acceptability, a PDA accepts a string when, after reading the entire
string, the PDA is in a final state. From the starting state, we can make moves that
end up in a final state with any stack values. The stack values are irrelevant as
long as we end up in a final state.
For a PDA (Q, ∑, S, δ, q0, I, F), the language accepted by the set of final states F
is –
L(PDA) = {w | (q0, w, I) ⊢* (q, ε, x), q ∈ F}
for any input stack string x.

Empty Stack Acceptability:


Here a PDA accepts a string when, after reading the entire string, the PDA has
emptied its stack.
For a PDA (Q, ∑, S, δ, q0, I, F), the language accepted by the empty stack is –
L(PDA) = {w | (q0, w, I) ⊢* (q, ε, ε), q ∈ Q}

Example - Construct a PDA that accepts L = {0n 1n | n ≥ 0}


Solution:

This language accepts L = {ε, 01, 0011, 000111, ............................. }


Here, in this example, the number of ‘a’ and ‘b’ have to be same.
 Initially we put a special symbol ‘$’ into the empty stack.
 Then at state q2, if we encounter input 0 and top is Null, we push 0 into
stack. This may iterate. And if we encounter input 1 and top is 0, we pop
this 0.
 Then at state q3, if we encounter input 1 and top is 0, we pop this 0. This
may also iterate. And if we encounter input 1 and top is 0, we pop the top
element.
 If the special symbol ‘$’ is encountered at top of the stack, it is popped out
and it finally goes to the accepting state q4.

Example:
Construct a PDA that accepts L = { wwR | w = (a+b)* }
Solution:

Initially we put a special symbol ‘$’ into the empty stack. At state q2, the w is
being read. In state q3, each 0 or 1 is popped when it matches the input. If any
other input is given, the PDA will go to a dead state. When we reach that special
symbol ‘$’, we go to the accepting state q4.

PDA & Context-Free Grammar:


If a grammar G is context-free, we can build an equivalent nondeterministic PDA
which accepts the language that is produced by the context-free grammar G. A
parser can be built for the grammar G.
Also, if P is a pushdown automaton, an equivalent context-free grammar G can
be constructed where,
L(G) = L(P)
In the next two topics, we will discuss how to convert from PDA to CFG and vice
versa.
Algorithm to find PDA corresponding to a given CFG:
Input − A CFG, G = (V, T, P, S)
Output − Equivalent PDA, P = (Q, ∑, S, δ, q0, I, F)
Step 1 − Convert the productions of the CFG into GNF.
Step 2 − The PDA will have only one state {q}.
Step 3 − The start symbol of CFG will be the start symbol in the PDA.
Step 4 − All non-terminals of the CFG will be the stack symbols of the PDA and
all the terminals of the CFG will be the input symbols of the PDA.
Step 5 − For each production in the form A → aX where a is terminal and A,
X are combination of terminal and non-terminals, make a transition δ (q, a, A).

Problem:
Construct a PDA from the following CFG.
G = ({S, X}, {a, b}, P, S)
where the productions are −
S → XS | ε , A → aXb | Ab | ab
Solution:
Let the equivalent PDA,
P = ({q}, {a, b}, {a, b, X, S}, δ, q, S)
where δ −
δ(q, ε , S) = {(q, XS), (q, ε )}
δ(q, ε , X) = {(q, aXb), (q, Xb), (q, ab)}
δ(q, a, a) = {(q, ε )}
δ(q, 1, 1) = {(q, ε )}
Algorithm to find CFG corresponding to a given PDA:
Input − A CFG, G = (V, T, P, S)
Output − Equivalent PDA, P = (Q, ∑, S, δ, q0, I, F) such that the non- terminals
of the grammar G will be {Xwx | w,x ∈ Q} and the start state will be Aq0,F.
Step 1 − For every w, x, y, z ∈ Q, m ∈ S and a, b ∈ ∑, if δ (w, a, ε) contains (y,
m) and (z, b, m) contains (x, ε), add the production rule Xwx → a Xyzb in grammar
G.
Step 2 − For every w, x, y, z ∈ Q, add the production rule Xwx → XwyXyx in
grammar G.
Step 3 − For w ∈ Q, add the production rule Xww → ε in grammar G.

Pushdown Automata & Parsing:


Parsing is used to derive a string using the production rules of a grammar. It is
used to check the acceptability of a string. Compiler is used to check whether or
not a string is syntactically correct. A parser takes the inputs and builds a parse
tree.
A parser can be of two types −
 Top-Down Parser − Top-down parsing starts from the top with the start-
symbol and derives a string using a parse tree.
 Bottom-Up Parser − Bottom-up parsing starts from the bottom with the
string and comes to the start symbol using a parse tree.

Design of Top-Down Parser:


For top-down parsing, a PDA has the following four types of transitions −
 Pop the non-terminal on the left hand side of the production at the top of
the stack and push its right-hand side string.
 If the top symbol of the stack matches with the input symbol being read,
pop it.
 Push the start symbol ‘S’ into the stack.
 If the input string is fully read and the stack is empty, go to the final state
‘F’.
Example:
Design a top-down parser for the expression "x+y*z" for the grammar G with the
following production rules −
P: S → S+X | X, X → X*Y | Y, Y → (S) | id
Solution:
If the PDA is (Q, ∑, S, δ, q0, I, F), then the top-down parsing is −
(x+y*z, I) ⊢(x +y*z, SI) ⊢ (x+y*z, S+XI) ⊢(x+y*z, X+XI)
⊢(x+y*z, Y+X I) ⊢(x+y*z, x+XI) ⊢(+y*z, +XI) ⊢ (y*z, XI)
⊢(y*z, X*YI) ⊢(y*z, y*YI) ⊢(*z,*YI) ⊢(z, YI) ⊢(z, zI) ⊢(ε, I)

Design of a Bottom-Up Parser:


For bottom-up parsing, a PDA has the following four types of transitions −
 Push the current input symbol into the stack.
 Replace the right-hand side of a production at the top of the stack with its
left-hand side.
 If the top of the stack element matches with the current input symbol, pop
it.
 If the input string is fully read and only if the start symbol ‘S’ remains in
the stack, pop it and go to the final state ‘F’.

Example:
Design a top-down parser for the expression "x+y*z" for the grammar G with the
following production rules −
P: S → S+X | X, X → X*Y | Y, Y → (S) | id
Solution:
If the PDA is (Q, ∑, S, δ, q0, I, F), then the bottom-up parsing is −
(x+y*z, I) ⊢ (+y*z, xI) ⊢ (+y*z, YI) ⊢ (+y*z, XI) ⊢ (+y*z, SI)
⊢(y*z, +SI) ⊢ (*z, y+SI) ⊢ (*z, Y+SI) ⊢ (*z, X+SI) ⊢ (z, *X+SI)
⊢ (ε, z*X+SI) ⊢ (ε, Y*X+SI) ⊢ (ε, X+SI) ⊢ (ε, SI)
Types of Push-Down Automata:
Push-Down Automata are divided into two categories –
1. Deterministic Push-down Automata
2. Non-Deterministic Push-down Automata
Deterministic Push-Down Automata:
 DPDA stands for Deterministic Push-Down Automata.
 It does not produce any output.
 The Language which DPDA accepts is always context-free Language.
 Context-free Language is always generated from Context-free Grammar.
 Therefore it is one of the types of Push-down Automata; it also contains
seven elements.
 In DPDA, only one transition exists for an input alphabet when applying
over on any state.
 The expressive power of Deterministic push-down automata is always less
than the non-deterministic push-down automata.
 If we compare DPDA with DFA, we can observe that the default FA is
always DFA, but in PDA default, PDA is NPDA.
 The number of languages accepted by DPDA is always less than the of
NPDA.
 It is also represented using two methods: the empty stack and the final state.
 For every Deterministic Push-Down Automata, there exists a Non-
Deterministic Push-Down Automata.

Example of Deterministic push-down automata


Example – Design a Push-down Automata using Final state Acceptance method
for the given below Language, i.e., odd length palindrome –
L = { w x wR | w Є ( a , b )* and wR is the reverse of w }
Solution:
 LOGIC :
Let us take a random string which is satisfying the given above
LanguageLanguage –
w: a a b b
wR: b b a a
As mentioned in the above question that wR is the reverse string of the string taken
in w. In this, we are applying the logic as, whenever the combinations of strings
are encountered before the character ‘ x ’, we will apply push operation to push
the w string alphabet’s one by one into the stack, once ' x ' is encountered. We
will apply a bypass operation to skip the alphabet ' x ', and then we will apply the
pop operation for further combinations of a and b.
The amazing thing is that you will notice that wR is the reverse of w; hence, when
we apply the pop operation, the particular alphabet will pop out the same alphabet
itself. Here, only one transition exists for a single input alphabet for a specific
purpose, whether used for pushing, popping or bypassing; it depends on the logic
we prefer.

In the above stack, we are pushing string w alphabet's one by one, and now when
we apply push operation using wR string alphabets, i.e., b, b, a, a.
For removing the alphabet ‘ b ', which is present at the top of the stack, we will
use the alphabet b itself as it is at the beginning of the reverse of the string. Here
we can observe that alphabet a is used to pop out a from the stack. Similarly, in
this way respectively all alphabets are used to pop out their corresponding
alphabets and in this way, the stack becomes empty.
After removing all the elements from the stack, we will divert it to the final state,
which shows that the string is accepted from the Push-Down Automata.
 Transition Diagram :

 Machine Representation:
M = { Q , ࢣ , δ , q0 , F , τ , z0 }
Here,
 M represents the machine
 Q = { q0 , q1 , q2 }
 ࢣ={a,b}
 q0 represents the initial state.
 F represents the final state and F = { q2 }
 τ = { a , z0 }
 z0 represents the Initial stack symbol
 δ represents the Transition function –
1. δ ( q0 , a , z0 ) → ( q0 , a z0 )
2. δ ( q0 , b , z0 ) → ( q0 , b z0 )
3. δ ( q0 , a , a) → ( q0 , a a )
4. δ ( q0 , b , b) → ( q0 , b b )
5. δ ( q0 , a , b) → ( q0 , a b )
6. δ ( q0 , b , a) → ( q0 , b a )
7. δ ( q0 , x , a) → ( q1 , a )
8. δ ( q0 , x , b) → ( q1 , b )
9. δ ( q0 , x , z0 ) → ( q1 , z0 )
10. δ ( q1 , a , a) → ( q1 , Є )
11. δ ( q1 , b , b) → ( q1 , Є )
12. δ ( q1 , Є , z0 ) → ( q2 , z0 )
Hence, for the above problem, i.e., odd length palindrome, the Deterministic
Push-down Automata exists. Therefore, if DPDA exists, then NPDA for the above
problem also exists.

Non-Deterministic Push-Down Automata:


 NPDA stands for Non-Deterministic Push-Down Automata.
 It does not produce any output.
 Whenever we talk about PDA, we are always concerned with Non-
Deterministic Push-down Automata; Default PDA is NPDA.
 The Language which NPDA accepts is always context-free and regular.
 Therefore it is one of the types of Push-down Automata; it also contains
seven elements.
 In NPDA, more than one transition exists for an input alphabet when
applying over on any state.
 The expressive power of Non-Deterministic push-down automata is always
more than the deterministic push-down automata.
 The number of languages accepted by NPDA is always greater than the
DPDA.
 It is also represented using two methods: the empty stack and the final state.
 It is more powerful than the DPDA.

Example of Non-Deterministic push-down automata


Example – Design a Push-down Automata using Final state Acceptance method
for the given below Language, i.e., even length palindrome –
L = { w wR | w Є ( a , b )* and wR is the reverse of w }
Solution:
 LOGIC:
Let us take a random string which is satisfying the given above Language –
w: a a b b
wR: b b a a
Here, we can notice that the centre is not fixed; hence, to solve this problem, we
cannot use the Deterministic Push-down Automata because DPDA cannot
determine that it needs to push alphabets and from where it needs to start pop
operation.
So to overcome this problem, we will use the concept of Non-deterministic push-
down Automata by applying the multiple transitions on the single input alphabet
so that at the end, the whole transitions would be diverted to the final state and it
will easily solve the problem.

Here two cases arise:


1. When the centre comes after a particular alphabet, we need to perform a
pop operation to pop the whole string out of the stack easily.
2. When the centre is not coming after a particular alphabet, we need to
continue with the push operation so that the whole string is easily be pushed
into the stack.
If some while from one of the cases the strings cannot exhaust, the machine will
automatically be terminated and choose the other case.
Here we apply the two transitions on a single input alphabet so that the Push-
down Automata can easily accept the given string and can easily reach to the final
state.
 Transition Diagram:

 Machine Representation:
M = { Q , ࢣ , δ , q0 , F , τ , z 0 }
Here,
 M represents the machine
 Q = { q0 , q1 , q2 }
 ࢣ={a,b}
 q0 represents the initial state.
 F represents the final state and F = { q2 }
 τ = { a , z0 }
 z0 represents the Initial stack symbol
 δ represents the Transition function –
1. δ ( q0 , a , z0 ) → ( q0 , a z0 )
2. δ ( q0 , b , z0 ) → ( q0 , b z0 )
3. δ ( q0 , a , a) → ( q0 , a a )
4. δ ( q0 , b , b) → ( q0 , b b )
5. δ ( q0 , a , b) → ( q0 , a b )
6. δ ( q0 , b , a) → ( q0 , b a )
7. δ ( q0 , Є , z0 ) → ( q2 , z0 )
8. δ ( q0 , a , a) → ( q1 , Є )
9. δ ( q0 , b , b) → ( q1 , Є )
10. δ ( q1 , a , a) → ( q1 , Є )
11. δ ( q1 , b , b) → ( q1 , Є )
12. δ ( q1 , Є , z0 ) → ( q2 , z0 )
Hence, the Deterministic Push-down Automata does not exist for the above
problem, i.e., even length palindrome. Therefore, we have seen above that Non-
deterministic Push-down Automata exists; hence, it is proved that NPDA is more
powerful than DPDA. Expressive power and number of Languages acceptance
are more in Non-deterministic Push-down Automata (NPDA) than Deterministic
Push-down Automata (DPDA).

Points to remember –
1. The expressive power of Non-deterministic Push-down Automata (NPDA)
is always greater than Deterministic Push-down Automata (DPDA).
2. NPDA is more powerful than DPDA.
3. Whenever we are talking about PDA, then it is by default set that we are
considering the Non-Deterministic Push-down Automata.
4. The number of languages accepted by DPDA is always less than the of
NPDA.
5. Deterministic Push-down Automata were unable to convert into the Non-
deterministic Push-Down Automata.
6. All Deterministic Push-down Automata is Non-Deterministic Push-down
Automata.
7. But all NPDA is not DPDA.
8. As we compare it with Finite Automata, in Finite automata, the expressive
power of DFA is equal to the NFA.
9. PDA whether DPDA and NPDA, does not produce any output, they always
shows the language acceptance criteria.
10. Also, there are many closure properties in which NPDA is closed, but
DPDA is not closed under various operations, for example
– Union, Concatenation, Substitution, and many more.
Deterministic Context-free Languages:
Deterministic CFL are subset of CFL which can be recognized by Deterministic
PDA. Deterministic PDA has only one move from a given state and input symbol,
i.e., it does not have choice. For a language to be DCFL it should be clear when
to PUSH or POP.
For example, L1= { anbncm | m >= 0 and n >= 0} is a DCFL because for a’s, we
can push on stack and for b’s we can pop. It can be recognized by Deterministic
PDA. On the other hand, L3 = { anbncm ∪ anbmcm | n >= 0, m >= 0 } cannot be
recognized by DPDA because either number of a’s and b’s can be equal or either
number of b’s and c’s can be equal. So, it can only be implemented by NPDA.
Thus, it is CFL but not DCFL.
Note: DCFL are closed only under complementation and Inverse
Homomorphism.

The Pumping Lemma For CFL:


Pumping lemma for context free language (CFL) is used to prove that a language
is not a Context free language.

Theorem: Let L be a context-free language, then there exists an integer p ≥ 1


referred to as the pumping length such that the following holds:
Every string w in L with |w| ≥ p can be written as w = uvxyz such that:
 |vy| ≥ 1, that is, v and y are not both empty.
 |vxy| ≤ p
k k
 uv xy z ∈ L, for all k ≥ 0.

The given rules mean that if two substrings from a string of a CFL are pumped
i.e., parts v and y are repeated k number of times, it will still remain a part of the
CFL.
The lemma gives a result in the form of a contradiction. We suppose that a given
instance (string) of a language is context-free, and we contradict our supposition,
as the pumped string will not be a part of the language.
Steps to apply pumping lemma:
 Assume that L is context-free.
 The pumping length will be n.
 All strings longer than the length(n) can be pumped-> |w|>=n.
 Now we have to find a string 'w' in L such that |w|>=n.
 We will divide string 'w' into uvxyz.
 Now show that uvkxykz ∉L for some constant k.
 Then, we have to consider the ways that w can be divided into uvxyz.
 Show that none of these can satisfy all the 3 pumping conditions
simultaneously.
 A string 'w' cannot be pumped (contradiction) and so L is not context-free.

Example:
Find out whether L={xnynzn|n>=1} is context-free or not.
Solution:
 Let L be context-free.
 'L' must be satisfying the pumping length, say n.
 Now we can take a string such that s=xnynzn
 We divide s into 5 strings uvxyz.
Let n=4 so, s=x4y4z4
Case 1:
v and y each contain only one type of symbol.
{we are considering only v and y because v and y has power uv2xy2z}
X xx x yyyyz z zz
=uvkxykz when k=2
=uv2xy2z
=xxxxxxyyyyzzzzz
=x6y4z5
(Number of x # number of y #number of z)
Therefore, The resultant string is not satisfying the condition
x6y4z5 ∉ L
If one case fails, there is no need to check another condition.
Case 2:
Either v or y has more than one kind of symbols
Xx xx yy y y zzzz
=uvkxykz (k=2)
=uv2xy2z
=xxxxyyxxyyyyyzzzz
=x4y2x2y5z2
This string is not following the pattern of our string xnynzn
so, L is not context-free.

Closure Properties of Context-Free Languages:


Closure Properties of Context-Free Languages under different operations -
1. Subset Operation:
Let L1 is any context free language –

Here, we know that L1 is Context-free language and L2 is not a Context-free


language, but L2 is a subset of L1, hence Context-Free Languages is not closed
under the Subset operation.
If CFL is not closed under subset operation, then it is must that DCFL is also not
closed under subset operation.
2. Union Operation:
Let L1 and L2 are two Context free languages -

L1 union L2 symbolizes that either Language L1 will follow or Language L2 will


follow if any L1 or L2 will be closed under union operation. We can conclude
that CFL is closed under union operation.
Hence we know that L1 ∪ L2 can be construct using NPDA because it is an
NCFL, therefore, NCFL is closed under union operation.
But L1 ∪ L2 can not be constructed using DPDA because it is an NCFL, therefore,
DCFL is not closed under union operation.
Overall we can conclude that CFL is closed under union operation.

3. Intersection Operation:
Let L1 and L2 are two Context free languages –

L1 intersection L2 symbolizes the common part of both languages, i.e., L1 and


L2. We conclude the common part of L1 and L2 is not a context-free language,
hence intersection operation is not closed under intersection operation.
If CFL is not closed under intersection operation, then it is must that DCFL is
also not closed under intersection operation.

4. Concatenation Operation:
Let L1 and L2 are two Context free languages -
L1.L2 is a concatenation of L1 and L2. It is also a context-free language, i.e.,
CFL, because it is accepted by PDA using the logic, ' p ' is pushed into the stack
' n ' times till the ' q ' is encountered once ' q ' is encountered. We perform pop
operation ' n ' times so that ' q ' will pop out all the ' p ' till ' r ' is encountered, once
' r ' is encountered, then we perform pop operation ' m ' times into the stack ' m '
times and push all the ' r ' into the stack till ' s ' is encountered once ' s ' is
encountered. We perform pop operation into the stack ' m ' times and pop out all
the ' r ' until the stack becomes empty. Hence in this way, L1.L2 is accepted by
PDA.
Hence CFL is closed under concatenation operation.

5. Kleen Closure:
Let L1 is any context free Language -

Here, we know that L1 is Context-free language and L2 is also a Context-free


language because there exists a PDA which accepts the Language L2 that is Kleen
closure of L1, hence Context-Free Languages is closed under Kleen closure
operation.

Decision properties for CFL:


Decision properties are the properties derived to check whether the problem
is decidable or not.
To check the decidability of a problem, we use the Decision properties of CFG in
which we apply a Context-Free Grammar through the below three properties. The
decision properties consist of three properties. If that CFG will pass from
these three properties, then it is said that the problem is decidable.
Three Decision properties of Context-Free Grammar –
1. Emptiness Problem
2. Finiteness Problem
3. Membership Problem
Let us see in detail about these properties –
1. Emptiness Problem –
In the Emptiness decision property, from this property, we check whether the
Context-free Grammar can able generate any Language or not.
OR
The given Context-free Grammar can generate the strings from the given context-
free Grammar or not.
If the Grammar cannot derive or produce any string from it, then that Grammar
is said to be Empty Grammar.
Procedure to find the CFG is empty or not:
Steps to be followed –
a. Find and Remove all the Useless symbols from the Context-free Grammar
b. If you find the Start symbol in the set of useless symbols, then that
Grammar is empty.
c. If you cannot find the start symbol in the set of useless symbols, try to
generate any of the strings from that Grammar after removing all useless
symbols.
d. If it can generate a string, then that Grammar is non-empty; otherwise said
to be an empty grammar.

Example:
Check whether the given Context-free Grammar is empty or not.
S → AB | a
A→ a
B → bB
C→ a
Solution :
Here we would separate the set of terminals and non-terminals.
T={a,b}
NT or V = { S , A , B , C }
Let us check one by one starting from downwards -
C→ a
C derives a terminal that is ‘ a ' but is not reachable from the start symbol;
hence, symbol C is useless.
B → bB
B → BBB
B → bbbB
Here we can conclude that it cannot derive any string; hence symbol B is
useless.
A→ a
A can be useful; let us check further.
S → AB
S → aB
We can conclude here that B is a useless symbol, and from the starting symbol,
A is arriving with B, and B is not reaching any terminal; hence, symbol A is also
a useless symbol.
S → a
It is only a useful symbol.
Let us make a set of useful symbols and a set of useless symbols.
Useful symbols = { S }
Useless symbols = { A, B , C }

Final grammar generated is –


S→ a
Here we see that symbol S is a starting symbol and does not belong to the set of
useless symbols; hence, this Grammar is non-empty.
2. Finiteness Problem –
In the Finiteness decision property, from this property, we check whether the
language generated from the given Context-free Grammar is finite or not.
OR
By using this decision property, we can check whether the given Context-free
Grammar can generate a finite number of strings or not.
If the given Context-free Grammar can generate the language that contains a finite
number of strings, then that Grammar is said to be a Finite Grammar.
L ( CFG ) = Finite or not?
Procedure to Check the given Context-free Grammar is Finite or not –
1. Convert the Grammar into Chomsky Normal form (CNF).
2. After converting the Grammar into the CNF form then draw the CNF graph.
3. Make all Non-terminals or variables independent nodes of the graph.
4. After making notes, then make the edges from the nodes that are directed
towards another node
5. Please do not repeat the edges once you have marked them in the graph.
6. After constructing the whole graph, then check whether the cycle is present in
the graph or not.
7. If there is any cyclic-like structure in the graph, then the language generated
by that Grammar is not finite.
8. If the cyclic like structure is not present in the graph, then the language
generated by that Grammar is finite.

Example: Check whether the given Grammar is finite or not?


S → AB / a
A → BC / a
B → CC / b
C→a
Solution :
Steps to check the Grammar is finite or not –
1. Simplification of the Grammar –
To simplify the above Grammar, we need to follow the below steps–
i. Remove all the Epsilon ( Є ) productions –
Identification of epsilon ( Є ) production is
P → AB / Є
As we see in the above Grammar, there is no epsilon ( Є ); hence we do not need
to remove any epsilon production.
i. Remove all unit productions –
Identification of unit production is
P→A
As we see in the above Grammar, there is no unit production; hence we do not
need to remove any unit production.
i. Remove all the useless symbols –
Set of terminals = { a , b }
Set of Non-terminals = { S , A , B , C }
Let us check from downwards –
C→a
Symbol ‘C‘cannot be useless because it reaches the terminal, i.e., ‘a‘, and also
arrives in ‘B‘ for deriving any string.
B → CC
B→b
By applying further substitutions –
B → aC
B → aa
Here we can see that B is either reaching terminal a or terminal b; it is also
reachable from the starting symbol; hence symbol B cannot be useless.
A → BC
A→a
By applying further substitutions –
A → bC
A → ba
Or
A → BC
A → CCC
A → aCC
A → aaC
A → aaa
Here we can see that A is reaching terminal a; it is also reachable from the starting
symbol; hence symbol A cannot be useless.
S → AB
S→a
Here we see that symbol S, which is starting symbol, is reaching the terminal, i.e.,
a and from A and B, further productions can be possible; hence S cannot be
useless.
Now, dividing the set of useless symbols and useful symbols are –
Set of Useful symbols = { S , A, B , C }
Set of Useless symbols = { ɸ }
All the Non-terminals or variables are present in the above Context-free grammar
is useful, not a single variable is useless
Final simplified Grammar generated is –
S → AB / a
A → BC / a
B → CC / b
C→a
2. Compare the Grammar with the CNF form and change it into the required
CNF form –
CNF form are as follows –
P QR
P a
Now, comparing it with our simplified Grammar –
S → AB
S→a
A → BC
A→b
B → CC
B→b
C→a
We can easily observe that all the productions in the above grammar is matching
with the syntax of CNF form.
Here, we observe that our simplified Grammar is already is in the CNF form.
3. Now, Drawing the CNF Graph by converting the non-terminals or variables
into nodes and deriving arrows behaves as an edge of the graph. Here the graph
is follows -

Here, from the above CNF graph, we can observe that the above CNF Graph is
not forming any cycle like structure or don not producing any loop.
Hence we can conclude that the Grammar obtained which is –
S → AB / a
A → BC / a
B → CC / b
C→a
It is a finite Context-free Grammar.
3. Membership Problem –
In this membership decision property, we check whether the given string of any
Context-free Grammar is a member of the Grammar or not.
Or
Here, any random string is given to us from a particular Context-free grammar;
we will check whether the given string is generated from that Grammar is a
member of that Grammar or not.
To solve this problem, we will use CYK (Cocke-Younger-Kasami) algorithm.
Let us say, ‘ w ’ be any random string from any particular Context-free Grammar

We need to check in this -
w Є L (CFG ) or not?
Procedure to Check the given string from the Context-free Grammar is member
of that CFG or not –
1. Convert the Grammar into Chomsky Normal form ( CNF ).
2. After converting the given Context-free Grammar in the Chomsky Normal
form ( CNF ), we will apply the CYK algorithm.
3. CYK Algorithm is only applicable to the Context-free Grammar in the
Chomsky Normal Form ( CNF ).
CYK Algorithm:
Points to remember for CYK Algorithm –
1. The CYK Algorithm is a bottom-up parsing algorithm.
2. As the height of the table increases, the number of productions increases.
3. For the nth row, we are required to apply n-1 productions.
The procedure of using CYK Algorithm –
 Check the length of a given string.
 Construct the table using that length; let say the length of the string is ‘n’.
 Make ' n ' number of columns and ' n ' number of rows in the table, but
consider one thing that height of the table is ' n ' for the first column, height
will be ' n-1 ' for the second column, and so on, in the last column, the
height of the table will be one.
 After constructing the outlines of the table, then write the corresponding
terminals of the strings on the top of the table.
 Now, start from the first row and check the string's first terminal in the
Context-free Grammar of CNF form.
 If you find that terminal in the Grammar, check its correspondingly
Variable or Non-terminal present on the left-hand side. Mark that Variable
in the first field of the first row.
 If you find that, two Variables contain the same terminal. Now, mark the
whole as a set in that field.
 Fill the first row in the same way as mentioned above.
 For the first field of the second row, multiply the two fields, i.e., the first
field is just above the current field, and the second field is adjacent to the
first field.
 Do not change the order of variables after multiplication. Check each
multiplied value in the Context-free Grammar of CNF form and find the
variables present in the Grammar.
 Then, mark all the Corresponding variables from the left-hand side in
Context-free Grammar of CNF form in the specific field of the second row.
 Similarly, in the way mentioned above, fill all the fields of the second row
in the table.
 You need to multiply two times for the first field of the third row because
two levels are increased.
 For first multiplication, multiply the first field elements of the first row
with the second field elements of the second row.
 For second multiplication, multiply the elements of the first field of the
second row with the element of the second field of the first row, moving in
a diagonal direction.
 After that, match all the non-terminals with the CNF form of Context-free
Grammar and if you find that any of the elements matches, then write the
corresponding variables in the specified field.
 Similarly, in this way, we will fill the second third.
 As you move downwards, as the level increase, you have to fill all the fields
in the same way.
 As shown in the figure below, fill all the fields of the respective rows.

 In the circles, you need to place the respective results after the
multiplication of corresponding fields.
4. After applying the CYK Algorithm, match the last field of the table with the
CNF form of given Context-free Grammar, find whether one of the Variable or
non-terminal from the obtained set is the Grammar's start symbol.
5. If one of the Variables is starting symbol, then conclude that the given string is
the member of given Context-free Grammar.
6. Otherwise, the given string is not a member of the given Context-free
Grammar.
Example:
Verify whether the string ' bab ' is a member of the given Context-free Grammar
or not?
S → AB / a
A → BC / a
B → CC / b
C→a
Solution:
Steps to check the Grammar is finite or not –
1. Simplification of the Grammar –
To simplify the above Grammar, we need to follow the below steps
i. Remove all the Epsilon ( Є ) productions –
Identification of epsilon ( Є ) production is
P → AB / Є
As we see in the above Grammar, there is no epsilon ( Є ); hence we do not need
to remove any epsilon production.
i. Remove all unit productions –
Identification of unit production is
P→A
As we see in the above Grammar, there is no unit production; hence we do not
need to remove any unit production.
i. Remove all the useless symbols –
Set of terminals = { a , b }
Set of Non-terminals = { S , A , B , C }
Let us check from downwards –
C→a
Symbol ‘ C ‘ cannot be useless because it reaches the terminal, i.e., ‘ a ‘, and
arrives in ‘ B ‘ to derive any string.
B→b
Here we can see that ‘ B ‘ is either reaching terminal a or terminal b; it is also
reachable from the starting symbol; hence symbol B cannot be useless.
A→a
Here we can see that A is reaching terminal a; it is also reachable from the starting
symbol; hence symbol A cannot be useless.
S → AB
S→a
Here we see that symbol S, which is starting symbol, is reaching the terminal, i.e.,
a and from A and B, further productions can be possible; hence S cannot be
useless.
Now, dividing the set of useless symbols and useful symbols are –
Set of Useful symbols = { S , A, B , C }
Set of Useless symbols = { ɸ }
All the Non-terminals or variables are present in the above Context-free Grammar
is useful; not a single variable or non-terminal is useless.
Final simplified Grammar generated is –
S → AB / a
A → BC / a
B → CC / b
C→a
2. Compare the Grammar with the CNF form and change it into the required
CNF form –
CNF form are as follows –
P QR
P a
Now, comparing it with our simplified Grammar –
S → AB
S→a
A → BC
A→b
B → CC
B→b
C→a
We can easily observe that all the productions in the above Grammar match the
syntax of the CNF form.
Here, we observe that our simplified Grammar is already is in the CNF form.
3. Now, applying the CYK Algorithm –
1) Constructing the table –

2) Let us fill all the fields of corresponding rows using the CNF form of given
Context-free Grammar –
S → AB / a
A → BC / a
B → CC / b
C→a
 For Row 1 –
o Now for first field x11, finding ' b ' in the CNF form of given Context-
free Grammar. We can find the ‘ b ' on the right-hand side with the
corresponding variable ' B ' present on the left-hand side.
o Now similarly for second field x12, finding ' a ' in the above Context-
free Grammar. We can find the ' a ' on the right-hand side with
corresponding variables ' S ', ' A ' and ' C ' present on the left-hand
side.
o And same for third field x13, finding ' b 'in the above Context-free
Grammar. Similarly, as the first field, we were able to find the
corresponding Variable, i.e., ‘ B ’.
 For Row 2-
 For the first field, i.e., x21, we need to multiply the just above field, i.e., the
first field of the first row ( x11 ), with the adjacent field, i.e., the second field
of the first row
( x12 ).
 { B } x { S , A , C } results out as –
{ BS, BA, BC }
Now finding the above set of elements in the above Grammar, we observe that
only BC matches the corresponding variable ‘ A ’ on the left-hand side.
 Similarly, for the second field, i.e., x22, we need to multiply the just above
field, i.e., the second field of the first row ( x12 ), with the adjacent field,
i.e., the third field of the first row ( x13 ).
 { S , A , C } x { B } results out as –
{ SB, AB, CB }
Now finding the above set of elements in the above Grammar, we observe that
only AB matches the corresponding variable ‘ S ’ on the left-hand side.
 For Row 3 –
o For the remaining field, i.e., x31, we need two productions, first is the
multiplication of the first field of the first row, i.e., x11, with the second
field of just above the row of row 3, i.e., second row, i.e., x22. The second
is the multiplication of the first field of the second row, i.e., x21, with the
third field of the first row, i.e., x13.
o Two productions are as follows –
o { B } x { S } = { BS }
o { A } x { B } = { AB }
Now finding the above set of elements in the above Grammar, we observe that
only AB matches the corresponding variable ‘ S ’ on the left-hand side.
4. Here we find that the variable present at the last field of the last row is the start
symbol.
5. Hence the given string, i.e., ‘bab', is the member of the Given Context-free
Grammar.

Deciding properties of CFL:


The decision properties and the deciding properties of CFLs are the same. They
are the properties that can be decided by an algorithm. The only difference is that
the decision properties are usually phrased in terms of yes/no questions, while the
deciding properties are phrased in terms of algorithms.
For example, the membership problem is the decision property of whether a given
string is in a given CFL. The algorithm for solving the membership problem is
the CYK algorithm. So, the CYK algorithm is a deciding property of CFLs.
The other decision properties of CFLs are the emptiness problem, the finiteness
problem, the equality problem, the containment problem, the intersection
problem, the union problem, the difference problem, and the complement
problem. The algorithms for solving these problems are more complex than the
CYK algorithm, but they are all decidable.
The undecidable deciding properties of CFLs are the complement problem, the
intersection problem, the union problem, the difference problem, and the
containment problem. There is no algorithm that can solve these problems for all
CFLs.

THE END

You might also like