A grammar that is both left and right recursive for a non-terminal, is
View worked solution
Answer: C
To determine if a grammar that is both left and right recursive for a non-terminal is ambiguous, we must consider whether the recursive non-terminal is useful (i.e., reachable from the start symbol and capable of deriving a terminal string).
For a grammar to be ambiguous, there must exist at least one string in its language that has more than one distinct parse tree. Consider the following cases:
- Case 1 (Ambiguous): If the non-terminal is reachable and can derive terminal strings, such as in:
The string has multiple distinct parse trees, making the grammar ambiguous. - Case 2 (Unambiguous): If the recursive non-terminal is unreachable or cannot derive any terminal string, such as in:
Here, the language is . Since the only string in the language has a unique parse tree, the grammar is unambiguous.
Because the ambiguity depends on whether the grammar is reduced and the non-terminal is useful, the given information is not sufficient to decide.
Free practice · Your answers on this page are not saved.
