100% found this document useful (1 vote)
113 views

Homework By:: Pratik Pranav - 2018CS10368 Sarthak Behera - 2018CS10384 Shreyans Nagori - 2018CS10390

This document contains instructions for homework 3 in COL352. It includes 4 problems: 1. Proving that a grammar G generates all strings with twice as many a's as b's. This is done by induction on string length. 2. Showing that any language recognized by a PDA is also recognized by an empty stack PDA, and vice versa. Constructions of the corresponding automata are described. 3. Proving that the class of context-free languages is closed under intersection with regular languages, using a product construction. 4. Determining whether the languages L1={ambncmdn} and L2={ambncmdn} are context-free or not,

Uploaded by

pratik pranav
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
100% found this document useful (1 vote)
113 views

Homework By:: Pratik Pranav - 2018CS10368 Sarthak Behera - 2018CS10384 Shreyans Nagori - 2018CS10390

This document contains instructions for homework 3 in COL352. It includes 4 problems: 1. Proving that a grammar G generates all strings with twice as many a's as b's. This is done by induction on string length. 2. Showing that any language recognized by a PDA is also recognized by an empty stack PDA, and vice versa. Constructions of the corresponding automata are described. 3. Proving that the class of context-free languages is closed under intersection with regular languages, using a product construction. 4. Determining whether the languages L1={ambncmdn} and L2={ambncmdn} are context-free or not,

Uploaded by

pratik pranav
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/ 4

COL352 Homework 3

Release date: March 26, 2021 Deadline: April 2, 2021: 23:00

Homework by:
Pratik Pranav - 2018CS10368
Sarthak Behera - 2018CS10384
Shreyans Nagori - 2018CS10390

Read the instructions carefully.

1. [1 mark] Consider the grammar G = ({S}, {a, b}, R, S), where the set of production rules is

R = {S −→ SS, S −→ aaSb, S −→ bSaa, S −→ aSbSa, S −→ ε}.

Obviously, the language L generated by this grammar contains only those strings in which the number
of a’s is twice the number of b’s. (If you are unable to prove the last statement rigorously, you must
contact the instructor asap.) Prove that, in fact, L contains all strings in which the number of a’s is
twice the number of b’s.

The main idea we will use the below proof is concerned with prefixes. Let na (w) denote the number
of a in string w while nb (w) denotes the number of b in string w. Now let M be the set of all strings
such that w ∈ M iff na (w) = 2nb (w) and w ∈ {a, b}∗ . Thus M contains all strings with twice as
many a’s as b’s. We prove that for any string w ∈ M , we also have w ∈ L (where L is defined as
in the question). We proceed by induction on the length of string w. Suppose that for any string
w ∈ M with length < n also belongs to L. The base case is verified for the empty string. Now we
come to the induction step. For any string w ∈ M (with length n), consider all the prefixes of w
and compute the value na (p) − 2nb (p) where p is a prefix of w. If any such prefix p (s.t p 6= w and
p 6= ) has value 0 then w can be decomposed into pq where na (p) − 2nb (p) = na (q) − 2nb (q) = 0.
Now both p and q have length < n, so by induction hypothesis, both p and q are in L and the rule
of production S −→ SS implies that w = pq also belongs to L. Then we would be done. Now
another case to consider is when na (p) − 2nb (p) is never 0 (except for p = w and p = ). There are
3 subcases to consider here.
1. The value of na (p) − 2nb (p) remains positive as we traverse the string w. As it is positive for
the 1−letter prefix, the first symbol must be a. In fact the second symbol must also be a (as
it can’t be b lest na (p) − 2nb (p) will become negative for 2−letter prefix). It also becomes 0 in
the end after remaining positive so the last symbol must be b. So w is of the form aaxb. But x
has length < n and is in M so it also belongs to L by induction hypothesis. Rule S −→ aaSb
implies w is in L.
2. The value of na (p) − 2nb (p) remains negative as we traverse the string w. As it is negative for
the 1−letter prefix, the first symbol must be b. It becomes 0 in the end thus last symbol must
be a. In fact the 2nd last symbol also must be a (it can’t be b which would contradict that
prefix value remains negative). Thus w is of the form bxaa. But x has length < n and is in M
so it also belongs to L by induction hypothesis. Rule S −→ bSaa implies w is in L.
3. The value of na (p) − 2nb (p) first has positive value but then jumps to negative values without
touching 0 value. This jump is made possible as addition of 1 b decreases value by 2. So we
can jump from 1 to -1 without touching 0. Note that vice-versa is not possible as increase
in value occurs due to addition of a so it only increases 1 at a time. As the value is initially
positive, we infer that first symbol is a. There is a jump from 1 to -1 in between the string
so there must be a b somewhere in the middle. Value becomes 0 in the end after being in the
negative so the last symbol must be a. Thus w is of the form axbya. But x and y has length
< n and is in M so it also belongs to L by induction hypothesis.
Note that these are the only 3 subcases to consider when prefix values doesn’t touch 0 (we observe
that each rule specifically handled each subcase). Thus we have shown by induction that all w ∈ M
also belong to L and as the language generated by L has twice as many a as b, we conclude that L
is in fact the whole of M .

2. An empty stack pushdown automaton is a tuple P 0 = (Q, Σ, Γ, ∆, q0 , Z) where Q, Σ, Γ, ∆, and q0 have


the same meaning as in definition of a PDA. The last component Z ∈ Γ is the initial stack symbol: it is
present on the stack in the beginning of every run. In other words, the initial instantaneous description
in a run of P 0 on x ∈ Σ∗ is (q0 , x, Z). We say that such an automaton P 0 accepts string x if there is a
run of P 0 on x that ends with an empty stack (and in any state). Formally, P 0 accepts x if there exists
a state q ∈ Q such that (q0 , x, Z) `∗P 0 (q, ε, ε). The language recognized by an empty stack pushdown
automaton P 0 is the set of strings accepted by P 0 .
1. [1 mark] Prove that if a language L is recognized by some PDA P , then it is recognized by some
empty stack PDA P 0 . (Describe a construction of P 0 from P .)
2. [1 mark] Prove that if a language L is recognized by some empty stack PDA P 0 , then it is recognized
by some PDA P . (Describe a construction of P from P 0 .)

1. Now consider P = (Q, Σ, Γ, ∆, q0 ). Consider the empty stack pushdown automata P 0 =


(Q, Σ, Γ, ∆, q0 , ε). We see that any string s accepted by P will have (q0 , x, ε) `∗P (q, ε, ε). As P
0
and P have same transition functions, we see that (q0 , x, ε) `∗P 0 (q, ε, ε) as well, and by accepted
criteria is accepted by P 0 . Reverse follows by a similar logic, that if for any string s such that
0
(q0 , s, ε) `∗P 0 (q, ε, ε), (q0 , x, ε) `∗P (q, ε, ε) holds as well and thus any string accepted by P is ac-
cepted by P. Hence for a language recognised by a PDA P we can construct an other empty stack
pushdown automata as described which recognises the mentioned language.

2. Now consider empty stack pushdown automata P 0 = (Q, Σ, Γ, ∆, q0 , Z), and the pushdown
0 0 0 0
automata P = (Q , Σ, Γ, ∆ , q1 ) such that Q = Q ∪ {q1 } and ∆ = ∆ ∪ (q1, ε, ε, Z), i.e. a new
0
transition is added from the new initial state q1 of P to original initial state q0 of P which has
ε as input and pops ε while adding character Z to the stack of P. Any string accepted by P is
accepted by P 0 and vice versa. Any string accepted by P 0 must have (q0 , x, Z) `∗P 0 (q, ε, ε), and
same condition holds for being accepted by P as well, and hence we proved that for any empty stack
pushdown automata we can construct a corresponding accepting pushdown automata accepting the
same language.

3. [1 mark] Prove that the class of context-free languages is closed under intersection with regular lan-
guages. That is, prove that if L1 is a context-free language and L2 is a regular language, then L1 ∩ L2
is a context-free language.

We use the product construction to create P which accepts L1 ∩L2 . Let P1 be the PDA for language
L1 and D2 be the DFA for language L2 . In terms of instantaneous description of the PDA P , we
have ((q1 , q2 ), w, ∆) ` ((q10 , q20 ), w0 , ∆0 ) where w = aw0 and (q1 , w, ∆) ` (q10 , w0 , ∆0 ) holds for PDA
P1 and (q2 , a, q20 ) is a valid transition for D2 . Similarly we set the valid accepting states to be the
ones which both the states are in the respective accepting states, i.e., F1 × F2 is the accepting states
for P where F1 is a accepting state set for P1 and F2 is the accepting state set for D2 . Thus it is
easy to see that P is a valid PDA thus proving that intersection of CFL and RL is context-free.
The basic idea used was to consider the alphabets for both the PDA and DFA and use the stack
only for the PDA thus simulating both the DFA and PDA in parallel.

4. [1 mark] Consider the following two languages over the alphabet {a, b, c, d}.
L1 = {am bn cm dn | m, n ∈ N ∪ {0}},
L2 = {am bn cn dm | m, n ∈ N ∪ {0}}.
Determine whether each of these languages is context-free, and prove your answer.
1. In this question, we can observe that if we try to construct a PDA for L1 , we will not able to
check the count of c i.e., whether they are equal to m or not. These intuitions lead us to believe
that the language L1 might not be a context-free language. Now, we will be using pumping
lemma for context-free languages to prove that L1 is not a context-free language.
Let p ∈ N be the pumping length for using pumping lemma. Now, given the pumping length
let’s take the string w = ap bp cp dp . According to pumping lemma, w can be decoposed as
(a) w = uvxyz
(b) |vy| ≥ 1
(c) |vxy| ≤ p
(d) i ∈ N ∪ {0}
So, if we show that no matter which substring vxy is choosen and how it splits between v, x
and y considering (b) and (c), there is some i as defined above such that uv i xy i z is not in L1 .
Following cases are possible:
(a) vxy = an for some n such that 1 ≤ n ≤ p
(b) vxy = bn for some n such that 1 ≤ n ≤ p
(c) vxy = cn for some n such that 1 ≤ n ≤ p
(d) vxy = dn for some n such that 1 ≤ n ≤ p
(e) vxy = am bn for some m,n such that 1 ≤ m + n ≤ p
(f) vxy = bm cn for some m,n such that 1 ≤ m + n ≤ p
(g) vxy = cm dn for some m,n such that 1 ≤ m + n ≤ p
(h) vxy = ai bi for some i such that 1 ≤ len(ai bi ) ≤ p and ai bi is a substring of w.
(i) vxy = bi ci for some i such that 1 ≤ len(bi ci ) ≤ p and bi ci is a substring of w.
(j) vxy = ci di for some i such that 1 ≤ len(ci di ) ≤ p and ci di is a substring of w.
For each of the above cases described above and assigning i = 0, we can easily see uv i xy i z ∈
/ L1
as the either constraints of having same number of occurrences of a and c gets violated or con-
straints of having same number of occurrences of b and d gets violated. Hence, there is not
any string vxy such that ∀i ∈ N ∪ {0}, uv i xy i z ∈ L1 .

2. We will prove that L2 is a context-free language by providing a context-free grammar and later
on proving that the language indeed generated by the context free grammar is the language
defined in L2 . Consider the following production:
• S → aSd | S1 | 
• S1 → bS1 c | 
Claim: Any string generated by above grammar belongs to L2 .
Proof: The application of any of the two rules described above always produces a string s with
always having a’s at the start only and d’s at the ends and in between a’s and d’s it is S1 which
is just bn cn . So, in total the language generated by above grammar is always in the form of
am bn cn dm which belongs to L2 .

Claim: Any string belonging to L2 can be generated by above productions.


Proof: Let’s s be any string belonging to L2 . Note that s will be of the form {am bn cn dm | m, n ∈
N ∪ {0}}.. Now, we will just propose a method to produce a way to produce every such s using
the productions provided above. Suppose s = am bn cn dm . String s can be produced as first
using the production S → aSd m times recursively, then S → S1 , then S1 → bS1 c n times
recursively and finally, S1 → .
Hence, the above grammar indeed is the grammar for the required language. So, L2 is indeed
a context-free language.

5. [1 mark] Prove that the language {am bn | n is a multiple of m} ⊆ {a, b}∗ is not context-free.

We will use here pumping lemma for context-free languages to prove that the language L =
{am bn | n is a multiple of m} ⊆ {a, b}∗ is not context-free.
Let p ∈ L be the pumping length for using pumping lemma. Now, given the pumping length let’s
2 3
take the string w = ap bp . Now, According to pumping lemma, w can be decomposed as
1. w = uvxyz
2. |vy| ≥ 1
3. |vxy| ≤ p
4. i ∈ N ∪ {0}
So, if we show that no matter which substring vxy is chosen and how it splits between v, x and y
considering (b) and (c), there is some i as defined above such that uv i xy i z is not in L1 .
Following cases are possible:
1. vxy = an for some n such that 1 ≤ n ≤ p
2. vxy = bn for some n such that 1 ≤ n ≤ p
3. vxy = am bn for some m,n such that 1 ≤ m + n ≤ p
4. vxy = ai bi for some i such that 1 ≤ len(ai bi ) ≤ p and ai bi is a substring of w.
Case 1: For this case, we will use i = 2 for any substring possible of this structure after substituting
2 3
i = 2, our string w becomes w = uv 2 xy 2 z = ap +n bp for 1 ≤ n ≤ p. p3 lies between (p2 + n) ∗ (p − 1)
and (p2 + n) ∗ (p). So, p3 can never be multiple of p2 + n. Hence, after this we can easily see that
p2 + n for any n 1 ≤ n ≤ p doesn’t divide p3 . Hence, for every substring of the form described in
1. above. there is a i such that uv i xy i z ∈
/ L.

Case 2: For this case as well, i = 2 works. Any substring of this structure after substituting i = 2,
2 3
our string w becomes w = uv 2 xy 2 z = ap bp +n for 1 ≤ n ≤ p. p3 + n lies between (p2 ) ∗ (p) and
(p2 ) ∗ (p + 1). So, p3 + n can never be multiple of p2 . Hence, after this we can easily see that p2
for any n 1 ≤ n ≤ p doesn’t divide p3 + n. Hence, for every substring of the form described in 2.
above. there is a i such that uv i xy i z ∈
/ L.

Case 3: Any value of i ≥ 2 works as multiple substrings of ab starts appearing in the string.

Case 4: In this case also i = 2 works. for any substring possible of this structure after substituting
2 3
i = 2, our string w becomes w = uv 2 xy 2 z = ap +n bp +m for 1 ≤ n, m ≤ p. p3 + m lies between
(p2 + n) ∗ (p − 1) and (p2 + n) ∗ (p). So, p3 + m can never be multiple of p2 + n. Hence, after this we
can easily see that p2 + n for any n 1 ≤ n, m ≤ p doesn’t divide p3 + m. Hence, for every substring
of the form described in 4. above. there is a i such that uv i xy i z ∈
/ L.

For each of the cases described above and assigning i = 0,we can easily see uv i xy i z ∈ / L. Hence,
there is not any string vxy such that ∀i ∈ N ∪ {0}, uv i xy i z ∈ L. So, that above described language
is not context-free.

You might also like