ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149Eliminating Ambiguity โ€ฆ
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 10 of 32
    Topic Study Note Illustration
    10

    Eliminating Ambiguity, Left Recursion, and Left Factoring

    (in simple language with examples, diagrams, and key points)

    ๐Ÿ“˜ 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    635words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction