Toc 3
Toc 3
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.
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.
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.
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.
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.
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.
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.
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 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.
3. Intersection Operation:
Let L1 and L2 are two Context free languages –
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 -
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 }
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.
THE END