# Context free languages and Push-down automata

Question 1 |

L2 is context-free. | |

L1 intersection L2 is context-free. | |

Complement of L2 is recursive. | |

Complement of L1 is context-free but not regular. |

**GATE CS 2013**

**Context free languages and Push-down automata**

**Discuss it**

Question 1 Explanation:

(D) is false.
L1 is regular, so its complement would also be regular.
L1 is a regular language of the form 0^* 1^* 0^*. L2 on the other hand is a CFL as it can be derived from the following CFG
L2 = { 0^p 1^q 0^r | p,q,r>0 And p notEqualTo r }
S -> AC|CA
C -> 0C0|B
A -> 0A|0
B -> 1B|epsilon
If coming up with a CFG for L2 is difficult, one can intuitively see that by reducing it to a simpler problem. L2 is very similar to a known CFL L3 = { a^m b^l | m notEqualTo n }

**(A)**L2 is context free, which is true [CORRECT]**(B)**L1 intersection L2 is context free, which is again true because L1 is a regular language and L2 is a CFL. RL union CFL is always a CFL. Hence [CORRECT]**(C)**Complement of L2 is recursive, which is true due to the fact that complement of a CFL is CSL for sure (Context sensitive language), which in turn (CSL) is a subset of recursive languages. Hence [CORRECT]**(D)**Complement of L1 is context free but not regular, which is false due to closure laws of regular languages. Complement of a RL is always a RL. Hence [INCORRECT] This solution is contributed by**Vineet Purswani**.Question 2 |

Which of the following pairs have DIFFERENT expressive power?

Deterministic finite automata(DFA) and Non-deterministic finite automata(NFA) | |

Deterministic push down automata(DPDA)and Non-deterministic push down automata(NPDA) | |

Deterministic single-tape Turing machine and Non-deterministic single-tape Turing machine | |

Single-tape Turing machine and multi-tape Turing machine |

**GATE CS 2011**

**Context free languages and Push-down automata**

**Discuss it**

Question 2 Explanation:

NDPDA can handle languages or grammars with ambiguity, but DPDA cannot handle languages with ambiguity and any context-free grammar.

Question 3 |

1) Let P be a regular language and Q be context-free language such that Q P. (For example, let P be the language represented by the regular expression p*q* and Q be {p

^{n}q^{n}|n N}). Then which of the following is ALWAYS regular? (A) P Q (B) P - Q (C) * - P (D) * - QA | |

B | |

C | |

D |

**GATE CS 2011**

**Context free languages and Push-down automata**

**Discuss it**

Question 3 Explanation:

1. P ∩ Q would be Q, due to the given fact that Q ⊆ P, hence context free but not regular.
2. P − Q = P ∩ Q might not even be a context free language, due to the closure properties of context free languages.
3. Σ∗ − P is equivalently complement of P, hence regular. Refer to closure laws of regular languages.
4. Σ∗ − Q is equivalently complement of Q, hence it might not even be a context free language.
Refer to closure laws of CFLs.
Reference: http://quiz.geeksforgeeks.org/theory-of-computation-closure-properties-of-context-free-languages/
See http://www.geeksforgeeks.org/automata-theory-set-4/
This solution is contributed by

**Vineet Purswani.**Question 4 |

2) Consider the language L1,L2,L3 as given below.
L1={ | p,q N}
L2={ | p,q N and p=q}
L3={ | p,q,r N and p=q=r}
Which of the following statements is

**NOT TRUE**?Push Down Automata (PDA) can be used to recognize L1 and L2 | |

L1 is a regular language | |

All the three languages are context free | |

Turing machine can be used to recognize all the three languages |

**GATE CS 2011**

**Context free languages and Push-down automata**

**Discuss it**

Question 4 Explanation:

L1 is regular. Its DFA is given as

L2 is not regular, can be proved using pumping lemma (refer to Ullman). But L2 is CFL.

S → AB A → 0A|ε B → 1B|εL3 is not CFL, can be proved using pumping lemma (refer to Ullman). But L3 is Recursive. Every regular language is also a CFL. So PDA can be used to recognized L1 and L2. As a CFL and Regular language is algo a Recursive language. Hence, Turing machine can be used to recognise L1, L2 and L3. L2 is not regular, can be proved using pumping lemma (refer to Ullman). But L2 is CFL.

S → AB A → 0A|ε B → 1B|εL3 is not CFL, can be proved using pumping lemma (refer to Ullman). But L3 is Recursive. Every regular language is also a CFL. So PDA can be used to recognised L1 and L2. As a CFL and Regular language is algo a Recursive language. Hence, turing machine can be used to recognise L1, L2 and L3. Source: http://clweb.csa.iisc.ernet.in/rahulsharma/gate2011key.html

Question 5 |

Consider the languages
L1 = {0

^{i}1^{j}| i != j}. L2 = {0^{i}1^{j}| i = j}. L3 = {0^{i}1^{j}| i = 2j+1}. L4 = {0^{i}1^{j}| i != 2j}.Only L2 is context free | |

Only L2 and L3 are context free | |

Only L1 and L2 are context free | |

All are context free |

**GATE CS 2010**

**Context free languages and Push-down automata**

**Discuss it**

Question 6 |

S -> aSa|bSb|a|b; The language generated by the above grammar over the alphabet {a,b} is the set of

All palindromes | |

All odd length palindromes. | |

Strings that begin and end with the same symbol | |

All even length palindromes |

**GATE-CS-2009**

**Context free languages and Push-down automata**

**Discuss it**

Question 6 Explanation:

The possible palindrome generated by above grammar can be of odd length only as there is no rule for S ->
For example generated palindromes are aba, aaa, bab, ababa, aaaaa, ..

Question 7 |

Let L = L1∩L2, where L1 and L2 are languages as defined below:

L1 = { | m, n >= 0 } L2 = { | i, j, k >= 0 }Then L is

Not recursive | |

Regular | |

Context free but not regular | |

Recursively enumerable but not context free. |

**GATE-CS-2009**

**Context free languages and Push-down automata**

**Discuss it**

Question 7 Explanation:

The language L1 accept strings {c, abc, abcab, aabbcab, aabbcaabb, …} and L2 accept strings {a, b, c, ab, abc, aabc, aabbc, … }. Intersection of these two languages is which is context free, but not regular.

Question 8 |

The language L= {0

^{i}21^{i}| i≥0 } over the alphabet {0,1, 2} is:not recursive | |

is recursive and is a deterministic CFL. | |

is a regular language. | |

is not a deterministic CFL but a CFL. |

**Context free languages and Push-down automata**

**GATE-CS-2007**

**Discuss it**

Question 8 Explanation:

Let us first

**design a deterministic pushdown automata**for the given language.- For each occurrence of ‘0’ , we PUSH X in the stack.
- When ‘2’ appears, no stack operation is performed. But, state of the automata is changed.
- For each occurrence of ‘1’ , we POP X from the stack.
- If at the end Z
_{0}is on the stack top then input string is accepted

**design a Turing machine**for the given language.- When ‘0’ appears in the input string , we replace it with X .Then, traverse to the rightmost corner and replace ‘1’ with Y.
- We go back to the leftmost ‘0’ and repeat the above process.
- While traversing rightwards from the beginning of the input string, if after X, ‘2’ appears and after ‘2’, Y appears then we reach the HALT state.
**Thus, the given language is recursive. Every recursive language is a CFL.****Thus, option (B) is the answer.**Please comment below if you find anything wrong in the above post.

Question 9 |

Consider the CFG with {S,A,B) as the non-terminal alphabet, {a,b) as the terminal alphabet, S as the start symbol and the following set of production rules

S --> aB S --> bA B --> b A --> a B --> bS A --> aS B --> aBB A --> bAAWhich of the following strings is generated by the grammar?

aaaabb | |

aabbbb | |

aabbab | |

abbbba |

**Context free languages and Push-down automata**

**GATE-CS-2007**

**Discuss it**

Question 9 Explanation:

Given below production rules.

S --> aB S --> bA B --> b A --> a B --> bS A --> aS B --> aBB A --> bAAWe can derive

**aabbab**using below sequenceS -> aB [Using S --> aB] -> aaBB [Using B --> aBB] -> aabB [Using B --> b] -> aabbS [Using B --> bS] -> aabbaB [Using S --> aB] -> aabbab [Using B --> b]

Question 10 |

For the correct answer strings to above question, how many derivation trees are there?

1 | |

2 | |

3 | |

4 |

**Context free languages and Push-down automata**

**GATE-CS-2007**

**Discuss it**

Question 10 Explanation:

When it asks about the no of derivations tree, we should consider either left most derivation(LMD) or right most derivations(RMD), but not both.
Here two left most derivations are possible for the correct string of the previous question "aabbab" from the given grammar.

**LMD-1**S -> aB [Using S --> aB] -> aaBB [Using B --> aBB] -> aabB [Using B --> b] -> aabbS [Using B --> bS] -> aabbaB [Using S --> aB] -> aabbab [Using B --> b]**LMD-2**S -> aB [Using S --> aB] -> aaBB [Using B --> aBB] -> aabSB [Using B --> bS] -> aabbAB [Using S --> bA] -> aabbaB [Using A --> a] -> aabbab [Using B --> b] The Derivation tress are shown below :There are 93 questions to complete.