0
0
Compiler Designknowledge~5 mins

Ambiguity in grammars in Compiler Design - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA string can have multiple parse trees
BGrammar has syntax errors
CGrammar cannot generate any string
DGrammar has only one production rule
Which of the following is a sign of an ambiguous grammar?
AMultiple parse trees for the same string
BNo parse tree for any string
COnly one production rule
DGrammar is context-free
Why is ambiguity bad for compilers?
AIt reduces code size
BIt makes code run faster
CIt causes confusion in interpreting code
DIt helps optimize code
Which expression shows ambiguity in the grammar E → E + E | E * E | id?
Aid
Bid + id
Cid * id
Did + id * id
How can ambiguity be removed from a grammar?
ARemove all production rules
BRewrite grammar with precedence rules
CAdd more ambiguous rules
DIgnore 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.