Assignment_4_Compiler Design -Solution
Assignment_4_Compiler Design -Solution
Compiler Design
Assignment- Week 4
TYPE OF QUESTION:MCQ
Number ofquestions:11 Total mark: 11 X 1 = 11
1.
Ans: a)
2.
Ans: a)
Solution:
A grammar is ambiguous if a single string (sentence) derived from the grammar has
more than one distinct parse tree or derivation.
E→E+E ∣ E∗E ∣ id
allows multiple ways to derive the same expression. For example, for the string id + id *
i, we can have two different parse trees:
3.
• These two distinct parse trees show that the grammar does not enforce a unique
structure for the expression, making it ambiguous.
Ans: a)
Explanation: Deterministic CFGs are always unambiguous , and are an important
subclass of unambiguous CFGs; there are non-deterministic unambiguous CFGs,
however.
4. A language that admits only ambiguous grammar:
• This means that no unambiguous grammar can be written for such a language.
• Some context-free languages (CFLs) are inherently ambiguous, meaning that
ambiguity cannot be removed by rewriting the grammar.
5.
Ans: b)
Solution:
6. For the grammar rules {S Aa | bB, A c |ε}, FIRST(S) is
Ans: c)
Solution:
Explanation:
1. S → Aa | bB
2. A → c | e (where e represents ε, i.e., an empty string)
• S → Aa
o A can be c or ε
o If A → c, then Aa starts with c
o If A → ε, then Aa starts with a
o Thus, from this production, FIRST(S) includes {c, a}
• S → bB
o The first symbol is b, so FIRST(S) includes {b}
Ans: b)
Solution:
A grammar is unambiguous if every string in the language has a unique parse tree (or
derivation).
Given Grammar:
1. E → E + T | T
2. T → T * F | F
3. F → id
Using the given grammar, the only valid parse tree follows standard precedence rules:
r
CopyEdit
E
/ \
E + T
| / \
T T * F
| | |
F F id
| |
id id
• The multiplication (*) happens first, followed by addition (+), which is correct
operator precedence.
• There is only one unique parse tree, meaning the grammar is unambiguous.
8.
Ans: a)
Solution:
A top-down parser constructs the parse tree from the start symbol (root) to the leaves,
applying production rules from left to right.
Example:
1. S → A B
2. A → a
3. B → b
1. S ⇒ A B (Expand S)
2. A B ⇒ a B (Expand A first, leftmost non-terminal)
3. a B ⇒ a b (Expand B)
Here, the leftmost non-terminal is always expanded first, confirming leftmost derivation.
9.
Ans: a)
Solution:
Left recursion removal is mandatory for top-down parsing because top-down parsers
(like Recursive Descent and LL(1) parsers) cannot handle left-recursive grammars.
A grammar has left recursion if a non-terminal refers to itself as the leftmost symbol in
its production.
Example of left recursion:
Here, the non-terminal A appears at the beginning of its own production (A → Aα),
causing an infinite recursion in a top-down parser.
A→Aα∣
A→βA
A′→αA′∣ε
This transformation eliminates left recursion, making the grammar suitable for top-
down parsing.
10
.
Ans: b)
Solution:
A grammar is ambiguous if there exists at least one string that has more than one
distinct parse tree or derivation.
11
.
Ans: a)
Explanation: Backtracking problem is solved by constructing a tree of choices called as
the state-space tree. Its root represents an initial state before the search for a solution
begins.
END of Assignment