Assignment2 Solution Compiler Design
Assignment2 Solution Compiler Design
Assignment 2
No. of Question 13
Q1.
Detailed Solution: Regular expressions are used to define patterns that match strings in a language.
They describe the set of strings (constituent strings) that belong to a specific language. For example,
the regular expression a*b represents strings in the form of zero or more occurrences of a followed by
a single b, such as b, ab, aab, etc.
Q2.
Ans : c)
Explanation:
Lexeme
Token category
Sum “Identifier”
= “Assignment operator”
3 “Integer literal”
+ “Addition operator”
2 “Integer literal”
; “End of statement”
Q3.
Ans: C)
Q4.
Ans : d)
Ans: d)
Deatiled Solution: Regular expressions are used to define patterns for regular languages.
However, the language that accepts strings with exactly one more 1 than 0s is not a regular
language. This is because keeping track of the difference between the number of 1s and 0s
requires a form of counting or memory, which regular expressions (and finite automata) cannot
handle.
This type of language can only be recognized by more powerful computational models, such as a
pushdown automaton (for context-free languages) or a Turing machine. Hence, it is not
possible to write a regular expression for this condition.
Q6.
Ans : C)
Explanation: The regular expression has two 0′s surrounded by (0+1)* which means
accepted strings must have at least 2 0′s.
Deatiled Solution: Finite automata (both deterministic and non-deterministic) are mathematical
models used to recognize or implement regular languages, which are the types of languages
described by regular expressions.
• Regular expressions provide a way to describe patterns in strings, while finite automata
serve as the computational model that accepts or rejects strings based on those patterns.
• There is a direct correspondence between regular expressions and finite automata: for
every regular expression, there exists an equivalent finite automaton, and vice versa.
Q7.
Ans: A)
Deatiled Solution: Finite automata (both deterministic and non-deterministic) are mathematical
models used to recognize or implement regular languages, which are the types of languages
described by regular expressions.
• Regular expressions provide a way to describe patterns in strings, while finite automata
serve as the computational model that accepts or rejects strings based on those patterns.
• There is a direct correspondence between regular expressions and finite automata: for
every regular expression, there exists an equivalent finite automaton, and vice versa.
Q8.
Ans: A)
• NFA: Allows epsilon transitions, which means the automaton can move to a new state
without reading any input.
• DFA (Deterministic Finite Automaton): Does not allow epsilon transitions; every
transition in a DFA must consume an input symbol.
• Pushdown Automaton: While it allows stack operations, epsilon transitions are not
specifically related to finite state machines like NFAs.
• All of the mentioned: Incorrect because DFA does not support epsilon transitions.
Q9.
Ans: A)
Explanation: The ε-closure of a set of states, P, of an NFAis defined as the set of states
reachable from any state in P following e-transitions.
Q10.
Ans: c)
While an NFA may appear more "flexible," any NFA can be converted into an equivalent DFA.
The DFA might have exponentially more states in the worst case, but it recognizes the exact
same language as the NFA.
Thus, NFAs and DFAs are equally powerful in terms of the languages they can recognize.
Q11.
Ans: A)
Q12.
Ans: C)
Q13.
Ans: D)
Detailed Solution: n the scenario where a compiler automatically corrects errors like changing
"fi" to "if," the error correction involves transposing the characters.