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 PortalCOMP3149Shift Reduce Conflicts
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 17 of 32
    Topic Study Note Illustration
    17

    Shift-Reduce Conflicts

    In bottom-up parsing (especially LR parsing) , a shift-reduce conflict is one of the most important problems asked in exams.

    πŸ“˜ Shift-Reduce Conflicts (Compiler Construction)

    In bottom-up parsing (especially LR parsing), a shift-reduce conflict is one of the most important problems asked in exams.


    🧠 1. Definition

    βœ… Shift-Reduce Conflict

    A shift-reduce conflict occurs in parsing when the parser is unable to decide whether to:

    • πŸ”Ή Shift (move the next input symbol to the stack) OR
    • πŸ”Ή Reduce (replace symbols on stack using a grammar rule)

    πŸ‘‰ This happens for the same state and input symbol in the parsing table.


    πŸ“Œ 2. Simple Meaning

    πŸ‘‰ The parser is confused:

    β€œShould I read more input (shift) or reduce what I already have?”


    πŸ”§ 3. Where it Occurs?

    • LR(0) parsing
    • SLR(1) parsing
    • Bottom-up (shift-reduce) parsing
    • Especially in ambiguous grammars

    πŸ“Š 4. Example Grammar

    E β†’ E + E
    E β†’ E * E
    E β†’ id
    

    Input:

    id + id * id
    

    ⚠️ 5. Conflict Situation

    At a certain parser state:

    Stack contains:

    E + E
    

    Next input symbol:

    Now parser faces:

    ❓ Choices:

    • Shift * (to handle multiplication first)
    • Reduce E + E β†’ E

    πŸ‘‰ ❌ Conflict arises


    🌳 6. Why Shift-Reduce Conflict Happens

    Main Reasons:

    πŸ”Ή 1. Ambiguous Grammar

    Example:

    E β†’ E + E | E * E | id
    

    πŸ‘‰ No clear precedence or associativity


    πŸ”Ή 2. Missing Operator Precedence

    Parser doesn’t know:

    • * has higher priority than +

    πŸ”Ή 3. Incomplete LR(0) Information

    LR(0) has no lookahead, so it cannot decide correctly.


    🧠 7. Intuition

    πŸ‘‰ Shift-reduce conflict means:

    β€œShould I complete a rule now OR wait for more input?”


    πŸ“Š 8. Example Table Situation

    State Input Action
    i + Shift / Reduce ❌

    πŸ‘‰ Both actions are possible β†’ conflict


    πŸ”„ 9. Types of Shift-Reduce Conflict

    πŸ”Ή 1. Genuine Conflict

    • Grammar is truly ambiguous
    • Cannot be fixed without changing grammar

    πŸ”Ή 2. Artificial Conflict

    • Caused by poor grammar design

    • Can be fixed using:

      • Precedence rules
      • Left factoring
      • Removing ambiguity

    πŸ› οΈ 10. How to Resolve Shift-Reduce Conflict


    βœ… 1. Remove Ambiguity

    Rewrite grammar:

    ❌ Ambiguous:

    E β†’ E + E | E * E | id
    

    βœ… Fixed:

    E β†’ E + T | T
    T β†’ T * F | F
    F β†’ id
    

    βœ… 2. Use Operator Precedence

    • * has higher priority than +

    βœ… 3. Use Associativity Rules

    • Left associativity:
    a - b - c β†’ (a - b) - c
    

    βœ… 4. Use SLR / CLR Parsing

    • LR(1) uses lookahead β†’ fewer conflicts
    • CLR is most powerful β†’ avoids most conflicts

    βš–οΈ 11. Shift vs Reduce Decision

    Situation Action
    Input should continue Shift
    Rule complete Reduce
    Unclear grammar Conflict ❌

    πŸ“Œ 12. Real-Life Analogy

    πŸ‘‰ Imagine reading a sentence:

    • Shift = β€œread next word”
    • Reduce = β€œunderstand current phrase”

    Conflict =

    β€œShould I continue reading or interpret what I already have?”


    🎯 13. Key Exam Points

    πŸ‘‰ Very important:

    • Definition of shift-reduce conflict
    • Causes of conflict
    • Example grammar
    • How to resolve conflict
    • Difference between shift and reduce
    • Why LR(0) causes conflicts

    πŸ“ 14. Short Notes (Quick Revision)

    πŸ”΄ Shift-Reduce Conflict

    • Parser cannot decide shift or reduce
    • Occurs in bottom-up parsing

    πŸ”΅ Causes

    • Ambiguous grammar
    • Missing precedence
    • LR(0) limitation

    🟒 Solution

    • Rewrite grammar
    • Use precedence rules
    • Use LR(1) parsing

    πŸ“Š 15. Final Summary Table

    Aspect Shift Reduce Conflict
    Meaning Move input to stack Apply grammar rule Decision problem
    Purpose Read input Simplify stack Parsing issue
    Problem None alone None alone Happens in LR parsing
    Cause N/A N/A Ambiguity / LR(0)
    Solution N/A N/A Grammar fix / LR(1)

    βœ… Final Conclusion

    • A shift-reduce conflict is a major issue in bottom-up parsing
    • It occurs when the parser cannot decide between shift or reduce
    • It is mainly caused by ambiguous grammar or lack of lookahead
    • It is solved using grammar rewriting and advanced LR parsers (SLR, CLR, LALR)

    ← Previous topic 16
    LR(0) Automaton and Parsing Table
    Next topic 18 β†’
    SLR(1) Parsers: Automaton and Parsing Table

    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
    624words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction