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 PortalCOMP3149Bottom Up Shift Reduce…
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 14 of 32
    Topic Study Note Illustration
    14

    Bottom-Up Parsing: Reductions and Shift-Reduce Parsing

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

    πŸ“˜ Bottom-Up Parsing: Reductions and Shift-Reduce Parsing

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


    🧠 1. Bottom-Up Parsing

    βœ… Definition

    Bottom-Up Parsing is a parsing technique in which we start from the input string (leaves) and gradually reduce it to the start symbol (root).

    πŸ‘‰ It works by:

    β€œReducing a string to the start symbol using grammar rules.”


    πŸ“Š Basic Idea

    Input String β†’ Reductions β†’ Start Symbol (S)
    

    ⭐ Key Concept

    Bottom-up parsing builds the parse tree: πŸ‘‰ from leaves β†’ root


    πŸ”„ 2. Reductions

    βœ… Definition

    A reduction is the process of replacing a substring of input (handle) with a non-terminal using a production rule.


    πŸ“Œ Example Grammar

    E β†’ E + T
    E β†’ T
    T β†’ id
    

    πŸ“Œ Input String

    id + id
    

    πŸ”„ Reduction Steps

    We reduce step-by-step:

    id + id
    β‡’ T + id        (id β†’ T)
    β‡’ T + T         (id β†’ T)
    β‡’ E + T         (T β†’ E)
    β‡’ E             (E + T β†’ E)
    

    ⭐ Key Points (Exam)

    • Reduction = reverse of derivation
    • Always replaces handle
    • Moves from string β†’ start symbol

    🧠 3. Handle (Very Important)

    βœ… Definition

    A handle is a substring that:

    • Matches the RHS of a production
    • Can be reduced to its LHS
    • Represents one step in reverse derivation

    πŸ“Œ Example

    E β†’ E + T
    

    In:

    E + T
    

    πŸ‘‰ Handle = E + T


    ⭐ Key Point

    πŸ‘‰ Finding handle = core of bottom-up parsing


    πŸ”§ 4. Shift-Reduce Parsing

    βœ… Definition

    Shift-Reduce Parsing is a bottom-up parsing technique that uses:

    • Shift operation
    • Reduce operation
    • Stack

    πŸ“Š 5. Working of Shift-Reduce Parser

    πŸ”Ή Components

    • Input Buffer
    • Stack
    • Parser actions

    πŸ“Œ Basic Diagram

    Input β†’ [Shift-Reduce Parser Stack] β†’ Output
    

    πŸ”„ 6. Actions in Shift-Reduce Parsing


    πŸ”Ή 1. SHIFT

    πŸ‘‰ Move input symbol to stack

    Example:

    Input: id + id
    Stack: id
    

    πŸ”Ή 2. REDUCE

    πŸ‘‰ Replace stack symbols using grammar rule

    Example:

    id β†’ T
    

    πŸ”Ή 3. ACCEPT

    πŸ‘‰ Input successfully parsed


    πŸ”Ή 4. ERROR

    πŸ‘‰ Invalid input


    πŸ§ͺ 7. Example (Step-by-Step)

    Grammar:

    E β†’ E + T
    E β†’ T
    T β†’ id
    

    Input:

    id + id
    

    Shift-Reduce Process

    Step Stack Input Action
    1 id+id$ shift
    2 id +id$ reduce id β†’ T
    3 T +id$ reduce T β†’ E
    4 E +id$ shift +
    5 E + id$ shift id
    6 E + id $ reduce id β†’ T
    7 E + T $ reduce E + T β†’ E
    8 E $ ACCEPT

    🌳 8. Parse Tree Idea

    Bottom-up parsing builds:

    Leaves β†’ Root
    

    Example:

          E
         / \
        E   T
            |
            id
    

    ⚠️ 9. Problems in Shift-Reduce Parsing

    ❌ 1. Shift-Reduce Conflict

    • Cannot decide shift or reduce

    ❌ 2. Reduce-Reduce Conflict

    • Multiple reduction options

    πŸ‘‰ These are handled by LR parsers


    βš–οΈ 10. Shift-Reduce vs Top-Down Parsing

    Feature Bottom-Up (Shift-Reduce) Top-Down
    Direction Leaves β†’ Root Root β†’ Leaves
    Approach Reduce input Expand grammar
    Method Handles Productions
    Backtracking Not needed Possible
    Power More powerful Less powerful

    🎯 11. Advantages

    βœ” Handles larger class of grammars βœ” No backtracking βœ” Efficient parsing βœ” Used in real compilers


    ❌ 12. Disadvantages

    ❌ Complex implementation ❌ Conflicts (shift/reduce) ❌ Hard to debug


    πŸ“Œ 13. Important Exam Questions

    πŸ‘‰ Very frequently asked:

    • Define bottom-up parsing
    • What is reduction?
    • Explain handle in parsing
    • Steps of shift-reduce parsing
    • Solve shift-reduce parsing example
    • Difference between shift and reduce
    • Parse given string using bottom-up method

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

    πŸ”΅ Bottom-Up Parsing

    • Starts from input
    • Ends at start symbol

    πŸ”΅ Reduction

    • Replace handle with LHS

    πŸ”΅ Shift-Reduce Parsing

    • Uses stack
    • SHIFT + REDUCE operations

    πŸ“Š 15. Final Summary Table

    Aspect Bottom-Up Parsing Shift-Reduce Parsing
    Definition Builds tree from input Stack-based bottom-up parsing
    Method Reductions Shift + Reduce
    Direction Leaves β†’ Root Leaves β†’ Root
    Key Idea Reduce string Use stack operations
    Components Handles Stack, input buffer
    Complexity Moderate Moderate
    Use Syntax analysis Real compiler parsing

    βœ… Final Conclusion

    • Bottom-up parsing builds the parse tree from input to start symbol
    • Reduction is the reverse of derivation
    • Shift-reduce parsing is the most basic bottom-up method
    • It is powerful but may face conflicts

    ← Previous topic 13
    LL(1) Grammars and Non-recursive Predictive Parsing
    Next topic 15 β†’
    LR Parsing and LR(0) Parsers

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

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction