0
0
Compiler Designknowledge~20 mins

Ambiguity in grammars in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ambiguity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Identifying Ambiguity in a Grammar

Consider the grammar with rules:

S → aSb | ab

Is this grammar ambiguous?

ANo, because each string generated has a unique parse tree.
BNo, because the grammar only produces strings of length 2.
CYes, because the grammar generates infinite strings.
DYes, because the string 'aabb' can have two different parse trees.
Attempts:
2 left
💡 Hint

Try to draw parse trees for strings like 'ab' and 'aabb'.

📋 Factual
intermediate
1:30remaining
Definition of Ambiguous Grammar

Which of the following best defines an ambiguous grammar?

AA grammar that cannot generate any string.
BA grammar that generates at least one string with multiple distinct parse trees.
CA grammar that generates only one string.
DA grammar that has no terminal symbols.
Attempts:
2 left
💡 Hint

Think about what ambiguity means in terms of parse trees.

🔍 Analysis
advanced
2:30remaining
Ambiguity in Expression Grammar

Given the grammar:

E → E + E | E * E | (E) | id

What is the main cause of ambiguity in this grammar?

ALack of operator precedence and associativity rules.
BThe use of parentheses in expressions.
CThe presence of the terminal 'id'.
DThe grammar does not generate any strings.
Attempts:
2 left
💡 Hint

Consider how expressions like 'id + id * id' can be parsed differently.

Comparison
advanced
2:00remaining
Comparing Ambiguous and Unambiguous Grammars

Which statement correctly compares ambiguous and unambiguous grammars?

AAmbiguous grammars can generate strings with multiple parse trees; unambiguous grammars cannot.
BUnambiguous grammars generate no strings; ambiguous grammars generate all strings.
CUnambiguous grammars always have fewer production rules than ambiguous grammars.
DAmbiguous grammars always generate fewer strings than unambiguous grammars.
Attempts:
2 left
💡 Hint

Focus on the number of parse trees per string.

Reasoning
expert
3:00remaining
Detecting Ambiguity in a Complex Grammar

Consider the grammar:

S → aSbS | bSaS | ε

Which of the following is true about this grammar?

AIt is unambiguous because every string has a unique parse tree.
BIt is ambiguous because it generates only the empty string.
CIt is ambiguous because the string 'abab' can be derived in two different ways.
DIt is unambiguous because it does not generate any strings.
Attempts:
2 left
💡 Hint

Try to find two different parse trees for the string 'abab'.