Formal Language Review Question

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Part I: True/False Questions

1. S → aaSa, S → b is in Greibach Normal Form.


o False. In Greibach Normal Form, all production rules must have the form
A→aαA \rightarrow a\alphaA→aα where aaa is a terminal and α\alphaα is a
(possibly empty) string of non-terminals. The first production rule S→aaSaS \
rightarrow aaSaS→9aSa does not conform to this format.
2. Any regular language is generated by a regular grammar.
o True. By definition, regular languages can be generated by regular grammars,
which have production rules of the form A→aBA \rightarrow aBA→aB or
A→aA \rightarrow aA→a, where AAA and BBB are non-terminal symbols and
aaa is a terminal symbol.
3. A context-free grammar G is ambiguous if there is a string www element of
L(G)L(G)L(G) which has two different derivation trees or two leftmost derivations.
o True. A grammar is ambiguous if there exists at least one string that can be
generated in more than one way, resulting in different parse trees or leftmost
derivations.
4. To simplify a CFG, we should remove only nullable variable and unit production.
o False. Simplifying a CFG also involves removing useless symbols (symbols that
do not derive any terminal string) in addition to removing nullable productions
(productions that can derive the empty string) and unit productions (productions
of the form A→BA \rightarrow BA→B).
5. A production B→yB \rightarrow yB→y is useless if any of its variables is useless.
o True. If any variable in a production is useless (i.e., it does not appear in any
derivation of terminal strings), then the production itself is also useless.
6. For every regular language there is a regular expression, and for every regular
expression there is a regular language.
o True. This is a fundamental property of regular languages and regular
expressions; they are equivalent formalisms.

Part II: Multiple Choice Questions

1. Which is true for δ(q,ab)\delta (q, ab)δ(q,ab)


o A) δ(q,a)∪δ(q,b)\delta (q, a) \cup \delta (q, b)δ(q,a)∪δ(q,b)
o B) δ(δ(q,a),b)\delta (\delta (q, a), b)δ(δ(q,a),b)
o C) δ(q,a),b\delta (q, a), bδ(q,a),b
o D) δ(q,a)∩δ(q,b)\delta (q, a) \cap \delta (q, b)δ(q,a)∩δ(q,b)
o Correct Answer: B) δ(δ(q,a),b)\delta (\delta (q, a), b)δ(δ(q,a),b). This
represents the application of the transition function sequentially for each symbol
in the string.
2. If we have a grammar G=(V,T,S,P)G=(V, T, S, P)G=(V,T,S,P), with A→xA \
rightarrow xA→x where A(A∪T)∗A (A \cup T)*A(A∪T)∗, what type of grammar
is this?
o A) Context Free Grammar
o B) Linear Grammar
o C) Regular Grammar
o D) all of the above
o Correct Answer: B) Linear Grammar. A linear grammar is a context-free
grammar where each production has at most one non-terminal in its production
rule.

Part III: Workout

1. Given the following DFA:


o a. Three strings accepted by the DFA:
 Let's analyze the DFA diagram. Accepted strings are those that end in the
accepting state(s).
 Examples: "ab", "aab", "aaab".
o b. Three strings not accepted by the DFA with length not less than 5:
 Examples: "aaaaa", "aaaab", "baaab".
o c. Construct a transition table:
 Based on the DFA diagram, construct a table listing states and transitions
for each input symbol.
2. Construct a DFA with L={(q,q1,q2),{a,b},δ,q0,{q1}}L=\{(q, q1, q2), \{a, b\}, \delta,
q0, \{q1\}\}L={(q,q1,q2),{a,b},δ,q0,{q1}} where δ\deltaδ is given:
o Given transitions:
 δ(q,a)=q\delta (q, a)=qδ(q,a)=q
 δ(q,b)=q2\delta (q, b)=q2δ(q,b)=q2
 δ(q1,a)=q2\delta (q1, a)=q2δ(q1,a)=q2
 δ(q1,b)=q0\delta (q1, b)=q0δ(q1,b)=q0
 δ(q2,a)=q1\delta (q2, a)=q1δ(q2,a)=q1
 δ(q2,b)=q1\delta (q2, b)=q1δ(q2,b)=q1
 δ(q1,b)=q1\delta (q1, b)=q1δ(q1,b)=q1
o Use these transitions to draw the DFA.
3. Consider the grammar with productions:
o S→bBAS \rightarrow bBAS→bBA
o A→aBaA \rightarrow aBaA→aBa
o B→A/λB \rightarrow A/\lambdaB→A/λ
o Drive some string using:
 a) Leftmost derivations: Start with the leftmost variable.
 b) Rightmost derivations: Start with the rightmost variable.
 c) Derivation tree: Draw the tree structure showing derivations.
4. Given the following CFG, is it in CNF? If not, convert to CNF:
o S→EDbS \rightarrow EDbS→EDb
o E→bbE \rightarrow bbE→bb
o D→AeD \rightarrow AeD→Ae
o The given CFG is not in CNF. To convert it to CNF, we need to ensure all
productions are in the form A→BCA \rightarrow BCA→BC or A→aA \
rightarrow aA→a.
 Converting to CNF:
 S→EDbS \rightarrow EDbS→EDb
 E→bbE \rightarrow bbE→bb
 D→AeD \rightarrow AeD→Ae
 Replace complex rules with intermediary variables to satisfy CNF.
5. Convert the following NFA to an equivalent DFA:
o Analyze the NFA and create a corresponding DFA by constructing the state
transition table and ensuring all states are accounted for.
 Follow the subset construction algorithm to convert the NFA to DFA.
### Part I: Multiple Choice Questions (each question carries 1.5 marks)

1. **What is the language generated by the grammar \( S \rightarrow aSb, S \rightarrow A, A \


rightarrow aA, A \rightarrow \lambda \)?**

- a) \( a^m b^n \)

- b) \( \lambda \)

- c) \( a^n b^n \)

- d) \( a^m+1 b^m \)

- **Answer: c) \( a^n b^n \)**

- **Explanation**: The grammar produces strings with equal numbers of \( a \)'s and \( b \)'s.
The first rule \( S \rightarrow aSb \) ensures that for each \( a \) there is a corresponding \( b \)
following it. The second rule \( S \rightarrow A \) allows the transition to \( A \) which produces
a sequence of \( a \)'s followed by an empty string (since \( A \rightarrow \lambda \)).

2. **Which type of grammar is the following in particular \( S \rightarrow aSb, S \rightarrow


ab \)?**

- a) Unrestricted

- b) Context-sensitive grammar

- c) Context-free grammar

- d) Regular grammar

- **Answer: c) Context-free grammar**

- **Explanation**: This grammar fits the definition of a context-free grammar (CFG) because
each production rule has a single non-terminal on the left-hand side.

3. **\( (a, b)^* \) means**

- a) Any combination of \( a, b \) including null


- b) Any combination of \( a, b \) excluding null

- c) Any combination of \( a, b \), but \( a \) will come first

- d) None of these

- **Answer: a) Any combination of \( a, b \) including null**

- **Explanation**: \( (a, b)^* \) denotes the Kleene star operation, which includes all possible
strings formed by \( a \) and \( b \), including the empty string (null).

4. **Which of the following grammar generates strings with any number of 1's?**

- a) \( S \rightarrow 1A, A \rightarrow \epsilon \)

- b) \( S \rightarrow 1S, S \rightarrow \epsilon \)

- c) \( S \rightarrow S1, S \rightarrow \epsilon \)

- d) \( S \rightarrow S1, S \rightarrow 1S \)

- **Answer: b) \( S \rightarrow 1S, S \rightarrow \epsilon \)**

- **Explanation**: This grammar can generate strings with any number of 1's because \( S \
rightarrow 1S \) allows for the addition of any number of 1's, and \( S \rightarrow \epsilon \)
terminates the production.

5. **What is the highest type number to the grammar given by the following production rules \
( S \rightarrow Aa, A \rightarrow c|Ba, B \rightarrow abc \)?**

- a) Zero

- b) One

- c) Two

- d) Three

- **Answer: c) Two**
- **Explanation**: This is a context-free grammar, which is Type 2 in the Chomsky
hierarchy. The productions fit the context-free grammar definition where each production has a
single non-terminal on the left-hand side.

6. **A language set contains**

- a) Alphabet

- b) String

- c) Set

- d) None of these

- **Answer: b) String**

- **Explanation**: A language is a set of strings over an alphabet.

7. **Which is true for a dead state?**

- a) It cannot be reached anytime

- b) There is no necessity of the state

- c) If control enters, there is no way to come out from the state

- d) If control enters, FA is dead

- **Answer: c) If control enters, there is no way to come out from the state**

- **Explanation**: A dead state is one where, once entered, there are no transitions leading
out that lead to an accepting state. Hence, any string that reaches a dead state will be rejected by
the automaton.

8. **The transitional function of an NFA is**

- a) \( Q \times \Sigma \rightarrow Q \)

- b) \( Q \times \Sigma \rightarrow 2^Q \)


- c) \( Q \times \Sigma \rightarrow 2^{2^n} \)

- d) \( Q \times \Sigma \rightarrow Q^n \)

- **Answer: b) \( Q \times \Sigma \rightarrow 2^Q \)**

- **Explanation**: In an NFA (Non-deterministic Finite Automaton), the transition function


can map a state and input symbol to a set of possible next states, hence the power set of \( Q \)
(denoted as \( 2^Q \)).

9. **The language accepted by the given FA is:**

- (FA diagram shows states and transitions with specific labels, typically a small automaton)

- a) \( (ab)^* \)

- b) \( bb^*a \)

- c) \( b(ba)^*a \)

- d) Null

- **Answer: a) \( (ab)^* \)**

- **Explanation**: Based on the typical transitions of such a finite automaton (FA), the
language it accepts would involve alternating 'a' and 'b' in pairs, starting from the initial state and
returning to it after consuming each 'ab' pair.

These answers provide a detailed understanding of each question, explaining why each option is
correct or incorrect.

10. A string after full traversal is called not accepted by an NFA if it results in
o a) Some non-final states
o b) All non-final states
o c) A single non-final state
o d) Some final states
o Answer: b) All non-final states
 Explanation: An NFA accepts a string if at least one of the possible
traversal paths ends in a final state. If the string ends in all non-final states,
then it is not accepted.
11. Consider the strings u=ababbau = ababbau=ababba, v=babv = babv=bab, and
w=aabbw = aabbw=aabb. Which of the following statement is true for the given
transitional system?
o a) The automaton accepts uuu and vvv but not www.
o b) The automaton accepts each of u,v,u, v,u,v, and www.
o c) The automaton rejects each of u,v,u, v,u,v, and www.
o d) The automaton accepts uuu but rejects vvv and www.
o Answer: d) The automaton accepts uuu but rejects vvv and www.
 Explanation: The diagram needs to be analyzed to determine which
strings are accepted. u=ababbau = ababbau=ababba is accepted as it
follows the path through the states reaching a final state. vvv and www do
not reach the final state as per the given transitions.
12. Consider the following deterministic finite state automaton M. Let SSS denote the
set of seven-bit binary strings in which the first, fourth, and last bits are 1. The
number of strings in SSS that are accepted by MMM is
o a) 1
o b) 2
o c) 3
o d) none of the above
o Answer: a) 1
 Explanation: Given the conditions, there is only one seven-bit binary
string where the first, fourth, and last bits are 1. It should be in the form of
1xxx1xx1xxx1xx1xxx1xx and should be validated against the DFA.
13. If the final state and non-final states in the following DFA are interchanged, then
which of the following languages over the alphabet {a,b}\{a, b\}{a,b} will be accepted
by the new DFA?
o a) Set of all strings that do not end with ababab
o b) Set of all strings that begin with either an aaa or a bbb
o c) Set of all strings that do not contain the substring ababab
o d) The set described by the regular expression
b∗aa∗(ba)∗b∗b^*aa^*(ba)^*b^*b∗aa∗(ba)∗b∗
o Answer: c) Set of all strings that do not contain the substring ababab
 Explanation: Interchanging the final and non-final states will result in a
DFA that rejects any string containing ababab.
14. The language of all words with at least 2 aaa's can be described by the regular
expression
o a) (ab)∗a(ab)^*a(ab)∗a
o b) (a+b)∗ab∗a(a+b)∗(a + b)^*ab^*a(a + b)^*(a+b)∗ab∗a(a+b)∗
o c) b∗ab∗(a+b)∗b^*ab^*(a + b)^*b∗ab∗(a+b)∗
o d) All of the above
o Answer: d) All of the above
 Explanation: All given regular expressions can generate strings with at
least two aaa's in various ways.
15. Which of the strings do not belong to the regular expression (ba+baa)∗aabba(ba +
baa)^*aabba(ba+baa)∗aabba
o a) baaaab
o b) babaabaaab
o c) babababa
o d) baaaaba
o Answer: c) babababa
 Explanation: The regular expression specifies that strings are composed
of repetitions of "ba" or "baa" followed by "aabba". The string "babababa"
does not fit this pattern as it lacks the necessary structure and the "aabba"
ending.

Explanation for Incorrect Options

1. What is the language generated by the grammar S→aSb,S→A,A→aA,A→λS \


rightarrow aSb, S \rightarrow A, A \rightarrow aA, A \rightarrow \
lambdaS→aSb,S→A,A→aA,A→λ?
o a) ambna^m b^nambn: Incorrect. This denotes strings with different numbers of
aaa's and bbb's.
o b) λ\lambdaλ: Incorrect. The empty string is part of the language but not the entire
language.
o d) am+1bma^{m+1} b^mam+1bm: Incorrect. This implies one more aaa than
bbb.
2. Which type of grammar is the following in particular S→aSb,S→abS \rightarrow
aSb, S \rightarrow abS→aSb,S→ab?
o a) Unrestricted: Incorrect. Unrestricted grammars have no restrictions on the form
of productions.
o b) Context-sensitive grammar: Incorrect. Context-sensitive grammars have rules
where the length of the left side can be less than or equal to the right side.
o d) Regular grammar: Incorrect. Regular grammars have right-linear or left-linear
rules.
3. (a,b)∗(a, b)^*(a,b)∗ means
o b) Any combination of a,ba, ba,b excluding null: Incorrect. Kleene star includes
null.
o c) Any combination of a,ba, ba,b, but aaa will come first: Incorrect. Order is not
specified.
o d) None of these: Incorrect. Correct definition is given in option a.
4. Which of the following grammar generates strings with any number of 1's?
o a) S→1A,A→ϵS \rightarrow 1A, A \rightarrow \epsilonS→1A,A→ϵ: Incorrect.
Only generates strings starting with a single 1.
o c) S→S1,S→ϵS \rightarrow S1, S \rightarrow \epsilonS→S1,S→ϵ: Incorrect.
Generates the same as b.
o d) S→S1,S→1SS \rightarrow S1, S \rightarrow 1SS→S1,S→1S: Incorrect. Can
generate but not all possible strings as b does.
5. What is the highest type number to the grammar given by the following production
rules S→Aa,A→c∣Ba,B→abcS \rightarrow Aa, A \rightarrow c|Ba, B \rightarrow
abcS→Aa,A→c∣Ba,B→abc?
o a) Zero: Incorrect. Type 0 is unrestricted grammars.
o b) One: Incorrect. Type 1 is context-sensitive grammars.
o d) Three: Incorrect. Type 3 is regular grammars.
6. A language set contains
o a) Alphabet: Incorrect. An alphabet is a set of symbols.
o c) Set: Incorrect. A language is a set but specifically of strings.
o d) None of these: Incorrect. Language contains strings.
7. Which is true for a dead state?
o a) It cannot be reached anytime: Incorrect. It can be reached.
o b) There is no necessity of the state: Incorrect. It can still be part of the
automaton.
o d) If control enters, FA is dead: Incorrect. The FA is not dead, just the state.
8. The transitional function of an NFA is
o a) Q×Σ→QQ \times \Sigma \rightarrow QQ×Σ→Q: Incorrect. This is for DFA.
o c) Q×Σ→22nQ \times \Sigma \rightarrow 2^{2^n}Q×Σ→22n: Incorrect. Too
complex for NFA.
o d) Q×Σ→QnQ \times \Sigma \rightarrow Q^nQ×Σ→Qn: Incorrect. Incorrect
representation for transitions.
9. The language accepted by the given FA is:
o b) bb∗abb^*abb∗a: Incorrect. Does not match the pattern of the automaton.
o c) b(ba)∗ab(ba)^*ab(ba)∗a: Incorrect. Incorrect structure.
o d) Null: Incorrect. The automaton accepts strings.

Fill in blank
1. Regular expression is accepted by type 3 grammar.
o Explanation: Type 3 grammars are also known as regular grammars, which can
be used to describe regular languages. Regular expressions are equivalent to
regular grammars.
2. DFA is a finite automata where for all cases for a single input given to a single state, the
machine goes to a single state.
o Explanation: In a Deterministic Finite Automaton (DFA), each state has exactly
one transition for each input symbol.
3. In the tabular format representation of finite automata, by single circle a non-final state
is represented.
o Explanation: A single circle represents a non-final (non-accepting) state, while a
double circle represents a final (accepting) state.
4. The lexical analyzer is designed by regular expressions.
o Explanation: Lexical analyzers or lexers are typically designed using regular
expressions to match patterns in the input string.
5. If any finite automata contains any ɛ (null) move or transition, then that finite automata is
called NFA (Non-deterministic Finite Automaton) with ɛ moves.
o Explanation: An NFA with epsilon (ɛ) transitions allows for state transitions
without consuming any input symbols.
6. The set of all states that can be reached from that state to all the states with input ɛ is
called epsilon-closure.
o Explanation: Epsilon-closure of a state is the set of states that can be reached
from the given state by only ɛ transitions.
7. A CFG G is said to be ambiguous if there exists some W∈L(G)W \in L(G)W∈L(G) that
has at least two distinct parse trees.
o Explanation: A context-free grammar is ambiguous if there is at least one string
in the language generated by the grammar that has more than one distinct parse
tree or derivation.
8. Context-free languages are not closed under intersection and complementation.
o Explanation: Context-free languages are closed under union, concatenation, and
Kleene star, but not under intersection and complementation.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy