Ambiguity in grammars in Compiler Design - Time & Space Complexity
When we analyze ambiguity in grammars, we want to understand how hard it is to detect if a grammar can produce more than one parse tree for the same sentence.
The question is: how does the effort to find ambiguity grow as the grammar gets bigger?
Analyze the time complexity of checking ambiguity by trying all possible parse trees.
function isAmbiguous(grammar) {
for each sentence in language(grammar) {
parseTrees = generateAllParseTrees(sentence, grammar)
if (count(parseTrees) > 1) return true
}
return false
}
This code tries to find if any sentence has more than one parse tree, indicating ambiguity.
Look for repeated work in the code.
- Primary operation: Generating all parse trees for each sentence.
- How many times: For every sentence in the language, which can be very large or infinite.
As the grammar grows, the number of sentences and parse trees grows very fast.
| Input Size (grammar rules) | Approx. Operations |
|---|---|
| 10 | Thousands of parse trees to check |
| 100 | Millions or more parse trees to check |
| 1000 | Billions or more parse trees, practically impossible |
Pattern observation: The effort grows very fast, much faster than the grammar size itself.
Time Complexity: O(2^n)
This means the time needed doubles or more as the grammar size increases, making ambiguity detection very costly.
[X] Wrong: "Checking ambiguity is easy because we just need to find one sentence with two parses quickly."
[OK] Correct: The number of sentences and parse trees can be huge, so checking all possibilities takes a very long time.
Understanding how ambiguity checking grows helps you appreciate why some compiler tasks are hard and need smart approaches.
"What if we limit the grammar to only a certain type, like unambiguous grammars? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of ambiguity in grammar
Ambiguity means a sentence can be interpreted in multiple ways within the grammar.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.Final Answer:
A sentence can have more than one meaning -> Option AQuick Check:
Ambiguity = Multiple meanings [OK]
- Confusing ambiguity with grammar errors
- Thinking ambiguity means no rules
- Assuming ambiguity means all sentences are correct
Solution
Step 1: Analyze the grammar rules for ambiguity
Rule A allows multiple parse trees for strings like 'aabb', causing ambiguity.Step 2: Check other options for ambiguity
Options B, C, and D do not produce multiple parse trees for the same string.Final Answer:
S -> aSb | aSb-> Option CQuick Check:
Multiple parse trees = Ambiguous [OK]
- Ignoring repeated rules that cause ambiguity
- Confusing ambiguity with grammar repetition
- Assuming all recursive rules are ambiguous
E -> E + E | E * E | idWhat is the ambiguity in parsing the expression
id + id * id?Solution
Step 1: Understand the grammar's ambiguity
The grammar allows both '+' and '*' without precedence rules, causing multiple parse trees.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.Final Answer:
The expression can be parsed as (id + id) * id or id + (id * id) -> Option DQuick Check:
Multiple parse trees = Ambiguity [OK]
- Assuming only one parse tree exists
- Ignoring operator precedence
- Thinking expression is invalid
S -> S + S | S * S | idSolution
Step 1: Identify the cause of ambiguity
The grammar allows multiple parse trees because it lacks operator precedence rules.Step 2: Suggest a fix
Define separate rules for '+' and '*' with precedence to remove ambiguity.Final Answer:
Ambiguity due to missing operator precedence; fix by defining precedence rules -> Option AQuick Check:
Missing precedence = Ambiguity fix [OK]
- Thinking recursion causes ambiguity
- Assuming grammar syntax is invalid
- Ignoring operator precedence importance
Solution
Step 1: Understand the cause of ambiguity
Ambiguity arises when the grammar allows multiple parse trees for the same sentence.Step 2: Apply a solution
Rewriting the grammar to include operator precedence and associativity rules removes ambiguity.Final Answer:
By rewriting the grammar to enforce operator precedence and associativity -> Option BQuick Check:
Rewrite grammar with precedence = Resolve ambiguity [OK]
- Adding recursion increases ambiguity
- Removing terminals breaks grammar
- Accepting ambiguity as normal
Ready to prove it?
Pass the test and this pattern moves unknown
