TOC-DEC-19 StrangeR (3)
TOC-DEC-19 StrangeR (3)
Q.NO QUESTION
1A Find DFA that accepts only the language of all strings with b as the second letter over the
alphabet £={a,b}.
ANS: Let's denote the states as q0, q1, and q2.
• q0: Initial state (where the second letter hasn't been encountered yet)
• q1: State after encountering 'b'
• q2: State after encountering any subsequent letters (valid or invalid)
The transitions can be defined as follows:
• From q0:
• On 'a', stay at q0 (as 'a' can't be the second letter)
• On 'b', move to q1 (as 'b' is the required second letter)
• From q1:
• On any input, move to q2 (since the requirement of having 'b' as the second
letter is fulfilled)
• From q2:
• On any input, stay at q2 (since the condition has already been met)
Now, let's construct the DFA transition table:
Here, q0 is the initial state, q1 is the accepting state (where 'b' is the second letter), and q2 is
the non-accepting state for any subsequent letters.
This DFA will accept strings where 'b' is the second letter in the input string.
2A Explain Chomsky classification of grammars
ANS: Noam Chomsky, a renowned linguist and computer scientist, classified grammars into four types
known as Chomsky's hierarchy. These types are categorized based on their generative power or
expressive capability in generating languages.
Type 0: Unrestricted or Recursively Enumerable (Turing Machine)
• Description: These grammars generate languages that can be recognized by a Turing
machine. They have no restrictions on the rules.
• Formalism: There are no limitations on the production rules.
• Example: Any programming language is an example of a Type 0 language.
Type 1: Context-Sensitive (Linear Bounded Automaton)
• Description: These grammars generate languages that can be recognized by linear
bounded automata, where productions must respect the context of the string being
generated.
• Formalism: Productions must be of the form αAβ → αγβ, where A is a non-terminal, α
and β are strings (possibly empty), and γ is a non-empty string.
• Example: Natural languages often fit into Type 1 grammars due to their context-
sensitive rules.
Type 2: Context-Free (Pushdown Automaton)
• Description: These grammars generate languages that can be recognized by pushdown
automata, where productions have a more straightforward structure than Type 1.
• Formalism: Productions must be of the form A → β, where A is a non-terminal and β is a
string (possibly empty) that can include terminals and non-terminals.
• Example: Many programming language constructs are described by context-free
grammars.
Type 3: Regular (Finite State Automaton)
• Description: These grammars generate languages that can be recognized by finite state
automata. They are the simplest and most restrictive type.
• Formalism: Productions must be of the form A → aB or A → a, where A and B are non-
terminals, and a is a terminal symbol.
• Example: Regular expressions and simple pattern matching often fall into this category.
Each type in Chomsky's hierarchy represents a different level of complexity and expressive
power. As you move from Type 3 to Type 0, the languages become more expressive, but the
grammar rules also become more complex. This hierarchy is fundamental in understanding the
capabilities and limitations of different types of formal languages.
FIND
a) Leftmost derivation
b) Rightmost derivation
c) Parse tree
for the string “ aabbaa ”.
ANS: break down the steps for finding the leftmost derivation, rightmost derivation, and the parse
tree for the given string "aabbaa" using the provided grammar:
The grammar:
Leftmost Derivation:
To derive "aabbaa" using leftmost derivation, at each step, always replace the leftmost non-
terminal.
Rightmost Derivation:
To derive "aabbaa" using rightmost derivation, at each step, always replace the rightmost non-
terminal.
Parse Tree:
The parse tree visually represents the derivations. Constructing a parse tree for the string
"aabbaa":
This tree demonstrates the structure of the string "aabbaa" according to the given grammar.
Each non-terminal in the tree represents a step in the derivation process, and the terminals are
the final symbols of the string.
2C Find a reduced grammar G to the grammar given below
S -> AB|CA
A -> a
B -> BC|AB
C -> aB|b
ANS: start by examining the productions of each non-terminal:
Given Grammar:
• S→AB ∣ CA
• A→a
• B→BC ∣ AB
• C→aB ∣ b
Step 1: Eliminate Non-Generating Symbols
Find and eliminate non-generating symbols (symbols that cannot produce any terminal string):
• A→a (Generating)
• C→aB (C is generating because B is generating)
• C→b (Generating)
Step 2: Eliminate Non-Reachable Symbols
Find and eliminate non-reachable symbols (symbols that cannot be reached from the start
symbol):
• S→AB ∣ CA (Start symbol, reachable)
• B→BC ∣ AB (B is reachable from S)
• A→a (Generated by S)
• C→aB ∣ b (C is reachable from S)
Step 3: Remove Unit Productions
Remove unit productions (productions of the form A -> B):
• No unit productions in this case.
Step 4: Remove ε-Productions (if any)
There are no ε-productions in this grammar.
Step 5: Remove Useless Productions
Combine productions that have common non-terminals on the right-hand side:
• S→AB ∣ CA
• A→a
• B→BC ∣ AB
• C→aB ∣ b
Let's combine similar productions and remove redundancy:
• S→AB ∣ CA
• A→a
• B→BC ∣ AB
• C→aB ∣ b
The grammar is already in a simple form, and there are no redundant productions to remove.
Therefore, this reduced grammar remains the same as the given grammar:
• S→AB ∣ CA
• A→a
• B→BC ∣ AB
• C→aB ∣ b
ANS: to create a Turing machine that accepts the language L={0n1n2n ∣ n≤1}, we'll design a Turing
machine that verifies whether the string follows the pattern 0n1n2n where n is less than or
equal to 1.
Turing Machine Description:
States:
1. q0 (Initial state)
2. q1 (State to move to the end of the tape)
3. qaccept (Accepting state)
4. qreject (Rejecting state)
Transitions:
1. Initialization:
• q0,0,→,q0 (Move right until a 0 is found)
• q0,1,→,q1 (If a 1 is encountered without a preceding 0, move to the end of the
tape)
2. Verify 0's and 1's:
• q0,0,→,q2 (Mark the first 0 and move right)
• q2,0,→,q2 (Continue marking 0's)
• q2,1,→,q3 (Mark the first 1 and move right)
• q3,1,→,q3 (Continue marking 1's)
3. Verify 2's and move to the end:
• q3,2,→,qaccept (If a 2 is encountered after 0's and 1's, accept)
• q0,_,→,qaccept (If there are no symbols left after moving right from q1)
4. Rejection state:
• rejectq0,_,→,qreject (If no 0 is found initially)
• rejectq2,_,→,qreject (If 0's are not followed by 1's)
• rejectq3,_,→,qreject (If 1's are not followed by 2's)
This Turing machine will move through the input tape, marking the sequence of 0's, 1's, and 2's.
If the sequence adheres to the pattern 0n1n2n where n is less than or equal to 1, it will reach
the accepting state qaccept. Otherwise, it will reach the rejecting state qreject.