← RGPV CS-501 Theory of Computation May 2023 Solved Paper
Question
Define ambiguity and explain it using two examples.
Asked in: CS-501 | May 2023 | 4(b)
Answer
A CFG is ambiguous if some generated word has two distinct parse trees, equivalently two distinct leftmost derivations.
Example 1: E→E+E | E*E | id. The word id+id*id can be parsed as (id+id)*id or id+(id*id). Enforce precedence with E→E+T|T, T→T*F|F, F→id|(E).
Example 2: S→SS|a. The word aaa has two groupings: (aa)a and a(aa). Leftmost derivations are S⇒SS⇒SSS⇒aSS⇒aaS⇒aaa and S⇒SS⇒aS⇒aSS⇒aaS⇒aaa. An unambiguous grammar for the same language is S→aS|a.
Ambiguity concerns a grammar, not necessarily its language. Some languages are inherently ambiguous, meaning no unambiguous CFG exists.
Discussion
Ask a question, share your thoughts or report an error.
Sign in to participate in the discussion.
Join the discussion