Regular Language Questions
1. Which of the following grammar is Type 2 grammar according to Chomsky
classification of grammar?
(a) aSaS->a,S->b (b) SA ->aA | bBBc| a
(c) aAS->abA, A->λ (d) None of these
Explanation:
According to the Chomsky hierarchy, Type 2 grammar refers to context-free grammars (CFG), where each production rule has the form:
Here, is a single non-terminal symbol, and is a string consisting of terminals and/or non-terminals. The key property is that the left-hand side of the production rule must be a single non-terminal.
========================================================================
2. Consider the following grammar G with productions,
S ->aAbB, aA->bBA| bB, B ->b | λ, then G is (a) Regular grammar (b) Context free grammar (c) Context Sensitive grammar (d) Unrestricted grammar
Explanation:
Context-Free Grammar (CFG):
A grammar is context-free if all production rules are of the form , where is a single non-terminal symbol, and is a string of terminals and/or non-terminals.
- In this grammar, each left-hand side of the production rules starts with a non-terminal, and the right-hand side is a string of terminals and/or non-terminals.
- The rule B → λ is valid for CFGs as context-free grammars allow null (ε) productions.
Thus, the grammar fits the definition of a context-free grammar.
========================================================================
3. How many of the following rules would not be permitted in a context-sensitive grammar?__
1. aA → bbA 2.bA → aB 3.ABb → C 4.ab → B 5. cAAb → aaaa 6. A ->λ
Explanation:
In a context-sensitive grammar, all production rules must satisfy the condition that the length of the string on the left-hand side is less than or equal to the length of the string on the right-hand side, i.e., no shrinking of strings is allowed. Additionally, context-sensitive grammars do not allow the production of the empty string (λ) unless the grammar only produces λ.
Rules analysis:
aA → bbA
- Left-hand side: "aA" (length = 2)
- Right-hand side: "bbA" (length = 3)
- This rule is permitted because the length on the right is greater than the left.
bA → aB
- Left-hand side: "bA" (length = 2)
- Right-hand side: "aB" (length = 2)
- This rule is permitted because the length on both sides is equal.
ABb → C
- Left-hand side: "ABb" (length = 3)
- Right-hand side: "C" (length = 1)
- This rule is not permitted because the right-hand side is shorter than the left (shrinking).
ab → B
- Left-hand side: "ab" (length = 2)
- Right-hand side: "B" (length = 1)
- This rule is not permitted because the right-hand side is shorter than the left (shrinking).
cAAb → aaaa
- Left-hand side: "cAAb" (length = 4)
- Right-hand side: "aaaa" (length = 4)
- This rule is permitted because the lengths are equal.
A → λ
- This rule is not permitted in a context-sensitive grammar, as λ productions (producing the empty string) are not allowed unless the grammar only generates λ.
S-> RST|abc| d, T->cdB | Rab, R->abcR | ab Then G is which type of Chomsky classification of grammar? (a) Type 0 (b) Type 1 (c) Type 2 (d) Type 3
Context-Free Grammar (CFG) - Type 2:
A grammar is context-free if all production rules are of the form:
where is a single non-terminal, and is a string of terminals and/or non-terminals.
In this grammar:
- Each rule has a single non-terminal on the left-hand side (e.g., , , or ).
- The right-hand sides of the production rules are strings consisting of terminals and non-terminals.
Since all the rules conform to the form , this grammar satisfies the conditions of a context-free grammar (Type 2).
=========================================================================
5. Which of the following languages cannot be accepted by an NFA? (i) {akn | n, k ≥ 0 and n is multiple of k} (ii) {(ab)n | n ≥ 0}.{an | n ≥ 0} (iii) {anbbam | n, m ≥ 0} (iv) {anbban | n ≥ 0} (a) i, iv only (b) ii, iii only (c) i, ii, iv only (d) iii only
Explanation:
(i)
- This language describes strings where is a multiple of . For instance, for every , must satisfy for some integer .
- This is a complex relationship between and that requires counting and comparing values of and , which cannot be done by a finite automaton (neither DFA nor NFA) since finite automata cannot perform arithmetic comparisons or count dependencies like "multiples" between two variables.
- Conclusion: This language cannot be accepted by an NFA.
(ii)
- This language consists of two parts: the first part has alternating "ab" pairs repeated times, followed by "a"s.
- This requires the automaton to count how many "ab" pairs have been seen and ensure that the number of "a"s in the second part matches that count. NFAs (and DFAs) cannot count and store the value of without unbounded memory, making it impossible for an NFA to recognize this pattern.
- Conclusion: This language cannot be accepted by an NFA.
(iii)
- This language consists of any number of "a"s followed by two "b"s and then any number of "a"s. NFAs can easily handle patterns where the number of symbols before and after a middle segment (like the "bb") can vary independently.
- Conclusion: This language can be accepted by an NFA.
(iv)
- This language requires the number of "a"s before and after the "bb" to be exactly equal. This is a classic example of a language that requires counting and comparison between two segments (the "a"s before and after the "bb"). Finite automata (including NFAs) cannot count or remember how many "a"s have been seen in one part to compare with the number of "a"s in another part.
- Conclusion: This language cannot be accepted by an NFA.
====================================================================
Statement 1:
If the complement of is non-regular, then is also non-regular.
- This statement is true. By the closure properties of regular languages, if is regular, then its complement is also regular. Therefore, if is non-regular, must also be non-regular.
Statement 2:
If is regular, then must be regular.
- This statement is false. For example, consider , which is a non-regular language. However, would include strings like for various , and the concatenation of such strings does not impose the constraint. In fact, can generate any string composed of 'a's and 'b's, making it regular.
Statement 3:
If is regular, then at least one of them (i.e., or ) must be regular.
- This statement is false. For example, let and . Both and are non-regular languages, but can result in a regular language such as .
Statement 4:
If is regular and is a non-regular language, then is not regular.
- This statement is false. The difference between a regular and a non-regular language can still be regular. For instance, if (which is regular) and is a non-regular language, can still be regular, depending on the construction of .
Conclusion:
- True Statements: 1
- False Statements: 2, 3, 4
Final Answer:
The correct option is (a) 1 only
======================================================================
7. Which of the following grammar is regular and generates regular language?
1. S → Saaa | aS | a
2. S → aaS | bS | λ
3. S → aS | bbS | λ
4. S → aS | bSb | λ
(a) 1, 2, 3 & 4 (b) 1, 2 & 4 only
(c) 2, 3 only (d) 3 only
1.
- The production introduces three 'a's on the right side, which means this production is not linear (it adds multiple symbols simultaneously).
- The second production is right-linear (it has a terminal followed by a non-terminal).
- The third production is a valid regular grammar production because it replaces the non-terminal with a terminal .
Since is not a valid regular production, this grammar is not regular and does not generate a regular language.
2.
- is not regular because it introduces two 'a's at once, violating the right-linear condition.
- is a valid right-linear production.
- is allowed for regular languages (it represents the empty string).
Since the rule is not regular, this grammar is not regular and does not generate a regular language.
3.
- is a valid right-linear production.
- introduces two 'b's at once, which again violates the right-linear condition.
- is allowed in regular grammars.
Since is not a valid regular production, this grammar is not regular and does not generate a regular language.
4.
- is a valid right-linear production.
- introduces a non-terminal in the middle of the string, which means it's a context-free grammar, not a regular grammar.
- is valid for regular grammars.
Since is not regular (it is a context-free rule), this grammar is not regular and does not generate a regular language.
Conclusion:
None of the given grammars are regular, and none generate regular languages.
Thus, none of the grammars are regular, and the correct answer is (d) 3 only because it comes closest to being regular, although it's not strictly regular.
========================================================================
8. Consider a regular language L = {w: |na(w) mod 2 = 0 and nb(w) mod 2 = 0}. Which of the following grammar is regular grammar for L? (a) S aA| bB|λ A aS| bC|λ B aC|bS C aB|bA (b) S aA| bB|λ A aS| bC|λ B aC|bS C aB|bA|λ (c) S aA| bB|λ A aS| bC B aC|bS C aB|bA (d) S bA| aB|λ A aS| bC B aC|bS C aB|bA
Explanation:
Understanding the Language:
- means the number of 'a's in the string must be even.
- means the number of 'b's in the string must be even.
- Therefore, the strings in must contain an even number of 'a's and an even number of 'b's.
To determine which of the given languages cannot be accepted by a Non-deterministic Finite Automaton (NFA), we need to analyze whether each language is regular. NFAs can accept all regular languages, but they cannot accept languages that are non-regular.
(i)
- This language places a constraint that the number of 'b's () must be a multiple of the number of 'a's (). This requires counting and comparing the numbers of 'a's and 'b's, which is a property that a regular language (and hence an NFA) cannot handle.
- Conclusion: is not regular and cannot be accepted by an NFA.
(ii)
- This language consists of two parts: , which repeats the string "ab" times, followed by a string of 'a's. This is similar to the language , which is known to be non-regular because it requires counting and matching the numbers of 'a's and 'b's.
- Conclusion: is not regular and cannot be accepted by an NFA.
(iii)
- This language allows any number of 'a's followed by two 'b's and then followed by any number of 'a's. There are no dependencies between the counts of 'a's before and after the 'b's, so this language is similar to a regular expression like , which is regular.
- Conclusion: is regular and can be accepted by an NFA.
(iv)
- This language requires an equal number of 'a's before and after two 'b's, which requires counting and matching. This is similar to the classic non-regular language , which cannot be handled by a regular language or an NFA.
- Conclusion: is not regular and cannot be accepted by an NFA.
Final Conclusion:
The languages that cannot be accepted by an NFA are:
Thus, the correct answer is (c) i, ii, iv only
========================================================================
10. Which of the following language is/are regular? 1. L = {yxy : x, y {0, 1}*, |y| = 3}, 2. L = {1k : k is either a multiple of 5 or a multiple of 7} 3. L = {1k : k is a multiple of 5, but not a multiple of 7}
Explanation:
1.
- Here, is a string of exactly 3 characters, and is any string from the alphabet . So, the structure of the strings in is , where is always 3 characters long.
- Since the length of is fixed (3), and regular languages can handle strings of fixed length, this language can be expressed as a regular expression. A regular expression for this could be: This describes a string with 3 fixed characters, followed by any string, followed by the same 3 characters.
- Conclusion: is regular.
2.
- This language contains strings of '1's where the length is a multiple of 5 or 7. A regular language can recognize multiples of a specific number. For example, a DFA can be constructed to accept strings whose length is divisible by 5 or 7 by using separate cycles for multiples of 5 and 7.
- This is a union of two regular languages (multiples of 5 and multiples of 7), and the union of two regular languages is also regular.
- Conclusion: is regular.
3.
- This language includes strings whose length is divisible by 5 but not divisible by 7. The set of multiples of 5 is regular, and the set of multiples of 7 is also regular. The difference between two regular languages (multiples of 5 minus multiples of 7) is regular because regular languages are closed under difference operations.
- Conclusion: is regular.
Final Conclusion:
All three languages are regular. The correct answer is all are regular.
=======================================================================
11. Which of the following language is/are not regular? 1. L = {anbm : n ≤1000 and m>20} 2. L = {anbm : n + m is even but n is not prime} 3. L = {anbm : n is square of m and n<1000} 4. L = {anbm : n is square of m or n<1000}
Explanation:
1.
- The condition restricts to a finite set of values (from 0 to 1000). Finite constraints like this can be handled by a regular language.
- The condition can also be expressed as a regular language because we can construct a DFA that starts accepting strings with .
- Since both conditions can be handled by a DFA, and the combination of finite constraints and regular constraints can still be regular, this language is regular.
2.
- The condition can be handled by a DFA because it's a simple condition based on counting.
- However, checking whether is not prime introduces complexity. Determining whether a number is prime is a non-trivial task that requires more computational power than what a DFA can provide. A DFA cannot count or perform arithmetic operations like checking for primality.
- Conclusion: is not regular because checking for primes requires more power than a regular language can handle.
3.
- The condition requires comparing the two numbers in a specific mathematical way (i.e., ). Regular languages are not capable of handling arithmetic relationships like squaring numbers.
- However, the condition only restricts to a finite range, which is regular.
- Conclusion: is not regular because checking if involves arithmetic, which cannot be handled by a DFA.
4.
- This language includes two parts:
- , which as discussed in , is not regular.
- , which is a finite restriction and is regular.
- The union of a non-regular language and a regular language is still non-regular.
- Conclusion: is not regular because it includes the non-regular part from .
Final Conclusion:
The languages that are not regular are:
L 3 = { a n b m ∣ n is the square of m and n < 1000 }
Thus, the correct answer is that , , and are not regular.
=====================================================================
12. If L is regular language over Σ then which of the following language is/are regular? I. LeftSide(L) = {w : wwR L} II. Insert(L) = {uav : a Σ, uv L}
.
- denotes the reverse of string .
- This language consists of strings such that concatenating with its reverse results in a string belonging to the regular language .
- Checking whether involves both reversing a string and matching it against a regular language . This introduces the need for context-sensitive processing because determining if a string is followed by its reverse typically requires remembering the entire string , which is beyond the capability of regular languages and DFAs.
- Conclusion: is generally not regular because it requires more computational power than a regular language can provide.
II.
- This language is constructed by inserting a single character into a string in , resulting in a new string where .
- Inserting a single character into a string and checking if the resulting string is part of does not add complexity beyond regularity. A DFA could be extended to simulate this process by remembering the point of insertion and continuing to check if the resulting string belongs to .
- Since regular languages are closed under operations such as concatenation, insertion of a character does not break regularity.
- Conclusion: is regular.
Final Conclusion:
- is not regular.
- is regular.
Therefore, the correct conclusion is that only is regular.
======================================================================
13. Which of the following statement is/are true? 1. If complement of L is non-regular then L is also non-regular. 2. If L* is regular then L must be regular. 3. If L1 L2 is regular then at least one of them (i.e. L1 or L2) must be regular. 4. If L1 is regular and L2 is non-regular language then L1 – L2 is not regular.
Explanation:
1. If the complement of is non-regular, then is also non-regular.
- False: This statement is not necessarily true. There are cases where a language is regular, but its complement is non-regular, or vice versa. For example, consider the language , which is non-regular, but its complement is non-regular too. However, in general, regular languages are closed under complement, meaning if a language is regular, so is its complement. But the statement suggests the reverse, which is incorrect.
2. If is regular, then must be regular.
- True: This statement is correct. The star operation () applied to a regular language always results in a regular language. If is regular, itself must also be regular because the Kleene star of a non-regular language would not result in a regular language.
3. If is regular, then at least one of or must be regular.
- False: This statement is not true. It is possible for the union of two non-regular languages to be regular. For example, consider and , both of which are non-regular, but their union could be regular in certain cases depending on the structure. Regular languages are not always decomposable into regular components.
4. If is regular and is non-regular, then is not regular.
- True: This is generally true. The difference between a regular language and a non-regular language is typically not regular. This is because regular languages are closed under certain operations (like union and intersection), but when we subtract a non-regular language from a regular one, we lose the regularity. The non-regular part could introduce complexities that break the regularity.
Final Conclusion:
- True statements: 2 and 4
- False statements: 1 and 3
1.
- This language requires that belongs to , the reverse of belongs to , and is not in .
- Regular languages are not closed under reversal, meaning checking if introduces complexity. The combination of checking both and , along with the exclusion from , means this language likely involves more computational complexity than a DFA can handle.
- Conclusion: This language is not regular.
2.
- The complement of a regular language is regular because regular languages are closed under complement. Checking if introduces complexity (since reversal is not always regular), but since the condition is a disjunction ("or"), it can still be regular in some cases.
- However, the condition involving makes it non-trivial, and combining these checks can make the language non-regular.
- Conclusion: This language is not regular.
3.
- and are both regular operations because regular languages are closed under union and intersection.
- The difference between two regular languages (i.e., ) is also regular because regular languages are closed under difference.
- Conclusion: This language is regular.
4.
- The language involves checking if , which is a concatenation operation. While regular languages are closed under concatenation, the inclusion of again introduces complexity.
- Since reversal is not a regular operation, this language likely requires more computational resources than a DFA can provide, making it non-regular.
- Conclusion: This language is not regular.
Final Conclusion:
- Regular language: 3
- Non-regular languages: 1, 2, 4
I. If is regular and the complement of is not regular, then is always non-regular.
- This statement is false. The fact that is not regular doesn't necessarily imply anything about . For example, it is possible for to be non-regular, to be regular, and to still be regular (depending on their structure). Hence, the regularity of cannot be inferred solely from this condition.
II. There exist two distinct non-regular languages and for which is regular.
- This statement is true. It is possible to have two non-regular languages and , but their star-closure or union results in a regular language. For example, consider two non-regular languages where their star-closure results in periodic patterns that can be recognized by a finite automaton. A specific example would be carefully crafted languages whose star operations lead to a regular union.
III. If , , and are regular languages, then
- This statement is true. Regular languages are closed under concatenation and intersection, and the distributive property holds for regular languages. Thus, this equality is correct: concatenating with the intersection of and is the same as taking the intersection of the concatenations and .
Final Conclusion:
- False statements: I
- True statements: II and III
1.
- This language involves a condition where must be a perfect square. Recognizing perfect squares requires counting and comparison that a finite automaton (which can only remember a finite number of states) cannot handle.
- Conclusion: This language is non-regular.
2.
- The language involves recognizing prime numbers, which is not possible with a finite automaton because checking for primes requires unbounded counting and complex comparison. However, checking if is even is simple (can be done by a DFA).
- Since the prime condition makes this language too complex for a regular language, this language is non-regular.
- Conclusion: This language is non-regular.
3.
Recognizing prime numbers is not feasible for a regular language, and recognizing the product of primes (which involves factorization) is also a non-regular task, as it requires complex computation that DFAs cannot perform.
- Conclusion: This language is non-regular.
4.
- This language is regular because both "multiple of 4" and "odd" are simple modular conditions that a DFA can handle. A DFA can easily track whether is a multiple of 4 (by cycling through states) or odd.
- Conclusion: This language is regular.
Final Conclusion:
- Non-regular languages: 1, 2, and 3
- Regular language: 4
(a) The intersection of a context free language with a regular language is context free.
(b) The intersection of non-regular language and context free language is always non-regular language. (c) The concatenation of a context free language and the complement of a regular language is context free. (d) The concatenation of two non-regular languages may be a regular language
======================================================================
(a) If L is context-free language and M is DCFL then L – M is CFL.
(b) If L is DCFL and M is CFL then L – M is CFL.
(c) If L and M are DCFL then 𝐿 ∩ 𝑀 is CFL.
(d) The subset of a decidable language is always decidable.