ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›Eliminating Ambiguity, Left Recursion, and Left Factoring
    Compiler ConstructionTopic 10 of 32

    Eliminating Ambiguity, Left Recursion, and Left Factoring

    4 minread
    635words
    Beginnerlevel

    📘 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

    Previous topic 9
    Syntax Analysis and Role of the Parser
    Next topic 11
    Top-Down Parsing and Recursive-Descent Parsing

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count635
      Code examples0
      DifficultyBeginner