The grammar whose productions are
is ambiguous because
if a then if b then c:= d
has more than two parse trees
if a then if b then c:= d
give rise to different parse trees
if a then if b then c:= d else c:= f
has more than two parse trees
if a then if b then c:= d else c:= f
has two parse trees
View worked solution
Answer: D
A grammar is ambiguous if there exists at least one string in its language that has more than one distinct parse tree (or leftmost/rightmost derivation).
The given grammar represents the classic dangling-else problem. Consider the sentence in option D:
if a then if b then c:= d else c:= f
This sentence has exactly two distinct parse trees depending on which if the else clause associates with:
- Inner Association: The
elsebelongs to the innerif.
Structure:if a then (if b then c:= d else c:= f) - Outer Association: The
elsebelongs to the outerif.
Structure:if a then (if b then c:= d) else c:= f
Since this sentence yields exactly two valid parse trees, the grammar is ambiguous, making D the correct option.
Free practice · Your answers on this page are not saved.
