Context-free languages (CFLs) are languages generated by context-free grammars (CFGs) or equivalently accepted by push-down automata (PDA). A CFG has productions of the form A → ρ where A is a non-terminal (A ∈ N) and ρ is a string from (T ∪ N)* (terminals ∪ non-terminals).
Important additional facts
Deterministic CFLs (DCFLs) are those recognised by a deterministic push-down automaton (DPDA). A DPDA has at most one transition possible from a given state with a given input symbol and top-of-stack symbol (no nondeterministic choices).
Key points about DCFLs:
Examples from the earlier section:
Practical constructions
Question : Consider the language L1,L2,L3 as given below.
L1 = { ambn | m, n ≥ 0 }
L2 = { anbn | n ≥ 0 }
L3 = { anbncn | n ≥ 0 }
Which of the following statements is NOT TRUE?
A. Push Down Automata (PDA) can be used to recognize L1 and L2
B. L1 is a regular language
C. All the three languages are context free
D. Turing machine can be used to recognize all the three languages
Solution : Option (A) says PDA can be used to recognize L1 and L2. L1 contains all strings with any number of a followed by any number of b. So, it can be accepted by PDA. L2 contains strings with n number of a's followed by n number of b's. It can also be accepted by PDA. So, option (A) is correct.
Option (B) says that L1 is regular. It is true as regular expression for L1 is a*b*.
Option (C) says L1, L2 and L3 are context free. L3 languages contains all strings with n number of a's followed by n number of b's followed by n number of c's. But it can't be accepted by PDA. So option ( C) is not correct.
Option (D) is correct as Turing machine can be used to recognize all the three languages.
Question : The language L = { 0i12i | i ≥ 0 } over the alphabet {0, 1, 2} is :
A. Not recursive
B. Is recursive and deterministic CFL
C. Is regular
D. Is CFL bot not deterministic CFL.
Solution : The above language is deterministic CFL as for 0's, we can push 0 on stack and for 2's we can pop corresponding 0's. As there is no ambiguity which moves to take, it is deterministic. So, correct option is (B). As CFL is subset of recursive, it is recursive as well.
Question : Consider the following languages:
L1 = { 0n1n| n≥0 }
L2 = { wcwr | w ɛ {a,b}* }
L3 = { wwr | w ɛ {a,b}* }
Which of these languages are deterministic context-free languages?
A. None of the languages
B. Only L1
C. Only L1 and L2
D. All three languages
Solution : Language L1 contains all strings in which n 0's are followed by n 1's. Deterministic PDA can be constructed to accept L1. For 0's we can push it on stack and for 1's, we can pop from stack. Hence, it is DCFL.
L2 contains all strings of form wcwr where w is a string of a's and b's and wr is reverse of w. For example, aabbcbbaa. To accept this language, we can construct PDA which will push all symbols on stack before c. After c, if symbol on input string matches with symbol on stack, it is popped. So, L2 can also be accepted with deterministic PDA, hence it is also DCFL.
L3 contains all strings of form wwr where w is a string of a's and b's and wr is reverse of w. But we don't know where w ends and wr starts. e.g.; aabbaa is a string corresponding to L3. For first a, we will push it on stack. Next a can be either part of w or wr where w=a. So, there can be multiple moves from a state on an input symbol. So, only non-deterministic PDA can be used to accept this type of language. Hence, it is NCFL not DCFL.
So, correct option is (C). Only, L1 and L2 are DCFL.
Question : Which one of the following grammars generate the language L = { aibj | i ≠ j }
S → AC | CB, C → aCb | a | b, A → aA | ε, B → Bb | ε
S → aS | Sb | a | b
S → AC | CB, C → aCb | ε, A → aA | ε, B → Bb | ε
S → AC | CB, C → aCb | ε, A → aA | a, B → Bb | b
Solution : The best way to solve these type of questions is to eliminate options which do not satisfy conditions. The conditions for language L is no. of a's and no. of b's should be unequal.
In option (B), S ⇒ aS ⇒ ab. It can generate strings with equal a's and b's. So, this option is incorrect.
In option (C), S ⇒ AC ⇒ C ⇒ ε. In ε, a's and b's are equal (0), so it is not correct option.
In option (A), S will be replaced by either AC or CB. C will either generate number of a's more than number of b's by 1 or number of b's more than number of a's by 1. But one more a or one more b can be compensated by B → bB | ε or A → aA | ε respectively. So it may give strings with equal number of a's and b's. So, it is not a correct option.
In option (D), S will be replaced by either AC or CB. C will always generate equal number of a's and b's. If we replace S by AC, A will add at least one extra a and if we replace S by CB, B will add at least one extra b. So this grammar will never generate equal number of a's and b's. So, option (D) is correct.
Determining whether languages (or decision problems) are decidable, partially decidable (recursively enumerable), or undecidable is a central topic in computability theory. The following definitions are standard and used frequently.
Method: reductions
A standard method to show undecidability of a problem P2 is to reduce a known undecidable problem P1 to P2. If you can show that solving P2 (with a hypothetical decider) would allow you to solve P1, then P2 must also be undecidable. The halting problem is the canonical undecidable problem frequently used for reductions.
Example 1 - State Entry Problem
Problem statement - Given a Turing machine M and an input w, determine whether a particular state Q of M is ever reached during the computation on w.
Reduction idea from the Halting problem
Modify the machine M so that every originally undefined transition (which would have caused halting) is instead redirected to move to a new distinguished state Q. Then Q is reached if and only if the original machine would have halted.
If there were a decider for the State Entry problem that always halts and answers whether Q is reachable from the configuration on input w, then using the above transformation we could decide the Halting problem. Since the Halting problem is undecidable, the State Entry problem must also be undecidable.
Example 2 - Intersection membership for regular languages
Problem statement - Given two regular languages L1 and L2, decide whether there exists a string w that is in both L1 and L2.
Reasoning
Because regular languages are represented by DFAs, construct Turing machines TM1 and TM2 that simulate the DFAs for L1 and L2 respectively. Each TM will always halt on any input because DFAs always halt. To decide whether there exists some w ∈ L1 ∩ L2, we can algorithmically enumerate all strings (in some standard order) and simulate TM1 and TM2 on each string; whenever both accept, output "yes". This procedure will halt iff such a string exists. Alternatively, form the product DFA for intersection and check its language for emptiness; emptiness for regular languages is decidable. Therefore the problem is decidable.
Remarks and exam tips
Concluding summary
Context-free languages have useful closure properties: they are closed under union, concatenation, Kleene star and intersection with regular sets, but not under arbitrary intersection or complementation. Deterministic CFLs form a strict subclass that is closed under complementation but not under many other operations. Decidability questions are settled by constructive algorithms when they exist or by reductions to/from known undecidable problems when they do not. Regular practice with constructions and reductions develops the intuition and speed required to handle typical examination problems in theory of computation.
| 1. What is a context-free grammar? | ![]() |
| 2. What is a push-down automaton? | ![]() |
| 3. How are context-free grammars and push-down automata related? | ![]() |
| 4. What is the importance of context-free grammars and push-down automata in computer science? | ![]() |
| 5. Can you give an example of a context-free grammar and its corresponding push-down automaton? | ![]() |
![]() | Explore Courses for Computer Science Engineering (CSE) exam |
![]() | Get EduRev Notes directly in your Google search |