๐ Eliminating Ambiguity, Left Recursion, and Left Factoring
(in simple language with examples, diagrams, and key points)
๐ง 1. Eliminating Ambiguity
โ
What is Ambiguity?
A grammar is ambiguous if a string has more than one parse tree.
๐ Example (Ambiguous Grammar)
E โ E + E | E * E | id
String:
id + id * id
๐ Two interpretations:
- (id + id) * id โ
- id + (id * id) โ
โ Problem
Ambiguity causes:
- Confusion in meaning
- Wrong evaluation order
- Parsing difficulty
โ
How to Eliminate Ambiguity
We rewrite grammar using precedence rules.
๐ Unambiguous Grammar
E โ E + T | T
T โ T * F | F
F โ id
๐ฏ Meaning
* has higher precedence than +
- Correct evaluation is enforced
โญ Key Points (Exam)
- Ambiguity = multiple parse trees
- Remove using precedence-based grammar
- Very important for expression parsing
๐ 2. Left Recursion
โ
Definition
A grammar is left recursive if a non-terminal calls itself as the first symbol on RHS.
๐ Example
A โ Aฮฑ | ฮฒ
๐ Here:
- A โ Aฮฑ (left recursion โ)
โ Problem with Left Recursion
- Causes infinite recursion
- Not suitable for top-down parsing (LL parser)
๐ Types of Left Recursion
๐น 1. Immediate Left Recursion
A โ Aฮฑ | ฮฒ
๐น 2. Indirect Left Recursion
A โ Bฮฑ
B โ Aฮฒ
๐ ๏ธ 3. Eliminating Left Recursion
โ
Rule (Important)
If:
A โ Aฮฑ | ฮฒ
Then convert to:
A โ ฮฒA'
A' โ ฮฑA' | ฮต
๐ Example
Given:
A โ A a | b
After Removal:
A โ b A'
A' โ a A' | ฮต
โญ Key Points (Exam)
- Needed for LL(1) parsing
- Eliminates infinite loops
- Converts grammar into top-down form
๐ 3. Left Factoring
โ
Definition
Left factoring is a technique used when multiple productions of a non-terminal start with the same prefix.
๐ Example
A โ ฮฑฮฒ1 | ฮฑฮฒ2
๐ Common prefix = ฮฑ
โ Problem
Parser cannot decide which rule to choose.
๐ ๏ธ 4. Removing Left Factoring
โ
Rule
A โ ฮฑA'
A' โ ฮฒ1 | ฮฒ2
๐ Example
Given Grammar:
A โ id + T | id - T
Step 1: Common prefix = id
After Left Factoring:
A โ id A'
A' โ + T | - T
โญ Key Points (Exam)
- Removes common prefixes
- Helps in predictive parsing
- Avoids backtracking
โ๏ธ 5. Comparison Table
| Concept |
Problem |
Solution |
Purpose |
| Ambiguity |
Multiple parse trees |
Rewrite grammar |
Clear meaning |
| Left Recursion |
Infinite recursion |
Remove recursion |
Enable top-down parsing |
| Left Factoring |
Common prefixes |
Factor out prefix |
Avoid backtracking |
๐ 6. Relationship Between Concepts
Ambiguity โ Fix grammar structure
Left Recursion โ Fix parsing issue
Left Factoring โ Improve decision making
๐งช 7. Combined Example
Original Grammar:
A โ A a | A b | c
Step 1: Remove Left Recursion
A โ c A'
A' โ a A' | b A' | ฮต
Step 2: (If needed) Left Factoring applied similarly
๐ฏ 8. Important Exam Questions
๐ Very frequently asked:
- Define ambiguity with example
- Eliminate ambiguity from grammar
- Remove left recursion (direct/indirect)
- Explain left factoring
- Convert grammar into LL(1) form
- Difference between all three
๐ 9. Short Notes (Quick Revision)
๐ด Ambiguity
- Multiple parse trees
- Removed using precedence grammar
๐ต Left Recursion
- A โ Aฮฑ
- Removed using transformation
๐ข Left Factoring
- Common prefix problem
- Used for predictive parsing
๐ 10. Final Summary Table
| Feature |
Ambiguity |
Left Recursion |
Left Factoring |
| Problem |
Multiple meanings |
Infinite recursion |
Common prefixes |
| Affects |
Parsing clarity |
Top-down parsing |
Predictive parsing |
| Solution |
Rewrite grammar |
Remove recursion |
Factor prefixes |
| Used in |
Expression grammar |
LL(1) design |
LL(1) design |
| Importance |
Very High |
Very High |
Very High |
โ
Final Conclusion
- Ambiguity causes multiple interpretations โ
- Left recursion blocks top-down parsing โ
- Left factoring improves parsing decisions โ
- All three are essential for designing LL(1) grammars