Recall & Review
beginner
What does it mean when a grammar is ambiguous?
A grammar is ambiguous if there is at least one string that can be generated in more than one way, meaning it has more than one distinct parse tree or derivation.
Click to reveal answer
beginner
Why is ambiguity in grammars a problem in compiler design?
Ambiguity causes confusion in understanding the structure of code, making it hard for compilers to decide which meaning or parse tree to use, leading to incorrect program interpretation.
Click to reveal answer
intermediate
Give a simple example of an ambiguous grammar.
The grammar with rules: E → E + E | E * E | id is ambiguous because the expression 'id + id * id' can be parsed in two different ways, changing the order of operations.
Click to reveal answer
beginner
What is a parse tree and how does it relate to ambiguity?
A parse tree shows how a string is derived from a grammar. Ambiguity means there is more than one valid parse tree for the same string.
Click to reveal answer
intermediate
How can ambiguity in grammars be resolved?
Ambiguity can be resolved by rewriting the grammar to remove multiple interpretations, often by adding precedence rules or using unambiguous grammar forms.
Click to reveal answer
What does ambiguity in a grammar mean?
✗ Incorrect
Ambiguity means a string can be generated in more than one way, resulting in multiple parse trees.
Which of the following is a sign of an ambiguous grammar?
✗ Incorrect
Ambiguous grammars allow multiple parse trees for the same string.
Why is ambiguity bad for compilers?
✗ Incorrect
Ambiguity makes it unclear how to interpret code, causing errors in compilation.
Which expression shows ambiguity in the grammar E → E + E | E * E | id?
✗ Incorrect
The expression 'id + id * id' can be parsed in two ways, showing ambiguity.
How can ambiguity be removed from a grammar?
✗ Incorrect
Adding precedence rules or rewriting the grammar helps remove ambiguity.
Explain what ambiguity in grammars means and why it matters in compiler design.
Think about how one string can be understood in different ways.
You got /3 concepts.
Describe a simple example of an ambiguous grammar and how you might fix it.
Consider arithmetic expressions with + and * operators.
You got /3 concepts.