Q66.Marks: +2.0UGC NET Paper 2: Computer Science 18th June 2024 Shift 1 (Cancelled)
The following code :
stmt → if expr then stmt else stmt
| if expr then stmt
suffers from :
1.Ambiguity✓ Correct
2.Left factoring
3.Left Recurssion
4.λ-moves
Solution
The correct answer is Ambiguity
Explanation:
Ambiguity in a grammar means that there is more than one parse tree (or leftmost derivation) for the same string produced by the grammar. In this case, the ambiguity arises because it is unclear to which "if" an "else" should be matched when there are nested "if-then" statements without explicit braces to indicate the block structure.
Consider the input string: if expr1 then if expr2 then stmt else stmt
There are two possible ways to interpret this string:
The "else" is associated with the nearest "if" (expr2). This interpretation is as follows:
if expr1 then [if expr2 then stmt else stmt]
The "else" is associated with the outer "if" (expr1). This interpretation is as follows:
if expr1 then [if expr2 then stmt] else stmt
Both interpretations are valid in the given CFG, leading to ambiguity.