Bird
Raised Fist0
Compiler Designknowledge~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. What does ambiguity in a grammar mean?
      easy
      A. A sentence can have more than one meaning
      B. The grammar has no rules
      C. All sentences are correct
      D. The grammar is too simple

      Solution

      1. Step 1: Understand the definition of ambiguity in grammar

        Ambiguity means a sentence can be interpreted in multiple ways within the grammar.
      2. Step 2: Compare options with the definition

        Only A sentence can have more than one meaning correctly states that ambiguity means multiple meanings for a sentence.
      3. Final Answer:

        A sentence can have more than one meaning -> Option A
      4. Quick Check:

        Ambiguity = Multiple meanings [OK]
      Hint: Ambiguity means multiple meanings for one sentence [OK]
      Common Mistakes:
      • Confusing ambiguity with grammar errors
      • Thinking ambiguity means no rules
      • Assuming ambiguity means all sentences are correct
      2. Which of the following grammar rules is ambiguous?
      easy
      A. S -> aSb | ab
      B. S -> aS | b
      C. S -> aSb | aSb
      D. S -> ab | ba

      Solution

      1. Step 1: Analyze the grammar rules for ambiguity

        Rule A allows multiple parse trees for strings like 'aabb', causing ambiguity.
      2. Step 2: Check other options for ambiguity

        Options B, C, and D do not produce multiple parse trees for the same string.
      3. Final Answer:

        S -> aSb | aSb -> Option C
      4. Quick Check:

        Multiple parse trees = Ambiguous [OK]
      Hint: Look for rules generating multiple parse trees [OK]
      Common Mistakes:
      • Ignoring repeated rules that cause ambiguity
      • Confusing ambiguity with grammar repetition
      • Assuming all recursive rules are ambiguous
      3. Given the grammar:
      E -> E + E | E * E | id
      What is the ambiguity in parsing the expression id + id * id?
      medium
      A. The expression cannot contain '*'
      B. The expression is invalid
      C. The expression has only one parse tree
      D. The expression can be parsed as (id + id) * id or id + (id * id)

      Solution

      1. Step 1: Understand the grammar's ambiguity

        The grammar allows both '+' and '*' without precedence rules, causing multiple parse trees.
      2. Step 2: Analyze the expression parsing

        The expression 'id + id * id' can be parsed as either 'id + (id * id)' or '(id + id) * id', showing ambiguity.
      3. Final Answer:

        The expression can be parsed as (id + id) * id or id + (id * id) -> Option D
      4. Quick Check:

        Multiple parse trees = Ambiguity [OK]
      Hint: Check if operator precedence is missing [OK]
      Common Mistakes:
      • Assuming only one parse tree exists
      • Ignoring operator precedence
      • Thinking expression is invalid
      4. Identify the error in this ambiguous grammar and suggest a fix:
      S -> S + S | S * S | id
      medium
      A. Ambiguity due to missing operator precedence; fix by defining precedence rules
      B. No error, grammar is unambiguous
      C. Grammar is invalid syntax; fix by removing recursion
      D. Ambiguity due to missing terminals; fix by adding more terminals

      Solution

      1. Step 1: Identify the cause of ambiguity

        The grammar allows multiple parse trees because it lacks operator precedence rules.
      2. Step 2: Suggest a fix

        Define separate rules for '+' and '*' with precedence to remove ambiguity.
      3. Final Answer:

        Ambiguity due to missing operator precedence; fix by defining precedence rules -> Option A
      4. Quick Check:

        Missing precedence = Ambiguity fix [OK]
      Hint: Add precedence rules to fix ambiguity [OK]
      Common Mistakes:
      • Thinking recursion causes ambiguity
      • Assuming grammar syntax is invalid
      • Ignoring operator precedence importance
      5. How can ambiguity be resolved in a grammar that allows multiple parse trees for the same sentence?
      hard
      A. By allowing multiple parse trees
      B. By rewriting the grammar to enforce operator precedence and associativity
      C. By removing all terminals
      D. By adding more recursive rules

      Solution

      1. Step 1: Understand the cause of ambiguity

        Ambiguity arises when the grammar allows multiple parse trees for the same sentence.
      2. Step 2: Apply a solution

        Rewriting the grammar to include operator precedence and associativity rules removes ambiguity.
      3. Final Answer:

        By rewriting the grammar to enforce operator precedence and associativity -> Option B
      4. Quick Check:

        Rewrite grammar with precedence = Resolve ambiguity [OK]
      Hint: Use precedence and associativity rules to fix ambiguity [OK]
      Common Mistakes:
      • Adding recursion increases ambiguity
      • Removing terminals breaks grammar
      • Accepting ambiguity as normal
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard