Bird
Raised Fist0
Compiler Designknowledge~10 mins

Ambiguity in grammars in Compiler Design - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the sentence to define ambiguity in grammars.

Compiler Design
A grammar is ambiguous if it can generate a string with [1] parse trees.
Drag options to blanks, or click blank then click option'
Amultiple
Bone
Cno
Dinfinite
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'one' because a grammar usually has one parse tree per string.
2fill in blank
medium

Complete the sentence about ambiguity's effect on parsing.

Compiler Design
Ambiguity in a grammar makes it difficult to [1] the structure of sentences.
Drag options to blanks, or click blank then click option'
Aignore
Bsimplify
Crepeat
Ddetermine
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'simplify' because ambiguity actually complicates parsing.
3fill in blank
hard

Fix the error in the statement about ambiguous grammars.

Compiler Design
An ambiguous grammar has [1] parse trees for some string.
Drag options to blanks, or click blank then click option'
Azero
Bone
Cmultiple
Dno
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'one' because many grammars have one parse tree per string.
4fill in blank
hard

Fill both blanks to describe ambiguity in grammar rules.

Compiler Design
If a grammar allows [1] different parse trees for the same string, it is called [2].
Drag options to blanks, or click blank then click option'
Amultiple
Bunique
Cambiguous
Dsingle
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'unique' and 'ambiguous'.
5fill in blank
hard

Fill all three blanks to complete the explanation of ambiguity.

Compiler Design
Ambiguity can cause [1] in [2] and make [3] difficult.
Drag options to blanks, or click blank then click option'
Aconfusion
Bparsing
Ccompilation
Dexecution
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'execution' instead of 'compilation'.

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