# 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. |

**Explanation:**

**(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 |

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 |

**Explanation:**

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

Question 3 |

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) * - Q

A | |

B | |

C | |

D |

**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: https://www.geeksforgeeks.org/closure-properties-of-context-free-languages/ See http://www.geeksforgeeks.org/automata-theory-set-4/ This solution is contributed by **Vineet Purswani. **

Question 4 |

**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 |

**Explanation:**

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 |

^{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 |

Question 6 |

All palindromes | |

All odd length palindromes. | |

Strings that begin and end with the same symbol | |

All even length palindromes |

**Explanation:**

Question 7 |

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. |

**Explanation:**

Question 8 |

^{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. |

**Explanation:**

**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 |

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 |

**Explanation:**

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

**aabbab**using below sequence

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]

Question 10 |

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

1 | |

2 | |

3 | |

4 |

**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 :